make arena growable, add module declarations
This commit is contained in:
parent
79d128c872
commit
67c7c636d0
@ -1,6 +1,8 @@
|
||||
module dlib.aliases;
|
||||
|
||||
import core.memory;
|
||||
import std.stdint;
|
||||
import math;
|
||||
import dlib.math;
|
||||
|
||||
debug
|
||||
{
|
||||
|
||||
98
alloc.d
98
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
|
||||
|
||||
9
assets.d
9
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;
|
||||
|
||||
|
||||
10
fonts.d
10
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
|
||||
{
|
||||
|
||||
11
math.d
11
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;
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
import aliases;
|
||||
module dlib.platform;
|
||||
|
||||
import dlib.aliases;
|
||||
import includes;
|
||||
import std.stdio;
|
||||
import core.memory;
|
||||
|
||||
13
util.d
13
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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user