diff options
Diffstat (limited to 'src/zmalloc.c')
-rw-r--r-- | src/zmalloc.c | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/src/zmalloc.c b/src/zmalloc.c index c8d6c825f..1dc662e57 100644 --- a/src/zmalloc.c +++ b/src/zmalloc.c @@ -32,6 +32,7 @@ #include <stdlib.h> #include <stdint.h> #include <unistd.h> +#include <assert.h> /* This function provide us access to the original libc free(). This is useful * for instance to free results obtained by backtrace_symbols(). We need @@ -49,20 +50,21 @@ void zlibc_free(void *ptr) { #ifdef HAVE_MALLOC_SIZE #define PREFIX_SIZE (0) +#define ASSERT_NO_SIZE_OVERFLOW(sz) #else #if defined(__sun) || defined(__sparc) || defined(__sparc__) #define PREFIX_SIZE (sizeof(long long)) #else #define PREFIX_SIZE (sizeof(size_t)) #endif -#endif - -#if PREFIX_SIZE > 0 #define ASSERT_NO_SIZE_OVERFLOW(sz) assert((sz) + PREFIX_SIZE > (sz)) -#else -#define ASSERT_NO_SIZE_OVERFLOW(sz) #endif +/* When using the libc allocator, use a minimum allocation size to match the + * jemalloc behavior that doesn't return NULL in this case. + */ +#define MALLOC_MIN_SIZE(x) ((x) > 0 ? (x) : sizeof(long)) + /* Explicitly override malloc/free etc when using tcmalloc. */ #if defined(USE_TCMALLOC) #define malloc(size) tc_malloc(size) @@ -96,7 +98,7 @@ static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom; * '*usable' is set to the usable size if non NULL. */ void *ztrymalloc_usable(size_t size, size_t *usable) { ASSERT_NO_SIZE_OVERFLOW(size); - void *ptr = malloc(size+PREFIX_SIZE); + void *ptr = malloc(MALLOC_MIN_SIZE(size)+PREFIX_SIZE); if (!ptr) return NULL; #ifdef HAVE_MALLOC_SIZE @@ -156,7 +158,7 @@ void zfree_no_tcache(void *ptr) { * '*usable' is set to the usable size if non NULL. */ void *ztrycalloc_usable(size_t size, size_t *usable) { ASSERT_NO_SIZE_OVERFLOW(size); - void *ptr = calloc(1, size+PREFIX_SIZE); + void *ptr = calloc(1, MALLOC_MIN_SIZE(size)+PREFIX_SIZE); if (ptr == NULL) return NULL; #ifdef HAVE_MALLOC_SIZE @@ -678,6 +680,7 @@ int zmalloc_test(int argc, char **argv) { UNUSED(argc); UNUSED(argv); + printf("Malloc prefix size: %d\n", (int) PREFIX_SIZE); printf("Initial used memory: %zu\n", zmalloc_used_memory()); ptr = zmalloc(123); printf("Allocated 123 bytes; used: %zu\n", zmalloc_used_memory()); |