// slab_carve_box.h - Slab Carving Box // Purpose: Unified API for carving blocks from SuperSlabs // Used by: Warm pool hot path, normal refill path, P0 batch refill // License: MIT // Date: 2025-12-04 #ifndef HAK_SLAB_CARVE_BOX_H #define HAK_SLAB_CARVE_BOX_H #include #include #include "../hakmem_tiny_config.h" #include "../hakmem_tiny_superslab.h" #include "../superslab/superslab_inline.h" #include "../tiny_box_geometry.h" #include "../box/tiny_next_ptr_box.h" #include "../box/pagefault_telemetry_box.h" // ============================================================================ // Slab Carving API (Inline for Hot Path) // ============================================================================ // Try to carve blocks directly from a SuperSlab // Returns: Number of blocks produced (0 if carve failed) // // Parameters: // class_idx - Allocation class (determines block size) // ss - Target SuperSlab to carve from // out - Output buffer for carved blocks // max_blocks - Maximum blocks to carve // // Algorithm: // 1. Validate SuperSlab magic // 2. Scan all slabs in SuperSlab for class match // 3. For each matching slab: // a. Try freelist first (if available) // b. Fall back to linear carve (if capacity available) // c. Stop when max_blocks reached // 4. Return total blocks carved // // Performance: O(slabs_in_ss) linear scan, typically 3-4 iterations // static inline int slab_carve_from_ss(int class_idx, SuperSlab* ss, void** out, int max_blocks) { if (!ss || ss->magic != SUPERSLAB_MAGIC) return 0; // Find an available slab in this SuperSlab int cap = ss_slabs_capacity(ss); for (int slab_idx = 0; slab_idx < cap; slab_idx++) { TinySlabMeta* meta = &ss->slabs[slab_idx]; // Check if this slab matches our class and has capacity if (meta->class_idx != (uint8_t)class_idx) continue; if (meta->used >= meta->capacity && !meta->freelist) continue; // Carve blocks from this slab size_t bs = tiny_stride_for_class(class_idx); uint8_t* base = tiny_slab_base_for_geometry(ss, slab_idx); int produced = 0; while (produced < max_blocks) { void* p = NULL; if (meta->freelist) { // Pop from freelist p = meta->freelist; void* next_node = tiny_next_read(class_idx, p); #if HAKMEM_TINY_HEADER_CLASSIDX *(uint8_t*)p = (uint8_t)(0xa0 | (class_idx & 0x0f)); __atomic_thread_fence(__ATOMIC_RELEASE); #endif meta->freelist = next_node; meta->used++; } else if (meta->carved < meta->capacity) { // Linear carve p = (void*)(base + ((size_t)meta->carved * bs)); #if HAKMEM_TINY_HEADER_CLASSIDX *(uint8_t*)p = (uint8_t)(0xa0 | (class_idx & 0x0f)); #endif meta->carved++; meta->used++; } else { break; // This slab exhausted } if (p) { pagefault_telemetry_touch(class_idx, p); out[produced++] = p; } } if (produced > 0) return produced; // If this slab had no freelist and no carved capacity, continue to next } return 0; // No slab in this SuperSlab had available capacity } #endif // HAK_SLAB_CARVE_BOX_H