Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit e058ca6

Browse files
committed
Change how the default zone is found
On OSX 10.12, malloc_default_zone returns a special zone that is not present in the list of registered zones. That zone uses a "lite zone" if one is present (apparently enabled when malloc stack logging is enabled), or the first registered zone otherwise. In practice this means unless malloc stack logging is enabled, the first registered zone is the default. So get the list of zones to get the first one, instead of relying on malloc_default_zone.
1 parent 9355bd6 commit e058ca6

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/zone.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,33 @@ zone_force_unlock(malloc_zone_t *zone)
168168
jemalloc_postfork_parent();
169169
}
170170

171+
static malloc_zone_t *get_default_zone()
172+
{
173+
malloc_zone_t **zones = NULL;
174+
unsigned int num_zones = 0;
175+
176+
/*
177+
* On OSX 10.12, malloc_default_zone returns a special zone that is not
178+
* present in the list of registered zones. That zone uses a "lite zone"
179+
* if one is present (apparently enabled when malloc stack logging is
180+
* enabled), or the first registered zone otherwise. In practice this
181+
* means unless malloc stack logging is enabled, the first registered
182+
* zone is the default.
183+
* So get the list of zones to get the first one, instead of relying on
184+
* malloc_default_zone.
185+
*/
186+
if (KERN_SUCCESS != malloc_get_all_zones(0, NULL, (vm_address_t**) &zones,
187+
&num_zones)) {
188+
/* Reset the value in case the failure happened after it was set. */
189+
num_zones = 0;
190+
}
191+
192+
if (num_zones)
193+
return zones[0];
194+
195+
return malloc_default_zone();
196+
}
197+
171198
JEMALLOC_ATTR(constructor)
172199
void
173200
register_zone(void)
@@ -177,7 +204,7 @@ register_zone(void)
177204
* If something else replaced the system default zone allocator, don't
178205
* register jemalloc's.
179206
*/
180-
malloc_zone_t *default_zone = malloc_default_zone();
207+
malloc_zone_t *default_zone = get_default_zone();
181208
malloc_zone_t *purgeable_zone = NULL;
182209
if (!default_zone->zone_name ||
183210
strcmp(default_zone->zone_name, "DefaultMallocZone") != 0) {
@@ -272,6 +299,6 @@ register_zone(void)
272299
malloc_zone_register(purgeable_zone);
273300
}
274301

275-
default_zone = malloc_default_zone();
302+
default_zone = get_default_zone();
276303
} while (default_zone != &zone);
277304
}

0 commit comments

Comments
 (0)