make arena growable, add module declarations

This commit is contained in:
Matthew 2025-08-23 04:52:06 +10:00
parent 79d128c872
commit 67c7c636d0
8 changed files with 109 additions and 42 deletions

2
.ignore Normal file
View File

@ -0,0 +1,2 @@
.git
external

View File

@ -1,6 +1,8 @@
module dlib.aliases;
import core.memory;
import std.stdint;
import math;
import dlib.math;
debug
{

90
alloc.d
View File

@ -1,17 +1,27 @@
import aliases;
import math;
module dlib.alloc;
import dlib.aliases;
import dlib.math;
import dlib.platform;
import dlib.util;
import std.stdio;
import core.stdc.string : memset;
import core.memory;
import platform;
const DEFAULT_ALIGNMENT = (void *).sizeof * 2;
struct Arena
struct ArenaPool
{
u8* mem;
u64 length;
u64 pos;
}
struct Arena
{
SLList!(ArenaPool) pools;
u64 pool_length;
u64 length;
};
T*
@ -31,13 +41,13 @@ MAllocArray(T)(u64 count)
void
MFree(T)(T* ptr)
{
MemFree(cast(void*)ptr, T.sizeof);
MemFree(ptr, T.sizeof);
}
void
MFreeArray(T)(T[] slice)
{
MemFree(cast(void*)slice.ptr, cast(u64)slice.length * T.sizeof);
MemFree(slice.ptr, cast(u64)slice.length * T.sizeof);
}
T*
@ -60,16 +70,30 @@ Arena
CreateArena(u64 size)
{
Arena arena = {
mem: cast(u8 *)pureMalloc(size),
length: size,
pos: 0,
pool_length: size - Node!(ArenaPool).sizeof,
};
assert(arena.mem != null, "Unable to allocate memory for arena");
AddArenaPool(&arena);
return arena;
};
void
AddArenaPool(Arena* arena)
{
u8* mem = cast(u8*)MemAlloc(size);
Node!(ArenaPool)* node = cast(Node!(ArenaPool)*)mem;
node.value.mem = (cast(u8*)mem) + Node!(ArenaPool).sizeof;
node.value.pos = 0;
assert(node.value.mem != null, "Unable to allocate memory for arena");
PushFront(&arena.pools, node, null);
}
T[]
AllocArray(T)(Arena* arena, u64 count)
{
@ -89,36 +113,58 @@ Alloc(T)(Arena* arena)
void*
AllocAlign(Arena* arena, u64 size, u64 alignment)
{
assert(size <= arena.pool_length, "Unable to allocate memory within arena size");
void* ptr = null;
uintptr mem_pos = cast(uintptr)arena.mem;
uintptr current = mem_pos + arena.pos;
uintptr offset = AlignPow2(current, alignment) - mem_pos;
uintptr mem_pos, current, offset;
Node!(ArenaPool) node = arena.pools.front;
while (true)
{
if (node == null)
{
AddArenaPool(arena);
node = arena.pools.front;
}
if (offset+size <= arena.length)
mem_pos = cast(uintptr)node.value.mem;
current = mem_pos + node.value.pos;
offset = AlignPow2(current, alignment) - mem_pos;
if (offset+size <= arena.pool_length)
{
ptr = &arena.mem[offset];
arena.pos = offset+size;
break;
}
else
{
writefln("AllocAlign failure: out of memory, size requested: %llu", size);
assert(0);
node = node.next;
}
ptr = &node.value.mem[offset];
node.value.pos = offset+size;
return ptr;
};
void
Reset(Arena* arena)
{
arena.pos = 0;
Node!(ArenaPool) node = arena.pools.front;
while (node != null)
{
node.value.pos = 0;
node = node.next;
}
}
void
Free(Arena* arena)
{
pureFree(arena.mem);
Node!(ArenaPool) node = arena.pools.front;
while (node != null)
{
MemFree(node.value.mem, arena.length);
node = node.next;
}
}
void

View File

@ -1,9 +1,12 @@
import aliases;
module dlib.assets;
import dlib.aliases;
import dlib.util;
import dlib.alloc;
import std.file;
import std.stdio;
import util;
import std.exception;
import alloc;
File Asset_File;

10
fonts.d
View File

@ -1,7 +1,9 @@
import aliases;
import includes;
import util;
import alloc;
module dlib.fonts;
import dlib.aliases;
import dlib.includes;
import dlib.util;
import dlib.alloc;
enum AtlasType
{

11
math.d
View File

@ -1,9 +1,14 @@
import aliases;
module dlib.math;
import dlib.aliases;
import dlib.util;
import includes;
import util;
import core.stdc.math : tanf, cosf, sinf, sqrtf, fabsf;
import std.math;
import std.math.algebraic;
import core.stdc.math : tanf, cosf, sinf, sqrtf, fabsf;
import std.traits;
import std.meta;
import std.format;

View File

@ -1,4 +1,6 @@
import aliases;
module dlib.platform;
import dlib.aliases;
import includes;
import std.stdio;
import core.memory;

13
util.d
View File

@ -1,13 +1,18 @@
import aliases;
module dlib.util;
import dlib.aliases;
import dlib.alloc;
import xxhash3;
import includes;
import std.stdio;
import core.stdc.string : memset;
import alloc;
import core.simd;
import std.conv;
import std.string;
import core.stdc.string : memset;
import core.simd;
struct DynSlice(T)
{
T[][] slices;