From f0047372673da7f213f733465dab0d8825eb1c9f Mon Sep 17 00:00:00 2001 From: Jason Evans Date: Mon, 2 Apr 2012 15:18:24 -0700 Subject: [PATCH] Revert "Avoid NULL check in free() and malloc_usable_size()." This reverts commit 96d4120ac08db3f2d566e8e5c3bc134a24aa0afc. ivsalloc() depends on chunks_rtree being initialized. This can be worked around via a NULL pointer check. However, thread_allocated_tsd_get() also depends on initialization having occurred, and there is no way to guard its call in free() that is cheaper than checking whether ptr is NULL. --- Makefile.in | 2 +- .../jemalloc/internal/jemalloc_internal.h.in | 10 ++++--- src/jemalloc.c | 26 +++++++++++-------- test/null.c | 19 -------------- test/null.exp | 2 -- 5 files changed, 22 insertions(+), 37 deletions(-) delete mode 100644 test/null.c delete mode 100644 test/null.exp diff --git a/Makefile.in b/Makefile.in index d8b671ad..821c0634 100644 --- a/Makefile.in +++ b/Makefile.in @@ -65,7 +65,7 @@ DOCS_HTML := $(DOCS_XML:@objroot@%.xml=@srcroot@%.html) DOCS_MAN3 := $(DOCS_XML:@objroot@%.xml=@srcroot@%.3) DOCS := $(DOCS_HTML) $(DOCS_MAN3) CTESTS := @srcroot@test/aligned_alloc.c @srcroot@test/allocated.c \ - @srcroot@test/bitmap.c @srcroot@test/mremap.c @srcroot@test/null.c \ + @srcroot@test/bitmap.c @srcroot@test/mremap.c \ @srcroot@test/posix_memalign.c @srcroot@test/thread_arena.c \ @srcroot@test/thread_tcache_enabled.c ifeq (@enable_experimental@, 1) diff --git a/include/jemalloc/internal/jemalloc_internal.h.in b/include/jemalloc/internal/jemalloc_internal.h.in index ed21bbe7..db2deb03 100644 --- a/include/jemalloc/internal/jemalloc_internal.h.in +++ b/include/jemalloc/internal/jemalloc_internal.h.in @@ -633,6 +633,8 @@ isalloc(const void *ptr) size_t ret; arena_chunk_t *chunk; + assert(ptr != NULL); + chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); if (chunk != ptr) { /* Region. */ @@ -640,10 +642,8 @@ isalloc(const void *ptr) ret = arena_salloc_demote(ptr); else ret = arena_salloc(ptr); - } else if (ptr != NULL) + } else ret = huge_salloc(ptr); - else - ret = 0; return (ret); } @@ -664,10 +664,12 @@ idalloc(void *ptr) { arena_chunk_t *chunk; + assert(ptr != NULL); + chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); if (chunk != ptr) arena_dalloc(chunk->arena, chunk, ptr); - else if (ptr != NULL) + else huge_dalloc(ptr, true); } diff --git a/src/jemalloc.c b/src/jemalloc.c index 86ce695b..1deabcd9 100644 --- a/src/jemalloc.c +++ b/src/jemalloc.c @@ -1100,18 +1100,22 @@ JEMALLOC_ATTR(visibility("default")) void je_free(void *ptr) { - size_t usize; - assert(malloc_initialized || IS_INITIALIZER); + if (ptr != NULL) { + size_t usize; - if (config_prof && opt_prof) { - usize = isalloc(ptr); - prof_free(ptr, usize); - } else if (config_stats) - usize = isalloc(ptr); - if (config_stats) - thread_allocated_tsd_get()->deallocated += usize; - idalloc(ptr); + assert(malloc_initialized || IS_INITIALIZER); + + if (config_prof && opt_prof) { + usize = isalloc(ptr); + prof_free(ptr, usize); + } else if (config_stats) { + usize = isalloc(ptr); + } + if (config_stats) + thread_allocated_tsd_get()->deallocated += usize; + idalloc(ptr); + } } /* @@ -1196,7 +1200,7 @@ je_malloc_usable_size(const void *ptr) if (config_ivsalloc) ret = ivsalloc(ptr); else - ret = isalloc(ptr); + ret = (ptr != NULL) ? isalloc(ptr) : 0; return (ret); } diff --git a/test/null.c b/test/null.c deleted file mode 100644 index ccd7ced3..00000000 --- a/test/null.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -#define JEMALLOC_MANGLE -#include "jemalloc_test.h" - -int -main(void) -{ - - fprintf(stderr, "Test begin\n"); - - free(malloc(1)); - free(NULL); - assert(malloc_usable_size(NULL) == 0); - - fprintf(stderr, "Test end\n"); - return (0); -} diff --git a/test/null.exp b/test/null.exp deleted file mode 100644 index 369a88dd..00000000 --- a/test/null.exp +++ /dev/null @@ -1,2 +0,0 @@ -Test begin -Test end