Files
hakmem/core/tiny_mmap_gate.h

79 lines
2.7 KiB
C
Raw Normal View History

// 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)
// Phase 6: Registry Optimization - Use per-class registry for O(class_size) scan
{
// Phase 6: Use per-class registry (262K → ~10-100 entries per class!)
extern SuperSlab* g_super_reg_by_class[TINY_NUM_CLASSES][SUPER_REG_PER_CLASS];
extern int g_super_reg_class_size[TINY_NUM_CLASSES];
uint32_t self_tid = tiny_self_u32();
const int scan_max = tiny_reg_scan_max();
int reg_size = g_super_reg_class_size[class_idx];
int scan_limit = (scan_max < reg_size) ? scan_max : reg_size;
for (int i = 0; i < scan_limit; i++) {
SuperSlab* cand = g_super_reg_by_class[class_idx][i];
if (!cand || cand->magic != SUPERSLAB_MAGIC) continue;
// Note: class_idx check is not needed (per-class registry!)
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);
}
}
}
}
return NULL;
}