[PATCH 2/2] Avoid crashes when system libraries use the purgeable zone allocator

Mike Hommey mh+jemalloc at glandium.org
Tue Mar 27 05:20:13 PDT 2012


From: Mike Hommey <mh at glandium.org>

---
 src/zone.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/src/zone.c b/src/zone.c
index 4b6c75e..d5eae46 100644
--- a/src/zone.c
+++ b/src/zone.c
@@ -3,6 +3,13 @@
 #  error "This source file is for zones on Darwin (OS X)."
 #endif
 
+/*
+ * The malloc_default_purgeable_zone function is only available on >= 10.6.
+ * We need to check whether it is present at runtime, thus the weak_import.
+ */
+extern malloc_zone_t *malloc_default_purgeable_zone(void)
+JEMALLOC_ATTR(weak_import);
+
 /******************************************************************************/
 /* Data. */
 
@@ -207,6 +214,20 @@ register_zone(void)
 #endif
 #endif
 
+	/*
+	 * The default purgeable zone is created lazily by OSX's libc. It uses
+	 * the default zone when it is created for "small" allocations
+	 * (< 15 KiB), but assumes the default zone is a scalable_zone. This
+	 * obviously fails when the default zone is the jemalloc zone, so
+	 * malloc_default_purgeable_zone is called beforehand so that the
+	 * default purgeable zone is create when the default zone is still
+	 * a scalable_zone.
+	 * As purgeable zones only exist on >= 10.6, we need to check for the
+	 * function existence at runtime.
+	 */
+	if (malloc_default_purgeable_zone)
+		malloc_default_purgeable_zone();
+
 	/* Register the custom zone. At this point it won't be the default. */
 	malloc_zone_register(&zone);
 
-- 
1.7.9.1




More information about the jemalloc-discuss mailing list