36 lines
1.2 KiB
C
36 lines
1.2 KiB
C
|
|
#pragma once
|
||
|
|
#include <stddef.h>
|
||
|
|
#include <string.h>
|
||
|
|
#include "hakx/hakx_api.h"
|
||
|
|
#include "hakx/hakx_api.h" // ensure API visible for fallback
|
||
|
|
#include "hakmem_tiny.h" // tiny alloc/free + class tables / usable_size
|
||
|
|
#include "hakmem_super_registry.h" // superslab owner lookup
|
||
|
|
|
||
|
|
// Bench-only inlined front path to shave call overhead in tiny hot loops.
|
||
|
|
// Falls back to HAKMEM backend for non-tiny or miss.
|
||
|
|
|
||
|
|
static inline void* hakx_malloc_fast(size_t size) {
|
||
|
|
if (size <= 128u) {
|
||
|
|
void* p = hak_tiny_alloc(size);
|
||
|
|
if (p) return p;
|
||
|
|
}
|
||
|
|
return hakx_malloc(size); // backend (may be HAKMEM/mi/sys)
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline void hakx_free_fast(void* ptr) {
|
||
|
|
if (!ptr) return;
|
||
|
|
if (hak_tiny_owner_slab(ptr) || hak_super_lookup(ptr)) { hak_tiny_free(ptr); return; }
|
||
|
|
hakx_free(ptr);
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline void* hakx_realloc_fast(void* ptr, size_t new_size) {
|
||
|
|
if (!ptr) return hakx_malloc_fast(new_size);
|
||
|
|
if (new_size == 0) { hakx_free_fast(ptr); return NULL; }
|
||
|
|
// No size knowledge here; do a conservative move.
|
||
|
|
void* np = hakx_malloc_fast(new_size);
|
||
|
|
if (!np) return NULL;
|
||
|
|
memcpy(np, ptr, new_size);
|
||
|
|
hakx_free_fast(ptr);
|
||
|
|
return np;
|
||
|
|
}
|