69 lines
2.0 KiB
C
69 lines
2.0 KiB
C
|
|
// hakmem_tiny_simple.h
|
||
|
|
// Phase 6-1: Ultra-Simple Tiny Allocator (Learning-Based)
|
||
|
|
//
|
||
|
|
// Design Philosophy: "Simple Front + Smart Back"
|
||
|
|
// - Front: Ultra-simple fast path (3-4 instructions, tcache-style)
|
||
|
|
// - Back: Learning layer (adaptive capacity, hotness tracking)
|
||
|
|
//
|
||
|
|
// Inspired by Mid-Large HAKX success (+171%)
|
||
|
|
|
||
|
|
#ifndef HAKMEM_TINY_SIMPLE_H
|
||
|
|
#define HAKMEM_TINY_SIMPLE_H
|
||
|
|
|
||
|
|
#include <stddef.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <stdbool.h>
|
||
|
|
|
||
|
|
// ============================================================================
|
||
|
|
// Phase 1: Ultra-Simple Fast Path
|
||
|
|
// ============================================================================
|
||
|
|
|
||
|
|
// Size classes (same as existing Tiny)
|
||
|
|
#define TINY_NUM_CLASSES 8
|
||
|
|
|
||
|
|
// TLS Free List (per size class)
|
||
|
|
// This is the ONLY data structure in the fast path!
|
||
|
|
extern __thread void* g_tls_tiny_cache[TINY_NUM_CLASSES];
|
||
|
|
|
||
|
|
// Inline size-to-class conversion (must be FAST!)
|
||
|
|
static inline int hak_tiny_simple_size_to_class(size_t size) {
|
||
|
|
if (size <= 8) return 0;
|
||
|
|
if (size <= 16) return 1;
|
||
|
|
if (size <= 32) return 2;
|
||
|
|
if (size <= 64) return 3;
|
||
|
|
if (size <= 128) return 4;
|
||
|
|
if (size <= 256) return 5;
|
||
|
|
if (size <= 512) return 6;
|
||
|
|
if (size <= 1024) return 7;
|
||
|
|
return -1; // >1KB
|
||
|
|
}
|
||
|
|
|
||
|
|
// ============================================================================
|
||
|
|
// Public API
|
||
|
|
// ============================================================================
|
||
|
|
|
||
|
|
// Initialize simple tiny allocator
|
||
|
|
void hak_tiny_simple_init(void);
|
||
|
|
|
||
|
|
// Ultra-fast allocation (3-4 instructions!)
|
||
|
|
void* hak_tiny_simple_alloc(size_t size);
|
||
|
|
|
||
|
|
// Fast free
|
||
|
|
void hak_tiny_simple_free(void* ptr, size_t size);
|
||
|
|
|
||
|
|
// Slow path (refill from SuperSlab)
|
||
|
|
void* hak_tiny_simple_alloc_slow(size_t size, int class_idx);
|
||
|
|
|
||
|
|
// Stats (for debugging/profiling)
|
||
|
|
typedef struct {
|
||
|
|
uint64_t alloc_count;
|
||
|
|
uint64_t free_count;
|
||
|
|
uint64_t miss_count;
|
||
|
|
uint64_t hit_count;
|
||
|
|
} TinySimpleStats;
|
||
|
|
|
||
|
|
void hak_tiny_simple_get_stats(int class_idx, TinySimpleStats* stats);
|
||
|
|
void hak_tiny_simple_print_stats(void);
|
||
|
|
|
||
|
|
#endif // HAKMEM_TINY_SIMPLE_H
|