Changes in uspace/lib/c/generic/malloc.c [ffccdff0:dd50aa19] in mainline
- File:
-
- 1 edited
-
uspace/lib/c/generic/malloc.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/malloc.c
rffccdff0 rdd50aa19 34 34 */ 35 35 36 #include < malloc.h>36 #include <stdlib.h> 37 37 #include <stdalign.h> 38 38 #include <stdbool.h> 39 39 #include <stddef.h> 40 #include <stdio.h> 40 41 #include <as.h> 41 42 #include <align.h> … … 47 48 #include <stdlib.h> 48 49 #include <adt/gcdlcm.h> 50 #include <malloc.h> 49 51 50 52 #include "private/malloc.h" … … 724 726 * 725 727 */ 726 static void *malloc_internal( const size_t size, constsize_t align)728 static void *malloc_internal(size_t size, size_t align) 727 729 { 728 730 malloc_assert(first_heap_area != NULL); 729 731 732 if (size == 0) 733 size = 1; 734 730 735 if (align == 0) 731 return NULL;736 align = BASE_ALIGN; 732 737 733 738 size_t falign = lcm(align, BASE_ALIGN); … … 773 778 } 774 779 775 /** Allocate memory by number of elements776 *777 * @param nmemb Number of members to allocate.778 * @param size Size of one member in bytes.779 *780 * @return Allocated memory or NULL.781 *782 */783 void *calloc(const size_t nmemb, const size_t size)784 {785 // FIXME: Check for overflow786 787 void *block = malloc(nmemb * size);788 if (block == NULL)789 return NULL;790 791 memset(block, 0, nmemb * size);792 return block;793 }794 795 780 /** Allocate memory 796 781 * … … 840 825 * 841 826 */ 842 void *realloc(void *const addr, constsize_t size)827 void *realloc(void *const addr, size_t size) 843 828 { 844 829 if (size == 0) { 845 f ree(addr);846 return NULL;830 fprintf(stderr, "realloc() called with size 0\n"); 831 size = 1; 847 832 } 848 833 … … 949 934 } 950 935 936 /** Reallocate memory for an array 937 * 938 * Same as realloc(ptr, nelem * elsize), except the multiplication is checked 939 * for numerical overflow. Borrowed from POSIX 2024. 940 * 941 * @param ptr 942 * @param nelem 943 * @param elsize 944 * 945 * @return Reallocated memory or NULL. 946 */ 947 void *reallocarray(void *ptr, size_t nelem, size_t elsize) 948 { 949 if (nelem > SIZE_MAX / elsize) 950 return NULL; 951 952 return realloc(ptr, nelem * elsize); 953 } 954 951 955 /** Free a memory block 952 956 *
Note:
See TracChangeset
for help on using the changeset viewer.
