Implementation: - Step 1: TLS SLL Guard Box (push前meta/class/state突合) - Step 2: SP_REBIND_SLOT macro (原子的slab rebind) - Step 3: Unified Geometry Box (ポインタ演算API統一) - Step 4: Unified Guard Box (HAKMEM_TINY_GUARD=1 統一制御) New Files (545 lines): - core/box/tiny_guard_box.h (277L) - TLS push guard (SuperSlab/slab/class/state validation) - Recycle guard (EMPTY確認) - Drain guard (準備) - 統一ENV制御: HAKMEM_TINY_GUARD=1 - core/box/tiny_geometry_box.h (174L) - BASE_FROM_USER/USER_FROM_BASE conversion - SS_FROM_PTR/SLAB_IDX_FROM_PTR lookup - PTR_CLASSIFY combined helper - 85+箇所の重複コード削減候補を特定 - core/box/sp_rebind_slot_box.h (94L) - SP_REBIND_SLOT macro (geometry + TLS reset + class_map原子化) - 6箇所に適用 (Stage 0/0.5/1/2/3) - デバッグトレース: HAKMEM_SP_REBIND_TRACE=1 Results: - ✅ TLS_SLL_DUP完全根絶 (0 crashes, 0 guard rejects) - ✅ パフォーマンス改善 +5.9% (15.16M → 16.05M ops/s on WS8192) - ✅ コンパイル警告0件(新規) - ✅ Box Theory準拠 (Single Responsibility, Clear Contract, Observable, Composable) Test Results: - Debug build: HAKMEM_TINY_GUARD=1 で10M iterations完走 - Release build: 3回平均 16.05M ops/s - Guard reject rate: 0% - Core dump: なし Box Theory Compliance: - Single Responsibility: 各Boxが単一責任 (guard/rebind/geometry) - Clear Contract: 明確なAPI境界 - Observable: ENV変数で制御可能な検証 - Composable: 全allocation/free pathから利用可能 Performance Impact: - Release build (guard無効): 影響なし (+5.9%改善) - Debug build (guard有効): 数%のオーバーヘッド (検証コスト) Architecture Improvements: - ポインタ演算の一元管理 (85+箇所の統一候補) - Slab rebindの原子性保証 - 検証機能の統合 (単一ENV制御) Phase 9 Status: - 性能目標 (25-30M ops/s): 未達 (16.05M = 53-64%) - TLS_SLL_DUP根絶: ✅ 達成 - コード品質: ✅ 大幅向上 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
175 lines
4.7 KiB
C
175 lines
4.7 KiB
C
// tiny_geometry_box.h - Pointer Geometry Calculation Box
|
|
// Purpose: Unified pointer arithmetic for BASE/USER/SuperSlab/slab_idx conversions
|
|
// License: MIT
|
|
// Date: 2025-11-30
|
|
//
|
|
// Box Theory Principles:
|
|
// - Single Responsibility: Pointer/offset calculation only
|
|
// - Clear Contract: Input(ptr/ss/slab_idx) → Output(conversion result)
|
|
// - Observable: Debug builds validate all range checks
|
|
// - Composable: Used by all allocation/free paths
|
|
//
|
|
// Background:
|
|
// Phase 9-3 analysis revealed pointer arithmetic duplication across codebase:
|
|
// - BASE ↔ USER conversion scattered in 40+ locations
|
|
// - ptr → SuperSlab lookup duplicated in 20+ files
|
|
// - slab_idx calculation implemented inconsistently
|
|
//
|
|
// Solution:
|
|
// Unified API for all pointer geometry operations with single source of truth
|
|
|
|
#ifndef HAKMEM_TINY_GEOMETRY_BOX_H
|
|
#define HAKMEM_TINY_GEOMETRY_BOX_H
|
|
|
|
#include "../hakmem_tiny_superslab_internal.h"
|
|
#include "../hakmem_super_registry.h"
|
|
#include "../superslab/superslab_inline.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
// ========== BASE ↔ USER Pointer Conversion ==========
|
|
//
|
|
// BASE pointer: Points to block start (where header is stored for C1-C6)
|
|
// USER pointer: Points to usable memory (BASE + header_size)
|
|
//
|
|
// Class 0 (16B): header_size = 0 (no header)
|
|
// Class 1-6: header_size = 1 byte
|
|
// Class 7 (C7): header_size = 0 (no header, uses offset 0 for next)
|
|
|
|
// Convert USER pointer → BASE pointer
|
|
static inline void* BASE_FROM_USER(int class_idx, void* user_ptr)
|
|
{
|
|
if (!user_ptr) return NULL;
|
|
|
|
// Class 0 and C7: no header, USER == BASE
|
|
if (class_idx == 0 || class_idx == 7) {
|
|
return user_ptr;
|
|
}
|
|
|
|
// Class 1-6: header is 1 byte before user pointer
|
|
return (void*)((uint8_t*)user_ptr - 1);
|
|
}
|
|
|
|
// Convert BASE pointer → USER pointer
|
|
static inline void* USER_FROM_BASE(int class_idx, void* base_ptr)
|
|
{
|
|
if (!base_ptr) return NULL;
|
|
|
|
// Class 0 and C7: no header, BASE == USER
|
|
if (class_idx == 0 || class_idx == 7) {
|
|
return base_ptr;
|
|
}
|
|
|
|
// Class 1-6: user pointer is 1 byte after base
|
|
return (void*)((uint8_t*)base_ptr + 1);
|
|
}
|
|
|
|
// ========== SuperSlab Lookup ==========
|
|
//
|
|
// Find which SuperSlab a pointer belongs to (uses existing hash table)
|
|
|
|
static inline SuperSlab* SS_FROM_PTR(void* ptr)
|
|
{
|
|
if (!ptr) return NULL;
|
|
|
|
// Use existing registry-based lookup (safe, crash-free)
|
|
return hak_super_lookup(ptr);
|
|
}
|
|
|
|
// ========== Slab Index Lookup ==========
|
|
//
|
|
// Find which slab index within a SuperSlab a pointer belongs to
|
|
|
|
static inline int SLAB_IDX_FROM_PTR(SuperSlab* ss, void* ptr)
|
|
{
|
|
if (!ss || !ptr) return -1;
|
|
|
|
// Delegate to existing slab_index_for() function
|
|
// (defined in superslab_inline.h)
|
|
return slab_index_for(ss, ptr);
|
|
}
|
|
|
|
// ========== Slab Data Offset Calculation ==========
|
|
//
|
|
// Calculate data offset for a slab index within SuperSlab
|
|
|
|
static inline size_t SLAB_DATA_OFFSET(SuperSlab* ss, int slab_idx)
|
|
{
|
|
if (!ss || slab_idx < 0) return 0;
|
|
|
|
// Use existing geometry helper
|
|
extern size_t ss_slab_data_offset(SuperSlab* ss, int slab_idx);
|
|
return ss_slab_data_offset(ss, slab_idx);
|
|
}
|
|
|
|
// ========== Block Stride Calculation ==========
|
|
//
|
|
// Get block stride (total size including header) for a class
|
|
|
|
static inline size_t BLOCK_STRIDE(int class_idx)
|
|
{
|
|
extern const size_t g_tiny_class_sizes[TINY_NUM_CLASSES];
|
|
|
|
if (class_idx < 0 || class_idx >= TINY_NUM_CLASSES) {
|
|
return 0;
|
|
}
|
|
|
|
return g_tiny_class_sizes[class_idx];
|
|
}
|
|
|
|
// ========== Debug Range Validation ==========
|
|
|
|
#if !HAKMEM_BUILD_RELEASE
|
|
static inline bool VALIDATE_PTR_RANGE(void* ptr, const char* context)
|
|
{
|
|
if (!ptr) return false;
|
|
|
|
uintptr_t addr = (uintptr_t)ptr;
|
|
|
|
// Check for NULL-ish and canonical address range
|
|
if (addr < 4096 || addr > 0x00007fffffffffffULL) {
|
|
fprintf(stderr, "[GEOMETRY_INVALID_PTR] context=%s ptr=%p\n",
|
|
context ? context : "(null)", ptr);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
#else
|
|
#define VALIDATE_PTR_RANGE(ptr, context) (1)
|
|
#endif
|
|
|
|
// ========== Combined Lookup Helpers ==========
|
|
//
|
|
// Common pattern: ptr → (SuperSlab, slab_idx, meta)
|
|
|
|
typedef struct {
|
|
SuperSlab* ss;
|
|
int slab_idx;
|
|
TinySlabMeta* meta;
|
|
} PtrGeometry;
|
|
|
|
static inline PtrGeometry PTR_CLASSIFY(void* ptr)
|
|
{
|
|
PtrGeometry result = {NULL, -1, NULL};
|
|
|
|
if (!VALIDATE_PTR_RANGE(ptr, "PTR_CLASSIFY")) {
|
|
return result;
|
|
}
|
|
|
|
result.ss = SS_FROM_PTR(ptr);
|
|
if (!result.ss) {
|
|
return result;
|
|
}
|
|
|
|
result.slab_idx = SLAB_IDX_FROM_PTR(result.ss, ptr);
|
|
if (result.slab_idx < 0) {
|
|
return result;
|
|
}
|
|
|
|
result.meta = &result.ss->slabs[result.slab_idx];
|
|
return result;
|
|
}
|
|
|
|
#endif // HAKMEM_TINY_GEOMETRY_BOX_H
|