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
|
|
|
|
2017-10-02 08:22:06 +08:00
|
|
|
#include "jemalloc/internal/bin.h"
|
2017-04-18 06:52:44 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal_types.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"
|
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
|
|
|
}
|
|
|
|
|
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;
|
2017-08-11 05:27:58 +08:00
|
|
|
cache_bin_t *bin;
|
2015-10-28 06:12:10 +08:00
|
|
|
bool tcache_success;
|
|
|
|
size_t usize JEMALLOC_CC_SILENCE_INIT(0);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2017-12-15 04:46:39 +08:00
|
|
|
assert(binind < SC_NBINS);
|
2017-08-11 05:27:58 +08:00
|
|
|
bin = tcache_small_bin_get(tcache, 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
|
|
|
}
|
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);
|
|
|
|
/*
|
|
|
|
* Only compute usize if required. The checks in the following if
|
|
|
|
* statement are all static.
|
|
|
|
*/
|
|
|
|
if (config_prof || (slow_path && config_fill) || unlikely(zero)) {
|
2017-05-31 01:45:37 +08:00
|
|
|
usize = sz_index2size(binind);
|
2016-05-11 13:21:10 +08:00
|
|
|
assert(tcache_salloc(tsd_tsdn(tsd), ret) == usize);
|
2015-10-28 06:12:10 +08:00
|
|
|
}
|
2020-02-29 03:37:39 +08:00
|
|
|
if (unlikely(zero)) {
|
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;
|
2017-08-11 05:27:58 +08:00
|
|
|
cache_bin_t *bin;
|
2015-10-28 06:12:10 +08:00
|
|
|
bool tcache_success;
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2017-12-15 04:46:39 +08:00
|
|
|
assert(binind >= SC_NBINS &&binind < nhbins);
|
2017-08-11 05:27:58 +08:00
|
|
|
bin = tcache_large_bin_get(tcache, 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
|
|
|
}
|
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 {
|
2016-02-26 07:29:49 +08:00
|
|
|
size_t usize JEMALLOC_CC_SILENCE_INIT(0);
|
|
|
|
|
2015-10-28 06:12:10 +08:00
|
|
|
/* Only compute usize on demand */
|
2016-02-26 07:29:49 +08:00
|
|
|
if (config_prof || (slow_path && config_fill) ||
|
|
|
|
unlikely(zero)) {
|
2017-05-31 01:45:37 +08:00
|
|
|
usize = sz_index2size(binind);
|
2015-10-28 06:12:10 +08:00
|
|
|
assert(usize <= tcache_maxclass);
|
|
|
|
}
|
|
|
|
|
2020-02-29 03:37:39 +08:00
|
|
|
if (unlikely(zero)) {
|
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) {
|
2017-08-11 05:27:58 +08:00
|
|
|
cache_bin_t *bin;
|
2010-01-17 01:53:50 +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);
|
2010-04-01 07:45:04 +08:00
|
|
|
|
2017-08-11 05:27:58 +08:00
|
|
|
bin = tcache_small_bin_get(tcache, 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(
|
|
|
|
&tcache_bin_info[binind]) >> 1;
|
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) {
|
2017-08-11 05:27:58 +08:00
|
|
|
cache_bin_t *bin;
|
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
|
|
|
|
2017-08-11 05:27:58 +08:00
|
|
|
bin = tcache_large_bin_get(tcache, 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(
|
|
|
|
&tcache_bin_info[binind]) >> 1;
|
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 */
|