diff --git a/core/box/tiny_ptr_convert_box.h b/core/box/tiny_ptr_convert_box.h new file mode 100644 index 00000000..e27ef827 --- /dev/null +++ b/core/box/tiny_ptr_convert_box.h @@ -0,0 +1,32 @@ +#ifndef HAKMEM_TINY_PTR_CONVERT_BOX_H +#define HAKMEM_TINY_PTR_CONVERT_BOX_H + +#include + +// Purpose: Zero-overhead inline conversions between BASE and USER pointers +// Contract: Header is stored at offset -1 from USER pointer +// +// Example: +// - Allocation: base → user (user = base + 1) +// - Deallocation: user → base (base = user - 1) +// +// Offset definition (centralized for future extension) +#define TINY_HEADER_OFFSET 1 + +// Direct inline macros (zero cost, lvalue compatible in some contexts) +#define tiny_base_to_user_inline(base) \ + ((void*)((char*)(base) + TINY_HEADER_OFFSET)) + +#define tiny_user_to_base_inline(user) \ + ((void*)((char*)(user) - TINY_HEADER_OFFSET)) + +// Verbose variants (for clarity in critical paths) +static inline void* tiny_base_to_user(void* base) { + return tiny_base_to_user_inline(base); +} + +static inline void* tiny_user_to_base(void* user) { + return tiny_user_to_base_inline(user); +} + +#endif // HAKMEM_TINY_PTR_CONVERT_BOX_H diff --git a/core/front/malloc_tiny_fast.h b/core/front/malloc_tiny_fast.h index 01c6ec64..dc4c30d0 100644 --- a/core/front/malloc_tiny_fast.h +++ b/core/front/malloc_tiny_fast.h @@ -49,6 +49,7 @@ #include "../box/tiny_c6_ultra_free_box.h" // Phase 4-2: C6 ULTRA-free (free-only, C6-only) #include "../box/tiny_ultra_classes_box.h" // Phase REFACTOR-1: Named constants for C6/C7 #include "../box/tiny_legacy_fallback_box.h" // Phase REFACTOR-2: Legacy fallback logic unification +#include "../box/tiny_ptr_convert_box.h" // Phase REFACTOR-3: Inline pointer macro centralization #include "../box/tiny_front_v3_env_box.h" // Tiny front v3 snapshot gate #include "../box/tiny_heap_env_box.h" // ENV gate for TinyHeap front (A/B) #include "../box/tiny_route_env_box.h" // Route snapshot (Heap vs Legacy) @@ -177,7 +178,7 @@ static inline void* malloc_tiny_fast(size_t size) { FREE_PATH_STAT_INC(c6_ultra_alloc_hit); // BASE pointer のまま、USER pointer に変換して返す // (header は既に base[0] にある前提) - return (uint8_t*)base + 1; + return tiny_base_to_user_inline(base); } } @@ -295,7 +296,7 @@ static inline int free_tiny_fast(void* ptr) { } // 4. BASE を計算して Unified Cache に push - void* base = (void*)((char*)ptr - 1); + void* base = tiny_user_to_base_inline(ptr); tiny_front_free_stat_inc(class_idx); // Phase FREE-LEGACY-BREAKDOWN-1: カウンタ散布 (1. 関数入口)