2017-05-24 05:26:31 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_RTREE_H
|
|
|
|
#define JEMALLOC_INTERNAL_RTREE_H
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2017-05-24 05:26:31 +08:00
|
|
|
#include "jemalloc/internal/atomic.h"
|
|
|
|
#include "jemalloc/internal/mutex.h"
|
|
|
|
#include "jemalloc/internal/rtree_tsd.h"
|
2017-12-15 04:46:39 +08:00
|
|
|
#include "jemalloc/internal/sc.h"
|
2017-05-24 05:26:31 +08:00
|
|
|
#include "jemalloc/internal/tsd.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This radix tree implementation is tailored to the singular purpose of
|
|
|
|
* associating metadata with extents that are currently owned by jemalloc.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Number of high insignificant bits. */
|
|
|
|
#define RTREE_NHIB ((1U << (LG_SIZEOF_PTR+3)) - LG_VADDR)
|
|
|
|
/* Number of low insigificant bits. */
|
|
|
|
#define RTREE_NLIB LG_PAGE
|
|
|
|
/* Number of significant bits. */
|
|
|
|
#define RTREE_NSB (LG_VADDR - RTREE_NLIB)
|
|
|
|
/* Number of levels in radix tree. */
|
|
|
|
#if RTREE_NSB <= 10
|
|
|
|
# define RTREE_HEIGHT 1
|
|
|
|
#elif RTREE_NSB <= 36
|
|
|
|
# define RTREE_HEIGHT 2
|
|
|
|
#elif RTREE_NSB <= 52
|
|
|
|
# define RTREE_HEIGHT 3
|
|
|
|
#else
|
|
|
|
# error Unsupported number of significant virtual address bits
|
|
|
|
#endif
|
|
|
|
/* Use compact leaf representation if virtual address encoding allows. */
|
2017-12-15 04:46:39 +08:00
|
|
|
#if RTREE_NHIB >= LG_CEIL(SC_NSIZES)
|
2017-05-24 05:26:31 +08:00
|
|
|
# define RTREE_LEAF_COMPACT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Needed for initialization only. */
|
|
|
|
#define RTREE_LEAFKEY_INVALID ((uintptr_t)1)
|
|
|
|
|
|
|
|
typedef struct rtree_node_elm_s rtree_node_elm_t;
|
|
|
|
struct rtree_node_elm_s {
|
|
|
|
atomic_p_t child; /* (rtree_{node,leaf}_elm_t *) */
|
|
|
|
};
|
|
|
|
|
2020-03-14 02:47:51 +08:00
|
|
|
typedef struct rtree_leaf_elm_contents_s rtree_leaf_elm_contents_t;
|
|
|
|
struct rtree_leaf_elm_contents_s {
|
|
|
|
edata_t *edata;
|
|
|
|
szind_t szind;
|
|
|
|
bool slab;
|
|
|
|
};
|
|
|
|
|
2017-05-24 05:26:31 +08:00
|
|
|
struct rtree_leaf_elm_s {
|
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
|
|
|
/*
|
|
|
|
* Single pointer-width field containing all three leaf element fields.
|
|
|
|
* For example, on a 64-bit x64 system with 48 significant virtual
|
2019-12-10 06:36:45 +08:00
|
|
|
* memory address bits, the index, edata, and slab fields are packed as
|
2017-05-24 05:26:31 +08:00
|
|
|
* such:
|
|
|
|
*
|
|
|
|
* x: index
|
2019-12-10 06:36:45 +08:00
|
|
|
* e: edata
|
2017-05-24 05:26:31 +08:00
|
|
|
* b: slab
|
|
|
|
*
|
|
|
|
* 00000000 xxxxxxxx eeeeeeee [...] eeeeeeee eeee000b
|
|
|
|
*/
|
|
|
|
atomic_p_t le_bits;
|
|
|
|
#else
|
2019-12-10 06:36:45 +08:00
|
|
|
atomic_p_t le_edata; /* (edata_t *) */
|
2017-05-24 05:26:31 +08:00
|
|
|
atomic_u_t le_szind; /* (szind_t) */
|
|
|
|
atomic_b_t le_slab; /* (bool) */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct rtree_level_s rtree_level_t;
|
|
|
|
struct rtree_level_s {
|
|
|
|
/* Number of key bits distinguished by this level. */
|
|
|
|
unsigned bits;
|
|
|
|
/*
|
|
|
|
* Cumulative number of key bits distinguished by traversing to
|
|
|
|
* corresponding tree level.
|
|
|
|
*/
|
|
|
|
unsigned cumbits;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct rtree_s rtree_t;
|
|
|
|
struct rtree_s {
|
2020-02-18 05:11:10 +08:00
|
|
|
base_t *base;
|
2017-05-24 05:26:31 +08:00
|
|
|
malloc_mutex_t init_lock;
|
|
|
|
/* Number of elements based on rtree_levels[0].bits. */
|
|
|
|
#if RTREE_HEIGHT > 1
|
|
|
|
rtree_node_elm_t root[1U << (RTREE_NSB/RTREE_HEIGHT)];
|
|
|
|
#else
|
|
|
|
rtree_leaf_elm_t root[1U << (RTREE_NSB/RTREE_HEIGHT)];
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Split the bits into one to three partitions depending on number of
|
|
|
|
* significant bits. It the number of bits does not divide evenly into the
|
|
|
|
* number of levels, place one remainder bit per level starting at the leaf
|
|
|
|
* level.
|
|
|
|
*/
|
|
|
|
static const rtree_level_t rtree_levels[] = {
|
|
|
|
#if RTREE_HEIGHT == 1
|
|
|
|
{RTREE_NSB, RTREE_NHIB + RTREE_NSB}
|
|
|
|
#elif RTREE_HEIGHT == 2
|
|
|
|
{RTREE_NSB/2, RTREE_NHIB + RTREE_NSB/2},
|
|
|
|
{RTREE_NSB/2 + RTREE_NSB%2, RTREE_NHIB + RTREE_NSB}
|
|
|
|
#elif RTREE_HEIGHT == 3
|
|
|
|
{RTREE_NSB/3, RTREE_NHIB + RTREE_NSB/3},
|
|
|
|
{RTREE_NSB/3 + RTREE_NSB%3/2,
|
|
|
|
RTREE_NHIB + RTREE_NSB/3*2 + RTREE_NSB%3/2},
|
|
|
|
{RTREE_NSB/3 + RTREE_NSB%3 - RTREE_NSB%3/2, RTREE_NHIB + RTREE_NSB}
|
|
|
|
#else
|
|
|
|
# error Unsupported rtree height
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-02-18 05:11:10 +08:00
|
|
|
bool rtree_new(rtree_t *rtree, base_t *base, bool zeroed);
|
2017-05-24 05:26:31 +08:00
|
|
|
|
|
|
|
rtree_leaf_elm_t *rtree_leaf_elm_lookup_hard(tsdn_t *tsdn, rtree_t *rtree,
|
|
|
|
rtree_ctx_t *rtree_ctx, uintptr_t key, bool dependent, bool init_missing);
|
2017-04-18 07:35:04 +08:00
|
|
|
|
2017-02-04 12:21:56 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE uintptr_t
|
2017-02-07 05:17:12 +08:00
|
|
|
rtree_leafkey(uintptr_t key) {
|
2017-02-04 12:21:56 +08:00
|
|
|
unsigned ptrbits = ZU(1) << (LG_SIZEOF_PTR+3);
|
2017-02-07 05:17:12 +08:00
|
|
|
unsigned cumbits = (rtree_levels[RTREE_HEIGHT-1].cumbits -
|
|
|
|
rtree_levels[RTREE_HEIGHT-1].bits);
|
2017-02-04 12:21:56 +08:00
|
|
|
unsigned maskbits = ptrbits - cumbits;
|
|
|
|
uintptr_t mask = ~((ZU(1) << maskbits) - 1);
|
|
|
|
return (key & mask);
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
2017-04-15 02:05:38 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
|
|
|
rtree_cache_direct_map(uintptr_t key) {
|
|
|
|
unsigned ptrbits = ZU(1) << (LG_SIZEOF_PTR+3);
|
|
|
|
unsigned cumbits = (rtree_levels[RTREE_HEIGHT-1].cumbits -
|
|
|
|
rtree_levels[RTREE_HEIGHT-1].bits);
|
|
|
|
unsigned maskbits = ptrbits - cumbits;
|
|
|
|
return (size_t)((key >> maskbits) & (RTREE_CTX_NCACHE - 1));
|
|
|
|
}
|
|
|
|
|
2016-03-24 07:14:41 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE uintptr_t
|
2017-02-07 05:17:12 +08:00
|
|
|
rtree_subkey(uintptr_t key, unsigned level) {
|
2017-02-04 12:21:56 +08:00
|
|
|
unsigned ptrbits = ZU(1) << (LG_SIZEOF_PTR+3);
|
2017-02-07 05:17:12 +08:00
|
|
|
unsigned cumbits = rtree_levels[level].cumbits;
|
2017-02-04 12:21:56 +08:00
|
|
|
unsigned shiftbits = ptrbits - cumbits;
|
2017-02-07 05:17:12 +08:00
|
|
|
unsigned maskbits = rtree_levels[level].bits;
|
2017-02-10 04:31:11 +08:00
|
|
|
uintptr_t mask = (ZU(1) << maskbits) - 1;
|
2017-02-04 12:21:56 +08:00
|
|
|
return ((key >> shiftbits) & mask);
|
2015-01-31 14:54:08 +08:00
|
|
|
}
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2017-03-17 08:57:52 +08:00
|
|
|
/*
|
|
|
|
* Atomic getters.
|
|
|
|
*
|
|
|
|
* dependent: Reading a value on behalf of a pointer to a valid allocation
|
|
|
|
* is guaranteed to be a clean read even without synchronization,
|
|
|
|
* because the rtree update became visible in memory before the
|
|
|
|
* pointer came into existence.
|
|
|
|
* !dependent: An arbitrary read, e.g. on behalf of ivsalloc(), may not be
|
|
|
|
* dependent on a previous rtree write, which means a stale read
|
|
|
|
* could result if synchronization were omitted here.
|
|
|
|
*/
|
2017-03-21 07:38:21 +08:00
|
|
|
# ifdef RTREE_LEAF_COMPACT
|
|
|
|
JEMALLOC_ALWAYS_INLINE uintptr_t
|
2018-05-03 17:40:53 +08:00
|
|
|
rtree_leaf_elm_bits_read(tsdn_t *tsdn, rtree_t *rtree,
|
|
|
|
rtree_leaf_elm_t *elm, bool dependent) {
|
2017-03-21 07:38:21 +08:00
|
|
|
return (uintptr_t)atomic_load_p(&elm->le_bits, dependent
|
2017-05-03 12:42:33 +08:00
|
|
|
? ATOMIC_RELAXED : ATOMIC_ACQUIRE);
|
2017-03-21 07:38:21 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 02:47:51 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE uintptr_t
|
|
|
|
rtree_leaf_elm_bits_encode(rtree_leaf_elm_contents_t contents) {
|
|
|
|
uintptr_t edata_bits = (uintptr_t)contents.edata
|
|
|
|
& (((uintptr_t)1 << LG_VADDR) - 1);
|
|
|
|
uintptr_t szind_bits = (uintptr_t)contents.szind << LG_VADDR;
|
|
|
|
/*
|
|
|
|
* Slab shares the low bit of edata; we know edata is on an even address
|
|
|
|
* (in fact, it's 128 bytes on 64-bit systems; we can enforce this
|
|
|
|
* alignment if we want to steal 6 extra rtree leaf bits someday.
|
|
|
|
*/
|
|
|
|
uintptr_t slab_bits = (uintptr_t)contents.slab;
|
|
|
|
return szind_bits | edata_bits | slab_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE rtree_leaf_elm_contents_t
|
|
|
|
rtree_leaf_elm_bits_decode(uintptr_t bits) {
|
|
|
|
rtree_leaf_elm_contents_t contents;
|
|
|
|
/* Do the easy things first. */
|
|
|
|
contents.szind = bits >> LG_VADDR;
|
|
|
|
contents.slab = (bool)(bits & 1);
|
2017-09-30 04:54:08 +08:00
|
|
|
# ifdef __aarch64__
|
|
|
|
/*
|
|
|
|
* aarch64 doesn't sign extend the highest virtual address bit to set
|
2020-03-14 02:47:51 +08:00
|
|
|
* the higher ones. Instead, the high bits get zeroed.
|
2017-09-30 04:54:08 +08:00
|
|
|
*/
|
|
|
|
uintptr_t high_bit_mask = ((uintptr_t)1 << LG_VADDR) - 1;
|
|
|
|
/* Mask off the slab bit. */
|
|
|
|
uintptr_t low_bit_mask = ~(uintptr_t)1;
|
|
|
|
uintptr_t mask = high_bit_mask & low_bit_mask;
|
2020-03-14 02:47:51 +08:00
|
|
|
contents.edata = (edata_t *)(bits & mask);
|
2017-09-30 04:54:08 +08:00
|
|
|
# else
|
2017-05-16 05:23:51 +08:00
|
|
|
/* Restore sign-extended high bits, mask slab bit. */
|
2020-03-14 02:47:51 +08:00
|
|
|
contents.edata = (edata_t *)((uintptr_t)((intptr_t)(bits << RTREE_NHIB)
|
|
|
|
>> RTREE_NHIB) & ~((uintptr_t)0x1));
|
2017-09-30 04:54:08 +08:00
|
|
|
# endif
|
2020-03-14 02:47:51 +08:00
|
|
|
return contents;
|
2017-03-21 07:38:21 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 02:47:51 +08:00
|
|
|
# endif /* RTREE_LEAF_COMPACT */
|
2017-03-21 07:38:21 +08:00
|
|
|
|
2019-12-10 06:36:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE edata_t *
|
|
|
|
rtree_leaf_elm_edata_read(tsdn_t *tsdn, rtree_t *rtree,
|
2018-04-17 03:08:27 +08:00
|
|
|
rtree_leaf_elm_t *elm, bool dependent) {
|
2017-03-21 07:38:21 +08:00
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
2017-05-16 05:23:51 +08:00
|
|
|
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(bits);
|
|
|
|
return contents.edata;
|
2017-03-21 07:38:21 +08:00
|
|
|
#else
|
2019-12-10 06:36:45 +08:00
|
|
|
edata_t *edata = (edata_t *)atomic_load_p(&elm->le_edata, dependent
|
2017-03-17 08:57:52 +08:00
|
|
|
? ATOMIC_RELAXED : ATOMIC_ACQUIRE);
|
2019-12-10 06:36:45 +08:00
|
|
|
return edata;
|
2017-03-21 07:38:21 +08:00
|
|
|
#endif
|
2015-01-31 14:54:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 08:57:52 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE szind_t
|
2018-05-03 17:40:53 +08:00
|
|
|
rtree_leaf_elm_szind_read(tsdn_t *tsdn, rtree_t *rtree,
|
2018-04-17 03:08:27 +08:00
|
|
|
rtree_leaf_elm_t *elm, bool dependent) {
|
2017-03-21 07:38:21 +08:00
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
2017-05-16 05:23:51 +08:00
|
|
|
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(bits);
|
|
|
|
return contents.szind;
|
2017-03-21 07:38:21 +08:00
|
|
|
#else
|
2017-03-17 08:57:52 +08:00
|
|
|
return (szind_t)atomic_load_u(&elm->le_szind, dependent ? ATOMIC_RELAXED
|
|
|
|
: ATOMIC_ACQUIRE);
|
2017-03-21 07:38:21 +08:00
|
|
|
#endif
|
2017-03-17 08:57:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
2018-05-03 17:40:53 +08:00
|
|
|
rtree_leaf_elm_slab_read(tsdn_t *tsdn, rtree_t *rtree,
|
2018-04-17 03:08:27 +08:00
|
|
|
rtree_leaf_elm_t *elm, bool dependent) {
|
2017-03-21 07:38:21 +08:00
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
2017-05-16 05:23:51 +08:00
|
|
|
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(bits);
|
|
|
|
return contents.slab;
|
2017-03-21 07:38:21 +08:00
|
|
|
#else
|
2017-03-17 08:57:52 +08:00
|
|
|
return atomic_load_b(&elm->le_slab, dependent ? ATOMIC_RELAXED :
|
|
|
|
ATOMIC_ACQUIRE);
|
2017-03-21 07:38:21 +08:00
|
|
|
#endif
|
2017-03-17 08:57:52 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static inline void
|
2019-12-10 06:36:45 +08:00
|
|
|
rtree_leaf_elm_edata_write(tsdn_t *tsdn, rtree_t *rtree,
|
|
|
|
rtree_leaf_elm_t *elm, edata_t *edata) {
|
2017-03-21 07:38:21 +08:00
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
2017-05-16 05:23:51 +08:00
|
|
|
uintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, true);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(
|
|
|
|
old_bits);
|
|
|
|
contents.edata = edata;
|
|
|
|
uintptr_t bits = rtree_leaf_elm_bits_encode(contents);
|
2017-03-21 07:38:21 +08:00
|
|
|
atomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);
|
|
|
|
#else
|
2019-12-10 06:36:45 +08:00
|
|
|
atomic_store_p(&elm->le_edata, edata, ATOMIC_RELEASE);
|
2017-03-21 07:38:21 +08:00
|
|
|
#endif
|
2017-03-17 08:57:52 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static inline void
|
2018-05-03 17:40:53 +08:00
|
|
|
rtree_leaf_elm_szind_write(tsdn_t *tsdn, rtree_t *rtree,
|
2018-04-17 03:08:27 +08:00
|
|
|
rtree_leaf_elm_t *elm, szind_t szind) {
|
2017-12-15 04:46:39 +08:00
|
|
|
assert(szind <= SC_NSIZES);
|
2017-03-17 08:57:52 +08:00
|
|
|
|
2017-03-21 07:38:21 +08:00
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
|
|
|
uintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm,
|
2017-05-16 05:23:51 +08:00
|
|
|
true);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(
|
|
|
|
old_bits);
|
|
|
|
contents.szind = szind;
|
|
|
|
uintptr_t bits = rtree_leaf_elm_bits_encode(contents);
|
2017-03-21 07:38:21 +08:00
|
|
|
atomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);
|
|
|
|
#else
|
2017-03-17 08:57:52 +08:00
|
|
|
atomic_store_u(&elm->le_szind, szind, ATOMIC_RELEASE);
|
2017-03-21 07:38:21 +08:00
|
|
|
#endif
|
2017-03-17 08:57:52 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static inline void
|
2018-05-03 17:40:53 +08:00
|
|
|
rtree_leaf_elm_slab_write(tsdn_t *tsdn, rtree_t *rtree,
|
2018-04-17 03:08:27 +08:00
|
|
|
rtree_leaf_elm_t *elm, bool slab) {
|
2017-03-21 07:38:21 +08:00
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
|
|
|
uintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm,
|
2017-05-16 05:23:51 +08:00
|
|
|
true);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(
|
|
|
|
old_bits);
|
|
|
|
contents.slab = slab;
|
|
|
|
uintptr_t bits = rtree_leaf_elm_bits_encode(contents);
|
2017-03-21 07:38:21 +08:00
|
|
|
atomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);
|
|
|
|
#else
|
2017-03-17 08:57:52 +08:00
|
|
|
atomic_store_b(&elm->le_slab, slab, ATOMIC_RELEASE);
|
2017-03-21 07:38:21 +08:00
|
|
|
#endif
|
2017-03-17 08:57:52 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static inline void
|
2018-05-03 17:40:53 +08:00
|
|
|
rtree_leaf_elm_write(tsdn_t *tsdn, rtree_t *rtree,
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_t *elm, rtree_leaf_elm_contents_t contents) {
|
2017-03-27 19:08:51 +08:00
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
2020-03-14 02:47:51 +08:00
|
|
|
uintptr_t bits = rtree_leaf_elm_bits_encode(contents);
|
2017-03-21 07:38:21 +08:00
|
|
|
atomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);
|
|
|
|
#else
|
2017-05-16 05:23:51 +08:00
|
|
|
rtree_leaf_elm_slab_write(tsdn, rtree, elm, slab);
|
|
|
|
rtree_leaf_elm_szind_write(tsdn, rtree, elm, szind);
|
2017-03-17 08:57:52 +08:00
|
|
|
/*
|
2019-12-10 06:36:45 +08:00
|
|
|
* Write edata last, since the element is atomically considered valid
|
|
|
|
* as soon as the edata field is non-NULL.
|
2017-03-17 08:57:52 +08:00
|
|
|
*/
|
2019-12-10 06:36:45 +08:00
|
|
|
rtree_leaf_elm_edata_write(tsdn, rtree, elm, edata);
|
2017-03-21 07:38:21 +08:00
|
|
|
#endif
|
2015-01-31 14:54:08 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static inline void
|
2017-03-27 19:08:51 +08:00
|
|
|
rtree_leaf_elm_szind_slab_update(tsdn_t *tsdn, rtree_t *rtree,
|
|
|
|
rtree_leaf_elm_t *elm, szind_t szind, bool slab) {
|
2017-12-15 04:46:39 +08:00
|
|
|
assert(!slab || szind < SC_NBINS);
|
2017-03-27 19:08:51 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The caller implicitly assures that it is the only writer to the szind
|
2019-12-10 06:36:45 +08:00
|
|
|
* and slab fields, and that the edata field cannot currently change.
|
2017-03-27 19:08:51 +08:00
|
|
|
*/
|
2017-05-16 05:23:51 +08:00
|
|
|
rtree_leaf_elm_slab_write(tsdn, rtree, elm, slab);
|
|
|
|
rtree_leaf_elm_szind_write(tsdn, rtree, elm, szind);
|
2017-03-27 19:08:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE rtree_leaf_elm_t *
|
|
|
|
rtree_leaf_elm_lookup(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
|
2017-01-16 08:56:30 +08:00
|
|
|
uintptr_t key, bool dependent, bool init_missing) {
|
2017-02-05 18:50:59 +08:00
|
|
|
assert(key != 0);
|
2016-03-28 18:06:35 +08:00
|
|
|
assert(!dependent || !init_missing);
|
2015-01-31 14:54:08 +08:00
|
|
|
|
2017-04-15 02:05:38 +08:00
|
|
|
size_t slot = rtree_cache_direct_map(key);
|
2017-02-05 18:50:59 +08:00
|
|
|
uintptr_t leafkey = rtree_leafkey(key);
|
2017-03-28 12:50:38 +08:00
|
|
|
assert(leafkey != RTREE_LEAFKEY_INVALID);
|
|
|
|
|
2017-04-15 02:05:38 +08:00
|
|
|
/* Fast path: L1 direct mapped cache. */
|
|
|
|
if (likely(rtree_ctx->cache[slot].leafkey == leafkey)) {
|
|
|
|
rtree_leaf_elm_t *leaf = rtree_ctx->cache[slot].leaf;
|
|
|
|
assert(leaf != NULL);
|
|
|
|
uintptr_t subkey = rtree_subkey(key, RTREE_HEIGHT-1);
|
|
|
|
return &leaf[subkey];
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Search the L2 LRU cache. On hit, swap the matching element into the
|
|
|
|
* slot in L1 cache, and move the position in L2 up by 1.
|
|
|
|
*/
|
|
|
|
#define RTREE_CACHE_CHECK_L2(i) do { \
|
|
|
|
if (likely(rtree_ctx->l2_cache[i].leafkey == leafkey)) { \
|
|
|
|
rtree_leaf_elm_t *leaf = rtree_ctx->l2_cache[i].leaf; \
|
2017-03-29 08:14:43 +08:00
|
|
|
assert(leaf != NULL); \
|
|
|
|
if (i > 0) { \
|
2017-03-28 02:48:39 +08:00
|
|
|
/* Bubble up by one. */ \
|
2017-04-15 02:05:38 +08:00
|
|
|
rtree_ctx->l2_cache[i].leafkey = \
|
|
|
|
rtree_ctx->l2_cache[i - 1].leafkey; \
|
|
|
|
rtree_ctx->l2_cache[i].leaf = \
|
|
|
|
rtree_ctx->l2_cache[i - 1].leaf; \
|
|
|
|
rtree_ctx->l2_cache[i - 1].leafkey = \
|
|
|
|
rtree_ctx->cache[slot].leafkey; \
|
|
|
|
rtree_ctx->l2_cache[i - 1].leaf = \
|
|
|
|
rtree_ctx->cache[slot].leaf; \
|
|
|
|
} else { \
|
|
|
|
rtree_ctx->l2_cache[0].leafkey = \
|
|
|
|
rtree_ctx->cache[slot].leafkey; \
|
|
|
|
rtree_ctx->l2_cache[0].leaf = \
|
|
|
|
rtree_ctx->cache[slot].leaf; \
|
2017-02-04 12:21:56 +08:00
|
|
|
} \
|
2017-04-15 02:05:38 +08:00
|
|
|
rtree_ctx->cache[slot].leafkey = leafkey; \
|
|
|
|
rtree_ctx->cache[slot].leaf = leaf; \
|
2017-03-29 08:14:43 +08:00
|
|
|
uintptr_t subkey = rtree_subkey(key, RTREE_HEIGHT-1); \
|
|
|
|
return &leaf[subkey]; \
|
2017-02-05 18:50:59 +08:00
|
|
|
} \
|
2017-02-04 12:21:56 +08:00
|
|
|
} while (0)
|
2017-03-28 02:48:39 +08:00
|
|
|
/* Check the first cache entry. */
|
2017-04-15 02:05:38 +08:00
|
|
|
RTREE_CACHE_CHECK_L2(0);
|
|
|
|
/* Search the remaining cache elements. */
|
|
|
|
for (unsigned i = 1; i < RTREE_CTX_NCACHE_L2; i++) {
|
|
|
|
RTREE_CACHE_CHECK_L2(i);
|
2017-02-05 18:50:59 +08:00
|
|
|
}
|
2017-04-15 02:05:38 +08:00
|
|
|
#undef RTREE_CACHE_CHECK_L2
|
2015-01-31 14:54:08 +08:00
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
return rtree_leaf_elm_lookup_hard(tsdn, rtree, rtree_ctx, key,
|
|
|
|
dependent, init_missing);
|
2015-01-31 14:54:08 +08:00
|
|
|
}
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static inline bool
|
2016-06-03 09:43:10 +08:00
|
|
|
rtree_write(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx, uintptr_t key,
|
2019-12-10 06:36:45 +08:00
|
|
|
edata_t *edata, szind_t szind, bool slab) {
|
|
|
|
/* Use rtree_clear() to set the edata to NULL. */
|
|
|
|
assert(edata != NULL);
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2017-03-17 08:57:52 +08:00
|
|
|
rtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, rtree, rtree_ctx,
|
|
|
|
key, false, true);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (elm == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-03-17 08:57:52 +08:00
|
|
|
|
2019-12-10 06:36:45 +08:00
|
|
|
assert(rtree_leaf_elm_edata_read(tsdn, rtree, elm, false) == NULL);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents;
|
|
|
|
contents.edata = edata;
|
|
|
|
contents.szind = szind;
|
|
|
|
contents.slab = slab;
|
|
|
|
rtree_leaf_elm_write(tsdn, rtree, elm, contents);
|
2016-03-28 18:06:35 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2016-03-28 18:06:35 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 08:57:52 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE rtree_leaf_elm_t *
|
2016-06-03 09:43:10 +08:00
|
|
|
rtree_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx, uintptr_t key,
|
2017-01-16 08:56:30 +08:00
|
|
|
bool dependent) {
|
2017-03-17 08:57:52 +08:00
|
|
|
rtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, rtree, rtree_ctx,
|
|
|
|
key, dependent, false);
|
|
|
|
if (!dependent && elm == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
assert(elm != NULL);
|
|
|
|
return elm;
|
|
|
|
}
|
2016-03-28 18:06:35 +08:00
|
|
|
|
2019-12-10 06:36:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE edata_t *
|
|
|
|
rtree_edata_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
|
2017-03-17 08:57:52 +08:00
|
|
|
uintptr_t key, bool dependent) {
|
|
|
|
rtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,
|
|
|
|
dependent);
|
2017-02-04 12:17:47 +08:00
|
|
|
if (!dependent && elm == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2019-12-10 06:36:45 +08:00
|
|
|
return rtree_leaf_elm_edata_read(tsdn, rtree, elm, dependent);
|
2017-03-17 08:57:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE szind_t
|
|
|
|
rtree_szind_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
|
|
|
|
uintptr_t key, bool dependent) {
|
|
|
|
rtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,
|
|
|
|
dependent);
|
|
|
|
if (!dependent && elm == NULL) {
|
2017-12-15 04:46:39 +08:00
|
|
|
return SC_NSIZES;
|
2017-03-17 08:57:52 +08:00
|
|
|
}
|
2017-05-16 05:23:51 +08:00
|
|
|
return rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);
|
2017-03-17 08:57:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rtree_slab_read() is intentionally omitted because slab is always read in
|
|
|
|
* conjunction with szind, which makes rtree_szind_slab_read() a better choice.
|
|
|
|
*/
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
2020-02-07 05:16:07 +08:00
|
|
|
rtree_edata_szind_slab_read(tsdn_t *tsdn, rtree_t *rtree,
|
|
|
|
rtree_ctx_t *rtree_ctx, uintptr_t key, bool dependent, edata_t **r_edata,
|
|
|
|
szind_t *r_szind, bool *r_slab) {
|
2017-03-17 08:57:52 +08:00
|
|
|
rtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,
|
|
|
|
dependent);
|
|
|
|
if (!dependent && elm == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-07 05:16:07 +08:00
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
|
|
|
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(bits);
|
|
|
|
|
|
|
|
*r_edata = contents.edata;
|
|
|
|
*r_szind = contents.szind;
|
|
|
|
*r_slab = contents.slab;
|
2020-02-07 05:16:07 +08:00
|
|
|
#else
|
2019-12-10 06:36:45 +08:00
|
|
|
*r_edata = rtree_leaf_elm_edata_read(tsdn, rtree, elm, dependent);
|
2017-05-16 05:23:51 +08:00
|
|
|
*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);
|
2020-02-07 05:16:07 +08:00
|
|
|
*r_slab = rtree_leaf_elm_slab_read(tsdn, rtree, elm, dependent);
|
|
|
|
#endif
|
2017-03-17 08:57:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-28 18:06:35 +08:00
|
|
|
|
2018-10-19 03:51:54 +08:00
|
|
|
/*
|
|
|
|
* Try to read szind_slab from the L1 cache. Returns true on a hit,
|
|
|
|
* and fills in r_szind and r_slab. Otherwise returns false.
|
|
|
|
*
|
|
|
|
* Key is allowed to be NULL in order to save an extra branch on the
|
|
|
|
* fastpath. returns false in this case.
|
|
|
|
*/
|
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
|
|
rtree_szind_slab_read_fast(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
|
|
|
|
uintptr_t key, szind_t *r_szind, bool *r_slab) {
|
|
|
|
rtree_leaf_elm_t *elm;
|
|
|
|
|
|
|
|
size_t slot = rtree_cache_direct_map(key);
|
|
|
|
uintptr_t leafkey = rtree_leafkey(key);
|
|
|
|
assert(leafkey != RTREE_LEAFKEY_INVALID);
|
|
|
|
|
|
|
|
if (likely(rtree_ctx->cache[slot].leafkey == leafkey)) {
|
|
|
|
rtree_leaf_elm_t *leaf = rtree_ctx->cache[slot].leaf;
|
|
|
|
assert(leaf != NULL);
|
|
|
|
uintptr_t subkey = rtree_subkey(key, RTREE_HEIGHT-1);
|
|
|
|
elm = &leaf[subkey];
|
|
|
|
|
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
|
|
|
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree,
|
|
|
|
elm, true);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(
|
|
|
|
bits);
|
|
|
|
*r_szind = contents.szind;
|
|
|
|
*r_slab = contents.slab;
|
2018-10-19 03:51:54 +08:00
|
|
|
#else
|
|
|
|
*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, true);
|
|
|
|
*r_slab = rtree_leaf_elm_slab_read(tsdn, rtree, elm, true);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 08:57:52 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
|
|
rtree_szind_slab_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
|
|
|
|
uintptr_t key, bool dependent, szind_t *r_szind, bool *r_slab) {
|
|
|
|
rtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,
|
|
|
|
dependent);
|
|
|
|
if (!dependent && elm == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-06 05:37:17 +08:00
|
|
|
#ifdef RTREE_LEAF_COMPACT
|
|
|
|
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(bits);
|
|
|
|
*r_szind = contents.szind;
|
|
|
|
*r_slab = contents.slab;
|
2018-04-06 05:37:17 +08:00
|
|
|
#else
|
2017-05-16 05:23:51 +08:00
|
|
|
*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);
|
|
|
|
*r_slab = rtree_leaf_elm_slab_read(tsdn, rtree, elm, dependent);
|
2018-04-06 05:37:17 +08:00
|
|
|
#endif
|
2017-03-17 08:57:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static inline void
|
2017-03-27 19:08:51 +08:00
|
|
|
rtree_szind_slab_update(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
|
|
|
|
uintptr_t key, szind_t szind, bool slab) {
|
2017-12-15 04:46:39 +08:00
|
|
|
assert(!slab || szind < SC_NBINS);
|
2017-03-27 19:08:51 +08:00
|
|
|
|
|
|
|
rtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key, true);
|
|
|
|
rtree_leaf_elm_szind_slab_update(tsdn, rtree, elm, szind, slab);
|
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static inline void
|
2017-01-16 08:56:30 +08:00
|
|
|
rtree_clear(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
|
|
|
|
uintptr_t key) {
|
2017-03-27 19:10:11 +08:00
|
|
|
rtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key, true);
|
2019-12-10 06:36:45 +08:00
|
|
|
assert(rtree_leaf_elm_edata_read(tsdn, rtree, elm, false) !=
|
2017-03-27 19:10:11 +08:00
|
|
|
NULL);
|
2020-03-14 02:47:51 +08:00
|
|
|
rtree_leaf_elm_contents_t contents;
|
|
|
|
contents.edata = NULL;
|
|
|
|
contents.szind = SC_NSIZES;
|
|
|
|
contents.slab = false;
|
|
|
|
rtree_leaf_elm_write(tsdn, rtree, elm, contents);
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
|
|
|
|
2017-05-24 05:26:31 +08:00
|
|
|
#endif /* JEMALLOC_INTERNAL_RTREE_H */
|