Phase REFACTOR-2: Legacy Fallback Logic Unification

Consolidate duplicated legacy free logic into a single reusable function.
Previously, hak_tiny_free_legacy_inline() and hak_tiny_free_legacy_impl()
contained identical implementations in malloc_tiny_fast.h and
tiny_c6_ultra_free_box.c.

Changes:
- NEW: core/box/tiny_legacy_fallback_box.h
  - tiny_legacy_fallback_free_base(): Unified legacy free implementation
  - Encapsulates: Unified Cache push + per-class stats + final fallback
  - Contract: BASE pointer input (already extracted from USER ptr)

- Modified: core/front/malloc_tiny_fast.h
  - Removed: hak_tiny_free_legacy_inline() (lines 96-111)
  - Replaced call: hak_tiny_free_legacy_inline → tiny_legacy_fallback_free_base

- Modified: core/box/tiny_c6_ultra_free_box.c
  - Removed: hak_tiny_free_legacy_impl() (lines 17-39)
  - Replaced call: hak_tiny_free_legacy_impl → tiny_legacy_fallback_free_base

Benefits:
- Single source of truth (DRY principle)
- Easier to maintain and test
- Consistent behavior across all free paths
- No performance impact (always_inline preserved)

No semantic changes - identical logic, just centralized.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-12-11 19:01:59 +09:00
parent 3cf88dab84
commit 0752688785
3 changed files with 53 additions and 51 deletions

View File

@ -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);
}

View File

@ -0,0 +1,44 @@
#ifndef HAKMEM_TINY_LEGACY_FALLBACK_BOX_H
#define HAKMEM_TINY_LEGACY_FALLBACK_BOX_H
#include <stdbool.h>
#include <stdint.h>
#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

View File

@ -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