// capacity_box.h - Box Capacity Manager // Priority 1 Box: TLS Cache Capacity Management // // Purpose: // - Centralize all capacity calculations (adaptive sizing, SLL cap, etc.) // - Prevent initialization order bugs (root cause of prewarm double-free) // - Provide simple, safe API for capacity queries // // Design: // - Wraps adaptive_sizing system // - Idempotent initialization // - Bounds checking built-in // - Thread-safe (uses TLS) #ifndef HAKMEM_BOX_CAPACITY_H #define HAKMEM_BOX_CAPACITY_H #include #include // ============================================================================ // Box Capacity API // ============================================================================ // Initialize capacity system (idempotent - safe to call multiple times) // MUST be called before any other box_cap_* functions void box_cap_init(void); // Get current TLS SLL capacity for a class // Returns: capacity in blocks, or 0 if not initialized // Thread-safe: uses TLS uint32_t box_cap_get(int class_idx); // Check if TLS SLL has room for N blocks // Returns: true if N blocks can be added, false otherwise // Thread-safe: uses TLS bool box_cap_has_room(int class_idx, uint32_t n); // Get available space in TLS SLL // Returns: number of blocks that can be added // Thread-safe: uses TLS uint32_t box_cap_avail(int class_idx); // Update capacity (adaptive sizing hook) // Note: Normally called by adaptive sizing system, not manually void box_cap_update(int class_idx, uint32_t new_cap); // Check if capacity system is initialized // Returns: true if box_cap_init() was called bool box_cap_is_initialized(void); #endif // HAKMEM_BOX_CAPACITY_H