2017-04-11 09:17:55 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
|
|
|
#include "jemalloc/internal/jemalloc_internal_includes.h"
|
2017-04-12 05:43:12 +08:00
|
|
|
|
|
|
|
#include "jemalloc/internal/assert.h"
|
|
|
|
|
2010-09-06 01:35:13 +08:00
|
|
|
#ifndef JEMALLOC_ZONE
|
|
|
|
# error "This source file is for zones on Darwin (OS X)."
|
|
|
|
#endif
|
|
|
|
|
2017-01-17 14:54:36 +08:00
|
|
|
/* Definitions of the following structs in malloc/malloc.h might be too old
|
|
|
|
* for the built binary to run on newer versions of OSX. So use the newest
|
|
|
|
* possible version of those structs.
|
|
|
|
*/
|
|
|
|
typedef struct _malloc_zone_t {
|
|
|
|
void *reserved1;
|
|
|
|
void *reserved2;
|
|
|
|
size_t (*size)(struct _malloc_zone_t *, const void *);
|
|
|
|
void *(*malloc)(struct _malloc_zone_t *, size_t);
|
|
|
|
void *(*calloc)(struct _malloc_zone_t *, size_t, size_t);
|
|
|
|
void *(*valloc)(struct _malloc_zone_t *, size_t);
|
|
|
|
void (*free)(struct _malloc_zone_t *, void *);
|
|
|
|
void *(*realloc)(struct _malloc_zone_t *, void *, size_t);
|
|
|
|
void (*destroy)(struct _malloc_zone_t *);
|
|
|
|
const char *zone_name;
|
|
|
|
unsigned (*batch_malloc)(struct _malloc_zone_t *, size_t, void **, unsigned);
|
|
|
|
void (*batch_free)(struct _malloc_zone_t *, void **, unsigned);
|
|
|
|
struct malloc_introspection_t *introspect;
|
|
|
|
unsigned version;
|
|
|
|
void *(*memalign)(struct _malloc_zone_t *, size_t, size_t);
|
|
|
|
void (*free_definite_size)(struct _malloc_zone_t *, void *, size_t);
|
|
|
|
size_t (*pressure_relief)(struct _malloc_zone_t *, size_t);
|
|
|
|
} malloc_zone_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
vm_address_t address;
|
|
|
|
vm_size_t size;
|
|
|
|
} vm_range_t;
|
|
|
|
|
|
|
|
typedef struct malloc_statistics_t {
|
|
|
|
unsigned blocks_in_use;
|
|
|
|
size_t size_in_use;
|
|
|
|
size_t max_size_in_use;
|
|
|
|
size_t size_allocated;
|
|
|
|
} malloc_statistics_t;
|
|
|
|
|
|
|
|
typedef kern_return_t memory_reader_t(task_t, vm_address_t, vm_size_t, void **);
|
|
|
|
|
|
|
|
typedef void vm_range_recorder_t(task_t, void *, unsigned type, vm_range_t *, unsigned);
|
|
|
|
|
|
|
|
typedef struct malloc_introspection_t {
|
|
|
|
kern_return_t (*enumerator)(task_t, void *, unsigned, vm_address_t, memory_reader_t, vm_range_recorder_t);
|
|
|
|
size_t (*good_size)(malloc_zone_t *, size_t);
|
|
|
|
boolean_t (*check)(malloc_zone_t *);
|
|
|
|
void (*print)(malloc_zone_t *, boolean_t);
|
|
|
|
void (*log)(malloc_zone_t *, void *);
|
|
|
|
void (*force_lock)(malloc_zone_t *);
|
|
|
|
void (*force_unlock)(malloc_zone_t *);
|
|
|
|
void (*statistics)(malloc_zone_t *, malloc_statistics_t *);
|
|
|
|
boolean_t (*zone_locked)(malloc_zone_t *);
|
|
|
|
boolean_t (*enable_discharge_checking)(malloc_zone_t *);
|
|
|
|
boolean_t (*disable_discharge_checking)(malloc_zone_t *);
|
|
|
|
void (*discharge)(malloc_zone_t *, void *);
|
|
|
|
#ifdef __BLOCKS__
|
|
|
|
void (*enumerate_discharged_pointers)(malloc_zone_t *, void (^)(void *, void *));
|
|
|
|
#else
|
|
|
|
void *enumerate_unavailable_without_blocks;
|
|
|
|
#endif
|
|
|
|
void (*reinit_lock)(malloc_zone_t *);
|
|
|
|
} malloc_introspection_t;
|
|
|
|
|
|
|
|
extern kern_return_t malloc_get_all_zones(task_t, memory_reader_t, vm_address_t **, unsigned *);
|
|
|
|
|
|
|
|
extern malloc_zone_t *malloc_default_zone(void);
|
|
|
|
|
|
|
|
extern void malloc_zone_register(malloc_zone_t *zone);
|
|
|
|
|
|
|
|
extern void malloc_zone_unregister(malloc_zone_t *zone);
|
|
|
|
|
2012-03-27 20:20:13 +08:00
|
|
|
/*
|
2016-11-03 09:06:40 +08:00
|
|
|
* The malloc_default_purgeable_zone() function is only available on >= 10.6.
|
2012-03-27 20:20:13 +08:00
|
|
|
* We need to check whether it is present at runtime, thus the weak_import.
|
|
|
|
*/
|
|
|
|
extern malloc_zone_t *malloc_default_purgeable_zone(void)
|
|
|
|
JEMALLOC_ATTR(weak_import);
|
|
|
|
|
2010-09-06 01:35:13 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
2016-11-03 09:06:40 +08:00
|
|
|
static malloc_zone_t *default_zone, *purgeable_zone;
|
|
|
|
static malloc_zone_t jemalloc_zone;
|
|
|
|
static struct malloc_introspection_t jemalloc_zone_introspect;
|
2017-07-11 05:05:33 +08:00
|
|
|
static pid_t zone_force_lock_pid = -1;
|
2010-09-06 01:35:13 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Function prototypes for non-inline static functions. */
|
|
|
|
|
2017-01-17 14:54:36 +08:00
|
|
|
static size_t zone_size(malloc_zone_t *zone, const void *ptr);
|
2010-09-06 01:35:13 +08:00
|
|
|
static void *zone_malloc(malloc_zone_t *zone, size_t size);
|
|
|
|
static void *zone_calloc(malloc_zone_t *zone, size_t num, size_t size);
|
|
|
|
static void *zone_valloc(malloc_zone_t *zone, size_t size);
|
|
|
|
static void zone_free(malloc_zone_t *zone, void *ptr);
|
|
|
|
static void *zone_realloc(malloc_zone_t *zone, void *ptr, size_t size);
|
|
|
|
static void *zone_memalign(malloc_zone_t *zone, size_t alignment,
|
|
|
|
size_t size);
|
|
|
|
static void zone_free_definite_size(malloc_zone_t *zone, void *ptr,
|
|
|
|
size_t size);
|
2017-01-17 14:54:36 +08:00
|
|
|
static void zone_destroy(malloc_zone_t *zone);
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
static unsigned zone_batch_malloc(struct _malloc_zone_t *zone, size_t size,
|
|
|
|
void **results, unsigned num_requested);
|
|
|
|
static void zone_batch_free(struct _malloc_zone_t *zone,
|
|
|
|
void **to_be_freed, unsigned num_to_be_freed);
|
|
|
|
static size_t zone_pressure_relief(struct _malloc_zone_t *zone, size_t goal);
|
2010-09-06 01:35:13 +08:00
|
|
|
static size_t zone_good_size(malloc_zone_t *zone, size_t size);
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
static kern_return_t zone_enumerator(task_t task, void *data, unsigned type_mask,
|
|
|
|
vm_address_t zone_address, memory_reader_t reader,
|
|
|
|
vm_range_recorder_t recorder);
|
|
|
|
static boolean_t zone_check(malloc_zone_t *zone);
|
|
|
|
static void zone_print(malloc_zone_t *zone, boolean_t verbose);
|
|
|
|
static void zone_log(malloc_zone_t *zone, void *address);
|
2010-09-06 01:35:13 +08:00
|
|
|
static void zone_force_lock(malloc_zone_t *zone);
|
|
|
|
static void zone_force_unlock(malloc_zone_t *zone);
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
static void zone_statistics(malloc_zone_t *zone,
|
|
|
|
malloc_statistics_t *stats);
|
|
|
|
static boolean_t zone_locked(malloc_zone_t *zone);
|
|
|
|
static void zone_reinit_lock(malloc_zone_t *zone);
|
2010-09-06 01:35:13 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*
|
|
|
|
* Functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_size(malloc_zone_t *zone, const void *ptr) {
|
2010-09-06 01:35:13 +08:00
|
|
|
/*
|
|
|
|
* There appear to be places within Darwin (such as setenv(3)) that
|
|
|
|
* cause calls to this function with pointers that *no* zone owns. If
|
|
|
|
* we knew that all pointers were owned by *some* zone, we could split
|
|
|
|
* our zone into two parts, and use one as the default allocator and
|
|
|
|
* the other as the default deallocator/reallocator. Since that will
|
|
|
|
* not work in practice, we must check all pointers to assure that they
|
2016-06-02 04:53:56 +08:00
|
|
|
* reside within a mapped extent before determining size.
|
2010-09-06 01:35:13 +08:00
|
|
|
*/
|
2017-01-20 10:15:45 +08:00
|
|
|
return ivsalloc(tsdn_fetch(), ptr);
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_malloc(malloc_zone_t *zone, size_t size) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return je_malloc(size);
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_calloc(malloc_zone_t *zone, size_t num, size_t size) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return je_calloc(num, size);
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_valloc(malloc_zone_t *zone, size_t size) {
|
2010-09-06 01:35:13 +08:00
|
|
|
void *ret = NULL; /* Assignment avoids useless compiler warning. */
|
|
|
|
|
2012-04-02 22:04:34 +08:00
|
|
|
je_posix_memalign(&ret, PAGE, size);
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_free(malloc_zone_t *zone, void *ptr) {
|
2016-05-28 15:17:28 +08:00
|
|
|
if (ivsalloc(tsdn_fetch(), ptr) != 0) {
|
2012-03-27 00:39:35 +08:00
|
|
|
je_free(ptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ptr);
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_realloc(malloc_zone_t *zone, void *ptr, size_t size) {
|
|
|
|
if (ivsalloc(tsdn_fetch(), ptr) != 0) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return je_realloc(ptr, size);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-27 00:39:35 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return realloc(ptr, size);
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_memalign(malloc_zone_t *zone, size_t alignment, size_t size) {
|
2010-09-06 01:35:13 +08:00
|
|
|
void *ret = NULL; /* Assignment avoids useless compiler warning. */
|
|
|
|
|
2012-03-02 09:19:20 +08:00
|
|
|
je_posix_memalign(&ret, alignment, size);
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size) {
|
2015-11-12 19:59:29 +08:00
|
|
|
size_t alloc_size;
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2016-05-28 15:17:28 +08:00
|
|
|
alloc_size = ivsalloc(tsdn_fetch(), ptr);
|
2015-11-12 19:59:29 +08:00
|
|
|
if (alloc_size != 0) {
|
|
|
|
assert(alloc_size == size);
|
2012-03-27 00:39:35 +08:00
|
|
|
je_free(ptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ptr);
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
2017-01-17 14:54:36 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_destroy(malloc_zone_t *zone) {
|
2010-09-06 01:35:13 +08:00
|
|
|
/* This function should never be called. */
|
2013-10-22 05:56:27 +08:00
|
|
|
not_reached();
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
static unsigned
|
|
|
|
zone_batch_malloc(struct _malloc_zone_t *zone, size_t size, void **results,
|
2017-01-16 08:56:30 +08:00
|
|
|
unsigned num_requested) {
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_requested; i++) {
|
|
|
|
results[i] = je_malloc(size);
|
|
|
|
if (!results[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
zone_batch_free(struct _malloc_zone_t *zone, void **to_be_freed,
|
2017-01-16 08:56:30 +08:00
|
|
|
unsigned num_to_be_freed) {
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_to_be_freed; i++) {
|
|
|
|
zone_free(zone, to_be_freed[i]);
|
|
|
|
to_be_freed[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_pressure_relief(struct _malloc_zone_t *zone, size_t goal) {
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-06 01:35:13 +08:00
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_good_size(malloc_zone_t *zone, size_t size) {
|
|
|
|
if (size == 0) {
|
2012-03-01 04:58:39 +08:00
|
|
|
size = 1;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-05-31 01:45:37 +08:00
|
|
|
return sz_s2u(size);
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
static kern_return_t
|
|
|
|
zone_enumerator(task_t task, void *data, unsigned type_mask,
|
|
|
|
vm_address_t zone_address, memory_reader_t reader,
|
2017-01-16 08:56:30 +08:00
|
|
|
vm_range_recorder_t recorder) {
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
return KERN_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean_t
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_check(malloc_zone_t *zone) {
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_print(malloc_zone_t *zone, boolean_t verbose) {
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_log(malloc_zone_t *zone, void *address) {
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
}
|
|
|
|
|
2010-09-06 01:35:13 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_force_lock(malloc_zone_t *zone) {
|
|
|
|
if (isthreaded) {
|
2017-07-11 05:05:33 +08:00
|
|
|
/*
|
|
|
|
* See the note in zone_force_unlock, below, to see why we need
|
|
|
|
* this.
|
|
|
|
*/
|
|
|
|
assert(zone_force_lock_pid == -1);
|
|
|
|
zone_force_lock_pid = getpid();
|
2010-09-06 01:35:13 +08:00
|
|
|
jemalloc_prefork();
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_force_unlock(malloc_zone_t *zone) {
|
2016-11-03 09:06:40 +08:00
|
|
|
/*
|
2017-07-11 05:05:33 +08:00
|
|
|
* zone_force_lock and zone_force_unlock are the entry points to the
|
|
|
|
* forking machinery on OS X. The tricky thing is, the child is not
|
|
|
|
* allowed to unlock mutexes locked in the parent, even if owned by the
|
|
|
|
* forking thread (and the mutex type we use in OS X will fail an assert
|
|
|
|
* if we try). In the child, we can get away with reinitializing all
|
|
|
|
* the mutexes, which has the effect of unlocking them. In the parent,
|
|
|
|
* doing this would mean we wouldn't wake any waiters blocked on the
|
|
|
|
* mutexes we unlock. So, we record the pid of the current thread in
|
|
|
|
* zone_force_lock, and use that to detect if we're in the parent or
|
|
|
|
* child here, to decide which unlock logic we need.
|
2016-11-03 09:06:40 +08:00
|
|
|
*/
|
2017-01-16 08:56:30 +08:00
|
|
|
if (isthreaded) {
|
2017-07-11 05:05:33 +08:00
|
|
|
assert(zone_force_lock_pid != -1);
|
|
|
|
if (getpid() == zone_force_lock_pid) {
|
|
|
|
jemalloc_postfork_parent();
|
|
|
|
} else {
|
|
|
|
jemalloc_postfork_child();
|
|
|
|
}
|
|
|
|
zone_force_lock_pid = -1;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-11-03 09:06:40 +08:00
|
|
|
}
|
|
|
|
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_statistics(malloc_zone_t *zone, malloc_statistics_t *stats) {
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
/* We make no effort to actually fill the values */
|
|
|
|
stats->blocks_in_use = 0;
|
|
|
|
stats->size_in_use = 0;
|
|
|
|
stats->max_size_in_use = 0;
|
|
|
|
stats->size_allocated = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean_t
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_locked(malloc_zone_t *zone) {
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
/* Pretend no lock is being held */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_reinit_lock(malloc_zone_t *zone) {
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
/* As of OSX 10.12, this function is only used when force_unlock would
|
|
|
|
* be used if the zone version were < 9. So just use force_unlock. */
|
|
|
|
zone_force_unlock(zone);
|
|
|
|
}
|
|
|
|
|
2016-11-03 09:06:40 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_init(void) {
|
2017-01-17 14:54:36 +08:00
|
|
|
jemalloc_zone.size = zone_size;
|
|
|
|
jemalloc_zone.malloc = zone_malloc;
|
|
|
|
jemalloc_zone.calloc = zone_calloc;
|
|
|
|
jemalloc_zone.valloc = zone_valloc;
|
|
|
|
jemalloc_zone.free = zone_free;
|
|
|
|
jemalloc_zone.realloc = zone_realloc;
|
|
|
|
jemalloc_zone.destroy = zone_destroy;
|
2016-11-03 09:06:40 +08:00
|
|
|
jemalloc_zone.zone_name = "jemalloc_zone";
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
jemalloc_zone.batch_malloc = zone_batch_malloc;
|
|
|
|
jemalloc_zone.batch_free = zone_batch_free;
|
2016-11-03 09:06:40 +08:00
|
|
|
jemalloc_zone.introspect = &jemalloc_zone_introspect;
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
jemalloc_zone.version = 9;
|
2016-11-03 09:06:40 +08:00
|
|
|
jemalloc_zone.memalign = zone_memalign;
|
|
|
|
jemalloc_zone.free_definite_size = zone_free_definite_size;
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
jemalloc_zone.pressure_relief = zone_pressure_relief;
|
2016-11-03 09:06:40 +08:00
|
|
|
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
jemalloc_zone_introspect.enumerator = zone_enumerator;
|
2017-01-17 14:54:36 +08:00
|
|
|
jemalloc_zone_introspect.good_size = zone_good_size;
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
jemalloc_zone_introspect.check = zone_check;
|
|
|
|
jemalloc_zone_introspect.print = zone_print;
|
|
|
|
jemalloc_zone_introspect.log = zone_log;
|
2017-01-17 14:54:36 +08:00
|
|
|
jemalloc_zone_introspect.force_lock = zone_force_lock;
|
|
|
|
jemalloc_zone_introspect.force_unlock = zone_force_unlock;
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
jemalloc_zone_introspect.statistics = zone_statistics;
|
|
|
|
jemalloc_zone_introspect.zone_locked = zone_locked;
|
2016-11-03 09:06:40 +08:00
|
|
|
jemalloc_zone_introspect.enable_discharge_checking = NULL;
|
|
|
|
jemalloc_zone_introspect.disable_discharge_checking = NULL;
|
|
|
|
jemalloc_zone_introspect.discharge = NULL;
|
2017-01-17 14:54:36 +08:00
|
|
|
#ifdef __BLOCKS__
|
2016-11-03 09:06:40 +08:00
|
|
|
jemalloc_zone_introspect.enumerate_discharged_pointers = NULL;
|
2017-01-17 14:54:36 +08:00
|
|
|
#else
|
2016-11-03 09:06:40 +08:00
|
|
|
jemalloc_zone_introspect.enumerate_unavailable_without_blocks = NULL;
|
|
|
|
#endif
|
Add dummy implementations for most remaining OSX zone allocator functions
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
2017-01-17 15:20:05 +08:00
|
|
|
jemalloc_zone_introspect.reinit_lock = zone_reinit_lock;
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
2016-09-27 02:00:32 +08:00
|
|
|
static malloc_zone_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_default_get(void) {
|
2016-07-08 12:35:35 +08:00
|
|
|
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
|
2016-09-27 02:00:32 +08:00
|
|
|
* zone is the default. So get the list of zones to get the first one,
|
|
|
|
* instead of relying on malloc_default_zone.
|
2016-07-08 12:35:35 +08:00
|
|
|
*/
|
2016-11-03 09:06:40 +08:00
|
|
|
if (KERN_SUCCESS != malloc_get_all_zones(0, NULL,
|
2016-09-27 02:00:32 +08:00
|
|
|
(vm_address_t**)&zones, &num_zones)) {
|
|
|
|
/*
|
|
|
|
* Reset the value in case the failure happened after it was
|
|
|
|
* set.
|
|
|
|
*/
|
2016-07-08 12:35:35 +08:00
|
|
|
num_zones = 0;
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (num_zones) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return zones[0];
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-07-08 12:35:35 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return malloc_default_zone();
|
2016-07-08 12:35:35 +08:00
|
|
|
}
|
|
|
|
|
2016-11-03 09:06:40 +08:00
|
|
|
/* As written, this function can only promote jemalloc_zone. */
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_promote(void) {
|
2016-11-03 09:06:40 +08:00
|
|
|
malloc_zone_t *zone;
|
2012-03-27 20:20:12 +08:00
|
|
|
|
|
|
|
do {
|
2014-06-10 17:18:22 +08:00
|
|
|
/*
|
|
|
|
* Unregister and reregister the default zone. On OSX >= 10.6,
|
|
|
|
* unregistering takes the last registered zone and places it
|
|
|
|
* at the location of the specified zone. Unregistering the
|
|
|
|
* default zone thus makes the last registered one the default.
|
|
|
|
* On OSX < 10.6, unregistering shifts all registered zones.
|
|
|
|
* The first registered zone then becomes the default.
|
|
|
|
*/
|
2012-03-27 20:20:12 +08:00
|
|
|
malloc_zone_unregister(default_zone);
|
|
|
|
malloc_zone_register(default_zone);
|
2016-11-03 09:06:40 +08:00
|
|
|
|
2014-06-10 17:18:22 +08:00
|
|
|
/*
|
|
|
|
* On OSX 10.6, having the default purgeable zone appear before
|
|
|
|
* the default zone makes some things crash because it thinks it
|
2014-09-05 13:27:26 +08:00
|
|
|
* owns the default zone allocated pointers. We thus
|
|
|
|
* unregister/re-register it in order to ensure it's always
|
|
|
|
* after the default zone. On OSX < 10.6, there is no purgeable
|
|
|
|
* zone, so this does nothing. On OSX >= 10.6, unregistering
|
|
|
|
* replaces the purgeable zone with the last registered zone
|
2014-12-09 06:40:14 +08:00
|
|
|
* above, i.e. the default zone. Registering it again then puts
|
2014-09-05 13:27:26 +08:00
|
|
|
* it at the end, obviously after the default zone.
|
2014-06-10 17:18:22 +08:00
|
|
|
*/
|
2016-11-03 09:06:40 +08:00
|
|
|
if (purgeable_zone != NULL) {
|
2014-06-10 17:18:22 +08:00
|
|
|
malloc_zone_unregister(purgeable_zone);
|
|
|
|
malloc_zone_register(purgeable_zone);
|
|
|
|
}
|
2016-07-08 12:28:16 +08:00
|
|
|
|
2016-11-03 09:06:40 +08:00
|
|
|
zone = zone_default_get();
|
|
|
|
} while (zone != &jemalloc_zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ATTR(constructor)
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
zone_register(void) {
|
2016-11-03 09:06:40 +08:00
|
|
|
/*
|
|
|
|
* 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,
|
2017-01-16 08:56:30 +08:00
|
|
|
"DefaultMallocZone") != 0) {
|
2016-11-03 09:06:40 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-11-03 09:06:40 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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();
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|