Files
hakmem/core/hakmem_tiny_bg_bin.inc.h

38 lines
1.6 KiB
C
Raw Normal View History

// Inline helpers for Background Refill Bin (lock-free SLL)
// This header is textually included from hakmem_tiny.c after the following
// symbols are defined:
// - g_bg_bin_enable, g_bg_bin_target, g_bg_bin_head[]
// - tiny_bg_refill_main() declaration/definition if needed
static inline void* bgbin_pop(int class_idx) {
if (!g_bg_bin_enable) return NULL;
uintptr_t h = atomic_load_explicit(&g_bg_bin_head[class_idx], memory_order_acquire);
while (h != 0) {
void* p = (void*)h;
uintptr_t next = (uintptr_t)(*(void**)p);
if (atomic_compare_exchange_weak_explicit(&g_bg_bin_head[class_idx], &h, next,
memory_order_acq_rel, memory_order_acquire)) {
#if HAKMEM_DEBUG_COUNTERS
g_bgbin_pops[class_idx]++;
#endif
return p;
}
}
return NULL;
}
static inline void bgbin_push_chain(int class_idx, void* chain_head, void* chain_tail) {
if (!chain_head) return;
uintptr_t h = atomic_load_explicit(&g_bg_bin_head[class_idx], memory_order_acquire);
do { *(void**)chain_tail = (void*)h; }
while (!atomic_compare_exchange_weak_explicit(&g_bg_bin_head[class_idx], &h,
(uintptr_t)chain_head,
memory_order_acq_rel, memory_order_acquire));
}
static inline int bgbin_length_approx(int class_idx, int cap) {
uintptr_t h = atomic_load_explicit(&g_bg_bin_head[class_idx], memory_order_acquire);
int n = 0; while (h && n < cap) { void* p = (void*)h; h = (uintptr_t)(*(void**)p); n++; }
return n;
}