diff --git a/core/superslab/superslab_inline.h b/core/superslab/superslab_inline.h index ad3305a8..54cd64b7 100644 --- a/core/superslab/superslab_inline.h +++ b/core/superslab/superslab_inline.h @@ -30,19 +30,33 @@ static inline uint8_t* tiny_slab_base_for(SuperSlab* ss, int slab_idx) } // Compute slab index for a pointer inside ss. +// Box 5 wrapper: inverse of Box 3 geometry (tiny_slab_base_for_geometry). +// Layout (data regions): +// - Slab 0: [ss + SUPERSLAB_SLAB0_DATA_OFFSET, ss + SLAB_SIZE) +// - Slab 1: [ss + 1*SLAB_SIZE, ss + 2*SLAB_SIZE) +// - Slab k: [ss + k*SLAB_SIZE, ss + (k+1)*SLAB_SIZE) static inline int slab_index_for(SuperSlab* ss, void* ptr) { - if (!ss || !ptr) return -1; + if (!ss || !ptr) { + return -1; + } uintptr_t base = (uintptr_t)ss; uintptr_t p = (uintptr_t)ptr; size_t ss_size = (size_t)1 << ss->lg_size; + // Outside overall SuperSlab range if (p < base + SUPERSLAB_SLAB0_DATA_OFFSET || p >= base + ss_size) { return -1; } - size_t rel = p - (base + SUPERSLAB_SLAB0_DATA_OFFSET); + // Slab 0: from first data byte up to the end of first slab + if (p < base + SLAB_SIZE) { + return 0; + } + + // Slabs 1+ use simple SLAB_SIZE spacing from SuperSlab base + size_t rel = p - base; int idx = (int)(rel / SLAB_SIZE); if (idx < 0 || idx >= SLABS_PER_SUPERSLAB_MAX) { return -1;