Rename Dqn_MemArena to Dqn_ArenaAllocator

This commit is contained in:
doyle 2020-04-30 01:01:10 +10:00
parent 5173478766
commit 44d8f970b3
5 changed files with 164 additions and 164 deletions

View File

@ -1,7 +1,7 @@
@echo OFF @echo OFF
if not exist ..\Bin mkdir ..\Bin if not exist ..\Build mkdir ..\Build
pushd ..\Bin pushd ..\Build
REM MT Static CRT REM MT Static CRT
REM EHa- Disable exception handling REM EHa- Disable exception handling

View File

@ -637,9 +637,9 @@ enum struct Dqn_Allocator_Type
Custom, Custom,
}; };
#define DQN_ALLOCATOR_ALLOCATE_PROC(name) void *name(Dqn_isize size, Dqn_u8 alignment) #define DQN_ALLOCATOR_ALLOCATE_PROC(name) void *name(Dqn_isize size, Dqn_u8 alignment, void *user_context)
#define DQN_ALLOCATOR_REALLOC_PROC(name) void *name(void *old_ptr, Dqn_isize old_size, Dqn_isize new_size) #define DQN_ALLOCATOR_REALLOC_PROC(name) void *name(void *old_ptr, Dqn_isize old_size, Dqn_isize new_size, void *user_context)
#define DQN_ALLOCATOR_FREE_PROC(name) void name(void *ptr) #define DQN_ALLOCATOR_FREE_PROC(name) void name(void *ptr, void *user_context)
typedef DQN_ALLOCATOR_ALLOCATE_PROC(Dqn_Allocator_AllocateProc); typedef DQN_ALLOCATOR_ALLOCATE_PROC(Dqn_Allocator_AllocateProc);
typedef DQN_ALLOCATOR_REALLOC_PROC(Dqn_Allocator_ReallocProc); typedef DQN_ALLOCATOR_REALLOC_PROC(Dqn_Allocator_ReallocProc);
typedef DQN_ALLOCATOR_FREE_PROC(Dqn_Allocator_FreeProc); typedef DQN_ALLOCATOR_FREE_PROC(Dqn_Allocator_FreeProc);
@ -648,8 +648,8 @@ struct Dqn_Allocator
Dqn_Allocator_Type type; Dqn_Allocator_Type type;
union union
{ {
void *user; void *user;
struct Dqn_MemArena *arena; struct Dqn_ArenaAllocator *arena;
} context; } context;
Dqn_isize bytes_allocated; Dqn_isize bytes_allocated;
@ -665,28 +665,28 @@ struct Dqn_Allocator
}; };
DQN_HEADER_COPY_END DQN_HEADER_COPY_END
DQN_HEADER_COPY_PROTOTYPE(Dqn_Allocator, inline Dqn_Allocator_Null()) DQN_HEADER_COPY_PROTOTYPE(Dqn_Allocator, inline Dqn_Allocator_InitWithNull())
{ {
Dqn_Allocator result = {}; Dqn_Allocator result = {};
result.type = Dqn_Allocator_Type::Null; result.type = Dqn_Allocator_Type::Null;
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE(Dqn_Allocator, inline Dqn_Allocator_Heap()) DQN_HEADER_COPY_PROTOTYPE(Dqn_Allocator, inline Dqn_Allocator_InitWithHeap())
{ {
Dqn_Allocator result = {}; Dqn_Allocator result = {};
result.type = Dqn_Allocator_Type::Heap; result.type = Dqn_Allocator_Type::Heap;
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE(Dqn_Allocator, inline Dqn_Allocator_XHeap()) DQN_HEADER_COPY_PROTOTYPE(Dqn_Allocator, inline Dqn_Allocator_InitWithXHeap())
{ {
Dqn_Allocator result = {}; Dqn_Allocator result = {};
result.type = Dqn_Allocator_Type::XHeap; result.type = Dqn_Allocator_Type::XHeap;
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE(Dqn_Allocator, inline Dqn_Allocator_Arena(Dqn_MemArena *arena)) DQN_HEADER_COPY_PROTOTYPE(Dqn_Allocator, inline Dqn_Allocator_InitWithArena(Dqn_ArenaAllocator *arena))
{ {
Dqn_Allocator result = {}; Dqn_Allocator result = {};
result.type = Dqn_Allocator_Type::Arena; result.type = Dqn_Allocator_Type::Arena;
@ -718,53 +718,53 @@ DQN_HEADER_COPY_END
// @ ------------------------------------------------------------------------------------------------- // @ -------------------------------------------------------------------------------------------------
// @ // @
// @ NOTE: Dqn_MemArena // @ NOTE: Dqn_ArenaAllocator
// @ // @
// @ ------------------------------------------------------------------------------------------------- // @ -------------------------------------------------------------------------------------------------
DQN_HEADER_COPY_BEGIN DQN_HEADER_COPY_BEGIN
struct Dqn_MemBlock struct Dqn_ArenaAllocatorBlock
{ {
void *memory; void *memory;
Dqn_isize size; Dqn_isize size;
Dqn_isize used; Dqn_isize used;
Dqn_MemBlock *prev; Dqn_ArenaAllocatorBlock *prev;
Dqn_MemBlock *next; Dqn_ArenaAllocatorBlock *next;
}; };
Dqn_usize const DQN_MEM_ARENA_DEFAULT_MIN_BLOCK_SIZE = DQN_KILOBYTES(4); Dqn_usize const DQN_MEM_ARENA_DEFAULT_MIN_BLOCK_SIZE = DQN_KILOBYTES(4);
struct Dqn_MemArena struct Dqn_ArenaAllocator
{ {
// NOTE: Configuration (fill once after "Zero Initialisation {}") // NOTE: Configuration (fill once after "Zero Initialisation {}")
Dqn_isize min_block_size; Dqn_isize min_block_size;
Dqn_Allocator allocator; Dqn_Allocator allocator;
// NOTE: Read Only // NOTE: Read Only
Dqn_MemBlock *curr_mem_block; Dqn_ArenaAllocatorBlock *curr_mem_block;
Dqn_MemBlock *top_mem_block; Dqn_ArenaAllocatorBlock *top_mem_block;
Dqn_isize highest_used_mark; Dqn_isize highest_used_mark;
int total_allocated_mem_blocks; int total_allocated_mem_blocks;
}; };
struct Dqn_MemArenaRegion struct Dqn_ArenaAllocatorRegion
{ {
Dqn_MemArena *arena; Dqn_ArenaAllocator *arena;
Dqn_MemBlock *curr_mem_block; Dqn_ArenaAllocatorBlock *curr_mem_block;
Dqn_isize curr_mem_block_used; Dqn_isize curr_mem_block_used;
Dqn_MemBlock *top_mem_block; Dqn_ArenaAllocatorBlock *top_mem_block;
}; };
struct Dqn_MemArenaScopedRegion struct Dqn_ArenaAllocatorScopedRegion
{ {
Dqn_MemArenaScopedRegion(Dqn_MemArena *arena); Dqn_ArenaAllocatorScopedRegion(Dqn_ArenaAllocator *arena);
~Dqn_MemArenaScopedRegion(); ~Dqn_ArenaAllocatorScopedRegion();
Dqn_MemArenaRegion region; Dqn_ArenaAllocatorRegion region;
}; };
void * Dqn_MemArena_Allocate(Dqn_MemArena *arena, Dqn_isize size, Dqn_u8 alignment); void * Dqn_ArenaAllocator_Allocate(Dqn_ArenaAllocator *arena, Dqn_isize size, Dqn_u8 alignment);
Dqn_b32 Dqn_MemArena_Reserve(Dqn_MemArena *arena, Dqn_isize size); Dqn_b32 Dqn_ArenaAllocator_Reserve(Dqn_ArenaAllocator *arena, Dqn_isize size);
DQN_HEADER_COPY_PROTOTYPE(template <typename T> T *, Dqn_MemArena_AllocateType(Dqn_MemArena *arena, Dqn_isize num)) DQN_HEADER_COPY_PROTOTYPE(template <typename T> T *, Dqn_ArenaAllocator_AllocateType(Dqn_ArenaAllocator *arena, Dqn_isize num))
{ {
auto *result = DQN_CAST(T *)Dqn_MemArena_Allocate(arena, sizeof(T) * num, alignof(T)); auto *result = DQN_CAST(T *)Dqn_ArenaAllocator_Allocate(arena, sizeof(T) * num, alignof(T));
return result; return result;
} }
@ -1220,7 +1220,7 @@ DQN_HEADER_COPY_END
DQN_HEADER_COPY_PROTOTYPE(template <typename T> Dqn_Array<T>, Dqn_Array_InitWithMemory(T *memory, Dqn_isize max, Dqn_isize len = 0)) DQN_HEADER_COPY_PROTOTYPE(template <typename T> Dqn_Array<T>, Dqn_Array_InitWithMemory(T *memory, Dqn_isize max, Dqn_isize len = 0))
{ {
Dqn_Array<T> result = {}; Dqn_Array<T> result = {};
result.allocator = Dqn_Allocator_Null(); result.allocator = Dqn_Allocator_InitWithNull();
result.data = memory; result.data = memory;
result.len = len; result.len = len;
result.max = max; result.max = max;
@ -1605,14 +1605,14 @@ DQN_HEADER_COPY_PROTOTYPE(void *, Dqn_Allocator_Allocate(Dqn_Allocator *allocato
case Dqn_Allocator_Type::Arena: case Dqn_Allocator_Type::Arena:
{ {
result = DQN_CAST(char *)Dqn_MemArena_Allocate(allocator->context.arena, size, alignment); result = DQN_CAST(char *)Dqn_ArenaAllocator_Allocate(allocator->context.arena, size, alignment);
} }
break; break;
case Dqn_Allocator_Type::Custom: case Dqn_Allocator_Type::Custom:
{ {
if (allocator->allocate) if (allocator->allocate)
result = DQN_CAST(char *)allocator->allocate(size, alignment); result = DQN_CAST(char *)allocator->allocate(size, alignment, allocator->context.user);
} }
break; break;
} }
@ -1657,8 +1657,8 @@ DQN_HEADER_COPY_PROTOTYPE(void *, Dqn_Allocator_Realloc(Dqn_Allocator *allocator
case Dqn_Allocator_Type::Arena: case Dqn_Allocator_Type::Arena:
{ {
Dqn_MemArena *arena = allocator->context.arena; Dqn_ArenaAllocator *arena = allocator->context.arena;
result = Dqn_MemArena_Allocate(arena, new_size, metadata.alignment); result = Dqn_ArenaAllocator_Allocate(arena, new_size, metadata.alignment);
if (result) DQN_MEMCOPY(result, old_ptr, DQN_CAST(size_t) old_size); if (result) DQN_MEMCOPY(result, old_ptr, DQN_CAST(size_t) old_size);
} }
break; break;
@ -1666,7 +1666,7 @@ DQN_HEADER_COPY_PROTOTYPE(void *, Dqn_Allocator_Realloc(Dqn_Allocator *allocator
case Dqn_Allocator_Type::Custom: case Dqn_Allocator_Type::Custom:
{ {
if (allocator->realloc) if (allocator->realloc)
result = allocator->realloc(old_ptr, old_size, new_size); result = allocator->realloc(old_ptr, old_size, new_size, allocator->context.user);
} }
break; break;
} }
@ -1696,7 +1696,7 @@ DQN_HEADER_COPY_PROTOTYPE(void, Dqn_Allocator_Free(Dqn_Allocator *allocator, voi
case Dqn_Allocator_Type::Custom: case Dqn_Allocator_Type::Custom:
{ {
allocator->free(ptr); if (allocator->free) allocator->free(ptr, allocator->context.user);
} }
break; break;
@ -1714,17 +1714,17 @@ DQN_HEADER_COPY_PROTOTYPE(void, Dqn_Allocator_Free(Dqn_Allocator *allocator, voi
// @ ------------------------------------------------------------------------------------------------- // @ -------------------------------------------------------------------------------------------------
// @ // @
// @ NOTE: Dqn_MemArena // @ NOTE: Dqn_ArenaAllocator
// @ // @
// @ ------------------------------------------------------------------------------------------------- // @ -------------------------------------------------------------------------------------------------
DQN_FILE_SCOPE Dqn_MemBlock *Dqn_MemArena__AllocateBlock(Dqn_MemArena *arena, Dqn_isize requested_size) DQN_FILE_SCOPE Dqn_ArenaAllocatorBlock *Dqn_ArenaAllocator__AllocateBlock(Dqn_ArenaAllocator *arena, Dqn_isize requested_size)
{ {
Dqn_isize min_block_size = arena->min_block_size; Dqn_isize min_block_size = arena->min_block_size;
if (min_block_size == 0) min_block_size = DQN_MEM_ARENA_DEFAULT_MIN_BLOCK_SIZE; if (min_block_size == 0) min_block_size = DQN_MEM_ARENA_DEFAULT_MIN_BLOCK_SIZE;
Dqn_isize mem_block_size = DQN_MAX(min_block_size, requested_size); Dqn_isize mem_block_size = DQN_MAX(min_block_size, requested_size);
auto const allocate_size = DQN_CAST(Dqn_isize)(sizeof(*arena->curr_mem_block) + mem_block_size); auto const allocate_size = DQN_CAST(Dqn_isize)(sizeof(*arena->curr_mem_block) + mem_block_size);
auto *result = DQN_CAST(Dqn_MemBlock *)Dqn_Allocator_Allocate(&arena->allocator, allocate_size, alignof(Dqn_MemBlock)); auto *result = DQN_CAST(Dqn_ArenaAllocatorBlock *)Dqn_Allocator_Allocate(&arena->allocator, allocate_size, alignof(Dqn_ArenaAllocatorBlock));
if (!result) return result; if (!result) return result;
*result = {}; *result = {};
@ -1734,7 +1734,7 @@ DQN_FILE_SCOPE Dqn_MemBlock *Dqn_MemArena__AllocateBlock(Dqn_MemArena *arena, Dq
return result; return result;
} }
DQN_FILE_SCOPE void Dqn_MemArena__FreeBlock(Dqn_MemArena *arena, Dqn_MemBlock *block) DQN_FILE_SCOPE void Dqn_ArenaAllocator__FreeBlock(Dqn_ArenaAllocator *arena, Dqn_ArenaAllocatorBlock *block)
{ {
if (!block) if (!block)
return; return;
@ -1748,7 +1748,7 @@ DQN_FILE_SCOPE void Dqn_MemArena__FreeBlock(Dqn_MemArena *arena, Dqn_MemBlock *b
Dqn_Allocator_Free(&arena->allocator, block); Dqn_Allocator_Free(&arena->allocator, block);
} }
DQN_FILE_SCOPE void Dqn_MemArena__AttachBlock(Dqn_MemArena *arena, Dqn_MemBlock *new_block) DQN_FILE_SCOPE void Dqn_ArenaAllocator__AttachBlock(Dqn_ArenaAllocator *arena, Dqn_ArenaAllocatorBlock *new_block)
{ {
if (arena->top_mem_block) if (arena->top_mem_block)
{ {
@ -1764,11 +1764,11 @@ DQN_FILE_SCOPE void Dqn_MemArena__AttachBlock(Dqn_MemArena *arena, Dqn_MemBlock
} }
} }
DQN_HEADER_COPY_PROTOTYPE(void *, Dqn_MemArena_Allocate(Dqn_MemArena *arena, Dqn_isize size, Dqn_u8 alignment)) DQN_HEADER_COPY_PROTOTYPE(void *, Dqn_ArenaAllocator_Allocate(Dqn_ArenaAllocator *arena, Dqn_isize size, Dqn_u8 alignment))
{ {
Dqn_isize allocation_size = Dqn_AllocateMetadata_SizeRequired(size, alignment); Dqn_isize allocation_size = Dqn_AllocateMetadata_SizeRequired(size, alignment);
Dqn_b32 need_new_mem_block = true; Dqn_b32 need_new_mem_block = true;
for (Dqn_MemBlock *mem_block = arena->curr_mem_block; mem_block; mem_block = mem_block->next) for (Dqn_ArenaAllocatorBlock *mem_block = arena->curr_mem_block; mem_block; mem_block = mem_block->next)
{ {
Dqn_b32 can_fit_in_block = (mem_block->used + allocation_size) <= mem_block->size; Dqn_b32 can_fit_in_block = (mem_block->used + allocation_size) <= mem_block->size;
if (can_fit_in_block) if (can_fit_in_block)
@ -1781,9 +1781,9 @@ DQN_HEADER_COPY_PROTOTYPE(void *, Dqn_MemArena_Allocate(Dqn_MemArena *arena, Dqn
if (need_new_mem_block) if (need_new_mem_block)
{ {
Dqn_MemBlock *new_block = Dqn_MemArena__AllocateBlock(arena, allocation_size); Dqn_ArenaAllocatorBlock *new_block = Dqn_ArenaAllocator__AllocateBlock(arena, allocation_size);
if (!new_block) return nullptr; if (!new_block) return nullptr;
Dqn_MemArena__AttachBlock(arena, new_block); Dqn_ArenaAllocator__AttachBlock(arena, new_block);
arena->curr_mem_block = arena->top_mem_block; arena->curr_mem_block = arena->top_mem_block;
} }
@ -1795,13 +1795,13 @@ DQN_HEADER_COPY_PROTOTYPE(void *, Dqn_MemArena_Allocate(Dqn_MemArena *arena, Dqn
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE(void, Dqn_MemArena_Free(Dqn_MemArena *arena)) DQN_HEADER_COPY_PROTOTYPE(void, Dqn_ArenaAllocator_Free(Dqn_ArenaAllocator *arena))
{ {
for (Dqn_MemBlock *mem_block = arena->top_mem_block; mem_block;) for (Dqn_ArenaAllocatorBlock *mem_block = arena->top_mem_block; mem_block;)
{ {
Dqn_MemBlock *block_to_free = mem_block; Dqn_ArenaAllocatorBlock *block_to_free = mem_block;
mem_block = block_to_free->prev; mem_block = block_to_free->prev;
Dqn_MemArena__FreeBlock(arena, block_to_free); Dqn_ArenaAllocator__FreeBlock(arena, block_to_free);
} }
auto allocator = arena->allocator; auto allocator = arena->allocator;
@ -1811,7 +1811,7 @@ DQN_HEADER_COPY_PROTOTYPE(void, Dqn_MemArena_Free(Dqn_MemArena *arena))
arena->allocator = allocator; arena->allocator = allocator;
} }
DQN_HEADER_COPY_PROTOTYPE(Dqn_b32, Dqn_MemArena_Reserve(Dqn_MemArena *arena, Dqn_isize size)) DQN_HEADER_COPY_PROTOTYPE(Dqn_b32, Dqn_ArenaAllocator_Reserve(Dqn_ArenaAllocator *arena, Dqn_isize size))
{ {
if (arena->top_mem_block) if (arena->top_mem_block)
{ {
@ -1819,38 +1819,38 @@ DQN_HEADER_COPY_PROTOTYPE(Dqn_b32, Dqn_MemArena_Reserve(Dqn_MemArena *arena, Dqn
if (remaining_space >= size) return true; if (remaining_space >= size) return true;
} }
Dqn_MemBlock *new_block = Dqn_MemArena__AllocateBlock(arena, size); Dqn_ArenaAllocatorBlock *new_block = Dqn_ArenaAllocator__AllocateBlock(arena, size);
if (!new_block) return false; if (!new_block) return false;
Dqn_MemArena__AttachBlock(arena, new_block); Dqn_ArenaAllocator__AttachBlock(arena, new_block);
return true; return true;
} }
DQN_HEADER_COPY_PROTOTYPE(Dqn_MemArena, Dqn_MemArena_InitWithAllocator(Dqn_Allocator allocator, Dqn_isize size)) DQN_HEADER_COPY_PROTOTYPE(Dqn_ArenaAllocator, Dqn_ArenaAllocator_InitWithAllocator(Dqn_Allocator allocator, Dqn_isize size))
{ {
Dqn_MemArena result = {}; Dqn_ArenaAllocator result = {};
DQN_ASSERT_MSG(size >= DQN_ISIZEOF(*result.curr_mem_block), "(%zu >= %zu) There needs to be enough space to encode the Dqn_MemBlock struct into the memory buffer", size, sizeof(*result.curr_mem_block)); DQN_ASSERT_MSG(size >= DQN_ISIZEOF(*result.curr_mem_block), "(%zu >= %zu) There needs to be enough space to encode the Dqn_ArenaAllocatorBlock struct into the memory buffer", size, sizeof(*result.curr_mem_block));
result.allocator = allocator; result.allocator = allocator;
Dqn_MemBlock *mem_block = Dqn_MemArena__AllocateBlock(&result, size); Dqn_ArenaAllocatorBlock *mem_block = Dqn_ArenaAllocator__AllocateBlock(&result, size);
Dqn_MemArena__AttachBlock(&result, mem_block); Dqn_ArenaAllocator__AttachBlock(&result, mem_block);
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE(Dqn_MemArena, Dqn_MemArena_InitMemory(void *memory, Dqn_isize size)) DQN_HEADER_COPY_PROTOTYPE(Dqn_ArenaAllocator, Dqn_ArenaAllocator_InitWithMemory(void *memory, Dqn_isize size))
{ {
Dqn_MemArena result = {}; Dqn_ArenaAllocator result = {};
DQN_ASSERT_MSG(size >= DQN_ISIZEOF(*result.curr_mem_block), "(%zu >= %zu) There needs to be enough space to encode the Dqn_MemBlock struct into the memory buffer", size, sizeof(*result.curr_mem_block)); DQN_ASSERT_MSG(size >= DQN_ISIZEOF(*result.curr_mem_block), "(%zu >= %zu) There needs to be enough space to encode the Dqn_ArenaAllocatorBlock struct into the memory buffer", size, sizeof(*result.curr_mem_block));
result.allocator = Dqn_Allocator_Null(); result.allocator = Dqn_Allocator_InitWithNull();
auto *mem_block = DQN_CAST(Dqn_MemBlock *) memory; auto *mem_block = DQN_CAST(Dqn_ArenaAllocatorBlock *) memory;
*mem_block = {}; *mem_block = {};
mem_block->memory = DQN_CAST(Dqn_u8 *) memory + sizeof(*mem_block); mem_block->memory = DQN_CAST(Dqn_u8 *) memory + sizeof(*mem_block);
mem_block->size = size - DQN_CAST(Dqn_isize)sizeof(*mem_block); mem_block->size = size - DQN_CAST(Dqn_isize)sizeof(*mem_block);
Dqn_MemArena__AttachBlock(&result, mem_block); Dqn_ArenaAllocator__AttachBlock(&result, mem_block);
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE(void, Dqn_MemArena_ResetUsage(Dqn_MemArena *arena, Dqn_ZeroMem zero_mem)) DQN_HEADER_COPY_PROTOTYPE(void, Dqn_ArenaAllocator_ResetUsage(Dqn_ArenaAllocator *arena, Dqn_ZeroMem zero_mem))
{ {
for (Dqn_MemBlock *block = arena->top_mem_block; block; block = block->prev) for (Dqn_ArenaAllocatorBlock *block = arena->top_mem_block; block; block = block->prev)
{ {
if (zero_mem == Dqn_ZeroMem::Yes) if (zero_mem == Dqn_ZeroMem::Yes)
DQN_MEMSET(block->memory, 0, DQN_CAST(size_t)block->used); DQN_MEMSET(block->memory, 0, DQN_CAST(size_t)block->used);
@ -1859,9 +1859,9 @@ DQN_HEADER_COPY_PROTOTYPE(void, Dqn_MemArena_ResetUsage(Dqn_MemArena *arena, Dqn
} }
} }
Dqn_MemArenaRegion Dqn_MemArena_BeginRegion(Dqn_MemArena *arena) Dqn_ArenaAllocatorRegion Dqn_ArenaAllocator_BeginRegion(Dqn_ArenaAllocator *arena)
{ {
Dqn_MemArenaRegion result = {}; Dqn_ArenaAllocatorRegion result = {};
result.arena = arena; result.arena = arena;
result.curr_mem_block = arena->curr_mem_block; result.curr_mem_block = arena->curr_mem_block;
result.curr_mem_block_used = (arena->curr_mem_block) ? arena->curr_mem_block->used : 0; result.curr_mem_block_used = (arena->curr_mem_block) ? arena->curr_mem_block->used : 0;
@ -1869,18 +1869,18 @@ Dqn_MemArenaRegion Dqn_MemArena_BeginRegion(Dqn_MemArena *arena)
return result; return result;
} }
void Dqn_MemArena_EndRegion(Dqn_MemArenaRegion region) void Dqn_ArenaAllocator_EndRegion(Dqn_ArenaAllocatorRegion region)
{ {
while (region.top_mem_block != region.arena->top_mem_block) while (region.top_mem_block != region.arena->top_mem_block)
{ {
Dqn_MemBlock *block_to_free = region.arena->top_mem_block; Dqn_ArenaAllocatorBlock *block_to_free = region.arena->top_mem_block;
if (region.arena->curr_mem_block == block_to_free) if (region.arena->curr_mem_block == block_to_free)
region.arena->curr_mem_block = block_to_free->prev; region.arena->curr_mem_block = block_to_free->prev;
region.arena->top_mem_block = block_to_free->prev; region.arena->top_mem_block = block_to_free->prev;
Dqn_MemArena__FreeBlock(region.arena, block_to_free); Dqn_ArenaAllocator__FreeBlock(region.arena, block_to_free);
} }
for (Dqn_MemBlock *mem_block = region.arena->top_mem_block; mem_block != region.curr_mem_block; mem_block = mem_block->prev) for (Dqn_ArenaAllocatorBlock *mem_block = region.arena->top_mem_block; mem_block != region.curr_mem_block; mem_block = mem_block->prev)
mem_block->used = 0; mem_block->used = 0;
if (region.arena->curr_mem_block) if (region.arena->curr_mem_block)
@ -1888,19 +1888,19 @@ void Dqn_MemArena_EndRegion(Dqn_MemArenaRegion region)
region = {}; region = {};
} }
Dqn_MemArenaScopedRegion::Dqn_MemArenaScopedRegion(Dqn_MemArena *arena) Dqn_ArenaAllocatorScopedRegion::Dqn_ArenaAllocatorScopedRegion(Dqn_ArenaAllocator *arena)
{ {
this->region = Dqn_MemArena_BeginRegion(arena); this->region = Dqn_ArenaAllocator_BeginRegion(arena);
} }
Dqn_MemArenaScopedRegion::~Dqn_MemArenaScopedRegion() Dqn_ArenaAllocatorScopedRegion::~Dqn_ArenaAllocatorScopedRegion()
{ {
Dqn_MemArena_EndRegion(this->region); Dqn_ArenaAllocator_EndRegion(this->region);
} }
Dqn_MemArenaScopedRegion Dqn_MemArena_MakeScopedRegion(Dqn_MemArena *arena) Dqn_ArenaAllocatorScopedRegion Dqn_ArenaAllocator_MakeScopedRegion(Dqn_ArenaAllocator *arena)
{ {
return Dqn_MemArenaScopedRegion(arena); return Dqn_ArenaAllocatorScopedRegion(arena);
} }
// @ ------------------------------------------------------------------------------------------------- // @ -------------------------------------------------------------------------------------------------

View File

@ -163,10 +163,10 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
Dqn_MemArena arena = {}; Dqn_ArenaAllocator arena = {};
arena.allocator = Dqn_Allocator_XHeap(); arena.allocator = Dqn_Allocator_InitWithXHeap();
Dqn_Allocator allocator = Dqn_Allocator_Arena(&arena); Dqn_Allocator allocator = Dqn_Allocator_InitWithArena(&arena);
Dqn_MemArena_Reserve(&arena, DQN_MEGABYTES(16)); Dqn_ArenaAllocator_Reserve(&arena, DQN_MEGABYTES(16));
for (Dqn_isize arg_index = 1; arg_index < argc; ++arg_index) for (Dqn_isize arg_index = 1; arg_index < argc; ++arg_index)
{ {
char const *file = argv[arg_index]; char const *file = argv[arg_index];
@ -206,7 +206,7 @@ int main(int argc, char *argv[])
} }
} }
auto *header_entries = Dqn_MemArena_AllocateType<HeaderEntry>(&arena, num_header_entries); auto *header_entries = Dqn_ArenaAllocator_AllocateType<HeaderEntry>(&arena, num_header_entries);
Dqn_isize header_entries_index = 0; Dqn_isize header_entries_index = 0;
Dqn_isize max_prototype_return_val = 0; Dqn_isize max_prototype_return_val = 0;
@ -245,7 +245,7 @@ int main(int argc, char *argv[])
ptr++; ptr++;
Dqn_isize comment_len = ptr - comment_start; Dqn_isize comment_len = ptr - comment_start;
entry->comment.str = Dqn_MemArena_AllocateType<char>(&arena, comment_len); entry->comment.str = Dqn_ArenaAllocator_AllocateType<char>(&arena, comment_len);
DQN_FOR_EACH(comment_index, comment_len) DQN_FOR_EACH(comment_index, comment_len)
{ {
// NOTE: We capture "// @", and we want to skip the @ symbol, its ugly which is at the index 3 // NOTE: We capture "// @", and we want to skip the @ symbol, its ugly which is at the index 3
@ -272,8 +272,8 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
Dqn_isize copy_len = copy_end - copy_start; Dqn_isize copy_len = copy_end - copy_start;
entry->copy_range.str = Dqn_MemArena_AllocateType<char>(&arena, copy_len); entry->copy_range.str = Dqn_ArenaAllocator_AllocateType<char>(&arena, copy_len);
DQN_FOR_EACH(copy_index, copy_len) DQN_FOR_EACH(copy_index, copy_len)
{ {
char ch = copy_start[copy_index]; char ch = copy_start[copy_index];

View File

@ -217,9 +217,9 @@ enum struct Dqn_Allocator_Type
Custom, Custom,
}; };
#define DQN_ALLOCATOR_ALLOCATE_PROC(name) void *name(Dqn_isize size, Dqn_u8 alignment) #define DQN_ALLOCATOR_ALLOCATE_PROC(name) void *name(Dqn_isize size, Dqn_u8 alignment, void *user_context)
#define DQN_ALLOCATOR_REALLOC_PROC(name) void *name(void *old_ptr, Dqn_isize old_size, Dqn_isize new_size) #define DQN_ALLOCATOR_REALLOC_PROC(name) void *name(void *old_ptr, Dqn_isize old_size, Dqn_isize new_size, void *user_context)
#define DQN_ALLOCATOR_FREE_PROC(name) void name(void *ptr) #define DQN_ALLOCATOR_FREE_PROC(name) void name(void *ptr, void *user_context)
typedef DQN_ALLOCATOR_ALLOCATE_PROC(Dqn_Allocator_AllocateProc); typedef DQN_ALLOCATOR_ALLOCATE_PROC(Dqn_Allocator_AllocateProc);
typedef DQN_ALLOCATOR_REALLOC_PROC(Dqn_Allocator_ReallocProc); typedef DQN_ALLOCATOR_REALLOC_PROC(Dqn_Allocator_ReallocProc);
typedef DQN_ALLOCATOR_FREE_PROC(Dqn_Allocator_FreeProc); typedef DQN_ALLOCATOR_FREE_PROC(Dqn_Allocator_FreeProc);
@ -228,8 +228,8 @@ struct Dqn_Allocator
Dqn_Allocator_Type type; Dqn_Allocator_Type type;
union union
{ {
void *user; void *user;
struct Dqn_MemArena *arena; struct Dqn_ArenaAllocator *arena;
} context; } context;
Dqn_isize bytes_allocated; Dqn_isize bytes_allocated;
@ -244,10 +244,10 @@ struct Dqn_Allocator
Dqn_Allocator_FreeProc *free; Dqn_Allocator_FreeProc *free;
}; };
Dqn_Allocator inline Dqn_Allocator_Null(); Dqn_Allocator inline Dqn_Allocator_InitWithNull();
Dqn_Allocator inline Dqn_Allocator_Heap(); Dqn_Allocator inline Dqn_Allocator_InitWithHeap();
Dqn_Allocator inline Dqn_Allocator_XHeap(); Dqn_Allocator inline Dqn_Allocator_InitWithXHeap();
Dqn_Allocator inline Dqn_Allocator_Arena(Dqn_MemArena *arena); Dqn_Allocator inline Dqn_Allocator_InitWithArena(Dqn_ArenaAllocator *arena);
template <typename T> T * Dqn_Allocator_AllocateType(Dqn_Allocator *allocator, Dqn_isize num); template <typename T> T * Dqn_Allocator_AllocateType(Dqn_Allocator *allocator, Dqn_isize num);
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
// //
@ -263,52 +263,52 @@ struct Dqn_AllocateMetadata
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
// //
// NOTE: Dqn_MemArena // NOTE: Dqn_ArenaAllocator
// //
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
struct Dqn_MemBlock struct Dqn_ArenaAllocatorBlock
{ {
void *memory; void *memory;
Dqn_isize size; Dqn_isize size;
Dqn_isize used; Dqn_isize used;
Dqn_MemBlock *prev; Dqn_ArenaAllocatorBlock *prev;
Dqn_MemBlock *next; Dqn_ArenaAllocatorBlock *next;
}; };
Dqn_usize const DQN_MEM_ARENA_DEFAULT_MIN_BLOCK_SIZE = DQN_KILOBYTES(4); Dqn_usize const DQN_MEM_ARENA_DEFAULT_MIN_BLOCK_SIZE = DQN_KILOBYTES(4);
struct Dqn_MemArena struct Dqn_ArenaAllocator
{ {
// NOTE: Configuration (fill once after "Zero Initialisation {}") // NOTE: Configuration (fill once after "Zero Initialisation {}")
Dqn_isize min_block_size; Dqn_isize min_block_size;
Dqn_Allocator allocator; Dqn_Allocator allocator;
// NOTE: Read Only // NOTE: Read Only
Dqn_MemBlock *curr_mem_block; Dqn_ArenaAllocatorBlock *curr_mem_block;
Dqn_MemBlock *top_mem_block; Dqn_ArenaAllocatorBlock *top_mem_block;
Dqn_isize highest_used_mark; Dqn_isize highest_used_mark;
int total_allocated_mem_blocks; int total_allocated_mem_blocks;
}; };
struct Dqn_MemArenaRegion struct Dqn_ArenaAllocatorRegion
{ {
Dqn_MemArena *arena; Dqn_ArenaAllocator *arena;
Dqn_MemBlock *curr_mem_block; Dqn_ArenaAllocatorBlock *curr_mem_block;
Dqn_isize curr_mem_block_used; Dqn_isize curr_mem_block_used;
Dqn_MemBlock *top_mem_block; Dqn_ArenaAllocatorBlock *top_mem_block;
}; };
struct Dqn_MemArenaScopedRegion struct Dqn_ArenaAllocatorScopedRegion
{ {
Dqn_MemArenaScopedRegion(Dqn_MemArena *arena); Dqn_ArenaAllocatorScopedRegion(Dqn_ArenaAllocator *arena);
~Dqn_MemArenaScopedRegion(); ~Dqn_ArenaAllocatorScopedRegion();
Dqn_MemArenaRegion region; Dqn_ArenaAllocatorRegion region;
}; };
void * Dqn_MemArena_Allocate(Dqn_MemArena *arena, Dqn_isize size, Dqn_u8 alignment); void * Dqn_ArenaAllocator_Allocate(Dqn_ArenaAllocator *arena, Dqn_isize size, Dqn_u8 alignment);
Dqn_b32 Dqn_MemArena_Reserve(Dqn_MemArena *arena, Dqn_isize size); Dqn_b32 Dqn_ArenaAllocator_Reserve(Dqn_ArenaAllocator *arena, Dqn_isize size);
DQN_HEADER_COPY_PROTOTYPE(template <typename T> T *, Dqn_MemArena_AllocateType(Dqn_MemArena *arena, Dqn_isize num)) DQN_HEADER_COPY_PROTOTYPE(template <typename T> T *, Dqn_ArenaAllocator_AllocateType(Dqn_ArenaAllocator *arena, Dqn_isize num))
{ {
auto *result = DQN_CAST(T *)Dqn_MemArena_Allocate(arena, sizeof(T) * num, alignof(T)); auto *result = DQN_CAST(T *)Dqn_ArenaAllocator_Allocate(arena, sizeof(T) * num, alignof(T));
return result; return result;
} }
@ -340,37 +340,36 @@ struct Dqn_String
// NOTE: String Builder // NOTE: String Builder
// //
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
struct Dqn_StringBuilderBuffer struct Dqn_StringBuilderBlock
{ {
char *mem; char *mem;
Dqn_isize size; Dqn_isize size;
Dqn_isize used; Dqn_isize used;
Dqn_StringBuilderBuffer *next; Dqn_StringBuilderBlock *next;
}; };
Dqn_isize constexpr DQN_STRING_BUILDER_MIN_MEM_BUF_ALLOC_SIZE = DQN_KILOBYTES(4); Dqn_isize constexpr DQN_STRING_BUILDER_MIN_BLOCK_SIZE = DQN_KILOBYTES(4);
template <Dqn_usize N = DQN_KILOBYTES(16)> template <Dqn_isize N = DQN_KILOBYTES(16)>
struct Dqn_StringBuilder struct Dqn_StringBuilder
{ {
Dqn_Allocator allocator; Dqn_Allocator allocator;
char fixed_mem[N]; char fixed_mem[N];
Dqn_isize fixed_mem_used; Dqn_StringBuilderBlock fixed_mem_block;
Dqn_StringBuilderBuffer *next_mem_buf; Dqn_StringBuilderBlock *last_mem_block;
Dqn_StringBuilderBuffer *last_mem_buf;
Dqn_isize string_len;
}; };
// The necessary length to build the string, it returns the length including the null-terminator // size_required: The length of the string not including the null terminator.
template <Dqn_usize N> Dqn_isize Dqn_StringBuilder_BuildLen(Dqn_StringBuilder<N> const *builder); // The necessary length to build the string, it returns the length not including the null-terminator
template <Dqn_usize N> void Dqn_StringBuilder_BuildInBuffer(Dqn_StringBuilder<N> const *builder, char *dest, Dqn_usize dest_size); template <Dqn_isize N> Dqn_isize Dqn_StringBuilder_BuildLength(Dqn_StringBuilder<N> const *builder);
template <Dqn_usize N> char * Dqn_StringBuilder_Build(Dqn_StringBuilder<N> *builder, Dqn_Allocator *allocator, Dqn_isize *len = nullptr); template <Dqn_isize N> void Dqn_StringBuilder_BuildToDest(Dqn_StringBuilder<N> const *builder, char *dest, Dqn_usize dest_size);
template <Dqn_usize N> Dqn_String Dqn_StringBuilder_BuildString(Dqn_StringBuilder<N> *builder, Dqn_Allocator *allocator); template <Dqn_isize N> char * Dqn_StringBuilder_Build(Dqn_StringBuilder<N> *builder, Dqn_Allocator *allocator, Dqn_isize *len = nullptr);
template <Dqn_usize N> void Dqn_StringBuilder_VFmtAppend(Dqn_StringBuilder<N> *builder, char const *fmt, va_list va); template <Dqn_isize N> Dqn_String Dqn_StringBuilder_BuildString(Dqn_StringBuilder<N> *builder, Dqn_Allocator *allocator);
template <Dqn_usize N> void Dqn_StringBuilder_FmtAppend(Dqn_StringBuilder<N> *builder, char const *fmt, ...); template <Dqn_isize N> void Dqn_StringBuilder_VFmtAppend(Dqn_StringBuilder<N> *builder, char const *fmt, va_list va);
template <Dqn_usize N> void Dqn_StringBuilder_Append(Dqn_StringBuilder<N> *builder, char const *str, Dqn_isize len = -1); template <Dqn_isize N> void Dqn_StringBuilder_FmtAppend(Dqn_StringBuilder<N> *builder, char const *fmt, ...);
template <Dqn_usize N> void Dqn_StringBuilder_AppendString(Dqn_StringBuilder<N> *builder, Dqn_String const string); template <Dqn_isize N> void Dqn_StringBuilder_Append(Dqn_StringBuilder<N> *builder, char const *str, Dqn_isize len = -1);
template <Dqn_usize N> void Dqn_StringBuilder_AppendChar(Dqn_StringBuilder<N> *builder, char ch); template <Dqn_isize N> void Dqn_StringBuilder_AppendString(Dqn_StringBuilder<N> *builder, Dqn_String const string);
template <Dqn_usize N> void Dqn_StringBuilder_Free(Dqn_StringBuilder<N> *builder); template <Dqn_isize N> void Dqn_StringBuilder_AppendChar(Dqn_StringBuilder<N> *builder, char ch);
template <Dqn_isize N> void Dqn_StringBuilder_Free(Dqn_StringBuilder<N> *builder);
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
// //
// NOTE: Dqn_Slices // NOTE: Dqn_Slices
@ -465,6 +464,7 @@ struct Dqn_FixedString
{ {
union { char data[MAX_]; char str[MAX_]; char buf[MAX_]; }; union { char data[MAX_]; char str[MAX_]; char buf[MAX_]; };
Dqn_isize len; Dqn_isize len;
Dqn_isize max = MAX_;
Dqn_FixedString() { data[0] = 0; len = 0; } Dqn_FixedString() { data[0] = 0; len = 0; }
Dqn_FixedString(char const *fmt, ...) Dqn_FixedString(char const *fmt, ...)
@ -542,15 +542,15 @@ void * Dqn_Allocator_Realloc(Dqn_Allocator *
void Dqn_Allocator_Free(Dqn_Allocator *allocator, void *ptr); void Dqn_Allocator_Free(Dqn_Allocator *allocator, void *ptr);
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
// //
// NOTE: Dqn_MemArena // NOTE: Dqn_ArenaAllocator
// //
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
void * Dqn_MemArena_Allocate(Dqn_MemArena *arena, Dqn_isize size, Dqn_u8 alignment); void * Dqn_ArenaAllocator_Allocate(Dqn_ArenaAllocator *arena, Dqn_isize size, Dqn_u8 alignment);
void Dqn_MemArena_Free(Dqn_MemArena *arena); void Dqn_ArenaAllocator_Free(Dqn_ArenaAllocator *arena);
Dqn_b32 Dqn_MemArena_Reserve(Dqn_MemArena *arena, Dqn_isize size); Dqn_b32 Dqn_ArenaAllocator_Reserve(Dqn_ArenaAllocator *arena, Dqn_isize size);
Dqn_MemArena Dqn_MemArena_InitWithAllocator(Dqn_Allocator allocator, Dqn_isize size); Dqn_ArenaAllocator Dqn_ArenaAllocator_InitWithAllocator(Dqn_Allocator allocator, Dqn_isize size);
Dqn_MemArena Dqn_MemArena_InitMemory(void *memory, Dqn_isize size); Dqn_ArenaAllocator Dqn_ArenaAllocator_InitWithMemory(void *memory, Dqn_isize size);
void Dqn_MemArena_ResetUsage(Dqn_MemArena *arena, Dqn_ZeroMem zero_mem); void Dqn_ArenaAllocator_ResetUsage(Dqn_ArenaAllocator *arena, Dqn_ZeroMem zero_mem);
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
// //
// NOTE: Dqn_Asprintf (Allocate Sprintf) // NOTE: Dqn_Asprintf (Allocate Sprintf)
@ -613,8 +613,8 @@ Dqn_V4 operator*(Dqn_Mat4 const &mat, Dqn_V4
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
void Dqn_Bit_UnsetInplace(Dqn_u32 *flags, Dqn_u32 bitfield); void Dqn_Bit_UnsetInplace(Dqn_u32 *flags, Dqn_u32 bitfield);
void Dqn_Bit_SetInplace(Dqn_u32 *flags, Dqn_u32 bitfield); void Dqn_Bit_SetInplace(Dqn_u32 *flags, Dqn_u32 bitfield);
Dqn_b32 Dqn_Bit_IsSet(Dqn_u32 flags, Dqn_u32 bitfield); Dqn_b32 Dqn_Bit_IsSet(Dqn_u32 bits, Dqn_u32 bits_to_set);
Dqn_b32 Dqn_Bit_IsNotSet(Dqn_u32 flags, Dqn_u32 bitfield); Dqn_b32 Dqn_Bit_IsNotSet(Dqn_u32 bits, Dqn_u32 bits_to_check);
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
// //
// NOTE: Safe Arithmetic // NOTE: Safe Arithmetic

View File

@ -16,7 +16,7 @@ struct TestingState
int num_tests_in_group; int num_tests_in_group;
int num_tests_ok_in_group; int num_tests_ok_in_group;
TestState test; TestState test;
Dqn_MemArena arena_; Dqn_ArenaAllocator arena_;
Dqn_Allocator allocator; Dqn_Allocator allocator;
}; };
@ -33,8 +33,8 @@ struct TestingState
{ \ { \
if (testing_state.test.fail_expr.len == 0) testing_state.num_tests_ok_in_group++; \ if (testing_state.test.fail_expr.len == 0) testing_state.num_tests_ok_in_group++; \
TestState_PrintResult(&testing_state.test); \ TestState_PrintResult(&testing_state.test); \
Dqn_MemArena_ResetUsage(&testing_state.arena_, Dqn_ZeroMem::No); \ Dqn_ArenaAllocator_ResetUsage(&testing_state.arena_, Dqn_ZeroMem::No); \
testing_state.allocator = Dqn_Allocator_Arena(&testing_state.arena_); \ testing_state.allocator = Dqn_Allocator_InitWithArena(&testing_state.arena_); \
testing_state.test = {}; \ testing_state.test = {}; \
}; \ }; \
testing_state.test.name = Dqn_Asprintf(&testing_state.allocator, test_name); \ testing_state.test.name = Dqn_Asprintf(&testing_state.allocator, test_name); \
@ -121,7 +121,7 @@ DQN_FILE_SCOPE void UnitTests()
{ {
{ {
TEST_START_SCOPE(testing_state, "HeapAllocator - Allocate Small"); TEST_START_SCOPE(testing_state, "HeapAllocator - Allocate Small");
Dqn_Allocator allocator = Dqn_Allocator_Heap(); Dqn_Allocator allocator = Dqn_Allocator_InitWithHeap();
char constexpr EXPECT[] = "hello_world"; char constexpr EXPECT[] = "hello_world";
char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, Dqn_ArrayCount(EXPECT), alignof(char)); char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, Dqn_ArrayCount(EXPECT), alignof(char));
DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); }; DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); };
@ -131,7 +131,7 @@ DQN_FILE_SCOPE void UnitTests()
{ {
TEST_START_SCOPE(testing_state, "XHeapAllocator - Allocate Small"); TEST_START_SCOPE(testing_state, "XHeapAllocator - Allocate Small");
Dqn_Allocator allocator = Dqn_Allocator_XHeap(); Dqn_Allocator allocator = Dqn_Allocator_InitWithXHeap();
char constexpr EXPECT[] = "hello_world"; char constexpr EXPECT[] = "hello_world";
char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, Dqn_ArrayCount(EXPECT), alignof(char)); char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, Dqn_ArrayCount(EXPECT), alignof(char));
DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); }; DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); };
@ -141,9 +141,9 @@ DQN_FILE_SCOPE void UnitTests()
{ {
TEST_START_SCOPE(testing_state, "ArenaAllocator - Allocate Small"); TEST_START_SCOPE(testing_state, "ArenaAllocator - Allocate Small");
Dqn_MemArena arena = {}; Dqn_ArenaAllocator arena = {};
arena.allocator = Dqn_Allocator_Heap(); arena.allocator = Dqn_Allocator_InitWithHeap();
Dqn_Allocator allocator = Dqn_Allocator_Arena(&arena); Dqn_Allocator allocator = Dqn_Allocator_InitWithArena(&arena);
char constexpr EXPECT[] = "hello_world"; char constexpr EXPECT[] = "hello_world";
char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, Dqn_ArrayCount(EXPECT), alignof(char)); char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, Dqn_ArrayCount(EXPECT), alignof(char));
DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); }; DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); };
@ -158,7 +158,7 @@ DQN_FILE_SCOPE void UnitTests()
Dqn_u8 const NUM_BYTES = sizeof(Dqn_u32); Dqn_u8 const NUM_BYTES = sizeof(Dqn_u32);
{ {
TEST_START_SCOPE(testing_state, "HeapAllocator - Align to 3 bytes"); TEST_START_SCOPE(testing_state, "HeapAllocator - Align to 3 bytes");
Dqn_Allocator allocator = Dqn_Allocator_Heap(); Dqn_Allocator allocator = Dqn_Allocator_InitWithHeap();
auto *buf = DQN_CAST(Dqn_u32 *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3); auto *buf = DQN_CAST(Dqn_u32 *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3);
DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); }; DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); };
int buf_mod_alignment = DQN_CAST(int)(DQN_CAST(uintptr_t)buf % ALIGNMENT3); int buf_mod_alignment = DQN_CAST(int)(DQN_CAST(uintptr_t)buf % ALIGNMENT3);
@ -167,7 +167,7 @@ DQN_FILE_SCOPE void UnitTests()
{ {
TEST_START_SCOPE(testing_state, "XHeapAllocator - Align to 3 bytes"); TEST_START_SCOPE(testing_state, "XHeapAllocator - Align to 3 bytes");
Dqn_Allocator allocator = Dqn_Allocator_XHeap(); Dqn_Allocator allocator = Dqn_Allocator_InitWithXHeap();
auto *buf = DQN_CAST(Dqn_u32 *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3); auto *buf = DQN_CAST(Dqn_u32 *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3);
DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); }; DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); };
int buf_mod_alignment = DQN_CAST(int)(DQN_CAST(uintptr_t)buf % ALIGNMENT3); int buf_mod_alignment = DQN_CAST(int)(DQN_CAST(uintptr_t)buf % ALIGNMENT3);
@ -176,8 +176,8 @@ DQN_FILE_SCOPE void UnitTests()
{ {
TEST_START_SCOPE(testing_state, "ArenaAllocator - Align to 3 bytes"); TEST_START_SCOPE(testing_state, "ArenaAllocator - Align to 3 bytes");
Dqn_MemArena arena = {}; Dqn_ArenaAllocator arena = {};
Dqn_Allocator allocator = Dqn_Allocator_Arena(&arena); Dqn_Allocator allocator = Dqn_Allocator_InitWithArena(&arena);
auto *buf = DQN_CAST(Dqn_u32 *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3); auto *buf = DQN_CAST(Dqn_u32 *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3);
int buf_mod_alignment = DQN_CAST(int)(DQN_CAST(uintptr_t)buf % ALIGNMENT3); int buf_mod_alignment = DQN_CAST(int)(DQN_CAST(uintptr_t)buf % ALIGNMENT3);
TEST_EXPECT_MSG(testing_state, buf_mod_alignment == 0, "buf_mod_alignment: %d", buf_mod_alignment); TEST_EXPECT_MSG(testing_state, buf_mod_alignment == 0, "buf_mod_alignment: %d", buf_mod_alignment);
@ -191,7 +191,7 @@ DQN_FILE_SCOPE void UnitTests()
Dqn_u8 const MAX_OFFSET = (ALIGNMENT3 - 1) + sizeof(Dqn_AllocateMetadata); Dqn_u8 const MAX_OFFSET = (ALIGNMENT3 - 1) + sizeof(Dqn_AllocateMetadata);
{ {
TEST_START_SCOPE(testing_state, "HeapAllocator - Allocation metadata initialised"); TEST_START_SCOPE(testing_state, "HeapAllocator - Allocation metadata initialised");
Dqn_Allocator allocator = Dqn_Allocator_Heap(); Dqn_Allocator allocator = Dqn_Allocator_InitWithHeap();
char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3); char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3);
DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); }; DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); };
Dqn_AllocateMetadata metadata = Dqn_AllocateMetadata_Get(buf); Dqn_AllocateMetadata metadata = Dqn_AllocateMetadata_Get(buf);
@ -201,7 +201,7 @@ DQN_FILE_SCOPE void UnitTests()
{ {
TEST_START_SCOPE(testing_state, "XHeapAllocator - Allocation metadata initialised"); TEST_START_SCOPE(testing_state, "XHeapAllocator - Allocation metadata initialised");
Dqn_Allocator allocator = Dqn_Allocator_XHeap(); Dqn_Allocator allocator = Dqn_Allocator_InitWithXHeap();
char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3); char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3);
DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); }; DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); };
Dqn_AllocateMetadata metadata = Dqn_AllocateMetadata_Get(buf); Dqn_AllocateMetadata metadata = Dqn_AllocateMetadata_Get(buf);
@ -211,8 +211,8 @@ DQN_FILE_SCOPE void UnitTests()
{ {
TEST_START_SCOPE(testing_state, "ArenaAllocator - Allocation metadata initialised"); TEST_START_SCOPE(testing_state, "ArenaAllocator - Allocation metadata initialised");
Dqn_MemArena arena = {}; Dqn_ArenaAllocator arena = {};
Dqn_Allocator allocator = Dqn_Allocator_Arena(&arena); Dqn_Allocator allocator = Dqn_Allocator_InitWithArena(&arena);
char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3); char *buf = DQN_CAST(char *)Dqn_Allocator_Allocate(&allocator, NUM_BYTES, ALIGNMENT3);
DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); }; DQN_DEFER { Dqn_Allocator_Free(&allocator, buf); };
Dqn_AllocateMetadata metadata = Dqn_AllocateMetadata_Get(buf); Dqn_AllocateMetadata metadata = Dqn_AllocateMetadata_Get(buf);
@ -489,7 +489,7 @@ DQN_FILE_SCOPE void UnitTests()
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
{ {
TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_StringBuilder"); TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_StringBuilder");
Dqn_Allocator allocator = Dqn_Allocator_Heap(); Dqn_Allocator allocator = Dqn_Allocator_InitWithHeap();
// NOTE: Dqn_StringBuilder_Append // NOTE: Dqn_StringBuilder_Append
{ {
{ {