Cleanup tests to be more structured

This commit is contained in:
Doyle Thai 2018-02-02 23:26:05 +11:00
parent 15a4c6ad22
commit 1fc49c4fde
3 changed files with 455 additions and 318 deletions

View File

@ -31,7 +31,7 @@ REM Zi enables debug data, Z7 combines the debug files into one.
REM W4 warning level 4 REM W4 warning level 4
REM WX treat warnings as errors REM WX treat warnings as errors
REM wd4201 ignore: nonstandard extension used: nameless struct/union REM wd4201 ignore: nonstandard extension used: nameless struct/union
set CompileFlags=-EHsc -GR- -Oi -MT -Z7 -W4 -WX -wd4201 -FC -Od -wd4127 set CompileFlags=-EHsc -GR- -Oi -MT -Z7 -W4 -WX -wd4201 -FC -O2 -wd4127
REM Include directories REM Include directories
set IncludeFlags= set IncludeFlags=

37
dqn.h
View File

@ -357,6 +357,8 @@ struct DqnAllocatorMetadata
static u32 const ALIGNMENT_SIZE = sizeof(u8); static u32 const ALIGNMENT_SIZE = sizeof(u8);
static u32 const ALLOC_AMOUNT_SIZE = sizeof(usize); static u32 const ALLOC_AMOUNT_SIZE = sizeof(usize);
DqnArray<u8 *> allocations; // When BoundsGuard is enabled, tracks all allocations.
void Init (bool boundsGuard); void Init (bool boundsGuard);
void Free (); void Free ();
void AddAllocation (u8 *ptr) { DQN_ASSERT(allocations.Push(ptr) != nullptr); } void AddAllocation (u8 *ptr) { DQN_ASSERT(allocations.Push(ptr) != nullptr); }
@ -374,8 +376,6 @@ struct DqnAllocatorMetadata
usize *PtrToAllocAmount (u8 const *ptr) const; usize *PtrToAllocAmount (u8 const *ptr) const;
usize GetAllocationSize (usize size, u8 alignment) const { return GetAllocHeadSize() + size + GetAllocTailSize() + (alignment - 1); } usize GetAllocationSize (usize size, u8 alignment) const { return GetAllocHeadSize() + size + GetAllocTailSize() + (alignment - 1); }
DqnArray<u8 *> allocations;
private: private:
u32 boundsGuardSize; // sizeof(GUARD_VALUE) OR 0 if BoundsGuard is disabled. u32 boundsGuardSize; // sizeof(GUARD_VALUE) OR 0 if BoundsGuard is disabled.
u32 allocHeadSize; // Bounds Guard Size + Offset To Src Size + Alloc Amount Size u32 allocHeadSize; // Bounds Guard Size + Offset To Src Size + Alloc Amount Size
@ -393,18 +393,9 @@ private:
// locality against optimal space usage. // locality against optimal space usage.
// How To Use: // How To Use:
// 1. Create a DqnMemStack struct and pass it into an initialisation function // DqnMemStack stack = {};
// - InitWithFixedMem() allows you to pass in your own memory which is // stack.Init(DQN_MEGABYTE(1), true, DqnMemStack::Flag::BoundsGuard);
// converted to a memory block. This disables dynamic allocation. // u8 *data = stack.Push(128);
// NOTE: Space is reserved in the given memory for MemStackBlock metadata.
// - InitWithFixedSize() allows you to to disable dynamic allocations and
// sub-allocate from the initial MemStack allocation size only.
// 2. Use DqnMemStack_Push(..) to allocate memory for use.
// - "Freeing" memory is dealt by creating temporary MemStacks or using the
// BeginTempRegion and EndTempRegion functions. Specifically freeing
// individual items is typically not generalisable in this scheme.
struct DqnMemStack struct DqnMemStack
{ {
@ -3592,6 +3583,9 @@ void DqnMemStack::Free()
{ {
while (this->block) while (this->block)
this->FreeLastBlock(); this->FreeLastBlock();
if (Dqn_BitIsSet(this->flags, Flag::BoundsGuard))
this->metadata.allocations.Free();
} }
bool DqnMemStack::FreeMemBlock(DqnMemStack::Block *memBlock) bool DqnMemStack::FreeMemBlock(DqnMemStack::Block *memBlock)
@ -3613,6 +3607,21 @@ bool DqnMemStack::FreeMemBlock(DqnMemStack::Block *memBlock)
DqnMemStack::Block *blockToFree = *blockPtr; DqnMemStack::Block *blockToFree = *blockPtr;
(*blockPtr) = blockToFree->prevBlock; (*blockPtr) = blockToFree->prevBlock;
if (Dqn_BitIsSet(this->flags, Flag::BoundsGuard))
{
u8 const *blockStart = blockToFree->memory;
u8 const *blockEnd = blockToFree->memory + blockToFree->size;
for (auto index = 0; index < this->metadata.allocations.count; index++)
{
u8 *ptr = this->metadata.allocations.data[index];
if (ptr >= blockStart && ptr <= blockEnd)
{
this->metadata.allocations.RemoveStable(index);
index--;
}
}
}
usize realSize = blockToFree->size + sizeof(DqnMemStack::Block); usize realSize = blockToFree->size + sizeof(DqnMemStack::Block);
this->memAPI->Free(blockToFree, realSize); this->memAPI->Free(blockToFree, realSize);

File diff suppressed because it is too large Load Diff