Skip to content

Commit

Permalink
Bug 1284677 - Change how the default OSX malloc zone is found. r=njn
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Jul 8, 2016
1 parent 319e5bf commit 9bff51e
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions memory/build/replace_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,38 @@ zone_force_unlock(malloc_zone_t *zone)
static malloc_zone_t zone;
static struct malloc_introspection_t zone_introspect;

static malloc_zone_t *get_default_zone()
{
malloc_zone_t **zones = NULL;
unsigned int num_zones = 0;

/*
* 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.
*/
if (KERN_SUCCESS != malloc_get_all_zones(0, NULL, (vm_address_t**) &zones,
&num_zones)) {
/* Reset the value in case the failure happened after it was set. */
num_zones = 0;
}
if (num_zones) {
return zones[0];
}
return malloc_default_zone();
}


__attribute__((constructor)) void
register_zone(void)
{
malloc_zone_t *default_zone = get_default_zone();

zone.size = (void *)zone_size;
zone.malloc = (void *)zone_malloc;
zone.calloc = (void *)zone_calloc;
Expand Down Expand Up @@ -488,7 +517,6 @@ register_zone(void)
malloc_zone_register(&zone);

do {
malloc_zone_t *default_zone = malloc_default_zone();
/*
* Unregister and reregister the default zone. On OSX >= 10.6,
* unregistering takes the last registered zone and places it at the
Expand All @@ -512,6 +540,7 @@ register_zone(void)
*/
malloc_zone_unregister(purgeable_zone);
malloc_zone_register(purgeable_zone);
} while (malloc_default_zone() != &zone);
default_zone = get_default_zone();
} while (default_zone != &zone);
}
#endif

0 comments on commit 9bff51e

Please sign in to comment.