Revert "Avoid NULL check in free() and malloc_usable_size()."

This reverts commit 96d4120ac0.

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.
This commit is contained in:
Jason Evans 2012-04-02 15:18:24 -07:00
parent 96d4120ac0
commit f004737267
5 changed files with 22 additions and 37 deletions

View File

@ -65,7 +65,7 @@ DOCS_HTML := $(DOCS_XML:@objroot@%.xml=@srcroot@%.html)
DOCS_MAN3 := $(DOCS_XML:@objroot@%.xml=@srcroot@%.3) DOCS_MAN3 := $(DOCS_XML:@objroot@%.xml=@srcroot@%.3)
DOCS := $(DOCS_HTML) $(DOCS_MAN3) DOCS := $(DOCS_HTML) $(DOCS_MAN3)
CTESTS := @srcroot@test/aligned_alloc.c @srcroot@test/allocated.c \ 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/posix_memalign.c @srcroot@test/thread_arena.c \
@srcroot@test/thread_tcache_enabled.c @srcroot@test/thread_tcache_enabled.c
ifeq (@enable_experimental@, 1) ifeq (@enable_experimental@, 1)

View File

@ -633,6 +633,8 @@ isalloc(const void *ptr)
size_t ret; size_t ret;
arena_chunk_t *chunk; arena_chunk_t *chunk;
assert(ptr != NULL);
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
if (chunk != ptr) { if (chunk != ptr) {
/* Region. */ /* Region. */
@ -640,10 +642,8 @@ isalloc(const void *ptr)
ret = arena_salloc_demote(ptr); ret = arena_salloc_demote(ptr);
else else
ret = arena_salloc(ptr); ret = arena_salloc(ptr);
} else if (ptr != NULL) } else
ret = huge_salloc(ptr); ret = huge_salloc(ptr);
else
ret = 0;
return (ret); return (ret);
} }
@ -664,10 +664,12 @@ idalloc(void *ptr)
{ {
arena_chunk_t *chunk; arena_chunk_t *chunk;
assert(ptr != NULL);
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
if (chunk != ptr) if (chunk != ptr)
arena_dalloc(chunk->arena, chunk, ptr); arena_dalloc(chunk->arena, chunk, ptr);
else if (ptr != NULL) else
huge_dalloc(ptr, true); huge_dalloc(ptr, true);
} }

View File

@ -1100,6 +1100,8 @@ JEMALLOC_ATTR(visibility("default"))
void void
je_free(void *ptr) je_free(void *ptr)
{ {
if (ptr != NULL) {
size_t usize; size_t usize;
assert(malloc_initialized || IS_INITIALIZER); assert(malloc_initialized || IS_INITIALIZER);
@ -1107,11 +1109,13 @@ je_free(void *ptr)
if (config_prof && opt_prof) { if (config_prof && opt_prof) {
usize = isalloc(ptr); usize = isalloc(ptr);
prof_free(ptr, usize); prof_free(ptr, usize);
} else if (config_stats) } else if (config_stats) {
usize = isalloc(ptr); usize = isalloc(ptr);
}
if (config_stats) if (config_stats)
thread_allocated_tsd_get()->deallocated += usize; thread_allocated_tsd_get()->deallocated += usize;
idalloc(ptr); idalloc(ptr);
}
} }
/* /*
@ -1196,7 +1200,7 @@ je_malloc_usable_size(const void *ptr)
if (config_ivsalloc) if (config_ivsalloc)
ret = ivsalloc(ptr); ret = ivsalloc(ptr);
else else
ret = isalloc(ptr); ret = (ptr != NULL) ? isalloc(ptr) : 0;
return (ret); return (ret);
} }

View File

@ -1,19 +0,0 @@
#include <stdio.h>
#include <assert.h>
#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);
}

View File

@ -1,2 +0,0 @@
Test begin
Test end