From 67c7c636d014a211136e0344d6c2e5bc5de0119d Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 23 Aug 2025 04:52:06 +1000 Subject: [PATCH] make arena growable, add module declarations --- .ignore | 2 ++ aliases.d | 4 ++- alloc.d | 98 +++++++++++++++++++++++++++++++++++++++--------------- assets.d | 9 +++-- fonts.d | 10 +++--- math.d | 11 ++++-- platform.d | 4 ++- util.d | 13 +++++--- 8 files changed, 109 insertions(+), 42 deletions(-) create mode 100644 .ignore diff --git a/.ignore b/.ignore new file mode 100644 index 0000000..08aa99c --- /dev/null +++ b/.ignore @@ -0,0 +1,2 @@ +.git +external diff --git a/aliases.d b/aliases.d index e183734..93f949b 100644 --- a/aliases.d +++ b/aliases.d @@ -1,6 +1,8 @@ +module dlib.aliases; + import core.memory; import std.stdint; -import math; +import dlib.math; debug { diff --git a/alloc.d b/alloc.d index 5dd13c5..c821293 100644 --- a/alloc.d +++ b/alloc.d @@ -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* @@ -59,17 +69,31 @@ AllocArray(T)(u64 count) Arena CreateArena(u64 size) { - Arena arena = { - mem: cast(u8 *)pureMalloc(size), + Arena arena = { 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,22 +113,34 @@ 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; + } + + 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) + { + break; + } + + node = node.next; + } - if (offset+size <= arena.length) - { - ptr = &arena.mem[offset]; - arena.pos = offset+size; - } - else - { - writefln("AllocAlign failure: out of memory, size requested: %llu", size); - assert(0); - } + ptr = &node.value.mem[offset]; + node.value.pos = offset+size; return ptr; }; @@ -112,13 +148,23 @@ AllocAlign(Arena* arena, u64 size, u64 alignment) 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 diff --git a/assets.d b/assets.d index bf68e9d..300e3d1 100644 --- a/assets.d +++ b/assets.d @@ -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; diff --git a/fonts.d b/fonts.d index df1575a..6cf0265 100644 --- a/fonts.d +++ b/fonts.d @@ -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 { diff --git a/math.d b/math.d index 7f2c01f..f626cf2 100644 --- a/math.d +++ b/math.d @@ -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; diff --git a/platform.d b/platform.d index a44c8fa..8494307 100644 --- a/platform.d +++ b/platform.d @@ -1,4 +1,6 @@ -import aliases; +module dlib.platform; + +import dlib.aliases; import includes; import std.stdio; import core.memory; diff --git a/util.d b/util.d index dfaeffd..c5696a4 100644 --- a/util.d +++ b/util.d @@ -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;