Files
hakmem/core/hakmem_tiny_ultra_front.inc.h

53 lines
1.8 KiB
C

// Inline helpers for Ultra-Simple TLS front (tiny per-class stack + bump)
// This header is textually included from hakmem_tiny.c after the following
// symbols are defined:
// - static int g_ultra_simple;
// - static __thread TinyUltraFront g_tls_ultra[];
// - __thread void* g_tls_sll_head[]; __thread uint32_t g_tls_sll_count[];
// - tiny_mag_init_if_needed(), g_tls_mags[]
#include "box/tls_sll_box.h" // Box TLS-SLL API
static inline void ultra_init_if_needed(int class_idx) {
if (!g_ultra_simple || class_idx < 0) return;
// nothing to do; zero-initialized
}
static inline void* ultra_pop(int class_idx) {
if (!g_ultra_simple) return NULL;
TinyUltraFront* uf = &g_tls_ultra[class_idx];
if (__builtin_expect(uf->top > 0, 1)) {
return uf->slots[--uf->top];
}
return NULL;
}
static inline int ultra_push(int class_idx, void* ptr) {
if (!g_ultra_simple) return 0;
TinyUltraFront* uf = &g_tls_ultra[class_idx];
if (__builtin_expect(uf->top < ULTRA_FRONT_CAP, 1)) { uf->slots[uf->top++] = ptr; return 1; }
return 0;
}
static inline int ultra_refill_small(int class_idx) {
if (!g_ultra_simple) return 0;
TinyUltraFront* uf = &g_tls_ultra[class_idx];
int room = ULTRA_FRONT_CAP - (int)uf->top; if (room <= 0) return 0;
int took = 0;
if (g_tls_sll_enable) {
while (room > 0) {
void* h = NULL;
if (!tls_sll_pop(class_idx, &h)) break;
uf->slots[uf->top++] = h; room--; took++;
}
}
if (room > 0) {
tiny_mag_init_if_needed(class_idx);
TinyTLSMag* mag = &g_tls_mags[class_idx];
int take = mag->top < room ? mag->top : room;
for (int i = 0; i < take; i++) uf->slots[uf->top++] = mag->items[--mag->top].ptr;
took += take;
}
return took;
}