// pool_api.inc.h — Box: L2 Pool public API (alloc/free/lookup) #ifndef POOL_API_INC_H #define POOL_API_INC_H #include "pagefault_telemetry_box.h" // Box PageFaultTelemetry (PF_BUCKET_MID) #include "box/pool_hotbox_v2_box.h" #include "box/tiny_heap_env_box.h" // TinyHeap profile (C7_SAFE では flatten を無効化) #include "box/pool_zero_mode_box.h" // Pool zeroing policy (env cached) #include "box/pool_config_box.h" // Pool configuration & ENV gates #include "box/pool_stats_box.h" // Pool statistics & monitoring #include "box/pool_mid_desc_cache_box.h" // Mid descriptor TLS cache #include "box/pool_free_v1_box.h" // Pool v1 free implementation (L0-SplitBox + L1-FastBox/SlowBox) #include "box/pool_block_to_user_box.h" // Pool block to user pointer helpers #include "box/pool_free_v2_box.h" // Pool v2 free implementation (with hotbox v2) #include "box/pool_alloc_v1_flat_box.h" // Pool v1 flatten (TLS-only fast path) #include "box/pool_alloc_v2_box.h" // Pool v2 alloc implementation (with hotbox v2) #include "box/pool_alloc_v1_box.h" // Pool v1 alloc implementation (baseline) #include static inline int hak_pool_mid_lookup_v1_impl(void* ptr, size_t* out_size) { if (g_mf2_enabled) { MidPage* page = mf2_addr_to_page(ptr); if (page) { int c = (int)page->class_idx; if (c < 0 || c >= POOL_NUM_CLASSES) return 0; size_t sz = g_class_sizes[c]; if (sz == 0) return 0; if (out_size) *out_size = sz; return 1; } } MidPageDesc* d = mid_desc_lookup_cached(ptr); if (!d) return 0; int c = (int)d->class_idx; if (c < 0 || c >= POOL_NUM_CLASSES) return 0; size_t sz = g_class_sizes[c]; if (sz == 0) return 0; if (out_size) *out_size = sz; return 1; } static inline void hak_pool_free_fast_v1_impl(void* ptr, uintptr_t site_id) { if (!ptr || !g_pool.initialized) return; if (g_mf2_enabled) { MidPage* page = mf2_addr_to_page(ptr); if (page) { mf2_free(ptr); return; } } MidPageDesc* d = mid_desc_lookup_cached(ptr); if (!d) return; size_t sz = g_class_sizes[(int)d->class_idx]; if (sz == 0) return; hak_pool_free(ptr, sz, site_id); } // --- Public wrappers (env-gated) ---------------------------------------------- static inline int hak_pool_v2_route(void) { return hak_pool_v2_enabled(); } void* hak_pool_try_alloc(size_t size, uintptr_t site_id) { if (!hak_pool_v2_route()) { if (hak_pool_v1_flatten_enabled()) { return hak_pool_try_alloc_v1_flat(size, site_id); } return hak_pool_try_alloc_v1_impl(size, site_id); } return hak_pool_try_alloc_v2_impl(size, site_id); } void hak_pool_free(void* ptr, size_t size, uintptr_t site_id) { // Phase FREE-LEGACY-BREAKDOWN-1: pool v1 カウンタ extern void free_path_stat_inc_pool_v1_fast(void); free_path_stat_inc_pool_v1_fast(); if (!hak_pool_v2_route()) { if (hak_pool_v1_flatten_enabled()) { hak_pool_free_v1_flat(ptr, size, site_id); } else { hak_pool_free_v1_impl(ptr, size, site_id); } return; } hak_pool_free_v2_impl(ptr, size, site_id); } void hak_pool_free_fast(void* ptr, uintptr_t site_id) { if (!hak_pool_v2_route()) { // fast path lacks size; keep existing v1 fast implementation even when // flatten is enabled to avoid behavior drift. hak_pool_free_fast_v1_impl(ptr, site_id); return; } hak_pool_free_fast_v2_impl(ptr, site_id); } int hak_pool_mid_lookup(void* ptr, size_t* out_size) { if (!hak_pool_v2_route()) { return hak_pool_mid_lookup_v1_impl(ptr, out_size); } return hak_pool_mid_lookup_v2_impl(ptr, out_size); } #endif // POOL_API_INC_H