#include "remote_side_box.h" #include #include #include #include #ifndef REM_SIDE_LOG2 #define REM_SIDE_LOG2 20 #endif static _Atomic uint32_t g_remote_log2 = REM_SIDE_LOG2; static _Atomic uint32_t g_remote_size = (1u << REM_SIDE_LOG2); static _Atomic uint32_t g_remote_mask = (1u << REM_SIDE_LOG2) - 1; static _Atomic int g_remote_profile_inited = 0; static rem_side_entry* g_remote_slots = NULL; static _Atomic int g_remote_allocated = 0; static void remote_side_apply_profile(const char* profile) { if (g_remote_profile_inited) { return; } const char* env_profile = profile ? profile : getenv("HAKMEM_PROFILE"); int is_bench = (env_profile && strcmp(env_profile, "bench") == 0); uint32_t log2 = REM_SIDE_LOG2; if (is_bench && REM_SIDE_LOG2 > 4) { // bench 用: ハッシュ幅だけ 1/8〜1/16 程度に論理縮小 log2 = REM_SIDE_LOG2 - 3; // 1/8 if (log2 < 12) { log2 = 12; // 4096 entries までは確保 } } uint32_t size = (1u << log2); uint32_t mask = size - 1; atomic_store_explicit(&g_remote_log2, log2, memory_order_relaxed); atomic_store_explicit(&g_remote_size, size, memory_order_relaxed); atomic_store_explicit(&g_remote_mask, mask, memory_order_relaxed); atomic_store_explicit(&g_remote_profile_inited, 1, memory_order_release); } void remote_side_init(RemoteSideBox* box, const char* profile) { (void)box; remote_side_apply_profile(profile); if (atomic_load_explicit(&g_remote_allocated, memory_order_acquire)) { return; } uint32_t size = remote_side_effective_size(); g_remote_slots = (rem_side_entry*)calloc(size, sizeof(rem_side_entry)); if (!g_remote_slots) { fprintf(stderr, "[REMOTE_SIDE] failed to allocate %zu bytes\n", (size_t)size * sizeof(rem_side_entry)); abort(); } atomic_store_explicit(&g_remote_allocated, 1, memory_order_release); } uint32_t remote_side_effective_log2(void) { if (!atomic_load_explicit(&g_remote_profile_inited, memory_order_acquire)) { remote_side_apply_profile(NULL); } return atomic_load_explicit(&g_remote_log2, memory_order_relaxed); } uint32_t remote_side_effective_size(void) { if (!atomic_load_explicit(&g_remote_profile_inited, memory_order_acquire)) { remote_side_apply_profile(NULL); } return atomic_load_explicit(&g_remote_size, memory_order_relaxed); } uint32_t remote_side_effective_mask(void) { if (!atomic_load_explicit(&g_remote_profile_inited, memory_order_acquire)) { remote_side_apply_profile(NULL); } return atomic_load_explicit(&g_remote_mask, memory_order_relaxed); } rem_side_entry* remote_side_table(void) { if (!atomic_load_explicit(&g_remote_allocated, memory_order_acquire)) { remote_side_init(NULL, NULL); } return g_remote_slots; }