|
Packit |
345191 |
#ifndef JEMALLOC_INTERNAL_TCACHE_INLINES_H
|
|
Packit |
345191 |
#define JEMALLOC_INTERNAL_TCACHE_INLINES_H
|
|
Packit |
345191 |
|
|
Packit |
345191 |
#include "jemalloc/internal/bin.h"
|
|
Packit |
345191 |
#include "jemalloc/internal/jemalloc_internal_types.h"
|
|
Packit |
345191 |
#include "jemalloc/internal/sc.h"
|
|
Packit |
345191 |
#include "jemalloc/internal/sz.h"
|
|
Packit |
345191 |
#include "jemalloc/internal/ticker.h"
|
|
Packit |
345191 |
#include "jemalloc/internal/util.h"
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static inline bool
|
|
Packit |
345191 |
tcache_enabled_get(tsd_t *tsd) {
|
|
Packit |
345191 |
return tsd_tcache_enabled_get(tsd);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static inline void
|
|
Packit |
345191 |
tcache_enabled_set(tsd_t *tsd, bool enabled) {
|
|
Packit |
345191 |
bool was_enabled = tsd_tcache_enabled_get(tsd);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (!was_enabled && enabled) {
|
|
Packit |
345191 |
tsd_tcache_data_init(tsd);
|
|
Packit |
345191 |
} else if (was_enabled && !enabled) {
|
|
Packit |
345191 |
tcache_cleanup(tsd);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
/* Commit the state last. Above calls check current state. */
|
|
Packit |
345191 |
tsd_tcache_enabled_set(tsd, enabled);
|
|
Packit |
345191 |
tsd_slow_update(tsd);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
JEMALLOC_ALWAYS_INLINE void
|
|
Packit |
345191 |
tcache_event(tsd_t *tsd, tcache_t *tcache) {
|
|
Packit |
345191 |
if (TCACHE_GC_INCR == 0) {
|
|
Packit |
345191 |
return;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (unlikely(ticker_tick(&tcache->gc_ticker))) {
|
|
Packit |
345191 |
tcache_event_hard(tsd, tcache);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
JEMALLOC_ALWAYS_INLINE void *
|
|
Packit |
345191 |
tcache_alloc_small(tsd_t *tsd, arena_t *arena, tcache_t *tcache,
|
|
Packit |
345191 |
size_t size, szind_t binind, bool zero, bool slow_path) {
|
|
Packit |
345191 |
void *ret;
|
|
Packit |
345191 |
cache_bin_t *bin;
|
|
Packit |
345191 |
bool tcache_success;
|
|
Packit |
345191 |
size_t usize JEMALLOC_CC_SILENCE_INIT(0);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
assert(binind < SC_NBINS);
|
|
Packit |
345191 |
bin = tcache_small_bin_get(tcache, binind);
|
|
Packit |
345191 |
ret = cache_bin_alloc_easy(bin, &tcache_success);
|
|
Packit |
345191 |
assert(tcache_success == (ret != NULL));
|
|
Packit |
345191 |
if (unlikely(!tcache_success)) {
|
|
Packit |
345191 |
bool tcache_hard_success;
|
|
Packit |
345191 |
arena = arena_choose(tsd, arena);
|
|
Packit |
345191 |
if (unlikely(arena == NULL)) {
|
|
Packit |
345191 |
return NULL;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
ret = tcache_alloc_small_hard(tsd_tsdn(tsd), arena, tcache,
|
|
Packit |
345191 |
bin, binind, &tcache_hard_success);
|
|
Packit |
345191 |
if (tcache_hard_success == false) {
|
|
Packit |
345191 |
return NULL;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
assert(ret);
|
|
Packit |
345191 |
/*
|
|
Packit |
345191 |
* Only compute usize if required. The checks in the following if
|
|
Packit |
345191 |
* statement are all static.
|
|
Packit |
345191 |
*/
|
|
Packit |
345191 |
if (config_prof || (slow_path && config_fill) || unlikely(zero)) {
|
|
Packit |
345191 |
usize = sz_index2size(binind);
|
|
Packit |
345191 |
assert(tcache_salloc(tsd_tsdn(tsd), ret) == usize);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (likely(!zero)) {
|
|
Packit |
345191 |
if (slow_path && config_fill) {
|
|
Packit |
345191 |
if (unlikely(opt_junk_alloc)) {
|
|
Packit |
345191 |
arena_alloc_junk_small(ret, &bin_infos[binind],
|
|
Packit |
345191 |
false);
|
|
Packit |
345191 |
} else if (unlikely(opt_zero)) {
|
|
Packit |
345191 |
memset(ret, 0, usize);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
} else {
|
|
Packit |
345191 |
if (slow_path && config_fill && unlikely(opt_junk_alloc)) {
|
|
Packit |
345191 |
arena_alloc_junk_small(ret, &bin_infos[binind], true);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
memset(ret, 0, usize);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (config_stats) {
|
|
Packit |
345191 |
bin->tstats.nrequests++;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
if (config_prof) {
|
|
Packit |
345191 |
tcache->prof_accumbytes += usize;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
tcache_event(tsd, tcache);
|
|
Packit |
345191 |
return ret;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
JEMALLOC_ALWAYS_INLINE void *
|
|
Packit |
345191 |
tcache_alloc_large(tsd_t *tsd, arena_t *arena, tcache_t *tcache, size_t size,
|
|
Packit |
345191 |
szind_t binind, bool zero, bool slow_path) {
|
|
Packit |
345191 |
void *ret;
|
|
Packit |
345191 |
cache_bin_t *bin;
|
|
Packit |
345191 |
bool tcache_success;
|
|
Packit |
345191 |
|
|
Packit |
345191 |
assert(binind >= SC_NBINS &&binind < nhbins);
|
|
Packit |
345191 |
bin = tcache_large_bin_get(tcache, binind);
|
|
Packit |
345191 |
ret = cache_bin_alloc_easy(bin, &tcache_success);
|
|
Packit |
345191 |
assert(tcache_success == (ret != NULL));
|
|
Packit |
345191 |
if (unlikely(!tcache_success)) {
|
|
Packit |
345191 |
/*
|
|
Packit |
345191 |
* Only allocate one large object at a time, because it's quite
|
|
Packit |
345191 |
* expensive to create one and not use it.
|
|
Packit |
345191 |
*/
|
|
Packit |
345191 |
arena = arena_choose(tsd, arena);
|
|
Packit |
345191 |
if (unlikely(arena == NULL)) {
|
|
Packit |
345191 |
return NULL;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
ret = large_malloc(tsd_tsdn(tsd), arena, sz_s2u(size), zero);
|
|
Packit |
345191 |
if (ret == NULL) {
|
|
Packit |
345191 |
return NULL;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
} else {
|
|
Packit |
345191 |
size_t usize JEMALLOC_CC_SILENCE_INIT(0);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
/* Only compute usize on demand */
|
|
Packit |
345191 |
if (config_prof || (slow_path && config_fill) ||
|
|
Packit |
345191 |
unlikely(zero)) {
|
|
Packit |
345191 |
usize = sz_index2size(binind);
|
|
Packit |
345191 |
assert(usize <= tcache_maxclass);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (likely(!zero)) {
|
|
Packit |
345191 |
if (slow_path && config_fill) {
|
|
Packit |
345191 |
if (unlikely(opt_junk_alloc)) {
|
|
Packit |
345191 |
memset(ret, JEMALLOC_ALLOC_JUNK,
|
|
Packit |
345191 |
usize);
|
|
Packit |
345191 |
} else if (unlikely(opt_zero)) {
|
|
Packit |
345191 |
memset(ret, 0, usize);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
} else {
|
|
Packit |
345191 |
memset(ret, 0, usize);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (config_stats) {
|
|
Packit |
345191 |
bin->tstats.nrequests++;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
if (config_prof) {
|
|
Packit |
345191 |
tcache->prof_accumbytes += usize;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
tcache_event(tsd, tcache);
|
|
Packit |
345191 |
return ret;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
JEMALLOC_ALWAYS_INLINE void
|
|
Packit |
345191 |
tcache_dalloc_small(tsd_t *tsd, tcache_t *tcache, void *ptr, szind_t binind,
|
|
Packit |
345191 |
bool slow_path) {
|
|
Packit |
345191 |
cache_bin_t *bin;
|
|
Packit |
345191 |
cache_bin_info_t *bin_info;
|
|
Packit |
345191 |
|
|
Packit |
345191 |
assert(tcache_salloc(tsd_tsdn(tsd), ptr)
|
|
Packit |
345191 |
<= SC_SMALL_MAXCLASS);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (slow_path && config_fill && unlikely(opt_junk_free)) {
|
|
Packit |
345191 |
arena_dalloc_junk_small(ptr, &bin_infos[binind]);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
bin = tcache_small_bin_get(tcache, binind);
|
|
Packit |
345191 |
bin_info = &tcache_bin_info[binind];
|
|
Packit |
345191 |
if (unlikely(!cache_bin_dalloc_easy(bin, bin_info, ptr))) {
|
|
Packit |
345191 |
tcache_bin_flush_small(tsd, tcache, bin, binind,
|
|
Packit |
345191 |
(bin_info->ncached_max >> 1));
|
|
Packit |
345191 |
bool ret = cache_bin_dalloc_easy(bin, bin_info, ptr);
|
|
Packit |
345191 |
assert(ret);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
tcache_event(tsd, tcache);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
JEMALLOC_ALWAYS_INLINE void
|
|
Packit |
345191 |
tcache_dalloc_large(tsd_t *tsd, tcache_t *tcache, void *ptr, szind_t binind,
|
|
Packit |
345191 |
bool slow_path) {
|
|
Packit |
345191 |
cache_bin_t *bin;
|
|
Packit |
345191 |
cache_bin_info_t *bin_info;
|
|
Packit |
345191 |
|
|
Packit |
345191 |
assert(tcache_salloc(tsd_tsdn(tsd), ptr)
|
|
Packit |
345191 |
> SC_SMALL_MAXCLASS);
|
|
Packit |
345191 |
assert(tcache_salloc(tsd_tsdn(tsd), ptr) <= tcache_maxclass);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (slow_path && config_fill && unlikely(opt_junk_free)) {
|
|
Packit |
345191 |
large_dalloc_junk(ptr, sz_index2size(binind));
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
bin = tcache_large_bin_get(tcache, binind);
|
|
Packit |
345191 |
bin_info = &tcache_bin_info[binind];
|
|
Packit |
345191 |
if (unlikely(bin->ncached == bin_info->ncached_max)) {
|
|
Packit |
345191 |
tcache_bin_flush_large(tsd, bin, binind,
|
|
Packit |
345191 |
(bin_info->ncached_max >> 1), tcache);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
assert(bin->ncached < bin_info->ncached_max);
|
|
Packit |
345191 |
bin->ncached++;
|
|
Packit |
345191 |
*(bin->avail - bin->ncached) = ptr;
|
|
Packit |
345191 |
|
|
Packit |
345191 |
tcache_event(tsd, tcache);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
JEMALLOC_ALWAYS_INLINE tcache_t *
|
|
Packit |
345191 |
tcaches_get(tsd_t *tsd, unsigned ind) {
|
|
Packit |
345191 |
tcaches_t *elm = &tcaches[ind];
|
|
Packit |
345191 |
if (unlikely(elm->tcache == NULL)) {
|
|
Packit |
345191 |
malloc_printf("<jemalloc>: invalid tcache id (%u).\n", ind);
|
|
Packit |
345191 |
abort();
|
|
Packit |
345191 |
} else if (unlikely(elm->tcache == TCACHES_ELM_NEED_REINIT)) {
|
|
Packit |
345191 |
elm->tcache = tcache_create_explicit(tsd);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
return elm->tcache;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
#endif /* JEMALLOC_INTERNAL_TCACHE_INLINES_H */
|