diff --git a/core/box/tiny_c6_ultra_free_box.c b/core/box/tiny_c6_ultra_free_box.c index 0de25a4b..43207e4b 100644 --- a/core/box/tiny_c6_ultra_free_box.c +++ b/core/box/tiny_c6_ultra_free_box.c @@ -1,4 +1,5 @@ #include "tiny_c6_ultra_free_box.h" +#include "tiny_legacy_fallback_box.h" // Phase REFACTOR-2: Unified legacy fallback #include "free_path_stats_box.h" #include "tiny_front_v3_env_box.h" #include "../hakmem.h" // For HAK_BASE_FROM_RAW @@ -14,29 +15,7 @@ TinyC6UltraFreeTLS* tiny_c6_ultra_free_tls(void) { return &g_c6_ultra_free_tls; } -// Legacy free helper (shared with slow path) -static void hak_tiny_free_legacy_impl(void* base, uint32_t class_idx) { - const TinyFrontV3Snapshot* front_snap = - __builtin_expect(tiny_front_v3_enabled(), 0) ? tiny_front_v3_snapshot_get() : NULL; - - // Legacy fallback - Unified Cache push - if (!front_snap || front_snap->unified_cache_on) { - if (unified_cache_push(class_idx, HAK_BASE_FROM_RAW(base))) { - FREE_PATH_STAT_INC(legacy_fallback); - - // Phase 4-1: Legacy per-class breakdown - if (__builtin_expect(free_path_stats_enabled(), 0)) { - if (class_idx < 8) { - g_free_path_stats.legacy_by_class[class_idx]++; - } - } - return; - } - } - - // Final fallback - tiny_hot_free_fast(class_idx, base); -} +// Phase REFACTOR-2: Legacy free helper removed (now in tiny_legacy_fallback_box.h) // Fast path: TLS cache push void tiny_c6_ultra_free_fast(void* base, uint32_t class_idx) { @@ -68,5 +47,5 @@ void tiny_c6_ultra_free_fast(void* base, uint32_t class_idx) { // Slow path: fallback to legacy free void tiny_c6_ultra_free_slow(void* base, uint32_t class_idx) { - hak_tiny_free_legacy_impl(base, class_idx); + tiny_legacy_fallback_free_base(base, class_idx); } diff --git a/core/box/tiny_legacy_fallback_box.h b/core/box/tiny_legacy_fallback_box.h new file mode 100644 index 00000000..06934365 --- /dev/null +++ b/core/box/tiny_legacy_fallback_box.h @@ -0,0 +1,44 @@ +#ifndef HAKMEM_TINY_LEGACY_FALLBACK_BOX_H +#define HAKMEM_TINY_LEGACY_FALLBACK_BOX_H + +#include +#include +#include "../front/tiny_unified_cache.h" +#include "../hakmem.h" +#include "tiny_front_v3_env_box.h" +#include "free_path_stats_box.h" +#include "tiny_front_hot_box.h" + +// Purpose: Encapsulate legacy free logic (shared by multiple paths) +// Called by: malloc_tiny_fast.h (free path) + tiny_c6_ultra_free_box.c (C6 fallback) +// +// Contract: +// - base: BASE pointer (already extracted via ptr - 1) +// - class_idx: size class (0-7) +// - Returns: void (always succeeds or falls back to tiny_hot_free_fast) +// +__attribute__((always_inline)) +static inline void tiny_legacy_fallback_free_base(void* base, uint32_t class_idx) { + const TinyFrontV3Snapshot* front_snap = + __builtin_expect(tiny_front_v3_enabled(), 0) ? tiny_front_v3_snapshot_get() : NULL; + + // Legacy fallback - Unified Cache push + if (!front_snap || front_snap->unified_cache_on) { + if (unified_cache_push(class_idx, HAK_BASE_FROM_RAW(base))) { + FREE_PATH_STAT_INC(legacy_fallback); + + // Per-class breakdown (Phase 4-1) + if (__builtin_expect(free_path_stats_enabled(), 0)) { + if (class_idx < 8) { + g_free_path_stats.legacy_by_class[class_idx]++; + } + } + return; + } + } + + // Final fallback + tiny_hot_free_fast(class_idx, base); +} + +#endif // HAKMEM_TINY_LEGACY_FALLBACK_BOX_H diff --git a/core/front/malloc_tiny_fast.h b/core/front/malloc_tiny_fast.h index 1a313bc8..01c6ec64 100644 --- a/core/front/malloc_tiny_fast.h +++ b/core/front/malloc_tiny_fast.h @@ -48,6 +48,7 @@ #include "../box/tiny_c7_ultra_box.h" // C7 ULTRA stub (UF-1, delegates to v3) #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_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) @@ -83,33 +84,11 @@ static inline int front_gate_unified_enabled(void) { } // ============================================================================ -// Phase 4-2: Legacy free helper (shared implementation in tiny_c6_ultra_free_box.c) +// Phase REFACTOR-2: Legacy free helper (unified in tiny_legacy_fallback_box.h) // ============================================================================ -// Helper function to perform legacy free (used in both regular path and C6 ULTRA fallback) -__attribute__((always_inline)) -static inline void hak_tiny_free_legacy_inline(void* base, uint32_t class_idx) { - const TinyFrontV3Snapshot* front_snap = - __builtin_expect(tiny_front_v3_enabled(), 0) ? tiny_front_v3_snapshot_get() : NULL; - - // Legacy fallback - Unified Cache push - if (!front_snap || front_snap->unified_cache_on) { - if (unified_cache_push(class_idx, HAK_BASE_FROM_RAW(base))) { - FREE_PATH_STAT_INC(legacy_fallback); - - // Phase 4-1: Legacy per-class breakdown - if (__builtin_expect(free_path_stats_enabled(), 0)) { - if (class_idx < 8) { - g_free_path_stats.legacy_by_class[class_idx]++; - } - } - return; - } - } - - // Final fallback - tiny_hot_free_fast(class_idx, base); -} +// Legacy free handling is encapsulated in tiny_legacy_fallback_box.h +// (Removed inline implementation to avoid duplication) // ============================================================================ // Phase 4-Step2: malloc_tiny_fast() - Hot/Cold Path Box (ACTIVE) @@ -507,8 +486,8 @@ static inline int free_tiny_fast(void* ptr) { } #endif - // Phase 4-2: Legacy fallback (use inline helper) - hak_tiny_free_legacy_inline(base, class_idx); + // Phase REFACTOR-2: Legacy fallback (use unified helper) + tiny_legacy_fallback_free_base(base, class_idx); return 1; #else // No header mode - fall back to normal free