diff --git a/alloc.d b/alloc.d index ab49ec9..d48a589 100644 --- a/alloc.d +++ b/alloc.d @@ -75,13 +75,27 @@ Alloc(T)() } T[] -AllocArray(T)(u64 count) +Alloc(T)(u64 count) { void* mem = pureMalloc(T.sizeof * count); memset(mem, 0, T.sizeof * count); return (cast(T*)mem)[0 .. count]; } +T[] +AllocCopy(T)(T[] target) +{ + T[] arr = Alloc!(T)(target.length); + arr[] = target[]; + return arr; +} + +T[] +AllocArray(T)(u64 count) +{ + return Alloc!(T)(count); +} + T[] ReallocArray(T)(T[] arr, u64 count) { @@ -143,10 +157,22 @@ End(TempArena* t) } } +T[] +AllocCopy(T)(TempArena* t, T[] target) +{ + return AllocCopy!(T)(t.arena, target); +} + +T[] +Alloc(T)(TempArena* t, u64 count) +{ + return Alloc!(T)(t.arena, count); +} + T[] AllocArray(T)(TempArena* t, u64 count) { - return AllocArray!(T)(t.arena, count); + return Alloc!(T)(t.arena, count); } T* @@ -171,13 +197,27 @@ AddArenaPool(Arena* arena) } T[] -AllocArray(T)(Arena* arena, u64 count) +Alloc(T)(Arena* arena, u64 count) { void* mem = AllocAlign(arena, T.sizeof * count, DEFAULT_ALIGNMENT); memset(mem, 0, T.sizeof * count); return (cast(T*)mem)[0 .. count]; } +T[] +AllocArray(T)(Arena* arena, u64 count) +{ + return Alloc!(T)(arena, count); +} + +T[] +AllocCopy(T)(Arena* arena, T[] target) +{ + T[] arr = Alloc!(T)(arena, target.length); + arr[] = target[]; + return arr; +} + T* Alloc(T)(Arena* arena) { @@ -278,3 +318,20 @@ ScratchAlloc(T)(u64 count) { return AllocArray!(T)(&g_scratch.arena, count); } + +T[] +ScratchAllocCopy(T)(T[] target) +{ + T[] arr = ScratchAlloc!(T)(target.length); + arr[] = target[]; + return arr; +} + +unittest +{ + u64[5] arr = [1, 2, 3, 4, 5]; + + u64[] copy = AllocCopy!(u64)(arr); + + assert(arr == copy); +}