Hide bin slab-locality query behind arena_locality_hint

bin_t is an arena implementation detail; tcache should not reach into
it. Extract the slab-address lookup into bin.c as bin_current_slab_addr,
and expose it to tcache only through arena_locality_hint(tsdn, arena,
szind), which composes bin_choose + bin_current_slab_addr.
This commit is contained in:
Slobodan Predolac 2026-05-05 10:00:04 -07:00
parent e286fba00a
commit 3c1c6ae419
6 changed files with 71 additions and 23 deletions

View file

@ -211,6 +211,14 @@ arena_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,
}
}
void *
arena_locality_hint(tsdn_t *tsdn, arena_t *arena, szind_t szind) {
assert(szind < SC_NBINS);
bin_t *bin = bin_choose(tsdn, arena, szind, NULL);
assert(bin != NULL);
return bin_current_slab_addr(tsdn, bin);
}
static void
arena_background_thread_inactivity_check(
tsdn_t *tsdn, arena_t *arena, bool is_background_thread) {

View file

@ -330,3 +330,16 @@ bin_choose(tsdn_t *tsdn, arena_t *arena, szind_t binind,
}
return arena_get_bin(arena, binind, binshard);
}
void *
bin_current_slab_addr(tsdn_t *tsdn, bin_t *bin) {
malloc_mutex_lock(tsdn, &bin->lock);
edata_t *slab = (bin->slabcur != NULL)
? bin->slabcur
: edata_heap_first(&bin->slabs_nonfull);
assert(slab != NULL || edata_heap_empty(&bin->slabs_nonfull));
void *ret = (slab != NULL) ? edata_addr_get(slab) : NULL;
assert(ret != NULL || slab == NULL);
malloc_mutex_unlock(tsdn, &bin->lock);
return ret;
}

View file

@ -203,26 +203,6 @@ tcache_gc_item_delay_compute(szind_t szind) {
return (uint8_t)item_delay;
}
static inline void *
tcache_gc_small_heuristic_addr_get(
tsd_t *tsd, tcache_slow_t *tcache_slow, szind_t szind) {
assert(szind < SC_NBINS);
tsdn_t *tsdn = tsd_tsdn(tsd);
bin_t *bin = bin_choose(tsdn, tcache_slow->arena, szind, NULL);
assert(bin != NULL);
malloc_mutex_lock(tsdn, &bin->lock);
edata_t *slab = (bin->slabcur == NULL)
? edata_heap_first(&bin->slabs_nonfull)
: bin->slabcur;
assert(slab != NULL || edata_heap_empty(&bin->slabs_nonfull));
void *ret = (slab != NULL) ? edata_addr_get(slab) : NULL;
assert(ret != NULL || slab == NULL);
malloc_mutex_unlock(tsdn, &bin->lock);
return ret;
}
static inline bool
tcache_gc_is_addr_remote(void *addr, uintptr_t min, uintptr_t max) {
assert(addr != NULL);
@ -417,9 +397,9 @@ tcache_gc_small(
goto label_flush;
}
/* Query arena binshard to get heuristic locality info. */
void *addr = tcache_gc_small_heuristic_addr_get(
tsd, tcache_slow, szind);
/* Query arena for a locality anchor (small-bin slab address). */
void *addr = arena_locality_hint(tsd_tsdn(tsd), tcache_slow->arena,
szind);
if (addr == NULL) {
goto label_flush;
}