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
|
|
|
}
|
|
|
|
|
2023-08-07 02:38:30 +08:00
|
|
|
static inline unsigned
|
2023-08-23 07:31:54 +08:00
|
|
|
tcache_nbins_get(tcache_slow_t *tcache_slow) {
|
|
|
|
assert(tcache_slow != NULL);
|
|
|
|
unsigned nbins = tcache_slow->tcache_nbins;
|
|
|
|
assert(nbins <= TCACHE_NBINS_MAX);
|
|
|
|
return nbins;
|
2023-08-07 02:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t
|
2023-08-23 07:31:54 +08:00
|
|
|
tcache_max_get(tcache_slow_t *tcache_slow) {
|
|
|
|
assert(tcache_slow != NULL);
|
|
|
|
size_t tcache_max = sz_index2size(tcache_nbins_get(tcache_slow) - 1);
|
|
|
|
assert(tcache_max <= TCACHE_MAXCLASS_LIMIT);
|
|
|
|
return tcache_max;
|
2023-08-07 02:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2023-08-23 07:31:54 +08:00
|
|
|
tcache_max_set(tcache_slow_t *tcache_slow, size_t tcache_max) {
|
|
|
|
assert(tcache_slow != NULL);
|
2023-08-07 02:38:30 +08:00
|
|
|
assert(tcache_max <= TCACHE_MAXCLASS_LIMIT);
|
2023-08-23 07:31:54 +08:00
|
|
|
tcache_slow->tcache_nbins = sz_size2index(tcache_max) + 1;
|
2023-08-07 02:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2023-08-23 07:31:54 +08:00
|
|
|
tcache_bin_settings_backup(tcache_t *tcache,
|
|
|
|
cache_bin_info_t tcache_bin_info[TCACHE_NBINS_MAX]) {
|
|
|
|
for (unsigned i = 0; i < TCACHE_NBINS_MAX; i++) {
|
|
|
|
cache_bin_info_init(&tcache_bin_info[i],
|
2023-09-20 05:37:09 +08:00
|
|
|
cache_bin_ncached_max_get_unsafe(&tcache->bins[i]));
|
2023-08-07 02:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:47:57 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
2023-08-23 07:31:54 +08:00
|
|
|
tcache_bin_disabled(szind_t ind, cache_bin_t *bin,
|
|
|
|
tcache_slow_t *tcache_slow) {
|
2023-08-07 02:38:30 +08:00
|
|
|
assert(bin != NULL);
|
2023-09-20 05:37:09 +08:00
|
|
|
assert(ind < TCACHE_NBINS_MAX);
|
2023-08-23 07:31:54 +08:00
|
|
|
bool disabled = cache_bin_disabled(bin);
|
2020-10-22 10:47:57 +08:00
|
|
|
|
2023-08-23 07:31:54 +08:00
|
|
|
/*
|
|
|
|
* If a bin's ind >= nbins or ncached_max == 0, it must be disabled.
|
|
|
|
* However, when ind < nbins, it could be either enabled
|
|
|
|
* (ncached_max > 0) or disabled (ncached_max == 0). Similarly, when
|
|
|
|
* ncached_max > 0, it could be either enabled (ind < nbins) or
|
|
|
|
* disabled (ind >= nbins). Thus, if a bin is disabled, it has either
|
|
|
|
* ind >= nbins or ncached_max == 0. If a bin is enabled, it has
|
|
|
|
* ind < nbins and ncached_max > 0.
|
|
|
|
*/
|
|
|
|
unsigned nbins = tcache_nbins_get(tcache_slow);
|
2023-09-20 05:37:09 +08:00
|
|
|
cache_bin_sz_t ncached_max = cache_bin_ncached_max_get_unsafe(bin);
|
2023-08-23 07:31:54 +08:00
|
|
|
if (ind >= nbins) {
|
|
|
|
assert(disabled);
|
|
|
|
} else {
|
|
|
|
assert(!disabled || ncached_max == 0);
|
|
|
|
}
|
|
|
|
if (ncached_max == 0) {
|
|
|
|
assert(disabled);
|
|
|
|
} else {
|
|
|
|
assert(!disabled || ind >= nbins);
|
|
|
|
}
|
|
|
|
if (disabled) {
|
|
|
|
assert(ind >= nbins || ncached_max == 0);
|
|
|
|
} else {
|
|
|
|
assert(ind < nbins && ncached_max > 0);
|
|
|
|
}
|
2020-10-22 10:47:57 +08:00
|
|
|
|
2023-08-23 07:31:54 +08:00
|
|
|
return disabled;
|
2023-08-07 02:38:30 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2023-08-23 07:31:54 +08:00
|
|
|
if (unlikely(tcache_bin_disabled(binind, bin,
|
|
|
|
tcache->tcache_slow))) {
|
2020-10-22 10:47:57 +08:00
|
|
|
/* 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
|
|
|
cache_bin_t *bin = &tcache->bins[binind];
|
2023-08-23 07:31:54 +08:00
|
|
|
assert(binind >= SC_NBINS &&
|
|
|
|
!tcache_bin_disabled(binind, bin, tcache->tcache_slow));
|
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);
|
2023-08-23 07:31:54 +08:00
|
|
|
assert(usize <= tcache_max_get(tcache->tcache_slow));
|
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))) {
|
2023-08-23 07:31:54 +08:00
|
|
|
if (unlikely(tcache_bin_disabled(binind, bin,
|
|
|
|
tcache->tcache_slow))) {
|
2020-10-22 10:47:57 +08:00
|
|
|
arena_dalloc_small(tsd_tsdn(tsd), ptr);
|
|
|
|
return;
|
|
|
|
}
|
2023-08-23 07:31:54 +08:00
|
|
|
cache_bin_sz_t max = cache_bin_info_ncached_max_get(
|
|
|
|
bin, &bin->bin_info);
|
2020-10-22 10:47:57 +08:00
|
|
|
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
|
|
|
|
2023-08-07 02:38:30 +08:00
|
|
|
assert(tcache_salloc(tsd_tsdn(tsd), ptr) > SC_SMALL_MAXCLASS);
|
2023-08-23 07:31:54 +08:00
|
|
|
assert(tcache_salloc(tsd_tsdn(tsd), ptr) <=
|
|
|
|
tcache_max_get(tcache->tcache_slow));
|
2023-09-20 05:37:09 +08:00
|
|
|
assert(!tcache_bin_disabled(binind, &tcache->bins[binind],
|
|
|
|
tcache->tcache_slow));
|
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))) {
|
2023-08-23 07:31:54 +08:00
|
|
|
unsigned remain = cache_bin_info_ncached_max_get(
|
|
|
|
bin, &bin->bin_info) >> 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 */
|