[PATCH] Avoid NULL check in je_free

Mike Hommey mh+jemalloc at glandium.org
Mon Apr 2 02:37:32 PDT 2012


From: Mike Hommey <mh at glandium.org>

The original patch from Igor Bukanov <igor at mir2.org>, in
https://bugzilla.mozilla.org/show_bug.cgi?id=571332, was also
modifying idalloc and arena_dalloc to avoid two CHUNK_ADDR2BASE
invocation, but since both functions are inlined, I doubt this
actually makes a difference.
---
 include/jemalloc/internal/jemalloc_internal.h.in |   10 +++-------
 src/jemalloc.c                                   |   22 ++++++++++------------
 2 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/include/jemalloc/internal/jemalloc_internal.h.in b/include/jemalloc/internal/jemalloc_internal.h.in
index f6924a4..b997031 100644
--- a/include/jemalloc/internal/jemalloc_internal.h.in
+++ b/include/jemalloc/internal/jemalloc_internal.h.in
@@ -632,11 +632,9 @@ ipalloc(size_t usize, size_t alignment, bool zero)
 JEMALLOC_INLINE size_t
 isalloc(const void *ptr)
 {
-	size_t ret;
+	size_t ret = 0;
 	arena_chunk_t *chunk;
 
-	assert(ptr != NULL);
-
 	chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
 	if (chunk != ptr) {
 		/* Region. */
@@ -644,7 +642,7 @@ isalloc(const void *ptr)
 			ret = arena_salloc_demote(ptr);
 		else
 			ret = arena_salloc(ptr);
-	} else
+	} else if (ptr != NULL)
 		ret = huge_salloc(ptr);
 
 	return (ret);
@@ -666,12 +664,10 @@ 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
+	else if (ptr != NULL)
 		huge_dalloc(ptr, true);
 }
 
diff --git a/src/jemalloc.c b/src/jemalloc.c
index 23541ae..e883085 100644
--- a/src/jemalloc.c
+++ b/src/jemalloc.c
@@ -1101,21 +1101,19 @@ void
 je_free(void *ptr)
 {
 
-	if (ptr != NULL) {
-		size_t usize;
+	size_t usize;
 
-		assert(malloc_initialized || IS_INITIALIZER);
+	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);
+	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);
 }
 
 /*
-- 
1.7.9.5




More information about the jemalloc-discuss mailing list