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-04-20 06:09:01 +08:00
|
|
|
#include "jemalloc/internal/size_classes.h"
|
2017-05-31 01:45:37 +08:00
|
|
|
#include "jemalloc/internal/sz.h"
|
2017-04-20 04:39:33 +08:00
|
|
|
#include "jemalloc/internal/ticker.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
|
2017-01-16 08:56:30 +08:00
|
|
|
tcache_event(tsd_t *tsd, tcache_t *tcache) {
|
|
|
|
if (TCACHE_GC_INCR == 0) {
|
2010-01-17 01:53:50 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (unlikely(ticker_tick(&tcache->gc_ticker))) {
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache_event_hard(tsd, tcache);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
assert(binind < NBINS);
|
2017-08-11 05:27:58 +08:00
|
|
|
bin = tcache_small_bin_get(tcache, binind);
|
2017-10-02 09:54:25 +08:00
|
|
|
ret = cache_bin_alloc_easy(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
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (likely(!zero)) {
|
2015-10-28 06:12:10 +08:00
|
|
|
if (slow_path && config_fill) {
|
2014-12-09 05:12:41 +08:00
|
|
|
if (unlikely(opt_junk_alloc)) {
|
2017-10-02 08:22:06 +08:00
|
|
|
arena_alloc_junk_small(ret, &bin_infos[binind],
|
|
|
|
false);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else if (unlikely(opt_zero)) {
|
2014-10-06 08:54:10 +08:00
|
|
|
memset(ret, 0, usize);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
}
|
2012-04-06 15:35:09 +08:00
|
|
|
} else {
|
2015-10-28 06:12:10 +08:00
|
|
|
if (slow_path && config_fill && unlikely(opt_junk_alloc)) {
|
2017-10-02 08:22:06 +08:00
|
|
|
arena_alloc_junk_small(ret, &bin_infos[binind], true);
|
2012-04-06 15:35:09 +08:00
|
|
|
}
|
2014-10-06 08:54:10 +08:00
|
|
|
memset(ret, 0, usize);
|
2012-04-06 15:35:09 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +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
|
|
|
}
|
|
|
|
if (config_prof) {
|
2014-10-06 08:54:10 +08:00
|
|
|
tcache->prof_accumbytes += usize;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache_event(tsd, tcache);
|
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-04-07 03:35:22 +08:00
|
|
|
assert(binind >= NBINS &&binind < nhbins);
|
2017-08-11 05:27:58 +08:00
|
|
|
bin = tcache_large_bin_get(tcache, binind);
|
2017-10-02 09:54:25 +08:00
|
|
|
ret = cache_bin_alloc_easy(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);
|
|
|
|
}
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (likely(!zero)) {
|
2015-10-28 06:12:10 +08:00
|
|
|
if (slow_path && config_fill) {
|
2016-03-28 14:28:39 +08:00
|
|
|
if (unlikely(opt_junk_alloc)) {
|
|
|
|
memset(ret, JEMALLOC_ALLOC_JUNK,
|
|
|
|
usize);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else if (unlikely(opt_zero)) {
|
2014-10-06 08:54:10 +08:00
|
|
|
memset(ret, 0, usize);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
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
|
|
|
}
|
|
|
|
if (config_prof) {
|
2014-10-06 08:54:10 +08:00
|
|
|
tcache->prof_accumbytes += usize;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-03-18 07:27:39 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache_event(tsd, tcache);
|
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;
|
|
|
|
cache_bin_info_t *bin_info;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
assert(tcache_salloc(tsd_tsdn(tsd), ptr) <= SMALL_MAXCLASS);
|
2010-04-01 07:45:04 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (slow_path && config_fill && unlikely(opt_junk_free)) {
|
2017-10-02 08:22:06 +08:00
|
|
|
arena_dalloc_junk_small(ptr, &bin_infos[binind]);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2017-08-11 05:27:58 +08:00
|
|
|
bin = tcache_small_bin_get(tcache, binind);
|
|
|
|
bin_info = &tcache_bin_info[binind];
|
|
|
|
if (unlikely(bin->ncached == bin_info->ncached_max)) {
|
|
|
|
tcache_bin_flush_small(tsd, tcache, bin, binind,
|
|
|
|
(bin_info->ncached_max >> 1));
|
2010-03-18 07:27:39 +08:00
|
|
|
}
|
2017-08-11 05:27:58 +08:00
|
|
|
assert(bin->ncached < bin_info->ncached_max);
|
|
|
|
bin->ncached++;
|
|
|
|
*(bin->avail - bin->ncached) = ptr;
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache_event(tsd, tcache);
|
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;
|
|
|
|
cache_bin_info_t *bin_info;
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
assert(tcache_salloc(tsd_tsdn(tsd), ptr) > SMALL_MAXCLASS);
|
|
|
|
assert(tcache_salloc(tsd_tsdn(tsd), ptr) <= tcache_maxclass);
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (slow_path && config_fill && unlikely(opt_junk_free)) {
|
2017-05-31 01:45:37 +08:00
|
|
|
large_dalloc_junk(ptr, sz_index2size(binind));
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2017-08-11 05:27:58 +08:00
|
|
|
bin = tcache_large_bin_get(tcache, binind);
|
|
|
|
bin_info = &tcache_bin_info[binind];
|
|
|
|
if (unlikely(bin->ncached == bin_info->ncached_max)) {
|
|
|
|
tcache_bin_flush_large(tsd, bin, binind,
|
|
|
|
(bin_info->ncached_max >> 1), tcache);
|
2010-03-08 07:34:14 +08:00
|
|
|
}
|
2017-08-11 05:27:58 +08:00
|
|
|
assert(bin->ncached < bin_info->ncached_max);
|
|
|
|
bin->ncached++;
|
|
|
|
*(bin->avail - bin->ncached) = ptr;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache_event(tsd, tcache);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
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 */
|