2025-11-05 12:31:14 +09:00
|
|
|
// hakmem_tiny_bump.inc.h
|
|
|
|
|
// Layer 1: TLS Bump Allocator (Ultra-fast path)
|
|
|
|
|
//
|
|
|
|
|
// Purpose: 2-3 instruction allocation for hot classes (8B, 16B, 32B)
|
|
|
|
|
// Design: bcur += size; if (bcur <= bend) return old;
|
|
|
|
|
//
|
|
|
|
|
// Part of 3-layer architecture simplification (2025-11-01)
|
|
|
|
|
// Based on ChatGPT Pro UltraThink recommendations
|
|
|
|
|
|
|
|
|
|
#ifndef HAKMEM_TINY_BUMP_INC_H
|
|
|
|
|
#define HAKMEM_TINY_BUMP_INC_H
|
|
|
|
|
|
|
|
|
|
// likely/unlikely macros
|
|
|
|
|
#ifndef likely
|
|
|
|
|
#define likely(x) __builtin_expect(!!(x), 1)
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef unlikely
|
|
|
|
|
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// Data Structure
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
void* bcur; // Current bump pointer
|
|
|
|
|
void* bend; // Bump end (exclusive)
|
|
|
|
|
} TinyBump;
|
|
|
|
|
|
|
|
|
|
// Per-class bump allocator (hot classes only: class 0, 1, 2 = 8B, 16B, 32B)
|
|
|
|
|
static __thread TinyBump g_tiny_bump[3] = {{NULL, NULL}, {NULL, NULL}, {NULL, NULL}};
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// Layer 1: Ultra-fast bump allocation (2-3 instructions/op)
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
|
|
// Class 0: 8B
|
|
|
|
|
__attribute__((always_inline))
|
|
|
|
|
static inline void* tiny_bump_alloc_8B(void) {
|
|
|
|
|
void* old = g_tiny_bump[0].bcur;
|
|
|
|
|
void* new_cur = (char*)old + 8;
|
|
|
|
|
if (likely(new_cur <= g_tiny_bump[0].bend)) {
|
|
|
|
|
g_tiny_bump[0].bcur = new_cur;
|
|
|
|
|
return old;
|
|
|
|
|
}
|
|
|
|
|
return NULL; // Exhausted, fallback to Layer 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Class 1: 16B
|
|
|
|
|
__attribute__((always_inline))
|
|
|
|
|
static inline void* tiny_bump_alloc_16B(void) {
|
|
|
|
|
void* old = g_tiny_bump[1].bcur;
|
|
|
|
|
void* new_cur = (char*)old + 16;
|
|
|
|
|
if (likely(new_cur <= g_tiny_bump[1].bend)) {
|
|
|
|
|
g_tiny_bump[1].bcur = new_cur;
|
|
|
|
|
return old;
|
|
|
|
|
}
|
|
|
|
|
return NULL; // Exhausted, fallback to Layer 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Class 2: 32B
|
|
|
|
|
__attribute__((always_inline))
|
|
|
|
|
static inline void* tiny_bump_alloc_32B(void) {
|
|
|
|
|
void* old = g_tiny_bump[2].bcur;
|
|
|
|
|
void* new_cur = (char*)old + 32;
|
|
|
|
|
if (likely(new_cur <= g_tiny_bump[2].bend)) {
|
|
|
|
|
g_tiny_bump[2].bcur = new_cur;
|
|
|
|
|
return old;
|
|
|
|
|
}
|
|
|
|
|
return NULL; // Exhausted, fallback to Layer 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generic bump alloc (for use in slow path)
|
|
|
|
|
__attribute__((always_inline))
|
|
|
|
|
static inline void* tiny_bump_alloc(int class_idx) {
|
|
|
|
|
if (class_idx == 0) return tiny_bump_alloc_8B();
|
|
|
|
|
if (class_idx == 1) return tiny_bump_alloc_16B();
|
|
|
|
|
if (class_idx == 2) return tiny_bump_alloc_32B();
|
|
|
|
|
return NULL; // Not a hot class
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// Bump refill (called from Layer 3: slow path)
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
2025-11-07 01:27:04 +09:00
|
|
|
__attribute__((noinline, unused))
|
2025-11-05 12:31:14 +09:00
|
|
|
static void tiny_bump_refill(int class_idx, void* base, size_t size) {
|
|
|
|
|
if (class_idx < 0 || class_idx > 2) return; // Only hot classes
|
|
|
|
|
g_tiny_bump[class_idx].bcur = base;
|
|
|
|
|
g_tiny_bump[class_idx].bend = (char*)base + size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset bump allocator (e.g., thread shutdown)
|
|
|
|
|
static void tiny_bump_reset(int class_idx) {
|
|
|
|
|
if (class_idx < 0 || class_idx > 2) return;
|
|
|
|
|
g_tiny_bump[class_idx].bcur = NULL;
|
|
|
|
|
g_tiny_bump[class_idx].bend = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset all bump allocators
|
2025-11-07 01:27:04 +09:00
|
|
|
static __attribute__((unused)) void tiny_bump_reset_all(void) {
|
2025-11-05 12:31:14 +09:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
|
tiny_bump_reset(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // HAKMEM_TINY_BUMP_INC_H
|