- Root cause: header-based class indexing (HEADER_CLASSIDX=1) wrote a 1-byte header during allocation, but linear carve/refill and initial slab capacity still used bare class block sizes. This mismatch could overrun slab usable space and corrupt freelists, causing reproducible SEGV at ~100k iters. Changes - Superslab: compute capacity with effective stride (block_size + header for classes 0..6; class7 remains headerless) in superslab_init_slab(). Add a debug-only bound check in superslab_alloc_from_slab() to fail fast if carve would exceed usable bytes. - Refill (non-P0 and P0): use header-aware stride for all linear carving and TLS window bump operations. Ensure alignment/validation in tiny_refill_opt.h also uses stride, not raw class size. - Drain: keep existing defense-in-depth for remote sentinel and sanitize nodes before splicing into freelist (already present). Notes - This unifies the memory layout across alloc/linear-carve/refill with a single stride definition and keeps class7 (1024B) headerless as designed. - Debug builds add fail-fast checks; release builds remain lean. Next - Re-run Tiny benches (256/1024B) in debug to confirm stability, then in release. If any remaining crash persists, bisect with HAKMEM_TINY_P0_BATCH_REFILL=0 to isolate P0 batch carve, and continue reducing branch-miss as planned.
106 lines
4.3 KiB
C
106 lines
4.3 KiB
C
#include <stdio.h>
|
||
#include "hakmem_ace.h"
|
||
#include "hakmem_pool.h"
|
||
#include "hakmem_l25_pool.h"
|
||
#include "hakmem_ace_stats.h"
|
||
#include "hakmem_policy.h"
|
||
#include "hakmem_debug.h"
|
||
|
||
// Map size to MidPool/LargePool and allocate.
|
||
// Phase 6.21: Bridge classes (40KB, 52KB) now hardcoded, DYN1/DYN2 disabled
|
||
static inline size_t round_to_mid_class(size_t s, double wmax, const FrozenPolicy* pol) {
|
||
// Build candidate set: 2/4/8/16/32/40/52 KiB (Bridge classes now fixed)
|
||
size_t cand[7] = {
|
||
POOL_CLASS_2KB, POOL_CLASS_4KB, POOL_CLASS_8KB, POOL_CLASS_16KB, POOL_CLASS_32KB,
|
||
POOL_CLASS_40KB, POOL_CLASS_52KB // Phase 6.21: Bridge classes
|
||
};
|
||
// Legacy: DYN1/DYN2 support (currently disabled in Phase 6.21)
|
||
// Note: If DYN classes are re-enabled, they should override Bridge classes
|
||
if (pol) {
|
||
if (pol->mid_dyn1_bytes >= POOL_MIN_SIZE && pol->mid_dyn1_bytes <= POOL_MAX_SIZE) cand[5] = (size_t)pol->mid_dyn1_bytes;
|
||
if (pol->mid_dyn2_bytes >= POOL_MIN_SIZE && pol->mid_dyn2_bytes <= POOL_MAX_SIZE) cand[6] = (size_t)pol->mid_dyn2_bytes;
|
||
}
|
||
// Choose the smallest c >= s that also satisfies c ≤ wmax*s
|
||
size_t best = 0; // 0 = not found
|
||
for (int i = 0; i < 7; i++) {
|
||
size_t c = cand[i];
|
||
if (c == 0) continue;
|
||
if (c < s) continue;
|
||
if ((double)c > wmax * (double)s) continue;
|
||
if (best == 0 || c < best) best = c;
|
||
}
|
||
return best;
|
||
}
|
||
|
||
static inline size_t round_to_large_class(size_t s, double wmax) {
|
||
const size_t classes[L25_NUM_CLASSES] = {
|
||
L25_CLASS_64KB, L25_CLASS_128KB, L25_CLASS_256KB, L25_CLASS_512KB, L25_CLASS_1MB
|
||
};
|
||
for (int i = 0; i < L25_NUM_CLASSES; i++) {
|
||
size_t c = classes[i];
|
||
if (s <= c) {
|
||
if ((double)c <= wmax * (double)s) return c;
|
||
return 0; // not allowed → fallback
|
||
}
|
||
}
|
||
return 0; // above 1MB
|
||
}
|
||
|
||
void* hkm_ace_alloc(size_t size, uintptr_t site_id, const FrozenPolicy* pol) {
|
||
double wmax_mid = (pol ? pol->w_max_mid : 1.33);
|
||
double wmax_large = (pol ? pol->w_max_large : 1.25);
|
||
|
||
// MidPool: 2–52KiB (Phase 6.21: with Bridge classes for W_MAX rounding)
|
||
if (size >= 33000 && size <= 34000) {
|
||
fprintf(stderr, "[ACE] Processing 33KB: size=%zu, POOL_MAX_SIZE=%d\n", size, POOL_MAX_SIZE);
|
||
}
|
||
if (size <= POOL_MAX_SIZE) {
|
||
size_t r = round_to_mid_class(size, wmax_mid, pol);
|
||
if (size >= 33000 && size <= 34000) {
|
||
fprintf(stderr, "[ACE] round_to_mid_class returned: %zu (0 means no valid class)\n", r);
|
||
}
|
||
if (r != 0) {
|
||
// Debug: Log 33KB allocation routing (only in debug builds)
|
||
#ifdef HAKMEM_DEBUG_VERBOSE
|
||
if (size >= 33000 && size <= 34000) {
|
||
HAKMEM_LOG("[ACE] 33KB alloc: size=%zu → rounded=%zu (class 5: 40KB)\n", size, r);
|
||
}
|
||
#endif
|
||
if (size >= 33000 && size <= 34000) {
|
||
fprintf(stderr, "[ACE] Calling hak_pool_try_alloc with size=%zu\n", r);
|
||
}
|
||
HKM_TIME_START(t_mid_get);
|
||
void* p = hak_pool_try_alloc(r, site_id);
|
||
HKM_TIME_END(HKM_CAT_POOL_GET, t_mid_get);
|
||
hkm_ace_stat_mid_attempt(p != NULL);
|
||
if (p) return p;
|
||
}
|
||
// If rounding not allowed or miss, fallthrough to large class rounding below
|
||
}
|
||
|
||
// LargePool: 64KiB–1MiB (with W_MAX rounding)
|
||
if (size <= L25_MAX_SIZE) {
|
||
size_t r = round_to_large_class(size, wmax_large);
|
||
if (r != 0) {
|
||
HKM_TIME_START(t_l25_get);
|
||
void* p = hak_l25_pool_try_alloc(r, site_id);
|
||
HKM_TIME_END(HKM_CAT_L25_GET, t_l25_get);
|
||
hkm_ace_stat_large_attempt(p != NULL);
|
||
if (p) return p;
|
||
}
|
||
} else if (size > POOL_MAX_SIZE && size < L25_MIN_SIZE) {
|
||
// Gap 32–64KiB: try rounding up to 64KiB if permitted
|
||
// size_t r = round_to_large_class(L25_MIN_SIZE, wmax_large); // check 64KiB vs size (unused)
|
||
if ((double)L25_MIN_SIZE <= wmax_large * (double)size) {
|
||
HKM_TIME_START(t_l25_get2);
|
||
void* p = hak_l25_pool_try_alloc(L25_MIN_SIZE, site_id);
|
||
HKM_TIME_END(HKM_CAT_L25_GET, t_l25_get2);
|
||
hkm_ace_stat_large_attempt(p != NULL);
|
||
if (p) return p;
|
||
}
|
||
}
|
||
|
||
hkm_ace_stat_l1_fallback();
|
||
return NULL; // Miss: let caller fallback to malloc
|
||
}
|