Fix/refactor zone allocator integration code.
Fix zone_force_unlock() to reinitialize, rather than unlocking mutexes, since OS X 10.12 cannot tolerate a child unlocking mutexes that were locked by its parent. Refactor; this was a side effect of experimenting with zone {de,re}registration during fork(2).
This commit is contained in:
parent
b54072dfee
commit
d9f7b2a430
@ -383,7 +383,6 @@ psz2ind
|
|||||||
psz2ind_clamp
|
psz2ind_clamp
|
||||||
psz2ind_impl
|
psz2ind_impl
|
||||||
psz2u
|
psz2u
|
||||||
register_zone
|
|
||||||
rtree_child_read
|
rtree_child_read
|
||||||
rtree_child_read_hard
|
rtree_child_read_hard
|
||||||
rtree_child_tryread
|
rtree_child_tryread
|
||||||
@ -537,3 +536,4 @@ witness_postfork_parent
|
|||||||
witness_prefork
|
witness_prefork
|
||||||
witness_unlock
|
witness_unlock
|
||||||
witnesses_cleanup
|
witnesses_cleanup
|
||||||
|
zone_register
|
||||||
|
189
src/zone.c
189
src/zone.c
@ -4,7 +4,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The malloc_default_purgeable_zone function is only available on >= 10.6.
|
* 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.
|
* We need to check whether it is present at runtime, thus the weak_import.
|
||||||
*/
|
*/
|
||||||
extern malloc_zone_t *malloc_default_purgeable_zone(void)
|
extern malloc_zone_t *malloc_default_purgeable_zone(void)
|
||||||
@ -13,8 +13,9 @@ JEMALLOC_ATTR(weak_import);
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* Data. */
|
/* Data. */
|
||||||
|
|
||||||
static malloc_zone_t zone;
|
static malloc_zone_t *default_zone, *purgeable_zone;
|
||||||
static struct malloc_introspection_t zone_introspect;
|
static malloc_zone_t jemalloc_zone;
|
||||||
|
static struct malloc_introspection_t jemalloc_zone_introspect;
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* Function prototypes for non-inline static functions. */
|
/* Function prototypes for non-inline static functions. */
|
||||||
@ -164,12 +165,68 @@ static void
|
|||||||
zone_force_unlock(malloc_zone_t *zone)
|
zone_force_unlock(malloc_zone_t *zone)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Call jemalloc_postfork_child() rather than
|
||||||
|
* jemalloc_postfork_parent(), because this function is executed by both
|
||||||
|
* parent and child. The parent can tolerate having state
|
||||||
|
* reinitialized, but the child cannot unlock mutexes that were locked
|
||||||
|
* by the parent.
|
||||||
|
*/
|
||||||
if (isthreaded)
|
if (isthreaded)
|
||||||
jemalloc_postfork_parent();
|
jemalloc_postfork_child();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
zone_init(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
jemalloc_zone.size = (void *)zone_size;
|
||||||
|
jemalloc_zone.malloc = (void *)zone_malloc;
|
||||||
|
jemalloc_zone.calloc = (void *)zone_calloc;
|
||||||
|
jemalloc_zone.valloc = (void *)zone_valloc;
|
||||||
|
jemalloc_zone.free = (void *)zone_free;
|
||||||
|
jemalloc_zone.realloc = (void *)zone_realloc;
|
||||||
|
jemalloc_zone.destroy = (void *)zone_destroy;
|
||||||
|
jemalloc_zone.zone_name = "jemalloc_zone";
|
||||||
|
jemalloc_zone.batch_malloc = NULL;
|
||||||
|
jemalloc_zone.batch_free = NULL;
|
||||||
|
jemalloc_zone.introspect = &jemalloc_zone_introspect;
|
||||||
|
jemalloc_zone.version = JEMALLOC_ZONE_VERSION;
|
||||||
|
#if (JEMALLOC_ZONE_VERSION >= 5)
|
||||||
|
jemalloc_zone.memalign = zone_memalign;
|
||||||
|
#endif
|
||||||
|
#if (JEMALLOC_ZONE_VERSION >= 6)
|
||||||
|
jemalloc_zone.free_definite_size = zone_free_definite_size;
|
||||||
|
#endif
|
||||||
|
#if (JEMALLOC_ZONE_VERSION >= 8)
|
||||||
|
jemalloc_zone.pressure_relief = NULL;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
jemalloc_zone_introspect.enumerator = NULL;
|
||||||
|
jemalloc_zone_introspect.good_size = (void *)zone_good_size;
|
||||||
|
jemalloc_zone_introspect.check = NULL;
|
||||||
|
jemalloc_zone_introspect.print = NULL;
|
||||||
|
jemalloc_zone_introspect.log = NULL;
|
||||||
|
jemalloc_zone_introspect.force_lock = (void *)zone_force_lock;
|
||||||
|
jemalloc_zone_introspect.force_unlock = (void *)zone_force_unlock;
|
||||||
|
jemalloc_zone_introspect.statistics = NULL;
|
||||||
|
#if (JEMALLOC_ZONE_VERSION >= 6)
|
||||||
|
jemalloc_zone_introspect.zone_locked = NULL;
|
||||||
|
#endif
|
||||||
|
#if (JEMALLOC_ZONE_VERSION >= 7)
|
||||||
|
jemalloc_zone_introspect.enable_discharge_checking = NULL;
|
||||||
|
jemalloc_zone_introspect.disable_discharge_checking = NULL;
|
||||||
|
jemalloc_zone_introspect.discharge = NULL;
|
||||||
|
# ifdef __BLOCKS__
|
||||||
|
jemalloc_zone_introspect.enumerate_discharged_pointers = NULL;
|
||||||
|
# else
|
||||||
|
jemalloc_zone_introspect.enumerate_unavailable_without_blocks = NULL;
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static malloc_zone_t *
|
static malloc_zone_t *
|
||||||
get_default_zone(void)
|
zone_default_get(void)
|
||||||
{
|
{
|
||||||
malloc_zone_t **zones = NULL;
|
malloc_zone_t **zones = NULL;
|
||||||
unsigned int num_zones = 0;
|
unsigned int num_zones = 0;
|
||||||
@ -198,82 +255,11 @@ get_default_zone(void)
|
|||||||
return (malloc_default_zone());
|
return (malloc_default_zone());
|
||||||
}
|
}
|
||||||
|
|
||||||
JEMALLOC_ATTR(constructor)
|
/* As written, this function can only promote jemalloc_zone. */
|
||||||
void
|
static void
|
||||||
register_zone(void)
|
zone_promote(void)
|
||||||
{
|
{
|
||||||
|
malloc_zone_t *zone;
|
||||||
/*
|
|
||||||
* If something else replaced the system default zone allocator, don't
|
|
||||||
* register jemalloc's.
|
|
||||||
*/
|
|
||||||
malloc_zone_t *default_zone = get_default_zone();
|
|
||||||
malloc_zone_t *purgeable_zone = NULL;
|
|
||||||
if (!default_zone->zone_name ||
|
|
||||||
strcmp(default_zone->zone_name, "DefaultMallocZone") != 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
zone.size = (void *)zone_size;
|
|
||||||
zone.malloc = (void *)zone_malloc;
|
|
||||||
zone.calloc = (void *)zone_calloc;
|
|
||||||
zone.valloc = (void *)zone_valloc;
|
|
||||||
zone.free = (void *)zone_free;
|
|
||||||
zone.realloc = (void *)zone_realloc;
|
|
||||||
zone.destroy = (void *)zone_destroy;
|
|
||||||
zone.zone_name = "jemalloc_zone";
|
|
||||||
zone.batch_malloc = NULL;
|
|
||||||
zone.batch_free = NULL;
|
|
||||||
zone.introspect = &zone_introspect;
|
|
||||||
zone.version = JEMALLOC_ZONE_VERSION;
|
|
||||||
#if (JEMALLOC_ZONE_VERSION >= 5)
|
|
||||||
zone.memalign = zone_memalign;
|
|
||||||
#endif
|
|
||||||
#if (JEMALLOC_ZONE_VERSION >= 6)
|
|
||||||
zone.free_definite_size = zone_free_definite_size;
|
|
||||||
#endif
|
|
||||||
#if (JEMALLOC_ZONE_VERSION >= 8)
|
|
||||||
zone.pressure_relief = NULL;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
zone_introspect.enumerator = NULL;
|
|
||||||
zone_introspect.good_size = (void *)zone_good_size;
|
|
||||||
zone_introspect.check = NULL;
|
|
||||||
zone_introspect.print = NULL;
|
|
||||||
zone_introspect.log = NULL;
|
|
||||||
zone_introspect.force_lock = (void *)zone_force_lock;
|
|
||||||
zone_introspect.force_unlock = (void *)zone_force_unlock;
|
|
||||||
zone_introspect.statistics = NULL;
|
|
||||||
#if (JEMALLOC_ZONE_VERSION >= 6)
|
|
||||||
zone_introspect.zone_locked = NULL;
|
|
||||||
#endif
|
|
||||||
#if (JEMALLOC_ZONE_VERSION >= 7)
|
|
||||||
zone_introspect.enable_discharge_checking = NULL;
|
|
||||||
zone_introspect.disable_discharge_checking = NULL;
|
|
||||||
zone_introspect.discharge = NULL;
|
|
||||||
#ifdef __BLOCKS__
|
|
||||||
zone_introspect.enumerate_discharged_pointers = NULL;
|
|
||||||
#else
|
|
||||||
zone_introspect.enumerate_unavailable_without_blocks = NULL;
|
|
||||||
#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 created when the default zone is still
|
|
||||||
* a scalable_zone. As purgeable zones only exist on >= 10.6, we need
|
|
||||||
* to check for the existence of malloc_default_purgeable_zone() at
|
|
||||||
* run time.
|
|
||||||
*/
|
|
||||||
if (malloc_default_purgeable_zone != NULL)
|
|
||||||
purgeable_zone = malloc_default_purgeable_zone();
|
|
||||||
|
|
||||||
/* Register the custom zone. At this point it won't be the default. */
|
|
||||||
malloc_zone_register(&zone);
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
/*
|
/*
|
||||||
@ -286,6 +272,7 @@ register_zone(void)
|
|||||||
*/
|
*/
|
||||||
malloc_zone_unregister(default_zone);
|
malloc_zone_unregister(default_zone);
|
||||||
malloc_zone_register(default_zone);
|
malloc_zone_register(default_zone);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* On OSX 10.6, having the default purgeable zone appear before
|
* On OSX 10.6, having the default purgeable zone appear before
|
||||||
* the default zone makes some things crash because it thinks it
|
* the default zone makes some things crash because it thinks it
|
||||||
@ -297,11 +284,47 @@ register_zone(void)
|
|||||||
* above, i.e. the default zone. Registering it again then puts
|
* above, i.e. the default zone. Registering it again then puts
|
||||||
* it at the end, obviously after the default zone.
|
* it at the end, obviously after the default zone.
|
||||||
*/
|
*/
|
||||||
if (purgeable_zone) {
|
if (purgeable_zone != NULL) {
|
||||||
malloc_zone_unregister(purgeable_zone);
|
malloc_zone_unregister(purgeable_zone);
|
||||||
malloc_zone_register(purgeable_zone);
|
malloc_zone_register(purgeable_zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
default_zone = get_default_zone();
|
zone = zone_default_get();
|
||||||
} while (default_zone != &zone);
|
} while (zone != &jemalloc_zone);
|
||||||
|
}
|
||||||
|
|
||||||
|
JEMALLOC_ATTR(constructor)
|
||||||
|
void
|
||||||
|
zone_register(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If something else replaced the system default zone allocator, don't
|
||||||
|
* register jemalloc's.
|
||||||
|
*/
|
||||||
|
default_zone = zone_default_get();
|
||||||
|
if (!default_zone->zone_name || strcmp(default_zone->zone_name,
|
||||||
|
"DefaultMallocZone") != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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 created when the default zone is still
|
||||||
|
* a scalable_zone. As purgeable zones only exist on >= 10.6, we need
|
||||||
|
* to check for the existence of malloc_default_purgeable_zone() at
|
||||||
|
* run time.
|
||||||
|
*/
|
||||||
|
purgeable_zone = (malloc_default_purgeable_zone == NULL) ? NULL :
|
||||||
|
malloc_default_purgeable_zone();
|
||||||
|
|
||||||
|
/* Register the custom zone. At this point it won't be the default. */
|
||||||
|
zone_init();
|
||||||
|
malloc_zone_register(&jemalloc_zone);
|
||||||
|
|
||||||
|
/* Promote the custom zone to be default. */
|
||||||
|
zone_promote();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user