2017-04-11 09:17:55 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
|
|
|
#include "jemalloc/internal/jemalloc_internal_includes.h"
|
2012-03-22 09:33:03 +08:00
|
|
|
|
2017-04-12 05:43:12 +08:00
|
|
|
#include "jemalloc/internal/assert.h"
|
2017-05-24 03:28:19 +08:00
|
|
|
#include "jemalloc/internal/mutex.h"
|
2017-05-24 05:26:31 +08:00
|
|
|
#include "jemalloc/internal/rtree.h"
|
2017-04-12 05:43:12 +08:00
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
|
|
|
static unsigned ncleanups;
|
|
|
|
static malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX];
|
|
|
|
|
2018-05-03 17:40:53 +08:00
|
|
|
/* TSD_INITIALIZER triggers "-Wmissing-field-initializer" */
|
|
|
|
JEMALLOC_DIAGNOSTIC_PUSH
|
|
|
|
JEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS
|
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
#ifdef JEMALLOC_MALLOC_THREAD_CLEANUP
|
2019-04-16 06:36:31 +08:00
|
|
|
JEMALLOC_TSD_TYPE_ATTR(tsd_t) tsd_tls = TSD_INITIALIZER;
|
|
|
|
JEMALLOC_TSD_TYPE_ATTR(bool) JEMALLOC_TLS_MODEL tsd_initialized = false;
|
2017-04-27 09:37:44 +08:00
|
|
|
bool tsd_booted = false;
|
|
|
|
#elif (defined(JEMALLOC_TLS))
|
2019-04-16 06:36:31 +08:00
|
|
|
JEMALLOC_TSD_TYPE_ATTR(tsd_t) tsd_tls = TSD_INITIALIZER;
|
2017-04-27 09:37:44 +08:00
|
|
|
pthread_key_t tsd_tsd;
|
|
|
|
bool tsd_booted = false;
|
|
|
|
#elif (defined(_WIN32))
|
|
|
|
DWORD tsd_tsd;
|
|
|
|
tsd_wrapper_t tsd_boot_wrapper = {false, TSD_INITIALIZER};
|
|
|
|
bool tsd_booted = false;
|
|
|
|
#else
|
2017-05-24 03:28:19 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This contains a mutex, but it's pretty convenient to allow the mutex code to
|
|
|
|
* have a dependency on tsd. So we define the struct here, and only refer to it
|
|
|
|
* by pointer in the header.
|
|
|
|
*/
|
|
|
|
struct tsd_init_head_s {
|
|
|
|
ql_head(tsd_init_block_t) blocks;
|
|
|
|
malloc_mutex_t lock;
|
|
|
|
};
|
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
pthread_key_t tsd_tsd;
|
|
|
|
tsd_init_head_t tsd_init_head = {
|
|
|
|
ql_head_initializer(blocks),
|
|
|
|
MALLOC_MUTEX_INITIALIZER
|
|
|
|
};
|
2018-05-03 17:40:53 +08:00
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
tsd_wrapper_t tsd_boot_wrapper = {
|
|
|
|
false,
|
|
|
|
TSD_INITIALIZER
|
|
|
|
};
|
|
|
|
bool tsd_booted = false;
|
|
|
|
#endif
|
|
|
|
|
2018-05-03 17:40:53 +08:00
|
|
|
JEMALLOC_DIAGNOSTIC_POP
|
2014-09-23 12:09:23 +08:00
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-04-03 10:16:41 +08:00
|
|
|
/* A list of all the tsds in the nominal state. */
|
|
|
|
typedef ql_head(tsd_t) tsd_list_t;
|
|
|
|
static tsd_list_t tsd_nominal_tsds = ql_head_initializer(tsd_nominal_tsds);
|
|
|
|
static malloc_mutex_t tsd_nominal_tsds_lock;
|
|
|
|
|
|
|
|
/* How many slow-path-enabling features are turned on. */
|
|
|
|
static atomic_u32_t tsd_global_slow_count = ATOMIC_INIT(0);
|
|
|
|
|
|
|
|
static bool
|
|
|
|
tsd_in_nominal_list(tsd_t *tsd) {
|
|
|
|
tsd_t *tsd_list;
|
|
|
|
bool found = false;
|
|
|
|
/*
|
|
|
|
* We don't know that tsd is nominal; it might not be safe to get data
|
|
|
|
* out of it here.
|
|
|
|
*/
|
|
|
|
malloc_mutex_lock(TSDN_NULL, &tsd_nominal_tsds_lock);
|
2020-04-08 07:59:57 +08:00
|
|
|
ql_foreach(tsd_list, &tsd_nominal_tsds, TSD_MANGLE(tsd_link)) {
|
2018-04-03 10:16:41 +08:00
|
|
|
if (tsd == tsd_list) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
malloc_mutex_unlock(TSDN_NULL, &tsd_nominal_tsds_lock);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tsd_add_nominal(tsd_t *tsd) {
|
|
|
|
assert(!tsd_in_nominal_list(tsd));
|
|
|
|
assert(tsd_state_get(tsd) <= tsd_state_nominal_max);
|
2020-04-08 07:59:57 +08:00
|
|
|
ql_elm_new(tsd, TSD_MANGLE(tsd_link));
|
2018-04-03 10:16:41 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);
|
2020-04-08 07:59:57 +08:00
|
|
|
ql_tail_insert(&tsd_nominal_tsds, tsd, TSD_MANGLE(tsd_link));
|
2018-04-03 10:16:41 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tsd_remove_nominal(tsd_t *tsd) {
|
|
|
|
assert(tsd_in_nominal_list(tsd));
|
|
|
|
assert(tsd_state_get(tsd) <= tsd_state_nominal_max);
|
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);
|
2020-04-08 07:59:57 +08:00
|
|
|
ql_remove(&tsd_nominal_tsds, tsd, TSD_MANGLE(tsd_link));
|
2018-04-03 10:16:41 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tsd_force_recompute(tsdn_t *tsdn) {
|
|
|
|
/*
|
|
|
|
* The stores to tsd->state here need to synchronize with the exchange
|
|
|
|
* in tsd_slow_update.
|
|
|
|
*/
|
|
|
|
atomic_fence(ATOMIC_RELEASE);
|
|
|
|
malloc_mutex_lock(tsdn, &tsd_nominal_tsds_lock);
|
|
|
|
tsd_t *remote_tsd;
|
2020-04-08 07:59:57 +08:00
|
|
|
ql_foreach(remote_tsd, &tsd_nominal_tsds, TSD_MANGLE(tsd_link)) {
|
2019-03-08 08:01:55 +08:00
|
|
|
assert(tsd_atomic_load(&remote_tsd->state, ATOMIC_RELAXED)
|
2018-04-03 10:16:41 +08:00
|
|
|
<= tsd_state_nominal_max);
|
2019-11-12 08:34:48 +08:00
|
|
|
tsd_atomic_store(&remote_tsd->state,
|
|
|
|
tsd_state_nominal_recompute, ATOMIC_RELAXED);
|
2020-01-31 08:31:45 +08:00
|
|
|
/* See comments in te_recompute_fast_threshold(). */
|
2019-11-12 08:34:48 +08:00
|
|
|
atomic_fence(ATOMIC_SEQ_CST);
|
2020-01-31 08:31:45 +08:00
|
|
|
te_next_event_fast_set_non_nominal(remote_tsd);
|
2018-04-03 10:16:41 +08:00
|
|
|
}
|
|
|
|
malloc_mutex_unlock(tsdn, &tsd_nominal_tsds_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tsd_global_slow_inc(tsdn_t *tsdn) {
|
|
|
|
atomic_fetch_add_u32(&tsd_global_slow_count, 1, ATOMIC_RELAXED);
|
|
|
|
/*
|
|
|
|
* We unconditionally force a recompute, even if the global slow count
|
|
|
|
* was already positive. If we didn't, then it would be possible for us
|
|
|
|
* to return to the user, have the user synchronize externally with some
|
|
|
|
* other thread, and then have that other thread not have picked up the
|
|
|
|
* update yet (since the original incrementing thread might still be
|
|
|
|
* making its way through the tsd list).
|
|
|
|
*/
|
|
|
|
tsd_force_recompute(tsdn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tsd_global_slow_dec(tsdn_t *tsdn) {
|
|
|
|
atomic_fetch_sub_u32(&tsd_global_slow_count, 1, ATOMIC_RELAXED);
|
|
|
|
/* See the note in ..._inc(). */
|
|
|
|
tsd_force_recompute(tsdn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
tsd_local_slow(tsd_t *tsd) {
|
|
|
|
return !tsd_tcache_enabled_get(tsd)
|
|
|
|
|| tsd_reentrancy_level_get(tsd) > 0;
|
|
|
|
}
|
|
|
|
|
2018-05-12 07:43:43 +08:00
|
|
|
bool
|
2018-04-03 10:16:41 +08:00
|
|
|
tsd_global_slow() {
|
|
|
|
return atomic_load_u32(&tsd_global_slow_count, ATOMIC_RELAXED) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static uint8_t
|
|
|
|
tsd_state_compute(tsd_t *tsd) {
|
|
|
|
if (!tsd_nominal(tsd)) {
|
|
|
|
return tsd_state_get(tsd);
|
|
|
|
}
|
|
|
|
/* We're in *a* nominal state; but which one? */
|
|
|
|
if (malloc_slow || tsd_local_slow(tsd) || tsd_global_slow()) {
|
|
|
|
return tsd_state_nominal_slow;
|
|
|
|
} else {
|
|
|
|
return tsd_state_nominal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 14:13:45 +08:00
|
|
|
void
|
|
|
|
tsd_slow_update(tsd_t *tsd) {
|
2018-04-03 10:16:41 +08:00
|
|
|
uint8_t old_state;
|
|
|
|
do {
|
|
|
|
uint8_t new_state = tsd_state_compute(tsd);
|
2019-03-08 08:01:55 +08:00
|
|
|
old_state = tsd_atomic_exchange(&tsd->state, new_state,
|
2018-04-03 10:16:41 +08:00
|
|
|
ATOMIC_ACQUIRE);
|
|
|
|
} while (old_state == tsd_state_nominal_recompute);
|
2019-11-12 08:34:48 +08:00
|
|
|
|
2020-01-31 08:31:45 +08:00
|
|
|
te_recompute_fast_threshold(tsd);
|
2018-04-03 10:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tsd_state_set(tsd_t *tsd, uint8_t new_state) {
|
|
|
|
/* Only the tsd module can change the state *to* recompute. */
|
|
|
|
assert(new_state != tsd_state_nominal_recompute);
|
2019-03-08 08:01:55 +08:00
|
|
|
uint8_t old_state = tsd_atomic_load(&tsd->state, ATOMIC_RELAXED);
|
2018-04-03 10:16:41 +08:00
|
|
|
if (old_state > tsd_state_nominal_max) {
|
|
|
|
/*
|
|
|
|
* Not currently in the nominal list, but it might need to be
|
|
|
|
* inserted there.
|
|
|
|
*/
|
|
|
|
assert(!tsd_in_nominal_list(tsd));
|
2019-03-08 08:01:55 +08:00
|
|
|
tsd_atomic_store(&tsd->state, new_state, ATOMIC_RELAXED);
|
2018-04-03 10:16:41 +08:00
|
|
|
if (new_state <= tsd_state_nominal_max) {
|
|
|
|
tsd_add_nominal(tsd);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We're currently nominal. If the new state is non-nominal,
|
|
|
|
* great; we take ourselves off the list and just enter the new
|
|
|
|
* state.
|
|
|
|
*/
|
|
|
|
assert(tsd_in_nominal_list(tsd));
|
|
|
|
if (new_state > tsd_state_nominal_max) {
|
|
|
|
tsd_remove_nominal(tsd);
|
2019-03-08 08:01:55 +08:00
|
|
|
tsd_atomic_store(&tsd->state, new_state,
|
|
|
|
ATOMIC_RELAXED);
|
2017-04-12 14:13:45 +08:00
|
|
|
} else {
|
2018-04-03 10:16:41 +08:00
|
|
|
/*
|
|
|
|
* This is the tricky case. We're transitioning from
|
|
|
|
* one nominal state to another. The caller can't know
|
|
|
|
* about any races that are occuring at the same time,
|
|
|
|
* so we always have to recompute no matter what.
|
|
|
|
*/
|
|
|
|
tsd_slow_update(tsd);
|
2017-04-12 14:13:45 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-31 08:31:45 +08:00
|
|
|
te_recompute_fast_threshold(tsd);
|
2017-04-12 14:13:45 +08:00
|
|
|
}
|
|
|
|
|
2020-04-18 05:10:18 +08:00
|
|
|
static void
|
|
|
|
tsd_prng_state_init(tsd_t *tsd) {
|
2017-10-14 01:27:13 +08:00
|
|
|
/*
|
|
|
|
* A nondeterministic seed based on the address of tsd reduces
|
|
|
|
* the likelihood of lockstep non-uniform cache index
|
|
|
|
* utilization among identical concurrent processes, but at the
|
|
|
|
* cost of test repeatability. For debug builds, instead use a
|
|
|
|
* deterministic seed.
|
|
|
|
*/
|
2019-11-05 08:48:12 +08:00
|
|
|
*tsd_prng_statep_get(tsd) = config_debug ? 0 :
|
2017-10-14 01:27:13 +08:00
|
|
|
(uint64_t)(uintptr_t)tsd;
|
2020-04-18 05:10:18 +08:00
|
|
|
}
|
2017-10-14 01:27:13 +08:00
|
|
|
|
2020-04-18 05:10:18 +08:00
|
|
|
static bool
|
|
|
|
tsd_data_init(tsd_t *tsd) {
|
|
|
|
/*
|
|
|
|
* We initialize the rtree context first (before the tcache), since the
|
|
|
|
* tcache initialization depends on it.
|
|
|
|
*/
|
|
|
|
rtree_ctx_data_init(tsd_rtree_ctxp_get_unsafe(tsd));
|
|
|
|
tsd_prng_state_init(tsd);
|
|
|
|
tsd_te_init(tsd); /* event_init may use the prng state above. */
|
2017-06-07 12:44:39 +08:00
|
|
|
return tsd_tcache_enabled_data_init(tsd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
assert_tsd_data_cleanup_done(tsd_t *tsd) {
|
|
|
|
assert(!tsd_nominal(tsd));
|
2018-07-27 05:17:36 +08:00
|
|
|
assert(!tsd_in_nominal_list(tsd));
|
2017-06-07 12:44:39 +08:00
|
|
|
assert(*tsd_arenap_get_unsafe(tsd) == NULL);
|
|
|
|
assert(*tsd_iarenap_get_unsafe(tsd) == NULL);
|
|
|
|
assert(*tsd_arenas_tdata_bypassp_get_unsafe(tsd) == true);
|
|
|
|
assert(*tsd_arenas_tdatap_get_unsafe(tsd) == NULL);
|
|
|
|
assert(*tsd_tcache_enabledp_get_unsafe(tsd) == false);
|
|
|
|
assert(*tsd_prof_tdatap_get_unsafe(tsd) == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
tsd_data_init_nocleanup(tsd_t *tsd) {
|
2018-03-09 08:34:17 +08:00
|
|
|
assert(tsd_state_get(tsd) == tsd_state_reincarnated ||
|
|
|
|
tsd_state_get(tsd) == tsd_state_minimal_initialized);
|
2017-06-07 12:44:39 +08:00
|
|
|
/*
|
|
|
|
* During reincarnation, there is no guarantee that the cleanup function
|
|
|
|
* will be called (deallocation may happen after all tsd destructors).
|
|
|
|
* We set up tsd in a way that no cleanup is needed.
|
|
|
|
*/
|
|
|
|
rtree_ctx_data_init(tsd_rtree_ctxp_get_unsafe(tsd));
|
|
|
|
*tsd_arenas_tdata_bypassp_get(tsd) = true;
|
|
|
|
*tsd_tcache_enabledp_get_unsafe(tsd) = false;
|
|
|
|
*tsd_reentrancy_levelp_get(tsd) = 1;
|
2020-04-18 05:10:18 +08:00
|
|
|
tsd_prng_state_init(tsd);
|
|
|
|
tsd_te_init(tsd); /* event_init may use the prng state above. */
|
2017-06-07 12:44:39 +08:00
|
|
|
assert_tsd_data_cleanup_done(tsd);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-12 14:13:45 +08:00
|
|
|
tsd_t *
|
2017-06-16 07:53:22 +08:00
|
|
|
tsd_fetch_slow(tsd_t *tsd, bool minimal) {
|
|
|
|
assert(!tsd_fast(tsd));
|
2017-06-08 07:12:50 +08:00
|
|
|
|
2018-03-09 08:34:17 +08:00
|
|
|
if (tsd_state_get(tsd) == tsd_state_nominal_slow) {
|
2018-04-03 10:16:41 +08:00
|
|
|
/*
|
|
|
|
* On slow path but no work needed. Note that we can't
|
|
|
|
* necessarily *assert* that we're slow, because we might be
|
|
|
|
* slow because of an asynchronous modification to global state,
|
|
|
|
* which might be asynchronously modified *back*.
|
|
|
|
*/
|
|
|
|
} else if (tsd_state_get(tsd) == tsd_state_nominal_recompute) {
|
|
|
|
tsd_slow_update(tsd);
|
2018-03-09 08:34:17 +08:00
|
|
|
} else if (tsd_state_get(tsd) == tsd_state_uninitialized) {
|
2017-06-16 07:53:22 +08:00
|
|
|
if (!minimal) {
|
2018-10-04 05:47:31 +08:00
|
|
|
if (tsd_booted) {
|
|
|
|
tsd_state_set(tsd, tsd_state_nominal);
|
|
|
|
tsd_slow_update(tsd);
|
|
|
|
/* Trigger cleanup handler registration. */
|
|
|
|
tsd_set(tsd);
|
|
|
|
tsd_data_init(tsd);
|
|
|
|
}
|
2017-06-16 07:53:22 +08:00
|
|
|
} else {
|
2018-03-09 08:34:17 +08:00
|
|
|
tsd_state_set(tsd, tsd_state_minimal_initialized);
|
2017-06-16 07:53:22 +08:00
|
|
|
tsd_set(tsd);
|
|
|
|
tsd_data_init_nocleanup(tsd);
|
|
|
|
}
|
2018-03-09 08:34:17 +08:00
|
|
|
} else if (tsd_state_get(tsd) == tsd_state_minimal_initialized) {
|
2017-06-16 07:53:22 +08:00
|
|
|
if (!minimal) {
|
|
|
|
/* Switch to fully initialized. */
|
2018-03-09 08:34:17 +08:00
|
|
|
tsd_state_set(tsd, tsd_state_nominal);
|
2017-06-16 07:53:22 +08:00
|
|
|
assert(*tsd_reentrancy_levelp_get(tsd) >= 1);
|
|
|
|
(*tsd_reentrancy_levelp_get(tsd))--;
|
|
|
|
tsd_slow_update(tsd);
|
|
|
|
tsd_data_init(tsd);
|
|
|
|
} else {
|
|
|
|
assert_tsd_data_cleanup_done(tsd);
|
|
|
|
}
|
2018-03-09 08:34:17 +08:00
|
|
|
} else if (tsd_state_get(tsd) == tsd_state_purgatory) {
|
|
|
|
tsd_state_set(tsd, tsd_state_reincarnated);
|
2017-04-12 14:13:45 +08:00
|
|
|
tsd_set(tsd);
|
2017-06-07 12:44:39 +08:00
|
|
|
tsd_data_init_nocleanup(tsd);
|
2017-04-12 14:13:45 +08:00
|
|
|
} else {
|
2018-03-09 08:34:17 +08:00
|
|
|
assert(tsd_state_get(tsd) == tsd_state_reincarnated);
|
2017-04-12 14:13:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return tsd;
|
|
|
|
}
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
void *
|
2017-01-16 08:56:30 +08:00
|
|
|
malloc_tsd_malloc(size_t size) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return a0malloc(CACHELINE_CEILING(size));
|
2012-03-22 09:33:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
malloc_tsd_dalloc(void *wrapper) {
|
2015-01-21 07:37:51 +08:00
|
|
|
a0dalloc(wrapper);
|
2012-03-22 09:33:03 +08:00
|
|
|
}
|
|
|
|
|
2012-04-22 12:27:46 +08:00
|
|
|
#if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)
|
2012-04-30 18:38:29 +08:00
|
|
|
#ifndef _WIN32
|
|
|
|
JEMALLOC_EXPORT
|
|
|
|
#endif
|
2012-03-22 09:33:03 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
_malloc_thread_cleanup(void) {
|
2012-04-25 05:22:02 +08:00
|
|
|
bool pending[MALLOC_TSD_CLEANUPS_MAX], again;
|
2012-03-22 09:33:03 +08:00
|
|
|
unsigned i;
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < ncleanups; i++) {
|
2012-03-22 09:33:03 +08:00
|
|
|
pending[i] = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-22 09:33:03 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
again = false;
|
|
|
|
for (i = 0; i < ncleanups; i++) {
|
|
|
|
if (pending[i]) {
|
2012-04-19 00:29:49 +08:00
|
|
|
pending[i] = cleanups[i]();
|
2017-01-16 08:56:30 +08:00
|
|
|
if (pending[i]) {
|
2012-03-22 09:33:03 +08:00
|
|
|
again = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-22 09:33:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (again);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
malloc_tsd_cleanup_register(bool (*f)(void)) {
|
2012-03-22 09:33:03 +08:00
|
|
|
assert(ncleanups < MALLOC_TSD_CLEANUPS_MAX);
|
2012-04-19 00:29:49 +08:00
|
|
|
cleanups[ncleanups] = f;
|
2012-03-22 09:33:03 +08:00
|
|
|
ncleanups++;
|
|
|
|
}
|
|
|
|
|
2017-04-27 09:37:44 +08:00
|
|
|
static void
|
|
|
|
tsd_do_data_cleanup(tsd_t *tsd) {
|
|
|
|
prof_tdata_cleanup(tsd);
|
|
|
|
iarena_cleanup(tsd);
|
|
|
|
arena_cleanup(tsd);
|
|
|
|
arenas_tdata_cleanup(tsd);
|
|
|
|
tcache_cleanup(tsd);
|
2017-05-23 10:32:04 +08:00
|
|
|
witnesses_cleanup(tsd_witness_tsdp_get_unsafe(tsd));
|
2020-01-17 05:00:35 +08:00
|
|
|
*tsd_reentrancy_levelp_get(tsd) = 1;
|
2017-04-27 09:37:44 +08:00
|
|
|
}
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
tsd_cleanup(void *arg) {
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_t *tsd = (tsd_t *)arg;
|
|
|
|
|
2018-03-09 08:34:17 +08:00
|
|
|
switch (tsd_state_get(tsd)) {
|
2015-09-25 07:53:18 +08:00
|
|
|
case tsd_state_uninitialized:
|
|
|
|
/* Do nothing. */
|
|
|
|
break;
|
2017-06-16 07:53:22 +08:00
|
|
|
case tsd_state_minimal_initialized:
|
|
|
|
/* This implies the thread only did free() in its life time. */
|
|
|
|
/* Fall through. */
|
2017-03-30 08:00:52 +08:00
|
|
|
case tsd_state_reincarnated:
|
|
|
|
/*
|
|
|
|
* Reincarnated means another destructor deallocated memory
|
2017-06-07 12:44:39 +08:00
|
|
|
* after the destructor was called. Cleanup isn't required but
|
|
|
|
* is still called for testing and completeness.
|
2017-03-30 08:00:52 +08:00
|
|
|
*/
|
2017-06-07 12:44:39 +08:00
|
|
|
assert_tsd_data_cleanup_done(tsd);
|
2019-10-30 04:03:41 +08:00
|
|
|
JEMALLOC_FALLTHROUGH;
|
2017-06-07 12:44:39 +08:00
|
|
|
case tsd_state_nominal:
|
|
|
|
case tsd_state_nominal_slow:
|
2017-04-27 09:37:44 +08:00
|
|
|
tsd_do_data_cleanup(tsd);
|
2018-03-09 08:34:17 +08:00
|
|
|
tsd_state_set(tsd, tsd_state_purgatory);
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_set(tsd);
|
|
|
|
break;
|
|
|
|
case tsd_state_purgatory:
|
|
|
|
/*
|
|
|
|
* The previous time this destructor was called, we set the
|
|
|
|
* state to tsd_state_purgatory so that other destructors
|
|
|
|
* wouldn't cause re-creation of the tsd. This time, do
|
|
|
|
* nothing, and do not request another callback.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
not_reached();
|
|
|
|
}
|
2017-04-27 09:37:44 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
test_callback_t test_callback = *tsd_test_callbackp_get_unsafe(tsd);
|
|
|
|
int *data = tsd_test_datap_get_unsafe(tsd);
|
|
|
|
if (test_callback != NULL) {
|
|
|
|
test_callback(data);
|
|
|
|
}
|
|
|
|
#endif
|
2014-09-23 12:09:23 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
tsd_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
malloc_tsd_boot0(void) {
|
2016-04-14 14:36:15 +08:00
|
|
|
tsd_t *tsd;
|
2012-03-22 09:33:03 +08:00
|
|
|
|
|
|
|
ncleanups = 0;
|
2018-04-03 10:16:41 +08:00
|
|
|
if (malloc_mutex_init(&tsd_nominal_tsds_lock, "tsd_nominal_tsds_lock",
|
|
|
|
WITNESS_RANK_OMIT, malloc_mutex_rank_exclusive)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tsd_boot0()) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-04-14 14:36:15 +08:00
|
|
|
tsd = tsd_fetch();
|
|
|
|
*tsd_arenas_tdata_bypassp_get(tsd) = true;
|
2017-01-20 10:15:45 +08:00
|
|
|
return tsd;
|
2012-03-22 09:33:03 +08:00
|
|
|
}
|
2012-04-22 12:27:46 +08:00
|
|
|
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
malloc_tsd_boot1(void) {
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
tsd_boot1();
|
2017-04-12 14:13:45 +08:00
|
|
|
tsd_t *tsd = tsd_fetch();
|
|
|
|
/* malloc_slow has been set properly. Update tsd_slow. */
|
|
|
|
tsd_slow_update(tsd);
|
|
|
|
*tsd_arenas_tdata_bypassp_get(tsd) = false;
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
}
|
|
|
|
|
2012-04-22 12:27:46 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
static BOOL WINAPI
|
2017-01-16 08:56:30 +08:00
|
|
|
_tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
|
2012-04-22 12:27:46 +08:00
|
|
|
switch (fdwReason) {
|
|
|
|
#ifdef JEMALLOC_LAZY_LOCK
|
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
isthreaded = true;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
_malloc_thread_cleanup();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2012-04-22 12:27:46 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 08:30:54 +08:00
|
|
|
/*
|
|
|
|
* We need to be able to say "read" here (in the "pragma section"), but have
|
|
|
|
* hooked "read". We won't read for the rest of the file, so we can get away
|
|
|
|
* with unhooking.
|
|
|
|
*/
|
|
|
|
#ifdef read
|
|
|
|
# undef read
|
|
|
|
#endif
|
|
|
|
|
2012-04-30 18:38:31 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# ifdef _M_IX86
|
|
|
|
# pragma comment(linker, "/INCLUDE:__tls_used")
|
2016-02-02 18:27:18 +08:00
|
|
|
# pragma comment(linker, "/INCLUDE:_tls_callback")
|
2012-04-30 18:38:31 +08:00
|
|
|
# else
|
|
|
|
# pragma comment(linker, "/INCLUDE:_tls_used")
|
2019-02-23 03:00:46 +08:00
|
|
|
# pragma comment(linker, "/INCLUDE:" STRINGIFY(tls_callback) )
|
2012-04-30 18:38:31 +08:00
|
|
|
# endif
|
|
|
|
# pragma section(".CRT$XLY",long,read)
|
|
|
|
#endif
|
2012-04-30 18:38:29 +08:00
|
|
|
JEMALLOC_SECTION(".CRT$XLY") JEMALLOC_ATTR(used)
|
2016-02-02 18:27:18 +08:00
|
|
|
BOOL (WINAPI *const tls_callback)(HINSTANCE hinstDLL,
|
2015-07-08 11:28:22 +08:00
|
|
|
DWORD fdwReason, LPVOID lpvReserved) = _tls_callback;
|
2012-04-22 12:27:46 +08:00
|
|
|
#endif
|
2013-10-22 05:12:16 +08:00
|
|
|
|
|
|
|
#if (!defined(JEMALLOC_MALLOC_THREAD_CLEANUP) && !defined(JEMALLOC_TLS) && \
|
|
|
|
!defined(_WIN32))
|
|
|
|
void *
|
2017-01-16 08:56:30 +08:00
|
|
|
tsd_init_check_recursion(tsd_init_head_t *head, tsd_init_block_t *block) {
|
2013-10-22 05:12:16 +08:00
|
|
|
pthread_t self = pthread_self();
|
|
|
|
tsd_init_block_t *iter;
|
|
|
|
|
|
|
|
/* Check whether this thread has already inserted into the list. */
|
2016-05-13 12:07:08 +08:00
|
|
|
malloc_mutex_lock(TSDN_NULL, &head->lock);
|
2013-10-22 05:12:16 +08:00
|
|
|
ql_foreach(iter, &head->blocks, link) {
|
|
|
|
if (iter->thread == self) {
|
2016-05-13 12:07:08 +08:00
|
|
|
malloc_mutex_unlock(TSDN_NULL, &head->lock);
|
2017-01-20 10:15:45 +08:00
|
|
|
return iter->data;
|
2013-10-22 05:12:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Insert block into list. */
|
|
|
|
ql_elm_new(block, link);
|
|
|
|
block->thread = self;
|
|
|
|
ql_tail_insert(&head->blocks, block, link);
|
2016-05-13 12:07:08 +08:00
|
|
|
malloc_mutex_unlock(TSDN_NULL, &head->lock);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2013-10-22 05:12:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
tsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block) {
|
2016-05-13 12:07:08 +08:00
|
|
|
malloc_mutex_lock(TSDN_NULL, &head->lock);
|
2013-10-22 05:12:16 +08:00
|
|
|
ql_remove(&head->blocks, block, link);
|
2016-05-13 12:07:08 +08:00
|
|
|
malloc_mutex_unlock(TSDN_NULL, &head->lock);
|
2013-10-22 05:12:16 +08:00
|
|
|
}
|
|
|
|
#endif
|
2018-07-27 05:42:37 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
tsd_prefork(tsd_t *tsd) {
|
|
|
|
malloc_mutex_prefork(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tsd_postfork_parent(tsd_t *tsd) {
|
|
|
|
malloc_mutex_postfork_parent(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tsd_postfork_child(tsd_t *tsd) {
|
|
|
|
malloc_mutex_postfork_child(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);
|
|
|
|
ql_new(&tsd_nominal_tsds);
|
|
|
|
|
|
|
|
if (tsd_state_get(tsd) <= tsd_state_nominal_max) {
|
|
|
|
tsd_add_nominal(tsd);
|
|
|
|
}
|
|
|
|
}
|