add thread local scratch allocator

This commit is contained in:
Matthew 2025-09-12 06:39:33 +10:00
parent f30edc3bbe
commit f31cdfbedf

35
alloc.d
View File

@ -9,8 +9,16 @@ import std.stdio;
import core.stdc.string : memset; import core.stdc.string : memset;
import core.memory; import core.memory;
static Scratch g_scratch;
const DEFAULT_ALIGNMENT = (void *).sizeof * 2; const DEFAULT_ALIGNMENT = (void *).sizeof * 2;
struct Scratch
{
Arena arena;
bool init;
}
struct ArenaPool struct ArenaPool
{ {
u8* mem; u8* mem;
@ -173,7 +181,32 @@ FreeArray(T)(T[] arr)
pureFree(arr.ptr); pureFree(arr.ptr);
} }
void Free(T)(T* ptr) void
Free(T)(T* ptr)
{ {
pureFree(ptr); pureFree(ptr);
} }
void
ResetScratch(u64 size)
{
if (!g_scratch.init)
{
g_scratch.arena = CreateArena(size);
g_scratch.init = true;
}
Reset(&g_scratch.arena);
}
T*
ScratchAlloc(T)()
{
return Alloc!(T)(&g_scratch.arena);
}
T[]
ScratchAlloc(T)(u64 count)
{
return AllocArray!(T)(&g_scratch.arena, count);
}