diff --git a/core/hakmem_tiny.c b/core/hakmem_tiny.c index 8fdf0939..73538a95 100644 --- a/core/hakmem_tiny.c +++ b/core/hakmem_tiny.c @@ -113,28 +113,43 @@ static __thread unsigned char g_tls_bench_warm_done[4]; // Return helper: record tiny alloc stat (guarded) then return pointer static inline void tiny_debug_track_alloc_ret(int cls, void* ptr); -// Inject route commit into return helper so any successful allocation commits a fingerprint -// CRITICAL FIX (Phase 7-1.3): Guard legacy macro to allow Phase 7 override -// Phase 7 defines HAK_RET_ALLOC with header write in tiny_alloc_fast.inc.h -#ifndef HAK_RET_ALLOC -#ifdef HAKMEM_ENABLE_STATS -// Optional: sampling(ビルド時に有効化)。ホットパスは直接インライン呼び出し(間接分岐なし)。 -#ifdef HAKMEM_TINY_STAT_SAMPLING -static __thread unsigned g_tls_stat_accum_alloc[TINY_NUM_CLASSES]; -static int g_stat_rate_lg = 0; // 0=毎回、それ以外=2^lgごと -static inline __attribute__((always_inline)) void hkm_stat_alloc(int cls) { - if (__builtin_expect(g_stat_rate_lg == 0, 1)) { stats_record_alloc(cls); return; } - unsigned m = (1u << g_stat_rate_lg) - 1u; - if (((++g_tls_stat_accum_alloc[cls]) & m) == 0u) stats_record_alloc(cls); -} +// ========== HAK_RET_ALLOC: Single Definition Point ========== +// Choose implementation based on HAKMEM_TINY_HEADER_CLASSIDX +// - Phase 7 enabled: Write header and return user pointer +// - Phase 7 disabled: Legacy behavior (stats + route + return) + +#if HAKMEM_TINY_HEADER_CLASSIDX + // Phase 7: Write class_idx to header before returning + #define HAK_RET_ALLOC(cls, ptr) return tiny_region_id_write_header((ptr), (cls)) #else -static inline __attribute__((always_inline)) void hkm_stat_alloc(int cls) { stats_record_alloc(cls); } -#endif -#define HAK_RET_ALLOC(cls, ptr) do { tiny_debug_track_alloc_ret((cls), (ptr)); hkm_stat_alloc((cls)); ROUTE_COMMIT((cls), 0x7F); return (ptr); } while(0) -#else -#define HAK_RET_ALLOC(cls, ptr) do { tiny_debug_track_alloc_ret((cls), (ptr)); ROUTE_COMMIT((cls), 0x7F); return (ptr); } while(0) -#endif -#endif // HAK_RET_ALLOC + // Legacy: Stats and routing before return + #ifdef HAKMEM_ENABLE_STATS + // Optional: sampling(ビルド時に有効化)。ホットパスは直接インライン呼び出し(間接分岐なし)。 + #ifdef HAKMEM_TINY_STAT_SAMPLING + static __thread unsigned g_tls_stat_accum_alloc[TINY_NUM_CLASSES]; + static int g_stat_rate_lg = 0; // 0=毎回、それ以外=2^lgごと + static inline __attribute__((always_inline)) void hkm_stat_alloc(int cls) { + if (__builtin_expect(g_stat_rate_lg == 0, 1)) { stats_record_alloc(cls); return; } + unsigned m = (1u << g_stat_rate_lg) - 1u; + if (((++g_tls_stat_accum_alloc[cls]) & m) == 0u) stats_record_alloc(cls); + } + #else + static inline __attribute__((always_inline)) void hkm_stat_alloc(int cls) { stats_record_alloc(cls); } + #endif + #define HAK_RET_ALLOC(cls, ptr) do { \ + tiny_debug_track_alloc_ret((cls), (ptr)); \ + hkm_stat_alloc((cls)); \ + ROUTE_COMMIT((cls), 0x7F); \ + return (ptr); \ + } while(0) + #else + #define HAK_RET_ALLOC(cls, ptr) do { \ + tiny_debug_track_alloc_ret((cls), (ptr)); \ + ROUTE_COMMIT((cls), 0x7F); \ + return (ptr); \ + } while(0) + #endif +#endif // HAKMEM_TINY_HEADER_CLASSIDX // Free-side stats: compile-time zero when stats disabled #ifdef HAKMEM_ENABLE_STATS diff --git a/core/tiny_alloc_fast.inc.h b/core/tiny_alloc_fast.inc.h index 22a85611..88e77f67 100644 --- a/core/tiny_alloc_fast.inc.h +++ b/core/tiny_alloc_fast.inc.h @@ -63,13 +63,8 @@ extern int g_refill_count_hot; extern int g_refill_count_mid; extern int g_refill_count_class[TINY_NUM_CLASSES]; -// External macros -// Phase 7: Write header before returning (if enabled) -// CRITICAL: Undefine legacy macro to ensure Phase 7 version is used -#ifdef HAK_RET_ALLOC -#undef HAK_RET_ALLOC -#endif -#define HAK_RET_ALLOC(cls, ptr) return tiny_region_id_write_header((ptr), (cls)) +// HAK_RET_ALLOC macro is now defined in core/hakmem_tiny.c +// See lines 116-152 for single definition point based on HAKMEM_TINY_HEADER_CLASSIDX // ========== RDTSC Profiling (lightweight) ========== #ifdef __x86_64__