Files
hakmem/core/box/capacity_box.h

53 lines
1.7 KiB
C
Raw Normal View History

Box API Phase 1-3: Capacity Manager, Carve-Push, Prewarm 実装 Priority 1-3のBox Modulesを実装し、安全なpre-warming APIを提供。 既存の複雑なprewarmコードを1行のBox API呼び出しに置き換え。 ## 新規Box Modules 1. **Box Capacity Manager** (capacity_box.h/c) - TLS SLL容量の一元管理 - adaptive_sizing初期化保証 - Double-free バグ防止 2. **Box Carve-And-Push** (carve_push_box.h/c) - アトミックなblock carve + TLS SLL push - All-or-nothing semantics - Rollback保証(partial failure防止) 3. **Box Prewarm** (prewarm_box.h/c) - 安全なTLS cache pre-warming - 初期化依存性を隠蔽 - シンプルなAPI (1関数呼び出し) ## コード簡略化 hakmem_tiny_init.inc: 20行 → 1行 ```c // BEFORE: 複雑なP0分岐とエラー処理 adaptive_sizing_init(); if (prewarm > 0) { #if HAKMEM_TINY_P0_BATCH_REFILL int taken = sll_refill_batch_from_ss(5, prewarm); #else int taken = sll_refill_small_from_ss(5, prewarm); #endif } // AFTER: Box API 1行 int taken = box_prewarm_tls(5, prewarm); ``` ## シンボルExport修正 hakmem_tiny.c: 5つのシンボルをstatic → non-static - g_tls_slabs[] (TLS slab配列) - g_sll_multiplier (SLL容量乗数) - g_sll_cap_override[] (容量オーバーライド) - superslab_refill() (SuperSlab再充填) - ss_active_add() (アクティブカウンタ) ## ビルドシステム Makefile: TINY_BENCH_OBJS_BASEに3つのBox modules追加 - core/box/capacity_box.o - core/box/carve_push_box.o - core/box/prewarm_box.o ## 動作確認 ✅ Debug build成功 ✅ Box Prewarm API動作確認 [PREWARM] class=5 requested=128 taken=32 ## 次のステップ - Box Refill Manager (Priority 4) - Box SuperSlab Allocator (Priority 5) - Release build修正(tiny_debug_ring_record) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 01:45:30 +09:00
// 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 <stdint.h>
#include <stdbool.h>
// ============================================================================
// 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