Files
hakmem/core/box/prewarm_box.h
Moe Charm (CI) c7616fd161 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

55 lines
1.7 KiB
C

// prewarm_box.h - Box Prewarm
// Priority 3 Box: Safe TLS Cache Pre-warming
//
// Purpose:
// - Simple, safe API for pre-warming TLS caches
// - Hides complex initialization dependencies
// - Uses Box Capacity Manager + Box Carve-Push for safety
// - Prevents double-free bugs from initialization order issues
//
// Design:
// - Wraps capacity_box + carve_push_box
// - Handles SuperSlab allocation automatically
// - Idempotent: safe to call multiple times
// - Clear success/failure reporting
#ifndef HAKMEM_BOX_PREWARM_H
#define HAKMEM_BOX_PREWARM_H
#include <stdint.h>
#include <stdbool.h>
// ============================================================================
// Box Prewarm API
// ============================================================================
// Pre-warm TLS SLL cache for a class with N blocks
//
// What it does:
// 1. Ensures capacity system is initialized
// 2. Checks/allocates SuperSlab if needed
// 3. Atomically carves and pushes N blocks to TLS SLL
//
// Returns: actual count pushed
// - On success: count (or less if capacity limit reached)
// - On failure: 0
//
// Safety guarantees:
// - No orphaned blocks (all-or-nothing carve-push)
// - Correct initialization order
// - Active counters updated atomically
// - No double-free risk
//
// Thread-safe: uses TLS
// Idempotent: safe to call multiple times (subsequent calls are no-op if already full)
//
// Example:
// box_prewarm_tls(5, 128); // Pre-warm class 5 (256B) with 128 blocks
int box_prewarm_tls(int class_idx, int count);
// Check if prewarm is needed (TLS SLL is empty or below threshold)
// Returns: number of blocks to prewarm, or 0 if already warmed
int box_prewarm_needed(int class_idx, int target_count);
#endif // HAKMEM_BOX_PREWARM_H