From 5b3db098f73f467a03f87a2242c692268f796a56 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Mon, 26 Mar 2012 18:39:35 +0200 Subject: [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 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 | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/zone.c b/src/zone.c index a8f09c98..d3107f85 100644 --- a/src/zone.c +++ b/src/zone.c @@ -80,14 +80,22 @@ static void zone_free(malloc_zone_t *zone, void *ptr) { - je_free(ptr); + if (ivsalloc(ptr) != 0) { + je_free(ptr); + return; + } + + free(ptr); } static void * zone_realloc(malloc_zone_t *zone, void *ptr, size_t size) { - return (je_realloc(ptr, size)); + if (ivsalloc(ptr) != 0) + return (je_realloc(ptr, size)); + + return (realloc(ptr, size)); } #if (JEMALLOC_ZONE_VERSION >= 5) @@ -107,8 +115,13 @@ static void zone_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size) { - assert(ivsalloc(ptr) == size); - je_free(ptr); + if (ivsalloc(ptr) != 0) { + assert(ivsalloc(ptr) == size); + je_free(ptr); + return; + } + + free(ptr); } #endif