Files
hakmem/core/hakmem_bigcache.h

60 lines
2.2 KiB
C
Raw Normal View History

// hakmem_bigcache.h - Big-Block Cache (Box理論)
// Purpose: Per-site ring cache for large allocations (>= 1MB)
//
// License: MIT
// Date: 2025-10-21
#pragma once
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
// ============================================================================
// BigCache Box - サイト別大規模ブロックキャッシュ
// ============================================================================
// Configuration (環境変数で制御可能)
#define BIGCACHE_MAX_SITES 256 // Max cached sites (Phase 6.11: 64 → 256, 4x increase)
#define BIGCACHE_MIN_SIZE 524288 // 512KB minimum for caching (Phase 6.11: reduced from 1MB)
// Phase 6.11: Expanded size classes (4 → 8 classes, finer granularity to reduce internal fragmentation)
#define BIGCACHE_NUM_CLASSES 8 // Number of size classes (Phase 6.11: increased from 4)
#define BIGCACHE_CLASS_512KB 524288 // 512KB size class (NEW)
#define BIGCACHE_CLASS_1MB 1048576 // 1MB size class
#define BIGCACHE_CLASS_2MB 2097152 // 2MB size class
#define BIGCACHE_CLASS_3MB 3145728 // 3MB size class (NEW: reduces fragmentation)
#define BIGCACHE_CLASS_4MB 4194304 // 4MB size class
#define BIGCACHE_CLASS_6MB 6291456 // 6MB size class (NEW)
#define BIGCACHE_CLASS_8MB 8388608 // 8MB size class
#define BIGCACHE_CLASS_16MB 16777216 // 16MB size class (NEW)
// BigCache API (Box Interface)
// Try to get cached block for this site
// Returns: 1 if hit (out_ptr set), 0 if miss
int hak_bigcache_try_get(size_t size, uintptr_t site, void** out_ptr);
// Try to put freed block into cache
// Returns: 1 if cached, 0 if rejected (full/not cacheable)
int hak_bigcache_put(void* ptr, size_t class_bytes, uintptr_t site);
// Initialize BigCache (called from hak_init)
void hak_bigcache_init(void);
// Cleanup BigCache (called from hak_shutdown)
void hak_bigcache_shutdown(void);
// Stats (for debugging)
void hak_bigcache_print_stats(void);
// Set free callback (for proper cleanup on eviction)
typedef void (*hak_bigcache_free_fn_t)(void* ptr, size_t size);
void hak_bigcache_set_free_callback(hak_bigcache_free_fn_t fn);
#ifdef __cplusplus
}
#endif