add thread local scratch allocator
This commit is contained in:
parent
f30edc3bbe
commit
f31cdfbedf
35
alloc.d
35
alloc.d
@ -9,8 +9,16 @@ import std.stdio;
|
||||
import core.stdc.string : memset;
|
||||
import core.memory;
|
||||
|
||||
static Scratch g_scratch;
|
||||
|
||||
const DEFAULT_ALIGNMENT = (void *).sizeof * 2;
|
||||
|
||||
struct Scratch
|
||||
{
|
||||
Arena arena;
|
||||
bool init;
|
||||
}
|
||||
|
||||
struct ArenaPool
|
||||
{
|
||||
u8* mem;
|
||||
@ -173,7 +181,32 @@ FreeArray(T)(T[] arr)
|
||||
pureFree(arr.ptr);
|
||||
}
|
||||
|
||||
void Free(T)(T* ptr)
|
||||
void
|
||||
Free(T)(T* 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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user