diff --git a/core/front/tiny_heap_v2.h b/core/front/tiny_heap_v2.h index 60c86d45..38623dc4 100644 --- a/core/front/tiny_heap_v2.h +++ b/core/front/tiny_heap_v2.h @@ -79,6 +79,41 @@ static inline int tiny_heap_v2_refill_mag(int class_idx) { return 0; } +// Phase 13-A Step 2: Try to push a block into TinyHeapV2 magazine +// Called from free path to supply magazine with "leftover" blocks. +// Returns: 1 if pushed successfully, 0 if magazine is full +static inline int tiny_heap_v2_try_push(int class_idx, void* base) { + // 1. Check if class is enabled + if (class_idx < 0 || class_idx > 3) return 0; + if (!tiny_heap_v2_class_enabled(class_idx)) return 0; + + TinyHeapV2Mag* mag = &g_tiny_heap_v2_mag[class_idx]; + + // 2. Check if magazine has room + if (mag->top >= TINY_HEAP_V2_MAG_CAP) { + return 0; // Magazine full + } + + // 3. Push BASE pointer into magazine + mag->items[mag->top++] = base; + + // DEBUG: Log push events + static int g_push_dbg = -1; + if (g_push_dbg == -1) { + const char* e = getenv("HAKMEM_TINY_HEAP_V2_DEBUG"); + g_push_dbg = (e && *e && *e != '0') ? 1 : 0; + } + if (g_push_dbg) { + static __thread int g_push_count[TINY_NUM_CLASSES] = {0}; + if (g_push_count[class_idx] < 5) { + fprintf(stderr, "[HeapV2-PUSH] C%d push #%d, base=%p, mag->top=%d\n", + class_idx, g_push_count[class_idx]++, base, mag->top); + } + } + + return 1; // Success +} + // Tiny heap v2 alloc – returns BASE pointer or NULL. // Phase 13-A Step 1: Minimal "lucky hit" L0 cache (NO REFILL) // Strategy: Pop from magazine if available, else return NULL immediately.