2017-01-11 10:06:31 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_TCACHE_INLINES_H
|
|
|
|
#define JEMALLOC_INTERNAL_TCACHE_INLINES_H
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2023-06-10 08:37:47 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
|
|
|
#include "jemalloc/internal/arena_externs.h"
|
2017-10-02 08:22:06 +08:00
|
|
|
#include "jemalloc/internal/bin.h"
|
2023-06-10 08:37:47 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal_inlines_b.h"
|
2017-04-18 06:52:44 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal_types.h"
|
2023-06-10 08:37:47 +08:00
|
|
|
#include "jemalloc/internal/large_externs.h"
|
2021-10-19 08:33:15 +08:00
|
|
|
#include "jemalloc/internal/san.h"
|
2017-12-15 04:46:39 +08:00
|
|
|
#include "jemalloc/internal/sc.h"
|
2017-05-31 01:45:37 +08:00
|
|
|
#include "jemalloc/internal/sz.h"
|
2023-06-10 08:37:47 +08:00
|
|
|
#include "jemalloc/internal/tcache_externs.h"
|
2017-04-12 04:31:16 +08:00
|
|
|
#include "jemalloc/internal/util.h"
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static inline bool
|
2017-03-28 12:50:38 +08:00
|
|
|
tcache_enabled_get(tsd_t *tsd) {
|
2017-04-06 10:23:41 +08:00
|
|
|
return tsd_tcache_enabled_get(tsd);
|
2012-03-27 09:54:44 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static inline void
|
2017-03-28 12:50:38 +08:00
|
|
|
tcache_enabled_set(tsd_t *tsd, bool enabled) {
|
2017-04-06 10:23:41 +08:00
|
|
|
bool was_enabled = tsd_tcache_enabled_get(tsd);
|
2014-09-23 12:09:23 +08:00
|
|
|
|
2017-04-06 10:23:41 +08:00
|
|
|
if (!was_enabled && enabled) {
|
2017-03-28 12:50:38 +08:00
|
|
|
tsd_tcache_data_init(tsd);
|
2017-04-06 10:23:41 +08:00
|
|
|
} else if (was_enabled && !enabled) {
|
2014-09-23 12:09:23 +08:00
|
|
|
tcache_cleanup(tsd);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-03-28 12:50:38 +08:00
|
|
|
/* Commit the state last. Above calls check current state. */
|
2017-04-06 10:23:41 +08:00
|
|
|
tsd_tcache_enabled_set(tsd, enabled);
|
2017-04-12 14:13:45 +08:00
|
|
|
tsd_slow_update(tsd);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 10:47:57 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
|
|
tcache_small_bin_disabled(szind_t ind, cache_bin_t *bin) {
|
|
|
|
assert(ind < SC_NBINS);
|
|
|
|
bool ret = (cache_bin_info_ncached_max(&tcache_bin_info[ind]) == 0);
|
|
|
|
if (ret && bin != NULL) {
|
|
|
|
/* small size class but cache bin disabled. */
|
|
|
|
assert(ind >= nhbins);
|
|
|
|
assert((uintptr_t)(*bin->stack_head) ==
|
|
|
|
cache_bin_preceding_junk);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
2018-04-17 03:08:27 +08:00
|
|
|
tcache_alloc_small(tsd_t *tsd, arena_t *arena, tcache_t *tcache,
|
2018-05-03 17:40:53 +08:00
|
|
|
size_t size, szind_t binind, bool zero, bool slow_path) {
|
2010-01-17 01:53:50 +08:00
|
|
|
void *ret;
|
2015-10-28 06:12:10 +08:00
|
|
|
bool tcache_success;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2017-12-15 04:46:39 +08:00
|
|
|
assert(binind < SC_NBINS);
|
2020-04-08 11:04:46 +08:00
|
|
|
cache_bin_t *bin = &tcache->bins[binind];
|
2020-03-05 00:58:42 +08:00
|
|
|
ret = cache_bin_alloc(bin, &tcache_success);
|
2015-10-28 06:12:10 +08:00
|
|
|
assert(tcache_success == (ret != NULL));
|
|
|
|
if (unlikely(!tcache_success)) {
|
|
|
|
bool tcache_hard_success;
|
2016-05-04 06:00:42 +08:00
|
|
|
arena = arena_choose(tsd, arena);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (unlikely(arena == NULL)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2020-10-22 10:47:57 +08:00
|
|
|
if (unlikely(tcache_small_bin_disabled(binind, bin))) {
|
|
|
|
/* stats and zero are handled directly by the arena. */
|
|
|
|
return arena_malloc_hard(tsd_tsdn(tsd), arena, size,
|
2023-06-03 06:15:37 +08:00
|
|
|
binind, zero, /* slab */ true);
|
2020-10-22 10:47:57 +08:00
|
|
|
}
|
2021-10-19 08:33:15 +08:00
|
|
|
tcache_bin_flush_stashed(tsd, tcache, bin, binind,
|
|
|
|
/* is_small */ true);
|
2015-10-28 06:12:10 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = tcache_alloc_small_hard(tsd_tsdn(tsd), arena, tcache,
|
2017-08-11 05:27:58 +08:00
|
|
|
bin, binind, &tcache_hard_success);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tcache_hard_success == false) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2015-10-28 06:12:10 +08:00
|
|
|
|
|
|
|
assert(ret);
|
2020-02-29 03:37:39 +08:00
|
|
|
if (unlikely(zero)) {
|
2020-04-29 01:40:46 +08:00
|
|
|
size_t usize = sz_index2size(binind);
|
|
|
|
assert(tcache_salloc(tsd_tsdn(tsd), ret) == usize);
|
2014-10-06 08:54:10 +08:00
|
|
|
memset(ret, 0, usize);
|
2012-04-06 15:35:09 +08:00
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
if (config_stats) {
|
2017-08-11 05:27:58 +08:00
|
|
|
bin->tstats.nrequests++;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
2016-06-01 05:50:21 +08:00
|
|
|
tcache_alloc_large(tsd_t *tsd, arena_t *arena, tcache_t *tcache, size_t size,
|
2017-01-16 08:56:30 +08:00
|
|
|
szind_t binind, bool zero, bool slow_path) {
|
2010-03-18 07:27:39 +08:00
|
|
|
void *ret;
|
2015-10-28 06:12:10 +08:00
|
|
|
bool tcache_success;
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2020-04-08 11:04:46 +08:00
|
|
|
assert(binind >= SC_NBINS && binind < nhbins);
|
|
|
|
cache_bin_t *bin = &tcache->bins[binind];
|
2020-03-05 00:58:42 +08:00
|
|
|
ret = cache_bin_alloc(bin, &tcache_success);
|
2015-10-28 06:12:10 +08:00
|
|
|
assert(tcache_success == (ret != NULL));
|
|
|
|
if (unlikely(!tcache_success)) {
|
2010-03-18 07:27:39 +08:00
|
|
|
/*
|
2016-06-01 05:50:21 +08:00
|
|
|
* Only allocate one large object at a time, because it's quite
|
2010-03-18 07:27:39 +08:00
|
|
|
* expensive to create one and not use it.
|
|
|
|
*/
|
2016-05-04 06:00:42 +08:00
|
|
|
arena = arena_choose(tsd, arena);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (unlikely(arena == NULL)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2021-10-19 08:33:15 +08:00
|
|
|
tcache_bin_flush_stashed(tsd, tcache, bin, binind,
|
|
|
|
/* is_small */ false);
|
2015-10-28 06:12:10 +08:00
|
|
|
|
2017-05-31 01:45:37 +08:00
|
|
|
ret = large_malloc(tsd_tsdn(tsd), arena, sz_s2u(size), zero);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (ret == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-03-18 07:27:39 +08:00
|
|
|
} else {
|
2020-02-29 03:37:39 +08:00
|
|
|
if (unlikely(zero)) {
|
2020-04-29 01:40:46 +08:00
|
|
|
size_t usize = sz_index2size(binind);
|
|
|
|
assert(usize <= tcache_maxclass);
|
2014-10-06 08:54:10 +08:00
|
|
|
memset(ret, 0, usize);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (config_stats) {
|
2017-08-11 05:27:58 +08:00
|
|
|
bin->tstats.nrequests++;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-03-18 07:27:39 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-03-18 07:27:39 +08:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2015-10-28 06:12:10 +08:00
|
|
|
tcache_dalloc_small(tsd_t *tsd, tcache_t *tcache, void *ptr, szind_t binind,
|
2017-01-16 08:56:30 +08:00
|
|
|
bool slow_path) {
|
2020-10-22 10:47:57 +08:00
|
|
|
assert(tcache_salloc(tsd_tsdn(tsd), ptr) <= SC_SMALL_MAXCLASS);
|
2010-04-01 07:45:04 +08:00
|
|
|
|
2020-04-08 11:04:46 +08:00
|
|
|
cache_bin_t *bin = &tcache->bins[binind];
|
2021-10-19 08:33:15 +08:00
|
|
|
/*
|
|
|
|
* Not marking the branch unlikely because this is past free_fastpath()
|
|
|
|
* (which handles the most common cases), i.e. at this point it's often
|
|
|
|
* uncommon cases.
|
|
|
|
*/
|
|
|
|
if (cache_bin_nonfast_aligned(ptr)) {
|
|
|
|
/* Junk unconditionally, even if bin is full. */
|
|
|
|
san_junk_ptr(ptr, sz_index2size(binind));
|
|
|
|
if (cache_bin_stash(bin, ptr)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(cache_bin_full(bin));
|
|
|
|
/* Bin full; fall through into the flush branch. */
|
|
|
|
}
|
|
|
|
|
2019-08-10 13:12:47 +08:00
|
|
|
if (unlikely(!cache_bin_dalloc_easy(bin, ptr))) {
|
2020-10-22 10:47:57 +08:00
|
|
|
if (unlikely(tcache_small_bin_disabled(binind, bin))) {
|
|
|
|
arena_dalloc_small(tsd_tsdn(tsd), ptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cache_bin_sz_t max = cache_bin_info_ncached_max(
|
|
|
|
&tcache_bin_info[binind]);
|
|
|
|
unsigned remain = max >> opt_lg_tcache_flush_small_div;
|
2019-08-15 04:08:06 +08:00
|
|
|
tcache_bin_flush_small(tsd, tcache, bin, binind, remain);
|
2019-08-10 13:12:47 +08:00
|
|
|
bool ret = cache_bin_dalloc_easy(bin, ptr);
|
2018-10-19 04:13:57 +08:00
|
|
|
assert(ret);
|
2010-03-18 07:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 00:45:43 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2017-03-14 08:36:57 +08:00
|
|
|
tcache_dalloc_large(tsd_t *tsd, tcache_t *tcache, void *ptr, szind_t binind,
|
2017-01-16 08:56:30 +08:00
|
|
|
bool slow_path) {
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2017-12-15 04:46:39 +08:00
|
|
|
assert(tcache_salloc(tsd_tsdn(tsd), ptr)
|
2018-07-12 07:05:58 +08:00
|
|
|
> SC_SMALL_MAXCLASS);
|
2016-05-11 13:21:10 +08:00
|
|
|
assert(tcache_salloc(tsd_tsdn(tsd), ptr) <= tcache_maxclass);
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2020-04-08 11:04:46 +08:00
|
|
|
cache_bin_t *bin = &tcache->bins[binind];
|
2019-08-10 13:12:47 +08:00
|
|
|
if (unlikely(!cache_bin_dalloc_easy(bin, ptr))) {
|
2020-02-27 09:23:47 +08:00
|
|
|
unsigned remain = cache_bin_info_ncached_max(
|
2020-05-14 06:32:18 +08:00
|
|
|
&tcache_bin_info[binind]) >> opt_lg_tcache_flush_large_div;
|
2019-08-15 04:08:06 +08:00
|
|
|
tcache_bin_flush_large(tsd, tcache, bin, binind, remain);
|
2019-08-10 13:12:47 +08:00
|
|
|
bool ret = cache_bin_dalloc_easy(bin, ptr);
|
2019-08-13 02:11:01 +08:00
|
|
|
assert(ret);
|
2010-03-08 07:34:14 +08:00
|
|
|
}
|
2015-01-30 07:30:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE tcache_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
tcaches_get(tsd_t *tsd, unsigned ind) {
|
2015-01-30 07:30:47 +08:00
|
|
|
tcaches_t *elm = &tcaches[ind];
|
2016-04-23 05:34:14 +08:00
|
|
|
if (unlikely(elm->tcache == NULL)) {
|
2018-11-10 06:45:06 +08:00
|
|
|
malloc_printf("<jemalloc>: invalid tcache id (%u).\n", ind);
|
|
|
|
abort();
|
|
|
|
} else if (unlikely(elm->tcache == TCACHES_ELM_NEED_REINIT)) {
|
2017-03-28 12:50:38 +08:00
|
|
|
elm->tcache = tcache_create_explicit(tsd);
|
2016-04-23 05:34:14 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return elm->tcache;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2017-01-11 10:06:31 +08:00
|
|
|
#endif /* JEMALLOC_INTERNAL_TCACHE_INLINES_H */
|