| 1 | --- src/zone.c |
| 2 | +++ src/zone.c |
| 3 | @@ -168,6 +168,33 @@ zone_force_unlock(malloc_zone_t *zone) |
| 4 | jemalloc_postfork_parent(); |
| 5 | } |
| 6 | |
| 7 | +static malloc_zone_t *get_default_zone() |
| 8 | +{ |
| 9 | + malloc_zone_t **zones = NULL; |
| 10 | + unsigned int num_zones = 0; |
| 11 | + |
| 12 | + /* |
| 13 | + * On OSX 10.12, malloc_default_zone returns a special zone that is not |
| 14 | + * present in the list of registered zones. That zone uses a "lite zone" |
| 15 | + * if one is present (apparently enabled when malloc stack logging is |
| 16 | + * enabled), or the first registered zone otherwise. In practice this |
| 17 | + * means unless malloc stack logging is enabled, the first registered |
| 18 | + * zone is the default. |
| 19 | + * So get the list of zones to get the first one, instead of relying on |
| 20 | + * malloc_default_zone. |
| 21 | + */ |
| 22 | + if (KERN_SUCCESS != malloc_get_all_zones(0, NULL, (vm_address_t**) &zones, |
| 23 | + &num_zones)) { |
| 24 | + /* Reset the value in case the failure happened after it was set. */ |
| 25 | + num_zones = 0; |
| 26 | + } |
| 27 | + |
| 28 | + if (num_zones) |
| 29 | + return zones[0]; |
| 30 | + |
| 31 | + return malloc_default_zone(); |
| 32 | +} |
| 33 | + |
| 34 | JEMALLOC_ATTR(constructor) |
| 35 | void |
| 36 | register_zone(void) |
| 37 | @@ -177,7 +204,7 @@ register_zone(void) |
| 38 | * If something else replaced the system default zone allocator, don't |
| 39 | * register jemalloc's. |
| 40 | */ |
| 41 | - malloc_zone_t *default_zone = malloc_default_zone(); |
| 42 | + malloc_zone_t *default_zone = get_default_zone(); |
| 43 | malloc_zone_t *purgeable_zone = NULL; |
| 44 | if (!default_zone->zone_name || |
| 45 | strcmp(default_zone->zone_name, "DefaultMallocZone") != 0) { |
| 46 | @@ -246,7 +273,6 @@ register_zone(void) |
| 47 | malloc_zone_register(&zone); |
| 48 | |
| 49 | do { |
| 50 | - default_zone = malloc_default_zone(); |
| 51 | /* |
| 52 | * Unregister and reregister the default zone. On OSX >= 10.6, |
| 53 | * unregistering takes the last registered zone and places it |
| 54 | @@ -272,5 +298,7 @@ register_zone(void) |
| 55 | malloc_zone_unregister(purgeable_zone); |
| 56 | malloc_zone_register(purgeable_zone); |
| 57 | } |
| 58 | - } while (malloc_default_zone() != &zone); |
| 59 | + |
| 60 | + default_zone = get_default_zone(); |
| 61 | + } while (default_zone != &zone); |
| 62 | } |