// tiny_mmap_gate.h - Mmap Gate (must-adopt-before-mmap) #pragma once #include "hakmem_tiny_superslab.h" #include "tiny_refill.h" #include "hakmem_super_registry.h" // Returns adopted SuperSlab* or NULL static inline SuperSlab* tiny_must_adopt_gate(int class_idx, TinyTLSSlab* tls) { // Env: enable gate static int en = -1; if (__builtin_expect(en == -1, 0)) { const char* s = getenv("HAKMEM_TINY_MUST_ADOPT"); en = (s && atoi(s) != 0) ? 1 : 0; } if (!en) return NULL; // Try fast adopt once SuperSlab* ss = tiny_refill_try_fast(class_idx, tls); if (ss) return ss; // Optional light remote drain to surface supply if (!ss) { // If TLS holds an SS, lightly drain its remotes to expose freelist SuperSlab* cur = tls->ss; if (cur && cur->magic == SUPERSLAB_MAGIC) { ss_remote_drain_light(cur); } } // Optional yield between attempts static int yv = -1; if (__builtin_expect(yv == -1, 0)) { const char* y = getenv("HAKMEM_TINY_MMAP_YIELD"); yv = (y && atoi(y) != 0) ? 1 : 0; } if (yv) sched_yield(); // Try again after yield ss = tiny_refill_try_fast(class_idx, tls); if (ss) return ss; // Registry small-window adopt (one pass, limited scan) { uint32_t self_tid = tiny_self_u32(); int scanned = 0; const int scan_max = tiny_reg_scan_max(); for (int i = 0; i < SUPER_REG_SIZE && scanned < scan_max; i++) { SuperRegEntry* e = &g_super_reg[i]; uintptr_t base = atomic_load_explicit((_Atomic uintptr_t*)&e->base, memory_order_acquire); if (base == 0) continue; SuperSlab* cand = atomic_load_explicit(&e->ss, memory_order_acquire); if (!cand || cand->magic != SUPERSLAB_MAGIC) continue; if ((int)cand->size_class != class_idx) { scanned++; continue; } int cap = ss_slabs_capacity(cand); for (int s = 0; s < cap; s++) { // Box: Try to acquire ownership SlabHandle h = slab_try_acquire(cand, s, self_tid); if (slab_is_valid(&h)) { // Box: Safe to drain - ownership guaranteed slab_drain_remote_full(&h); if (slab_freelist(&h)) { tiny_tls_bind_slab(tls, h.ss, h.slab_idx); return h.ss; } slab_release(&h); } } scanned++; } } return NULL; }