Files
hakmem/core/box/tiny_heap_env_box.h
2025-12-07 22:49:28 +09:00

55 lines
2.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// tiny_heap_env_box.h - ENV gate for TinyHeap front (A/B 切り替え)
// 役割:
// - 新しい TinyHeap front を ON/OFF する環境変数の読み出しをホットパス外に分離。
// - デフォルト OFF環境変数が未設定または 0 のとき)。
#pragma once
#include <stdlib.h>
#include "c7_hotpath_env_box.h" // tiny_c7_hot_enabled()
// ENV: HAKMEM_TINY_HEAP_BOX=1 で TinyHeap front を有効化
static inline int tiny_heap_box_enabled(void) {
static int g_enable = -1;
if (__builtin_expect(g_enable == -1, 0)) {
const char* e = getenv("HAKMEM_TINY_HEAP_BOX");
g_enable = (e && *e && *e != '0') ? 1 : 0;
}
return g_enable;
}
// ENV: HAKMEM_TINY_HEAP_CLASSES (bitmask, bit i が 1 のクラスを TinyHeap 経路に載せる)
// 例: 0x80 (デフォルト) → C7 のみ / 0xC0 → C6 + C7 / 0xFF → 全クラス
static inline int tiny_heap_class_enabled(int class_idx) {
static int g_parsed = 0;
static unsigned g_mask = 0;
if (__builtin_expect(!g_parsed, 0)) {
g_mask = 0;
const char* e = getenv("HAKMEM_TINY_HEAP_CLASSES");
if (e && *e) {
unsigned v = (unsigned)strtoul(e, NULL, 0);
g_mask = v & 0xFFu;
} else {
// デフォルト: C7 のみ
g_mask = 1u << 7;
}
g_parsed = 1;
}
if (class_idx < 0 || class_idx >= TINY_NUM_CLASSES) return 0;
return (g_mask & (1u << class_idx)) != 0;
}
// TinyHeap front とクラス bitmask の両方が有効なときにだけ TinyHeap 経路を使う
static inline int tiny_heap_class_route_enabled(int class_idx) {
if (class_idx == 7) {
return tiny_heap_box_enabled() && tiny_c7_hot_enabled() && tiny_heap_class_enabled(class_idx);
}
return tiny_heap_box_enabled() && tiny_heap_class_enabled(class_idx);
}
// Helper: TinyHeap front + C7 Hot path が両方 ON のときに true
static inline int tiny_c7_heap_mode_enabled(void) {
return tiny_heap_class_route_enabled(7);
}