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>
This commit is contained in:
Moe Charm (CI)
2025-11-13 01:45:30 +09:00
parent 0543642dea
commit c7616fd161
14 changed files with 876 additions and 152 deletions

View File

@ -9,6 +9,7 @@
// - hak_tiny_alloc_superslab(): Main SuperSlab allocation entry point
#include "box/superslab_expansion_box.h" // Box E: Expansion with TLS state guarantee
#include "box/tiny_next_ptr_box.h" // Box API: Next pointer read/write
// ============================================================================
// Phase 6.23: SuperSlab Allocation Helpers
@ -152,7 +153,7 @@ static inline void* superslab_alloc_from_slab(SuperSlab* ss, int slab_idx) {
}
}
meta->freelist = *(void**)block; // Pop from freelist
meta->freelist = tiny_next_read(ss->size_class, block); // Pop from freelist
meta->used++;
if (__builtin_expect(tiny_refill_failfast_level() >= 2, 0)) {
@ -196,7 +197,7 @@ static inline int adopt_bind_if_safe(TinyTLSSlab* tls, SuperSlab* ss, int slab_i
}
// Phase 6.24 & 7.6: Refill TLS SuperSlab (with unified TLS cache + deferred allocation)
static SuperSlab* superslab_refill(int class_idx) {
SuperSlab* superslab_refill(int class_idx) {
#if HAKMEM_DEBUG_COUNTERS
g_superslab_refill_calls_dbg[class_idx]++;
#endif
@ -713,7 +714,7 @@ static inline void* hak_tiny_alloc_superslab(int class_idx) {
return NULL;
}
}
void* next = *(void**)block;
void* next = tiny_next_read(class_idx, block);
meta->freelist = next;
meta->used++;
// Optional: clear freelist bit when becomes empty
@ -770,21 +771,6 @@ static inline void* hak_tiny_alloc_superslab(int class_idx) {
// }
meta->used++;
// Debug: Log first C7 alloc for path verification (debug-only)
#if HAKMEM_DEBUG_VERBOSE
if (class_idx == 7) {
static _Atomic int c7_alloc_count = 0;
int count = atomic_fetch_add_explicit(&c7_alloc_count, 1, memory_order_relaxed);
if (count == 0) {
void* next = NULL;
// C7 has no header, next pointer is at base
if (block && ss->size_class == 7) {
next = *(void**)block;
}
fprintf(stderr, "[C7_FIRST_ALLOC] ptr=%p next=%p slab_idx=%d\n", block, next, slab_idx);
}
}
#endif
// Track active blocks in SuperSlab for conservative reclamation
ss_active_inc(ss);