Files
hakmem/core/front/tiny_ultrafront.h

124 lines
4.6 KiB
C
Raw Normal View History

// tiny_ultrafront.h - Phase UltraFront: 密結合 Tiny Front Path (実験箱)
//
// 目的:
// - 既存の FrontGate + Unified Cache を前提に、
// malloc/free の Tiny 経路を「1本のインラインパス」に近づける実験的フロント。
// - Box Theory 的には Front 層の別バリアント Box として扱い、
// ENV で A/B 切り替え可能にする。
//
// 特徴:
// - Tiny 範囲 (size <= tiny_get_max_size()) 専用。
// - Unified Cache を直接叩く (unified_cache_pop_or_refill / unified_cache_push)。
// - Header 書き込み/読取りは tiny_region_id_* を利用して安全性を維持。
//
// ENV:
// HAKMEM_TINY_ULTRA_FRONT=1 ... UltraFront 有効 (デフォルト: 0, 無効)
//
// 統合ポイント:
// - malloc ラッパ (hak_wrappers.inc.h) の FrontGate ブロック内から
// tiny_ultrafront_malloc(size) を first try として呼び出す。
// - free ラッパから tiny_ultrafront_free(ptr) を first try として呼び出す。
#ifndef HAK_FRONT_TINY_ULTRA_FRONT_H
#define HAK_FRONT_TINY_ULTRA_FRONT_H
#include <stddef.h>
#include <stdint.h>
#include "../hakmem_build_flags.h"
#include "../hakmem_tiny.h" // tiny_get_max_size, hak_tiny_size_to_class
// #include "tiny_unified_cache.h" // Removed (A/B test: OFF is faster)
#include "../tiny_region_id.h" // tiny_region_id_write_header / read_header
// ============================================================================
// ENV Control (cached, lazy init)
// ============================================================================
static inline int tiny_ultrafront_enabled(void) {
static int g_enable = -1;
if (__builtin_expect(g_enable == -1, 0)) {
const char* e = getenv("HAKMEM_TINY_ULTRA_FRONT");
g_enable = (e && *e && *e != '0') ? 1 : 0;
#if !HAKMEM_BUILD_RELEASE
if (g_enable) {
fprintf(stderr, "[UltraFront-INIT] tiny_ultrafront_enabled() = %d\n", g_enable);
fflush(stderr);
}
#endif
}
return g_enable;
}
// ============================================================================
// UltraFront malloc/free (Tiny 専用)
// ============================================================================
// UltraFront Tiny allocation
// - size: ユーザー要求サイズ
// - 戻り値: USER ポインタ or NULL (Unified miss時は通常経路にフォールバックさせる)
static inline void* tiny_ultrafront_malloc(size_t size) {
// Tiny 範囲外は扱わない
if (__builtin_expect(size == 0 || size > tiny_get_max_size(), 0)) {
return NULL;
}
// サイズ→クラス (branchless LUT)
int class_idx = hak_tiny_size_to_class(size);
if (__builtin_expect(class_idx < 0 || class_idx >= TINY_NUM_CLASSES, 0)) {
return NULL;
}
// Unified Cache から BASE を取得 (hit or refill)
// DELETED (A/B test: OFF is faster)
// void* base = unified_cache_pop_or_refill(class_idx);
// if (__builtin_expect(base == NULL, 0)) {
// // Unified Cache disabled or refill failed → 通常経路にフォールバック
// return NULL;
// }
// Unified Cache removed → 通常経路にフォールバック
return NULL;
}
// UltraFront Tiny free
// - ptr: USER ポインタ
// - 戻り値: 1=UltraFront で処理済み, 0=フォールバック (通常 free 経路へ)
static inline int tiny_ultrafront_free(void* ptr) {
if (__builtin_expect(!ptr, 0)) {
return 0;
}
#if HAKMEM_TINY_HEADER_CLASSIDX
// ページ境界ガード: ptr がページ先頭 (offset==0) の場合、ptr-1 は
// 別ページ/未マップ領域となり得るので UltraFront では扱わない。
uintptr_t off = (uintptr_t)ptr & 0xFFFu;
if (__builtin_expect(off == 0, 0)) {
return 0;
}
// Header ベースの class_idx 読取り (tiny_region_id_read_header は magic/範囲チェック込み)
int class_idx = tiny_region_id_read_header(ptr);
if (__builtin_expect(class_idx < 0 || class_idx >= TINY_NUM_CLASSES, 0)) {
// Tiny ヘッダが無い or 壊れている → 非Tiny / 別ドメインなのでフォールバック
return 0;
}
// void* base = (void*)((uint8_t*)ptr - 1);
// Unified Cache へ BASE を push
// DELETED (A/B test: OFF is faster)
// int pushed = unified_cache_push(class_idx, base);
// if (__builtin_expect(pushed, 1)) {
// return 1;
// }
// Unified Cache removed → 通常 free 経路へ
return 0;
#else
// ヘッダモードでなければ UltraFront は何もしない
(void)ptr;
return 0;
#endif
}
#endif // HAK_FRONT_TINY_ULTRA_FRONT_H