Files
hakmem/core/box/tiny_tcache_env_box.c

69 lines
2.4 KiB
C
Raw Normal View History

// ============================================================================
// Phase 14 v1: Tiny tcache ENV Box (L0) - Implementation
// ============================================================================
#include "tiny_tcache_env_box.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
// ============================================================================
// Global State
// ============================================================================
_Atomic int g_tiny_tcache_enabled = -1;
_Atomic uint16_t g_tiny_tcache_cap = 0;
// ============================================================================
// Init (Cold Path)
// ============================================================================
int tiny_tcache_env_init(void) {
const char* env_enabled = getenv("HAKMEM_TINY_TCACHE");
const char* env_cap = getenv("HAKMEM_TINY_TCACHE_CAP");
int enabled = 0; // default: OFF (opt-in)
uint16_t cap = 64; // default: 64 (glibc tcache-like)
// Parse HAKMEM_TINY_TCACHE
if (env_enabled && (env_enabled[0] == '1' || strcmp(env_enabled, "true") == 0 || strcmp(env_enabled, "TRUE") == 0)) {
enabled = 1;
}
// Parse HAKMEM_TINY_TCACHE_CAP
if (env_cap && *env_cap) {
int parsed = atoi(env_cap);
if (parsed > 0 && parsed <= 65535) {
cap = (uint16_t)parsed;
}
}
// Cache results
atomic_store_explicit(&g_tiny_tcache_enabled, enabled, memory_order_relaxed);
atomic_store_explicit(&g_tiny_tcache_cap, cap, memory_order_relaxed);
// Log once (stderr for immediate visibility)
if (enabled) {
char msg[128];
int n = snprintf(msg, sizeof(msg), "[TINY_TCACHE] enabled (cap=%u)\n", (unsigned)cap);
if (n > 0 && n < (int)sizeof(msg)) {
ssize_t w = write(2, msg, (size_t)n);
(void)w;
}
}
return enabled;
}
// ============================================================================
// Refresh (Cold Path, called from bench_profile)
// ============================================================================
void tiny_tcache_env_refresh_from_env(void) {
// Reset to uninitialized state (-1 / 0)
// Next call to tiny_tcache_enabled() / tiny_tcache_cap() will re-read ENV
atomic_store_explicit(&g_tiny_tcache_enabled, -1, memory_order_relaxed);
atomic_store_explicit(&g_tiny_tcache_cap, 0, memory_order_relaxed);
}