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

Commit da18fef

Browse files
authored
Merge pull request #15 from glandium/rust
Fix for rust-lang/rust#34674
2 parents aab1c0a + e058ca6 commit da18fef

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/zone.c

Lines changed: 31 additions & 3 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) {
@@ -246,7 +273,6 @@ register_zone(void)
246273
malloc_zone_register(&zone);
247274

248275
do {
249-
default_zone = malloc_default_zone();
250276
/*
251277
* Unregister and reregister the default zone. On OSX >= 10.6,
252278
* unregistering takes the last registered zone and places it
@@ -272,5 +298,7 @@ register_zone(void)
272298
malloc_zone_unregister(purgeable_zone);
273299
malloc_zone_register(purgeable_zone);
274300
}
275-
} while (malloc_default_zone() != &zone);
301+
302+
default_zone = get_default_zone();
303+
} while (default_zone != &zone);
276304
}

0 commit comments

Comments
 (0)