diff options
Diffstat (limited to 'common/dlmalloc.c')
-rw-r--r-- | common/dlmalloc.c | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 9c33cc2c95..c41487d54b 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -1,8 +1,10 @@ #include <config.h> +#include <errno.h> #include <malloc.h> #include <string.h> #include <memory.h> +#include <linux/build_bug.h> #include <stdio.h> #include <module.h> @@ -331,7 +333,7 @@ helps keep the system level memory demands of a long-lived program low. Mapped memory can never become `locked' between other chunks, as can happen with normally allocated chunks, which - menas that even trimming via malloc_trim would not release them. + means that even trimming via malloc_trim would not release them. However, it has the disadvantages that: @@ -529,7 +531,7 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ the malloc code, but "mem" is the pointer that is returned to the user. "Nextchunk" is the beginning of the next contiguous chunk. - Chunks always begin on even word boundries, so the mem portion + Chunks always begin on even word boundaries, so the mem portion (which is returned to the user) is also on an even word boundary, and thus double-word aligned. @@ -627,7 +629,13 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /* conversion from malloc headers to user pointers, and back */ #define chunk2mem(p) ((void*)((char*)(p) + 2*SIZE_SZ)) -#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ)) + +static inline mchunkptr mem2chunk(void *mem) +{ + OPTIMIZER_HIDE_VAR(mem); + + return mem - 2 * SIZE_SZ; +} /* pad request bytes into a usable size */ @@ -884,6 +892,8 @@ static mbinptr av_[NAV * 2 + 2] = { /* Other static bookkeeping data */ +static_assert(MALLOC_ALIGNMENT >= CONFIG_MALLOC_ALIGNMENT); + /* variables holding tunable values */ #ifndef __BAREBOX__ static unsigned long trim_threshold = DEFAULT_TRIM_THRESHOLD; @@ -1085,7 +1095,7 @@ static void malloc_extend_top(INTERNAL_SIZE_T nb) /* Main public routines */ /* - Malloc Algorthim: + Malloc Algorithm: The requested size is first converted into a usable form, `nb'. This currently means to add 4 bytes overhead plus possibly more to @@ -1158,8 +1168,10 @@ void *malloc(size_t bytes) INTERNAL_SIZE_T nb; - if ((long) bytes < 0) + if ((long) bytes < 0) { + errno = ENOMEM; return NULL; + } nb = request2size(bytes); /* padded request size; */ @@ -1313,8 +1325,10 @@ void *malloc(size_t bytes) if ((remainder_size = chunksize (top) - nb) < (long) MINSIZE) { /* Try to extend */ malloc_extend_top(nb); - if ((remainder_size = chunksize(top) - nb) < (long) MINSIZE) + if ((remainder_size = chunksize(top) - nb) < (long) MINSIZE) { + errno = ENOMEM; return NULL; /* propagate failure */ + } } victim = top; @@ -1478,8 +1492,10 @@ void *realloc(void *oldmem, size_t bytes) } #endif - if ((long)bytes < 0) + if ((long)bytes < 0) { + errno = ENOMEM; return NULL; + } /* realloc of null is supposed to be same as malloc */ if (!oldmem) @@ -1641,12 +1657,14 @@ void *memalign(size_t alignment, size_t bytes) char *brk; /* alignment point within p */ mchunkptr newp; /* chunk to return */ INTERNAL_SIZE_T newsize; /* its size */ - INTERNAL_SIZE_T leadsize; /* leading space befor alignment point */ + INTERNAL_SIZE_T leadsize; /* leading space before alignment point */ mchunkptr remainder; /* spare room at end to split off */ long remainder_size; /* its size */ - if ((long) bytes < 0) + if ((long) bytes < 0) { + errno = ENOMEM; return NULL; + } /* If need less alignment than we give anyway, just relay to malloc */ @@ -1680,8 +1698,8 @@ void *memalign(size_t alignment, size_t bytes) this is always possible. */ - brk = (char*) mem2chunk(((unsigned long) (m + alignment - 1)) & - -((signed) alignment)); + brk = (char*) mem2chunk((void *)(((unsigned long) (m + alignment - 1)) & + -((signed) alignment))); if ((long)(brk - (char*)(p)) < MINSIZE) brk = brk + alignment; @@ -1728,8 +1746,10 @@ void *calloc(size_t n, size_t elem_size) mchunkptr oldtop = top; INTERNAL_SIZE_T oldtopsize = chunksize(top); - if ((long)n < 0) + if ((long)n < 0) { + errno = ENOMEM; return NULL; + } mem = malloc(sz); @@ -1858,10 +1878,10 @@ History: Wolfram Gloger (Gloger@lrz.uni-muenchen.de). * Use last_remainder in more cases. * Pack bins using idea from colin@nyx10.cs.du.edu - * Use ordered bins instead of best-fit threshhold + * Use ordered bins instead of best-fit threshold * Eliminate block-local decls to simplify tracing and debugging. * Support another case of realloc via move into top - * Fix error occuring when initial sbrk_base not word-aligned. + * Fix error occurring when initial sbrk_base not word-aligned. * Rely on page size for units instead of SBRK_UNIT to avoid surprises about sbrk alignment conventions. * Add mallinfo, mallopt. Thanks to Raymond Nijssen |