Files
hakmem/core/tiny_region_id.h
Moe Charm (CI) 48fadea590 Phase 7-1.1: Fix 1024B crash (header validation + malloc fallback)
Fixed critical bugs preventing Phase 7 from working with 1024B allocations.

## Bug Fixes (by Task Agent Ultrathink)

1. **Header Validation Missing in Release Builds**
   - `core/tiny_region_id.h:73-97` - Removed `#if !HAKMEM_BUILD_RELEASE`
   - Always validate magic byte and class_idx (prevents SEGV on Mid/Large)

2. **1024B Malloc Fallback Missing**
   - `core/box/hak_alloc_api.inc.h:35-49` - Direct fallback to malloc
   - Phase 7 rejects 1024B (needs header) → skip ACE → use malloc

## Test Results

| Test | Result |
|------|--------|
| 128B, 512B, 1023B (Tiny) | +39%~+436%  |
| 1024B only (100 allocs) | 100% success  |
| Mixed 128B+1024B (200) | 100% success  |
| bench_random_mixed 1024B | Still crashes  |

## Known Issue

`bench_random_mixed` with 1024B still crashes (intermittent SEGV).
Simple tests pass, suggesting issue is with complex allocation patterns.
Investigation pending.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Task Agent Ultrathink
2025-11-08 03:35:07 +09:00

182 lines
5.1 KiB
C

// tiny_region_id.h - Region-ID Direct Lookup API (Phase 7)
// Purpose: O(1) class_idx lookup from pointer (eliminates SuperSlab lookup)
// Design: Smart Headers - 1-byte class_idx embedded before each block
// Performance: 2-3 cycles (vs 100+ cycles for SuperSlab lookup)
//
// Expected Impact: 1.2M → 40-60M ops/s (30-50x improvement)
#ifndef TINY_REGION_ID_H
#define TINY_REGION_ID_H
#include <stdint.h>
#include <stddef.h>
#include "hakmem_build_flags.h"
// Feature flag: Enable header-based class_idx lookup
#ifndef HAKMEM_TINY_HEADER_CLASSIDX
#define HAKMEM_TINY_HEADER_CLASSIDX 0
#endif
#if HAKMEM_TINY_HEADER_CLASSIDX
// ========== Header Layout ==========
//
// Memory layout:
// [Header: 1 byte] [User block: N bytes]
// ^ ^
// ptr-1 ptr (returned to user)
//
// Header format (1 byte):
// - Bits 0-3: class_idx (0-15, only 0-7 used for Tiny)
// - Bits 4-7: magic (0xA for validation in debug mode)
//
// Example:
// class_idx = 3 → header = 0xA3 (debug) or 0x03 (release)
#define HEADER_MAGIC 0xA0
#define HEADER_CLASS_MASK 0x0F
// ========== Write Header (Allocation) ==========
// Write class_idx to header (called after allocation)
// Input: base (block start from SuperSlab)
// Returns: user pointer (base + 1, skipping header)
static inline void* tiny_region_id_write_header(void* base, int class_idx) {
if (!base) return base;
// Write header at block start
uint8_t* header_ptr = (uint8_t*)base;
#if !HAKMEM_BUILD_RELEASE
// Debug: Write magic + class_idx
*header_ptr = HEADER_MAGIC | (class_idx & HEADER_CLASS_MASK);
#else
// Release: Write class_idx only (no magic overhead)
*header_ptr = (uint8_t)class_idx;
#endif
// Return user pointer (skip header)
return header_ptr + 1;
}
// ========== Read Header (Free) ==========
// Read class_idx from header (called during free)
// Returns: class_idx (0-7), or -1 if invalid
static inline int tiny_region_id_read_header(void* ptr) {
if (!ptr) return -1;
uint8_t* header_ptr = (uint8_t*)ptr - 1;
uint8_t header = *header_ptr;
// CRITICAL: Always validate magic byte (even in release builds)
// Reason: Mid/Large allocations don't have headers, must detect and reject them
uint8_t magic = header & 0xF0;
if (magic != HEADER_MAGIC) {
// Invalid header - likely non-header allocation (Mid/Large)
static int invalid_count = 0;
if (invalid_count < 5) {
fprintf(stderr, "[HEADER_INVALID] ptr=%p, header=%02x, magic=%02x (expected %02x)\n",
ptr, header, magic, HEADER_MAGIC);
invalid_count++;
}
return -1;
}
int class_idx = (int)(header & HEADER_CLASS_MASK);
// CRITICAL: Always validate class_idx range (even in release builds)
// Reason: Corrupted headers could cause out-of-bounds array access
#ifndef TINY_NUM_CLASSES
#define TINY_NUM_CLASSES 8
#endif
if (class_idx < 0 || class_idx >= TINY_NUM_CLASSES) {
// Corrupted header
return -1;
}
return class_idx;
}
// ========== Header Validation ==========
// Check if pointer has valid header (debug mode)
static inline int tiny_region_id_has_header(void* ptr) {
#if !HAKMEM_BUILD_RELEASE
if (!ptr) return 0;
uint8_t* header_ptr = (uint8_t*)ptr - 1;
uint8_t header = *header_ptr;
uint8_t magic = header & 0xF0;
return (magic == HEADER_MAGIC);
#else
// Release: Assume all allocations have headers
(void)ptr;
return 1;
#endif
}
// ========== Allocation Size Adjustment ==========
// Calculate allocation size including header (1 byte)
static inline size_t tiny_region_id_alloc_size(size_t user_size) {
return user_size + 1; // Add 1 byte for header
}
// Calculate user size from allocation size
static inline size_t tiny_region_id_user_size(size_t alloc_size) {
return alloc_size - 1;
}
// ========== Performance Notes ==========
//
// Header Read Performance:
// - Best case: 2 cycles (L1 hit, no validation)
// - Average: 3 cycles (with class_idx extraction)
// - Worst case: 5 cycles (debug validation)
// - vs SuperSlab lookup: 100+ cycles (50x faster!)
//
// Memory Overhead:
// - Per block: 1 byte
// - 8-byte blocks: 12.5% overhead
// - 128-byte blocks: 0.8% overhead
// - Average (typical workload): ~1.5%
// - Slab[0]: 0% (reuses 960B wasted padding)
//
// Cache Impact:
// - Excellent: Header is inline with user data
// - Prefetch: Header loaded with first user data access
// - No additional cache lines required
#else // !HAKMEM_TINY_HEADER_CLASSIDX
// Disabled: No-op implementations
static inline void* tiny_region_id_write_header(void* ptr, int class_idx) {
(void)class_idx;
return ptr;
}
static inline int tiny_region_id_read_header(void* ptr) {
(void)ptr;
return -1; // Not supported
}
static inline int tiny_region_id_has_header(void* ptr) {
(void)ptr;
return 0; // No headers
}
static inline size_t tiny_region_id_alloc_size(size_t user_size) {
return user_size; // No header
}
static inline size_t tiny_region_id_user_size(size_t alloc_size) {
return alloc_size;
}
#endif // HAKMEM_TINY_HEADER_CLASSIDX
#endif // TINY_REGION_ID_H