Switch membuffer to memstack and add pop

This commit is contained in:
Doyle Thai 2017-05-22 18:50:21 +10:00
parent 034de12235
commit 25744855ee
2 changed files with 307 additions and 257 deletions

321
dqn.h
View File

@ -67,92 +67,93 @@ DQN_FILE_SCOPE void *DqnMem_Realloc(void *memory, size_t newSize);
DQN_FILE_SCOPE void DqnMem_Free (void *memory); DQN_FILE_SCOPE void DqnMem_Free (void *memory);
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// DqnMemBuffer - Memory Buffer, For push buffer/ptr memory style management // DqnMemStack - Memory Stack, For push stack/ptr memory style management
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// DqnMemBuffer is a data structure to dynamically allocate memory in a stack // DqnMemStack is a data structure to dynamically allocate memory in a stack
// like style. It pre-allocates a block of memory in init and sub-allocates from // like style. It pre-allocates a block of memory in init and sub-allocates from
// this block to take advantage of memory locality. // this block to take advantage of memory locality.
// When an allocation requires a larger amount of memory than available in the // When an allocation requires a larger amount of memory than available in the
// block then the MemBuffer will allocate a new block of sufficient size for // block then the MemStack will allocate a new block of sufficient size for
// you in DqnMemBuffer_Allocate(..). This _DOES_ mean that there will be wasted // you in DqnMemStack_Allocate(..). This _DOES_ mean that there will be wasted
// space at the end of each block and is a tradeoff for memory locality against // space at the end of each block and is a tradeoff for memory locality against
// optimal space usage. // optimal space usage.
// How To Use: // How To Use:
// 1. Create a DqnMemBuffer struct and pass it into an initialisation function // 1. Create a DqnMemStack struct and pass it into an initialisation function
// - InitWithFixedMem() allows you to pass in your own memory which is // - InitWithFixedMem() allows you to pass in your own memory which is
// converted to a memory block. This disables dynamic allocation. // converted to a memory block. This disables dynamic allocation.
// NOTE: Space is reserved in the given memory for MemBufferBlock metadata. // NOTE: Space is reserved in the given memory for MemStackBlock metadata.
// - InitWithFixedSize() allows you to to disable dynamic allocations and // - InitWithFixedSize() allows you to to disable dynamic allocations and
// sub-allocate from the initial MemBuffer allocation size only. // sub-allocate from the initial MemStack allocation size only.
// 2. Use DqnMemBuffer_Allocate(..) to allocate memory for use. // 2. Use DqnMemStack_Allocate(..) to allocate memory for use.
// - "Freeing" memory is dealt by creating temporary MemBuffers or using the // - "Freeing" memory is dealt by creating temporary MemStacks or using the
// BeginTempRegion and EndTempRegion functions. Specifically freeing // BeginTempRegion and EndTempRegion functions. Specifically freeing
// individual items is typically not generalisable in this scheme. // individual items is typically not generalisable in this scheme.
typedef struct DqnMemBufferBlock typedef struct DqnMemStackBlock
{ {
u8 *memory; u8 *memory;
size_t used; size_t used;
size_t size; size_t size;
DqnMemBufferBlock *prevBlock; DqnMemStackBlock *prevBlock;
} DqnMemBufferBlock; } DqnMemStackBlock;
enum DqnMemBufferFlag enum DqnMemStackFlag
{ {
DqnMemBufferFlag_IsNotExpandable = (1 << 0), DqnMemStackFlag_IsNotExpandable = (1 << 0),
DqnMemBufferFlag_IsFixedMemoryFromUser = (1 << 1), // NOTE(doyle): Required to indicate we CAN'T free this memory when free is called. DqnMemStackFlag_IsFixedMemoryFromUser = (1 << 1), // NOTE(doyle): Required to indicate we CAN'T free this memory when free is called.
}; };
typedef struct DqnMemBuffer typedef struct DqnMemStack
{ {
DqnMemBufferBlock *block; DqnMemStackBlock *block;
u32 flags; u32 flags;
i32 tempBufferCount; i32 tempStackCount;
u32 byteAlign; u32 byteAlign;
} DqnMemBuffer; } DqnMemStack;
typedef struct DqnTempBuffer typedef struct DqnTempMemStack
{ {
DqnMemBuffer *buffer; DqnMemStack *stack;
DqnMemBufferBlock *startingBlock; DqnMemStackBlock *startingBlock;
size_t used; size_t used;
} DqnTempBuffer; } DqnTempMemStack;
DQN_FILE_SCOPE bool DqnMemBuffer_InitWithFixedMem (DqnMemBuffer *const buffer, u8 *const mem, const size_t memSize, const u32 byteAlign = 4); // Use preallocated memory, no further allocations, returns NULL on allocate if out of space DQN_FILE_SCOPE bool DqnMemStack_InitWithFixedMem (DqnMemStack *const stack, u8 *const mem, const size_t memSize, const u32 byteAlign = 4); // Use preallocated memory, no further allocations, returns NULL on allocate if out of space
DQN_FILE_SCOPE bool DqnMemBuffer_InitWithFixedSize(DqnMemBuffer *const buffer, size_t size, const bool zeroClear, const u32 byteAlign = 4); // Single allocation from platform, no further allocations, returns NULL of allocate if out of space DQN_FILE_SCOPE bool DqnMemStack_InitWithFixedSize(DqnMemStack *const stack, size_t size, const bool zeroClear, const u32 byteAlign = 4); // Single allocation from platform, no further allocations, returns NULL of allocate if out of space
DQN_FILE_SCOPE bool DqnMemBuffer_Init (DqnMemBuffer *const buffer, size_t size, const bool zeroClear, const u32 byteAlign = 4); // Allocates from platform dynamically as space runs out DQN_FILE_SCOPE bool DqnMemStack_Init (DqnMemStack *const stack, size_t size, const bool zeroClear, const u32 byteAlign = 4); // Allocates from platform dynamically as space runs out
DQN_FILE_SCOPE void *DqnMemBuffer_Allocate (DqnMemBuffer *const buffer, size_t size); // Returns NULL if out of space and buffer is using fixed memory/size, or platform allocation fails DQN_FILE_SCOPE void *DqnMemStack_Allocate (DqnMemStack *const stack, size_t size); // Returns NULL if out of space and stack is using fixed memory/size, or platform allocation fails
DQN_FILE_SCOPE void DqnMemBuffer_Free (DqnMemBuffer *const buffer); // Frees all blocks belonging to this buffer DQN_FILE_SCOPE bool DqnMemStack_Pop (DqnMemStack *const stack, void *ptr, size_t size); // Frees the given ptr. It MUST be the last allocated item in the stack
DQN_FILE_SCOPE bool DqnMemBuffer_FreeBufferBlock(DqnMemBuffer *const buffer, DqnMemBufferBlock *block); // Frees the specified block, returns false if block doesn't belong DQN_FILE_SCOPE void DqnMemStack_Free (DqnMemStack *const stack); // Frees all blocks belonging to this stack
DQN_FILE_SCOPE bool DqnMemBuffer_FreeLastBlock (DqnMemBuffer *const buffer); // Frees the last-most memory block. If last block, free that block, next allocate will attach a block. DQN_FILE_SCOPE bool DqnMemStack_FreeStackBlock (DqnMemStack *const stack, DqnMemStackBlock *block); // Frees the specified block, returns false if block doesn't belong
DQN_FILE_SCOPE void DqnMemBuffer_ClearCurrBlock (DqnMemBuffer *const buffer, const bool zeroClear); // Reset the current memory block usage to 0 DQN_FILE_SCOPE bool DqnMemStack_FreeLastBlock (DqnMemStack *const stack); // Frees the last-most memory block. If last block, free that block, next allocate will attach a block.
DQN_FILE_SCOPE void DqnMemStack_ClearCurrBlock (DqnMemStack *const stack, const bool zeroClear); // Reset the current memory block usage to 0
// TempBuffer is only required for the function. Once BeginTempRegion() is called, subsequent allocation calls can be made using the original buffer. // TempMemStack is only required for the function. Once BeginTempRegion() is called, subsequent allocation calls can be made using the original stack.
// Upon EndTempRegion() the original buffer will free any additional blocks it allocated during the temp region and revert to the original // Upon EndTempRegion() the original stack will free any additional blocks it allocated during the temp region and revert to the original
// state before BeginTempRegion() was called. // state before BeginTempRegion() was called.
// WARNING: Any calls to Free/Clear functions in a TempRegion will invalidate and trash the buffer structure. // WARNING: Any calls to Free/Clear functions in a TempRegion will invalidate and trash the stack structure.
// TODO(doyle): Look into a way of disallowing calls to free/clear in temp regions // TODO(doyle): Look into a way of disallowing calls to free/clear in temp regions
DQN_FILE_SCOPE DqnTempBuffer DqnMemBuffer_BeginTempRegion(DqnMemBuffer *const buffer); DQN_FILE_SCOPE DqnTempMemStack DqnMemStack_BeginTempRegion(DqnMemStack *const stack);
DQN_FILE_SCOPE void DqnMemBuffer_EndTempRegion (DqnTempBuffer tempBuffer); DQN_FILE_SCOPE void DqnMemStack_EndTempRegion (DqnTempMemStack tempstack);
// (OPTIONAL) DqnMemBuffer Advanced API // (OPTIONAL) DqnMemStack Advanced API
// This is useful for forcing a new block to be used. AllocateCompatibleBlock // This is useful for forcing a new block to be used. AllocateCompatibleBlock
// will fail if the supplied buffer has flags set such that the buffer is not // will fail if the supplied stack has flags set such that the stack is not
// allowed to have new blocks. // allowed to have new blocks.
DQN_FILE_SCOPE DqnMemBufferBlock *DqnMemBuffer_AllocateCompatibleBlock(const DqnMemBuffer *const buffer, size_t size); DQN_FILE_SCOPE DqnMemStackBlock *DqnMemStack_AllocateCompatibleBlock(const DqnMemStack *const stack, size_t size);
DQN_FILE_SCOPE bool DqnMemBuffer_AttachBlock (DqnMemBuffer *const buffer, DqnMemBufferBlock *const newBlock); DQN_FILE_SCOPE bool DqnMemStack_AttachBlock (DqnMemStack *const stack, DqnMemStackBlock *const newBlock);
// (IMPORTANT) Should only be used to free blocks that haven't been attached! // (IMPORTANT) Should only be used to free blocks that haven't been attached!
// Attached blocks should be freed using FreeBufferBlock(). // Attached blocks should be freed using FreeStackBlock().
DQN_FILE_SCOPE void DqnMemBuffer_FreeBlock(DqnMemBufferBlock *block); DQN_FILE_SCOPE void DqnMemStack_FreeBlock(DqnMemStackBlock *block);
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// DqnMemAPI - Memory API, For using custom allocators // DqnMemAPI - Memory API, For using custom allocators
@ -507,8 +508,15 @@ typedef union DqnV3
f32 e[3]; f32 e[3];
} DqnV3; } DqnV3;
// Create a vector using ints and typecast to floats typedef union DqnV3i
DQN_FILE_SCOPE DqnV3 DqnV3_3i(i32 x, i32 y, i32 z); {
struct { i32 x, y, z; };
struct { i32 r, g, b; };
i32 e[3];
} DqnV3i;
// DqnV3
DQN_FILE_SCOPE DqnV3 DqnV3_3i(i32 x, i32 y, i32 z); // Create a vector using ints and typecast to floats
DQN_FILE_SCOPE DqnV3 DqnV3_3f(f32 x, f32 y, f32 z); DQN_FILE_SCOPE DqnV3 DqnV3_3f(f32 x, f32 y, f32 z);
DQN_FILE_SCOPE DqnV3 DqnV3_Add (DqnV3 a, DqnV3 b); DQN_FILE_SCOPE DqnV3 DqnV3_Add (DqnV3 a, DqnV3 b);
@ -532,6 +540,10 @@ DQN_FILE_SCOPE inline DqnV3 &operator-=(DqnV3 &a, DqnV3 b) { return (a = DqnV3_S
DQN_FILE_SCOPE inline DqnV3 &operator+=(DqnV3 &a, DqnV3 b) { return (a = DqnV3_Add (a, b)); } DQN_FILE_SCOPE inline DqnV3 &operator+=(DqnV3 &a, DqnV3 b) { return (a = DqnV3_Add (a, b)); }
DQN_FILE_SCOPE inline bool operator==(DqnV3 a, DqnV3 b) { return DqnV3_Equals (a, b); } DQN_FILE_SCOPE inline bool operator==(DqnV3 a, DqnV3 b) { return DqnV3_Equals (a, b); }
// DqnV3i
DQN_FILE_SCOPE DqnV3i DqnV3i_3i(i32 x, i32 y, i32 z);
DQN_FILE_SCOPE DqnV3i DqnV3i_3f(f32 x, f32 y, f32 z);
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Vec4 // Vec4
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -1328,17 +1340,17 @@ DQN_FILE_SCOPE void DqnMem_Free(void *memory)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// DqnMemBuffer - Memory API, For using custom allocators // DqnMemStack - Memory API, For using custom allocators
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
DQN_FILE_SCOPE DqnMemBufferBlock * DQN_FILE_SCOPE DqnMemStackBlock *
DqnMemBuffer_AllocateBlockInternal(u32 byteAlign, size_t size) DqnMemStack_AllocateBlockInternal(u32 byteAlign, size_t size)
{ {
size_t alignedSize = DQN_ALIGN_POW_N(size, byteAlign); size_t alignedSize = DQN_ALIGN_POW_N(size, byteAlign);
size_t totalSize = alignedSize + sizeof(DqnMemBufferBlock) + (byteAlign -1); size_t totalSize = alignedSize + sizeof(DqnMemStackBlock) + (byteAlign -1);
// NOTE(doyle): Total size includes another (byteAlign-1) since we also want // NOTE(doyle): Total size includes another (byteAlign-1) since we also want
// to align the base pointer to memory that we receive. // to align the base pointer to memory that we receive.
DqnMemBufferBlock *result = (DqnMemBufferBlock *)DqnMem_Calloc(totalSize); DqnMemStackBlock *result = (DqnMemStackBlock *)DqnMem_Calloc(totalSize);
if (!result) return NULL; if (!result) return NULL;
result->memory = (u8 *)DQN_ALIGN_POW_N((u8 *)result + sizeof(*result), byteAlign); result->memory = (u8 *)DQN_ALIGN_POW_N((u8 *)result + sizeof(*result), byteAlign);
@ -1347,110 +1359,110 @@ DqnMemBuffer_AllocateBlockInternal(u32 byteAlign, size_t size)
return result; return result;
} }
DQN_FILE_SCOPE DqnMemBufferBlock * DQN_FILE_SCOPE DqnMemStackBlock *
DqnMemBuffer_AllocateCompatibleBlock(const DqnMemBuffer *const buffer, size_t size) DqnMemStack_AllocateCompatibleBlock(const DqnMemStack *const stack, size_t size)
{ {
if (!buffer) return NULL; if (!stack) return NULL;
if (buffer->flags & DqnMemBufferFlag_IsFixedMemoryFromUser) return NULL; if (stack->flags & DqnMemStackFlag_IsFixedMemoryFromUser) return NULL;
if (buffer->flags & DqnMemBufferFlag_IsNotExpandable) return NULL; if (stack->flags & DqnMemStackFlag_IsNotExpandable) return NULL;
DqnMemBufferBlock *block = DqnMemStackBlock *block =
DqnMemBuffer_AllocateBlockInternal(buffer->byteAlign, size); DqnMemStack_AllocateBlockInternal(stack->byteAlign, size);
return block; return block;
} }
DQN_FILE_SCOPE bool DqnMemBuffer_AttachBlock(DqnMemBuffer *const buffer, DQN_FILE_SCOPE bool DqnMemStack_AttachBlock(DqnMemStack *const stack,
DqnMemBufferBlock *const newBlock) DqnMemStackBlock *const newBlock)
{ {
if (!buffer || !newBlock) return false; if (!stack || !newBlock) return false;
if (buffer->flags & DqnMemBufferFlag_IsFixedMemoryFromUser) return false; if (stack->flags & DqnMemStackFlag_IsFixedMemoryFromUser) return false;
if (buffer->flags & DqnMemBufferFlag_IsNotExpandable) return false; if (stack->flags & DqnMemStackFlag_IsNotExpandable) return false;
newBlock->prevBlock = buffer->block; newBlock->prevBlock = stack->block;
buffer->block = newBlock; stack->block = newBlock;
return true; return true;
} }
DQN_FILE_SCOPE void DqnMemBuffer_FreeBlock(DqnMemBufferBlock *block) DQN_FILE_SCOPE void DqnMemStack_FreeBlock(DqnMemStackBlock *block)
{ {
if (!block) return; if (!block) return;
DqnMem_Free(block); DqnMem_Free(block);
} }
DQN_FILE_SCOPE bool DqnMemBuffer_InitWithFixedMem(DqnMemBuffer *const buffer, DQN_FILE_SCOPE bool DqnMemStack_InitWithFixedMem(DqnMemStack *const stack,
u8 *const mem, u8 *const mem,
const size_t memSize, const size_t memSize,
const u32 byteAlign) const u32 byteAlign)
{ {
if (!buffer || !mem) return false; if (!stack || !mem) return false;
DQN_ASSERT(!buffer->block); DQN_ASSERT(!stack->block);
// TODO(doyle): Better logging // TODO(doyle): Better logging
if (memSize < sizeof(DqnMemBufferBlock)) if (memSize < sizeof(DqnMemStackBlock))
DQN_ASSERT(DQN_INVALID_CODE_PATH); DQN_ASSERT(DQN_INVALID_CODE_PATH);
buffer->block = (DqnMemBufferBlock *)mem; stack->block = (DqnMemStackBlock *)mem;
buffer->block->memory = mem + sizeof(DqnMemBufferBlock); stack->block->memory = mem + sizeof(DqnMemStackBlock);
buffer->block->used = 0; stack->block->used = 0;
buffer->block->size = memSize - sizeof(DqnMemBufferBlock); stack->block->size = memSize - sizeof(DqnMemStackBlock);
buffer->flags = (DqnMemBufferFlag_IsFixedMemoryFromUser | DqnMemBufferFlag_IsNotExpandable); stack->flags = (DqnMemStackFlag_IsFixedMemoryFromUser | DqnMemStackFlag_IsNotExpandable);
const u32 DEFAULT_ALIGNMENT = 4; const u32 DEFAULT_ALIGNMENT = 4;
buffer->tempBufferCount = 0; stack->tempStackCount = 0;
buffer->byteAlign = (byteAlign == 0) ? DEFAULT_ALIGNMENT : byteAlign; stack->byteAlign = (byteAlign == 0) ? DEFAULT_ALIGNMENT : byteAlign;
return true; return true;
} }
DQN_FILE_SCOPE bool DqnMemBuffer_Init(DqnMemBuffer *const buffer, size_t size, DQN_FILE_SCOPE bool DqnMemStack_Init(DqnMemStack *const stack, size_t size,
const bool zeroClear, const bool zeroClear,
const u32 byteAlign) const u32 byteAlign)
{ {
if (!buffer || size <= 0) return false; if (!stack || size <= 0) return false;
DQN_ASSERT(!buffer->block); DQN_ASSERT(!stack->block);
buffer->block = DqnMemBuffer_AllocateBlockInternal(byteAlign, size); stack->block = DqnMemStack_AllocateBlockInternal(byteAlign, size);
if (!buffer->block) return false; if (!stack->block) return false;
buffer->tempBufferCount = 0; stack->tempStackCount = 0;
buffer->byteAlign = byteAlign; stack->byteAlign = byteAlign;
buffer->flags = 0; stack->flags = 0;
return true; return true;
} }
DQN_FILE_SCOPE bool DqnMemBuffer_InitWithFixedSize(DqnMemBuffer *const buffer, DQN_FILE_SCOPE bool DqnMemStack_InitWithFixedSize(DqnMemStack *const stack,
size_t size, size_t size,
const bool zeroClear, const bool zeroClear,
const u32 byteAlign) const u32 byteAlign)
{ {
bool result = DqnMemBuffer_Init(buffer, size, byteAlign); bool result = DqnMemStack_Init(stack, size, byteAlign);
if (result) if (result)
{ {
buffer->flags |= DqnMemBufferFlag_IsNotExpandable; stack->flags |= DqnMemStackFlag_IsNotExpandable;
return true; return true;
} }
return false; return false;
} }
DQN_FILE_SCOPE void *DqnMemBuffer_Allocate(DqnMemBuffer *const buffer, size_t size) DQN_FILE_SCOPE void *DqnMemStack_Allocate(DqnMemStack *const stack, size_t size)
{ {
if (!buffer || size == 0) return NULL; if (!stack || size == 0) return NULL;
size_t alignedSize = DQN_ALIGN_POW_N(size, buffer->byteAlign); size_t alignedSize = DQN_ALIGN_POW_N(size, stack->byteAlign);
if (!buffer->block || if (!stack->block ||
(buffer->block->used + alignedSize) > buffer->block->size) (stack->block->used + alignedSize) > stack->block->size)
{ {
size_t newBlockSize; size_t newBlockSize;
// TODO(doyle): Allocate block size based on the aligned size or // TODO(doyle): Allocate block size based on the aligned size or
// a minimum block size? Not allocate based on the current block // a minimum block size? Not allocate based on the current block
// size // size
if (buffer->block) newBlockSize = DQN_MAX(alignedSize, buffer->block->size); if (stack->block) newBlockSize = DQN_MAX(alignedSize, stack->block->size);
else newBlockSize = alignedSize; else newBlockSize = alignedSize;
DqnMemBufferBlock *newBlock = DqnMemBuffer_AllocateCompatibleBlock(buffer, newBlockSize); DqnMemStackBlock *newBlock = DqnMemStack_AllocateCompatibleBlock(stack, newBlockSize);
if (newBlock) if (newBlock)
{ {
if (!DqnMemBuffer_AttachBlock(buffer, newBlock)) if (!DqnMemStack_AttachBlock(stack, newBlock))
{ {
// IMPORTANT(doyle): This should be impossible, considering that // IMPORTANT(doyle): This should be impossible, considering that
// AllocateCompatibleBlock checks the preconditions that the new // AllocateCompatibleBlock checks the preconditions that the new
@ -1464,48 +1476,62 @@ DQN_FILE_SCOPE void *DqnMemBuffer_Allocate(DqnMemBuffer *const buffer, size_t si
} }
else else
{ {
// TODO: Better notifying to user, out of space in buffer OR buffer // TODO: Better notifying to user, out of space in stack OR stack
// is configured such that new blocks are not allowed. // is configured such that new blocks are not allowed.
return NULL; return NULL;
} }
} }
u8 *currPointer = buffer->block->memory + buffer->block->used; u8 *currPointer = stack->block->memory + stack->block->used;
u8 *alignedResult = (u8 *)DQN_ALIGN_POW_N(currPointer, buffer->byteAlign); u8 *alignedResult = (u8 *)DQN_ALIGN_POW_N(currPointer, stack->byteAlign);
size_t alignmentOffset = (size_t)(alignedResult - currPointer); size_t alignmentOffset = (size_t)(alignedResult - currPointer);
// NOTE(doyle): Since all buffers can't change alignment once they've been // NOTE(doyle): Since all stack can't change alignment once they've been
// initialised and that the base memory ptr is already aligned, then all // initialised and that the base memory ptr is already aligned, then all
// subsequent allocations should also be aligned automatically. // subsequent allocations should also be aligned automatically.
// TODO(doyle): In the future, do we want to allow arbitrary alignment PER // TODO(doyle): In the future, do we want to allow arbitrary alignment PER
// allocation, not per MemBuffer? // allocation, not per MemStack?
DQN_ASSERT(alignmentOffset == 0); DQN_ASSERT(alignmentOffset == 0);
void *result = alignedResult; void *result = alignedResult;
buffer->block->used += (alignedSize + alignmentOffset); stack->block->used += (alignedSize + alignmentOffset);
DQN_ASSERT(buffer->block->used <= buffer->block->size); DQN_ASSERT(stack->block->used <= stack->block->size);
return result; return result;
} }
DQN_FILE_SCOPE bool DqnMemBuffer_FreeBufferBlock(DqnMemBuffer *const buffer, DQN_FILE_SCOPE bool DqnMemStack_Pop(DqnMemStack *const stack, void *ptr, size_t size)
DqnMemBufferBlock *block)
{ {
if (!buffer || !block || !buffer->block) return false; if (!stack || !stack->block) return false;
if (buffer->flags & DqnMemBufferFlag_IsFixedMemoryFromUser) return false;
DqnMemBufferBlock **blockPtr = &buffer->block; u8 *currPtr = stack->block->memory + stack->block->used;
DQN_ASSERT((u8 *)ptr >= stack->block->memory && ptr < currPtr);
size_t calcSize = (size_t)currPtr - (size_t)ptr;
DQN_ASSERT(calcSize == size);
stack->block->used -= size;
return true;
}
DQN_FILE_SCOPE bool DqnMemStack_FreeStackBlock(DqnMemStack *const stack, DqnMemStackBlock *block)
{
if (!stack || !block || !stack->block) return false;
if (stack->flags & DqnMemStackFlag_IsFixedMemoryFromUser) return false;
DqnMemStackBlock **blockPtr = &stack->block;
while (*blockPtr && (*blockPtr) != block) while (*blockPtr && (*blockPtr) != block)
blockPtr = &((*blockPtr)->prevBlock); blockPtr = &((*blockPtr)->prevBlock);
if (*blockPtr) if (*blockPtr)
{ {
DqnMemBufferBlock *blockToFree = *blockPtr; DqnMemStackBlock *blockToFree = *blockPtr;
(*blockPtr) = blockToFree->prevBlock; (*blockPtr) = blockToFree->prevBlock;
DqnMem_Free(blockToFree); DqnMem_Free(blockToFree);
// No more blocks, then last block has been freed // No more blocks, then last block has been freed
if (!buffer->block) DQN_ASSERT(buffer->tempBufferCount == 0); if (!stack->block) DQN_ASSERT(stack->tempStackCount == 0);
return true; return true;
} }
@ -1513,73 +1539,72 @@ DQN_FILE_SCOPE bool DqnMemBuffer_FreeBufferBlock(DqnMemBuffer *const buffer,
} }
DQN_FILE_SCOPE bool DQN_FILE_SCOPE bool
DqnMemBuffer_FreeLastBlock(DqnMemBuffer *const buffer) DqnMemStack_FreeLastBlock(DqnMemStack *const stack)
{ {
bool result = DqnMemBuffer_FreeBufferBlock(buffer, buffer->block); bool result = DqnMemStack_FreeStackBlock(stack, stack->block);
return result; return result;
} }
DQN_FILE_SCOPE void DqnMemBuffer_Free(DqnMemBuffer *buffer) DQN_FILE_SCOPE void DqnMemStack_Free(DqnMemStack *stack)
{ {
if (!buffer) return; if (!stack) return;
// NOTE(doyle): User is in charge of freeing this memory, so all we need to // NOTE(doyle): User is in charge of freeing this memory, so all we need to
// do is clear the block. // do is clear the block.
if (buffer->flags & DqnMemBufferFlag_IsFixedMemoryFromUser) if (stack->flags & DqnMemStackFlag_IsFixedMemoryFromUser)
{ {
DQN_ASSERT(!buffer->block->prevBlock); DQN_ASSERT(!stack->block->prevBlock);
DqnMemBuffer_ClearCurrBlock(buffer, false); DqnMemStack_ClearCurrBlock(stack, false);
return; return;
} }
while (buffer->block) while (stack->block)
DqnMemBuffer_FreeLastBlock(buffer); DqnMemStack_FreeLastBlock(stack);
// After a buffer is free, we reset the not expandable flag so that if we // After a stack is free, we reset the not expandable flag so that if we
// allocate on an empty buffer it still works. // allocate on an empty stack it still works.
buffer->flags &= ~DqnMemBufferFlag_IsNotExpandable; stack->flags &= ~DqnMemStackFlag_IsNotExpandable;
} }
DQN_FILE_SCOPE void DqnMemBuffer_ClearCurrBlock(DqnMemBuffer *const buffer, DQN_FILE_SCOPE void DqnMemStack_ClearCurrBlock(DqnMemStack *const stack,
const bool zeroClear) const bool zeroClear)
{ {
if (!buffer) return; if (!stack) return;
if (buffer->block) if (stack->block)
{ {
buffer->block->used = 0; stack->block->used = 0;
if (zeroClear) if (zeroClear)
{ {
DqnMem_Clear(buffer->block->memory, 0, DqnMem_Clear(stack->block->memory, 0, stack->block->size);
buffer->block->size);
} }
} }
} }
DQN_FILE_SCOPE DqnTempBuffer DQN_FILE_SCOPE DqnTempMemStack
DqnMemBuffer_BeginTempRegion(DqnMemBuffer *const buffer) DqnMemStack_BeginTempRegion(DqnMemStack *const stack)
{ {
DqnTempBuffer result = {}; DqnTempMemStack result = {};
result.buffer = buffer; result.stack = stack;
result.startingBlock = buffer->block; result.startingBlock = stack->block;
result.used = buffer->block->used; result.used = stack->block->used;
buffer->tempBufferCount++; stack->tempStackCount++;
return result; return result;
} }
DQN_FILE_SCOPE void DqnMemBuffer_EndTempRegion(DqnTempBuffer tempBuffer) DQN_FILE_SCOPE void DqnMemStack_EndTempRegion(DqnTempMemStack tempStack)
{ {
DqnMemBuffer *buffer = tempBuffer.buffer; DqnMemStack *stack = tempStack.stack;
while (buffer->block != tempBuffer.startingBlock) while (stack->block != tempStack.startingBlock)
DqnMemBuffer_FreeLastBlock(buffer); DqnMemStack_FreeLastBlock(stack);
if (buffer->block) if (stack->block)
{ {
DQN_ASSERT(buffer->block->used >= tempBuffer.used); DQN_ASSERT(stack->block->used >= tempStack.used);
buffer->block->used = tempBuffer.used; stack->block->used = tempStack.used;
DQN_ASSERT(buffer->tempBufferCount >= 0); DQN_ASSERT(stack->tempStackCount >= 0);
} }
buffer->tempBufferCount--; stack->tempStackCount--;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -2096,6 +2121,18 @@ DQN_FILE_SCOPE DqnV3 DqnV3_Cross(DqnV3 a, DqnV3 b)
return result; return result;
} }
DQN_FILE_SCOPE DqnV3i DqnV3i_3i(i32 x, i32 y, i32 z)
{
DqnV3i result = {x, y, z};
return result;
}
DQN_FILE_SCOPE DqnV3i DqnV3i_3f(f32 x, f32 y, f32 z)
{
DqnV3i result = {(i32)x, (i32)y, (i32)z};
return result;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Vec4 // Vec4
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -1037,82 +1037,82 @@ void FileTest()
printf("FileTest(): Completed successfully\n"); printf("FileTest(): Completed successfully\n");
} }
void MemBufferTest() void MemStackTest()
{ {
// Test over allocation, alignments, temp regions // Test over allocation, alignments, temp regions
{ {
size_t allocSize = DQN_KILOBYTE(1); size_t allocSize = DQN_KILOBYTE(1);
DqnMemBuffer buffer = {}; DqnMemStack stack = {};
const u32 ALIGNMENT = 4; const u32 ALIGNMENT = 4;
DqnMemBuffer_Init(&buffer, allocSize, false, ALIGNMENT); DqnMemStack_Init(&stack, allocSize, false, ALIGNMENT);
DQN_ASSERT(buffer.block && buffer.block->memory); DQN_ASSERT(stack.block && stack.block->memory);
DQN_ASSERT(buffer.block->size == allocSize); DQN_ASSERT(stack.block->size == allocSize);
DQN_ASSERT(buffer.block->used == 0); DQN_ASSERT(stack.block->used == 0);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
// Alocate A // Alocate A
size_t sizeA = (size_t)(allocSize * 0.5f); size_t sizeA = (size_t)(allocSize * 0.5f);
void *resultA = DqnMemBuffer_Allocate(&buffer, sizeA); void *resultA = DqnMemStack_Allocate(&stack, sizeA);
u64 resultAddrA = *((u64 *)resultA); u64 resultAddrA = *((u64 *)resultA);
DQN_ASSERT(resultAddrA % ALIGNMENT == 0); DQN_ASSERT(resultAddrA % ALIGNMENT == 0);
DQN_ASSERT(buffer.block && buffer.block->memory); DQN_ASSERT(stack.block && stack.block->memory);
DQN_ASSERT(buffer.block->size == allocSize); DQN_ASSERT(stack.block->size == allocSize);
DQN_ASSERT(buffer.block->used >= sizeA + 0 && DQN_ASSERT(stack.block->used >= sizeA + 0 &&
buffer.block->used <= sizeA + 3); stack.block->used <= sizeA + 3);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
DQN_ASSERT(resultA); DQN_ASSERT(resultA);
u8 *ptrA = (u8 *)resultA; u8 *ptrA = (u8 *)resultA;
for (u32 i = 0; i < sizeA; i++) for (u32 i = 0; i < sizeA; i++)
ptrA[i] = 1; ptrA[i] = 1;
DqnMemBufferBlock *blockA = buffer.block; DqnMemStackBlock *blockA = stack.block;
// Alocate B // Alocate B
size_t sizeB = (size_t)(allocSize * 2.0f); size_t sizeB = (size_t)(allocSize * 2.0f);
void *resultB = DqnMemBuffer_Allocate(&buffer, sizeB); void *resultB = DqnMemStack_Allocate(&stack, sizeB);
u64 resultAddrB = *((u64 *)resultB); u64 resultAddrB = *((u64 *)resultB);
DQN_ASSERT(resultAddrB % ALIGNMENT == 0); DQN_ASSERT(resultAddrB % ALIGNMENT == 0);
DQN_ASSERT(buffer.block && buffer.block->memory); DQN_ASSERT(stack.block && stack.block->memory);
DQN_ASSERT(buffer.block->size == DQN_KILOBYTE(2)); DQN_ASSERT(stack.block->size == DQN_KILOBYTE(2));
// Since we alignment the pointers we return they can be within 0-3 // Since we alignment the pointers we return they can be within 0-3
// bytes of what we expect and since this is in a new block as well used // bytes of what we expect and since this is in a new block as well used
// will reflect just this allocation. // will reflect just this allocation.
DQN_ASSERT(buffer.block->used >= sizeB + 0 && DQN_ASSERT(stack.block->used >= sizeB + 0 &&
buffer.block->used <= sizeB + 3); stack.block->used <= sizeB + 3);
DQN_ASSERT(resultB); DQN_ASSERT(resultB);
u8 *ptrB = (u8 *)resultB; u8 *ptrB = (u8 *)resultB;
for (u32 i = 0; i < sizeB; i++) for (u32 i = 0; i < sizeB; i++)
ptrB[i] = 2; ptrB[i] = 2;
// Check that a new block was created since there wasn't enough space // Check that a new block was created since there wasn't enough space
DQN_ASSERT(buffer.block->prevBlock == blockA); DQN_ASSERT(stack.block->prevBlock == blockA);
DQN_ASSERT(buffer.block != blockA); DQN_ASSERT(stack.block != blockA);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
DQN_ASSERT(blockA->used == sizeA); DQN_ASSERT(blockA->used == sizeA);
DqnMemBufferBlock *blockB = buffer.block; DqnMemStackBlock *blockB = stack.block;
// Check temp regions work // Check temp regions work
DqnTempBuffer tempBuffer = DqnMemBuffer_BeginTempRegion(&buffer); DqnTempMemStack tempBuffer = DqnMemStack_BeginTempRegion(&stack);
size_t sizeC = 1024 + 1; size_t sizeC = 1024 + 1;
void *resultC = DqnMemBuffer_Allocate(tempBuffer.buffer, sizeC); void *resultC = DqnMemStack_Allocate(tempBuffer.stack, sizeC);
u64 resultAddrC = *((u64 *)resultC); u64 resultAddrC = *((u64 *)resultC);
DQN_ASSERT(resultAddrC % ALIGNMENT == 0); DQN_ASSERT(resultAddrC % ALIGNMENT == 0);
DQN_ASSERT(buffer.block != blockB && buffer.block != blockA); DQN_ASSERT(stack.block != blockB && stack.block != blockA);
DQN_ASSERT(buffer.block->used >= sizeC + 0 && DQN_ASSERT(stack.block->used >= sizeC + 0 &&
buffer.block->used <= sizeC + 3); stack.block->used <= sizeC + 3);
DQN_ASSERT(buffer.tempBufferCount == 1); DQN_ASSERT(stack.tempStackCount == 1);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
// NOTE: Allocation should be aligned to 4 byte boundary // NOTE: Allocation should be aligned to 4 byte boundary
DQN_ASSERT(tempBuffer.buffer->block->size == 2048); DQN_ASSERT(tempBuffer.stack->block->size == 2048);
u8 *ptrC = (u8 *)resultC; u8 *ptrC = (u8 *)resultC;
for (u32 i = 0; i < sizeC; i++) for (u32 i = 0; i < sizeC; i++)
ptrC[i] = 3; ptrC[i] = 3;
// Check that a new block was created since there wasn't enough space // Check that a new block was created since there wasn't enough space
DQN_ASSERT(buffer.block->prevBlock == blockB); DQN_ASSERT(stack.block->prevBlock == blockB);
DQN_ASSERT(buffer.block != blockB); DQN_ASSERT(stack.block != blockB);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
for (u32 i = 0; i < sizeA; i++) for (u32 i = 0; i < sizeA; i++)
DQN_ASSERT(ptrA[i] == 1); DQN_ASSERT(ptrA[i] == 1);
@ -1121,119 +1121,119 @@ void MemBufferTest()
for (u32 i = 0; i < sizeC; i++) for (u32 i = 0; i < sizeC; i++)
DQN_ASSERT(ptrC[i] == 3); DQN_ASSERT(ptrC[i] == 3);
// End temp region which should revert back to 2 linked buffers, A and B // End temp region which should revert back to 2 linked stacks, A and B
DqnMemBuffer_EndTempRegion(tempBuffer); DqnMemStack_EndTempRegion(tempBuffer);
DQN_ASSERT(buffer.block && buffer.block->memory); DQN_ASSERT(stack.block && stack.block->memory);
DQN_ASSERT(buffer.block->size == sizeB); DQN_ASSERT(stack.block->size == sizeB);
DQN_ASSERT(buffer.block->used >= sizeB + 0 && DQN_ASSERT(stack.block->used >= sizeB + 0 &&
buffer.block->used <= sizeB + 3); stack.block->used <= sizeB + 3);
DQN_ASSERT(buffer.tempBufferCount == 0); DQN_ASSERT(stack.tempStackCount == 0);
DQN_ASSERT(resultB); DQN_ASSERT(resultB);
DQN_ASSERT(buffer.block->prevBlock == blockA); DQN_ASSERT(stack.block->prevBlock == blockA);
DQN_ASSERT(buffer.block != blockA); DQN_ASSERT(stack.block != blockA);
DQN_ASSERT(blockA->used == sizeA); DQN_ASSERT(blockA->used == sizeA);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
// Release the last linked buffer from the push buffer // Release the last linked stack from the push stack
DqnMemBuffer_FreeLastBlock(&buffer); DqnMemStack_FreeLastBlock(&stack);
// Which should return back to the 1st allocation // Which should return back to the 1st allocation
DQN_ASSERT(buffer.block == blockA); DQN_ASSERT(stack.block == blockA);
DQN_ASSERT(buffer.block->memory); DQN_ASSERT(stack.block->memory);
DQN_ASSERT(buffer.block->size == allocSize); DQN_ASSERT(stack.block->size == allocSize);
DQN_ASSERT(buffer.block->used == sizeA); DQN_ASSERT(stack.block->used == sizeA);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
// Free once more to release buffer A memory // Free once more to release stack A memory
DqnMemBuffer_FreeLastBlock(&buffer); DqnMemStack_FreeLastBlock(&stack);
DQN_ASSERT(!buffer.block); DQN_ASSERT(!stack.block);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
DQN_ASSERT(buffer.tempBufferCount == 0); DQN_ASSERT(stack.tempStackCount == 0);
} }
// Test buffer with fixed memory does not allocate more // Test stack with fixed memory does not allocate more
{ {
u8 memory[DQN_KILOBYTE(1)] = {}; u8 memory[DQN_KILOBYTE(1)] = {};
DqnMemBuffer buffer = {}; DqnMemStack stack = {};
const u32 ALIGNMENT = 4; const u32 ALIGNMENT = 4;
DqnMemBuffer_InitWithFixedMem(&buffer, memory, DQN_ARRAY_COUNT(memory), DqnMemStack_InitWithFixedMem(&stack, memory, DQN_ARRAY_COUNT(memory),
ALIGNMENT); ALIGNMENT);
DQN_ASSERT(buffer.block && buffer.block->memory); DQN_ASSERT(stack.block && stack.block->memory);
DQN_ASSERT(buffer.block->size == DQN_ASSERT(stack.block->size ==
DQN_ARRAY_COUNT(memory) - sizeof(DqnMemBufferBlock)); DQN_ARRAY_COUNT(memory) - sizeof(DqnMemStackBlock));
DQN_ASSERT(buffer.block->used == 0); DQN_ASSERT(stack.block->used == 0);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
// Allocation larger than stack mem size should fail // Allocation larger than stack mem size should fail
DQN_ASSERT(!DqnMemBuffer_Allocate(&buffer, DQN_ARRAY_COUNT(memory) * 2)); DQN_ASSERT(!DqnMemStack_Allocate(&stack, DQN_ARRAY_COUNT(memory) * 2));
// Check free does nothing // Check free does nothing
DqnMemBuffer_Free(&buffer); DqnMemStack_Free(&stack);
DqnMemBuffer_FreeLastBlock(&buffer); DqnMemStack_FreeLastBlock(&stack);
DQN_ASSERT(buffer.block && buffer.block->memory); DQN_ASSERT(stack.block && stack.block->memory);
DQN_ASSERT(buffer.block->size == DQN_ASSERT(stack.block->size ==
DQN_ARRAY_COUNT(memory) - sizeof(DqnMemBufferBlock)); DQN_ARRAY_COUNT(memory) - sizeof(DqnMemStackBlock));
DQN_ASSERT(buffer.block->used == 0); DQN_ASSERT(stack.block->used == 0);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
} }
// Test buffer with fixed size, allocates once from platform but does not // Test stack with fixed size, allocates once from platform but does not
// grow further // grow further
{ {
size_t allocSize = DQN_KILOBYTE(1); size_t allocSize = DQN_KILOBYTE(1);
DqnMemBuffer buffer = {}; DqnMemStack stack = {};
const u32 ALIGNMENT = 4; const u32 ALIGNMENT = 4;
DqnMemBuffer_InitWithFixedSize(&buffer, allocSize, false, ALIGNMENT); DqnMemStack_InitWithFixedSize(&stack, allocSize, false, ALIGNMENT);
DQN_ASSERT(buffer.block && buffer.block->memory); DQN_ASSERT(stack.block && stack.block->memory);
DQN_ASSERT(buffer.block->size == allocSize); DQN_ASSERT(stack.block->size == allocSize);
DQN_ASSERT(buffer.block->used == 0); DQN_ASSERT(stack.block->used == 0);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
void *result = DqnMemBuffer_Allocate(&buffer, (size_t)(0.5f * allocSize)); void *result = DqnMemStack_Allocate(&stack, (size_t)(0.5f * allocSize));
DQN_ASSERT(result); DQN_ASSERT(result);
// Allocating more should fail // Allocating more should fail
DQN_ASSERT(!DqnMemBuffer_Allocate(&buffer, allocSize)); DQN_ASSERT(!DqnMemStack_Allocate(&stack, allocSize));
// Freeing should work // Freeing should work
DqnMemBuffer_Free(&buffer); DqnMemStack_Free(&stack);
DQN_ASSERT(!buffer.block); DQN_ASSERT(!stack.block);
} }
// Test freeing/clear block and alignment // Test freeing/clear block and alignment
{ {
size_t firstBlockSize = DQN_KILOBYTE(1); size_t firstBlockSize = DQN_KILOBYTE(1);
DqnMemBuffer buffer = {}; DqnMemStack stack = {};
const u32 ALIGNMENT = 16; const u32 ALIGNMENT = 16;
DqnMemBuffer_Init(&buffer, firstBlockSize, false, ALIGNMENT); DqnMemStack_Init(&stack, firstBlockSize, false, ALIGNMENT);
DqnMemBufferBlock *firstBlock = buffer.block; DqnMemStackBlock *firstBlock = stack.block;
u8 *first = NULL; u8 *first = NULL;
{ {
u32 allocate40Bytes = 40; u32 allocate40Bytes = 40;
u8 *data = (u8 *)DqnMemBuffer_Allocate(&buffer, allocate40Bytes); u8 *data = (u8 *)DqnMemStack_Allocate(&stack, allocate40Bytes);
// Test that the allocation got aligned to 16 byte boundary // Test that the allocation got aligned to 16 byte boundary
DQN_ASSERT(data); DQN_ASSERT(data);
DQN_ASSERT(buffer.block->size == firstBlockSize); DQN_ASSERT(stack.block->size == firstBlockSize);
DQN_ASSERT((size_t)data % ALIGNMENT == 0); DQN_ASSERT((size_t)data % ALIGNMENT == 0);
for (u32 i = 0; i < allocate40Bytes; i++) for (u32 i = 0; i < allocate40Bytes; i++)
data[i] = 'a'; data[i] = 'a';
// Clear the block, but don't zero it out // Clear the block, but don't zero it out
DqnMemBuffer_ClearCurrBlock(&buffer, false); DqnMemStack_ClearCurrBlock(&stack, false);
for (u32 i = 0; i < allocate40Bytes; i++) for (u32 i = 0; i < allocate40Bytes; i++)
DQN_ASSERT(data[i] == 'a'); DQN_ASSERT(data[i] == 'a');
// Test clear reverted the use pointer // Test clear reverted the use pointer
DQN_ASSERT(buffer.block->used == 0); DQN_ASSERT(stack.block->used == 0);
DQN_ASSERT(buffer.block->size == firstBlockSize); DQN_ASSERT(stack.block->size == firstBlockSize);
// Reallocate the data // Reallocate the data
data = (u8 *)DqnMemBuffer_Allocate(&buffer, firstBlockSize); data = (u8 *)DqnMemStack_Allocate(&stack, firstBlockSize);
DQN_ASSERT(buffer.block->size == firstBlockSize); DQN_ASSERT(stack.block->size == firstBlockSize);
DQN_ASSERT((size_t)data % ALIGNMENT == 0); DQN_ASSERT((size_t)data % ALIGNMENT == 0);
// Fill with 'b's // Fill with 'b's
@ -1241,19 +1241,19 @@ void MemBufferTest()
data[i] = 'b'; data[i] = 'b';
// Clear block and zero it out // Clear block and zero it out
DqnMemBuffer_ClearCurrBlock(&buffer, true); DqnMemStack_ClearCurrBlock(&stack, true);
for (u32 i = 0; i < firstBlockSize; i++) for (u32 i = 0; i < firstBlockSize; i++)
DQN_ASSERT(data[i] == 0); DQN_ASSERT(data[i] == 0);
// General Check buffer struct contains the values we expect from // General Check stack struct contains the values we expect from
// initialisation // initialisation
DQN_ASSERT(buffer.flags == 0); DQN_ASSERT(stack.flags == 0);
DQN_ASSERT(buffer.tempBufferCount == 0); DQN_ASSERT(stack.tempStackCount == 0);
DQN_ASSERT(buffer.byteAlign == ALIGNMENT); DQN_ASSERT(stack.byteAlign == ALIGNMENT);
DQN_ASSERT(buffer.block->size == firstBlockSize); DQN_ASSERT(stack.block->size == firstBlockSize);
// Write out data to current block // Write out data to current block
data = (u8 *)DqnMemBuffer_Allocate(&buffer, firstBlockSize); data = (u8 *)DqnMemStack_Allocate(&stack, firstBlockSize);
for (u32 i = 0; i < firstBlockSize; i++) for (u32 i = 0; i < firstBlockSize; i++)
data[i] = 'c'; data[i] = 'c';
@ -1262,20 +1262,20 @@ void MemBufferTest()
// Force it to allocate three new blocks and write out data to each // Force it to allocate three new blocks and write out data to each
size_t secondBlockSize = DQN_KILOBYTE(2); size_t secondBlockSize = DQN_KILOBYTE(2);
u8 *second = (u8 *)DqnMemBuffer_Allocate(&buffer, secondBlockSize); u8 *second = (u8 *)DqnMemStack_Allocate(&stack, secondBlockSize);
DqnMemBufferBlock *secondBlock = buffer.block; DqnMemStackBlock *secondBlock = stack.block;
for (u32 i = 0; i < secondBlockSize; i++) for (u32 i = 0; i < secondBlockSize; i++)
second[i] = 'd'; second[i] = 'd';
size_t thirdBlockSize = DQN_KILOBYTE(3); size_t thirdBlockSize = DQN_KILOBYTE(3);
u8 *third = (u8 *)DqnMemBuffer_Allocate(&buffer, thirdBlockSize); u8 *third = (u8 *)DqnMemStack_Allocate(&stack, thirdBlockSize);
DqnMemBufferBlock *thirdBlock = buffer.block; DqnMemStackBlock *thirdBlock = stack.block;
for (u32 i = 0; i < thirdBlockSize; i++) for (u32 i = 0; i < thirdBlockSize; i++)
third[i] = 'e'; third[i] = 'e';
size_t fourthBlockSize = DQN_KILOBYTE(4); size_t fourthBlockSize = DQN_KILOBYTE(4);
u8 *fourth = (u8 *)DqnMemBuffer_Allocate(&buffer, fourthBlockSize); u8 *fourth = (u8 *)DqnMemStack_Allocate(&stack, fourthBlockSize);
DqnMemBufferBlock *fourthBlock = buffer.block; DqnMemStackBlock *fourthBlock = stack.block;
for (u32 i = 0; i < fourthBlockSize; i++) for (u32 i = 0; i < fourthBlockSize; i++)
fourth[i] = 'f'; fourth[i] = 'f';
@ -1288,11 +1288,11 @@ void MemBufferTest()
// NOTE: Making blocks manually is not really recommended .. // NOTE: Making blocks manually is not really recommended ..
// Try and free an invalid block by mocking a fake block // Try and free an invalid block by mocking a fake block
u8 fakeBlockMem[DQN_KILOBYTE(3)] = {}; u8 fakeBlockMem[DQN_KILOBYTE(3)] = {};
DqnMemBufferBlock fakeBlock = {}; DqnMemStackBlock fakeBlock = {};
fakeBlock.memory = fakeBlockMem; fakeBlock.memory = fakeBlockMem;
fakeBlock.size = DQN_ARRAY_COUNT(fakeBlockMem); fakeBlock.size = DQN_ARRAY_COUNT(fakeBlockMem);
fakeBlock.used = 0; fakeBlock.used = 0;
DQN_ASSERT(!DqnMemBuffer_FreeBufferBlock(&buffer, &fakeBlock)); DQN_ASSERT(!DqnMemStack_FreeStackBlock(&stack, &fakeBlock));
//Ensure that the actual blocks are still valid and freeing did nothing //Ensure that the actual blocks are still valid and freeing did nothing
DQN_ASSERT(firstBlock->size == firstBlockSize); DQN_ASSERT(firstBlock->size == firstBlockSize);
@ -1324,7 +1324,7 @@ void MemBufferTest()
DQN_ASSERT(fourth[i] == 'f'); DQN_ASSERT(fourth[i] == 'f');
// Free the first block // Free the first block
DqnMemBuffer_FreeBufferBlock(&buffer, firstBlock); DqnMemStack_FreeStackBlock(&stack, firstBlock);
// Revalidate state // Revalidate state
DQN_ASSERT(secondBlock->size == secondBlockSize); DQN_ASSERT(secondBlock->size == secondBlockSize);
@ -1350,7 +1350,7 @@ void MemBufferTest()
DQN_ASSERT(fourth[i] == 'f'); DQN_ASSERT(fourth[i] == 'f');
// Free the third block // Free the third block
DqnMemBuffer_FreeBufferBlock(&buffer, thirdBlock); DqnMemStack_FreeStackBlock(&stack, thirdBlock);
// Revalidate state // Revalidate state
DQN_ASSERT(secondBlock->size == secondBlockSize); DQN_ASSERT(secondBlock->size == secondBlockSize);
@ -1370,7 +1370,7 @@ void MemBufferTest()
DQN_ASSERT(fourth[i] == 'f'); DQN_ASSERT(fourth[i] == 'f');
// Free the second block // Free the second block
DqnMemBuffer_FreeBufferBlock(&buffer, secondBlock); DqnMemStack_FreeStackBlock(&stack, secondBlock);
// Revalidate state // Revalidate state
DQN_ASSERT(fourthBlock->size == fourthBlockSize); DQN_ASSERT(fourthBlock->size == fourthBlockSize);
@ -1380,9 +1380,22 @@ void MemBufferTest()
for (u32 i = 0; i < fourthBlockSize; i++) for (u32 i = 0; i < fourthBlockSize; i++)
DQN_ASSERT(fourth[i] == 'f'); DQN_ASSERT(fourth[i] == 'f');
// Free the buffer // Free the stack
DqnMemBuffer_Free(&buffer); DqnMemStack_Free(&stack);
DQN_ASSERT(!buffer.block); DQN_ASSERT(!stack.block);
}
// Test pop
{
DqnMemStack stack = {};
DqnMemStack_Init(&stack, DQN_KILOBYTE(1), true);
size_t allocSize = 512;
void *alloc = DqnMemStack_Allocate(&stack, allocSize);
DQN_ASSERT(stack.block->used == allocSize);
DQN_ASSERT(DqnMemStack_Pop(&stack, alloc, allocSize));
DQN_ASSERT(stack.block->used == 0);
} }
} }
@ -1395,7 +1408,7 @@ int main(void)
OtherTest(); OtherTest();
ArrayTest(); ArrayTest();
FileTest(); FileTest();
MemBufferTest(); MemStackTest();
printf("\nPress 'Enter' Key to Exit\n"); printf("\nPress 'Enter' Key to Exit\n");
getchar(); getchar();