[PATCH] Make zone_{free, realloc, free_definite_size} fallback to the system allocator if they are called with a pointer that jemalloc didn't allocate

Mike Hommey mh+jemalloc at glandium.org
Mon Mar 26 09:39:35 PDT 2012


From: Mike Hommey <mh at glandium.org>

It turns out some OSX system libraries (like CoreGraphics on 10.6) like
to call malloc_zone_* functions, but giving them pointers that weren't
allocated with the zone they are using.

Possibly, they do malloc_zone_malloc(malloc_default_zone()) before we
register the jemalloc zone, and malloc_zone_realloc(malloc_default_zone())
after. malloc_default_zone() returning a different value in both cases.
---
 src/zone.c |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/zone.c b/src/zone.c
index a8f09c9..23f22fa 100644
--- a/src/zone.c
+++ b/src/zone.c
@@ -79,15 +79,21 @@ zone_valloc(malloc_zone_t *zone, size_t size)
 static void
 zone_free(malloc_zone_t *zone, void *ptr)
 {
+	if (ivsalloc(ptr) != 0) {
+		je_free(ptr);
+		return;
+	}
 
-	je_free(ptr);
+	free(ptr);
 }
 
 static void *
 zone_realloc(malloc_zone_t *zone, void *ptr, size_t size)
 {
+	if (ivsalloc(ptr) != 0)
+		return (je_realloc(ptr, size));
 
-	return (je_realloc(ptr, size));
+	return (realloc(ptr, size));
 }
 
 #if (JEMALLOC_ZONE_VERSION >= 5)
@@ -106,9 +112,13 @@ zone_memalign(malloc_zone_t *zone, size_t alignment, size_t size)
 static void
 zone_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size)
 {
+	if (ivsalloc(ptr) != 0) {
+		assert(ivsalloc(ptr) == size);
+		je_free(ptr);
+		return;
+	}
 
-	assert(ivsalloc(ptr) == size);
-	je_free(ptr);
+	free(ptr);
 }
 #endif
 
-- 
1.7.9.1




More information about the jemalloc-discuss mailing list