Add tests for FrameAllocator and DataChunker

This commit is contained in:
James Urquhart 2024-02-05 22:53:09 +00:00
parent 3781c7fae5
commit 7332dd6643
5 changed files with 597 additions and 9 deletions

View file

@ -39,6 +39,8 @@ public:
ChunkSize = 16384
};
typedef T AlignmentType;
struct alignas(uintptr_t) DataBlock : public AlignedBufferAllocator<T>
{
DataBlock* mNext;
@ -129,6 +131,19 @@ public:
AssertFatal(mChunkHead == NULL, "Tried setting AFTER init");
mChunkSize = size;
}
bool isManagedByChunker(void* ptr) const
{
U8* chkPtr = (U8*)ptr;
for (DataBlock* itr = mChunkHead; itr; itr = itr->mNext)
{
const U8* blockStart = (U8*)itr->getAlignedBuffer();
const U8* blockEnd = (U8*)itr->getAlignedBufferEnd();
if (chkPtr >= blockStart && chkPtr < blockEnd)
return true;
}
return false;
}
};
class DataChunker : public BaseDataChunker<uintptr_t>
@ -166,6 +181,8 @@ public:
class MultiTypedChunker : private BaseDataChunker<uintptr_t>
{
public:
typedef uintptr_t AlignmentType;
MultiTypedChunker(dsize_t size = BaseDataChunker<uintptr_t>::ChunkSize) : BaseDataChunker<uintptr_t>(std::max<uintptr_t>(sizeof(uintptr_t), size))
{
}
@ -195,7 +212,7 @@ template<class T> struct ChunkerFreeClassList
mNextList = NULL;
}
bool isEmpty()
bool isEmpty() const
{
return mNextList == NULL;
}
@ -248,7 +265,15 @@ public:
void freeBlocks(bool keepOne = false)
{
BaseDataChunker<T>::freeBlocks(keepOne);
mFreeListHead.reset();
}
inline bool isManagedByChunker(void* ptr) const
{
return BaseDataChunker<T>::isManagedByChunker(ptr);
}
inline ChunkerFreeClassList<T>& getFreeListHead() { return mFreeListHead; }
};
/// Implements a chunker which uses the data of another BaseDataChunker
@ -390,6 +415,8 @@ public:
default:
break;
}
item.ptr = NULL;
}
void freeBlocks(bool keepOne = false)
@ -398,4 +425,8 @@ public:
mT2.freeBlocks(keepOne);
mT3.freeBlocks(keepOne);
}
inline ClassChunker<K1>& getT1Chunker() { return mT1; }
inline ClassChunker<K2>& getT2Chunker() { return mT2; }
inline ClassChunker<K3>& getT3Chunker() { return mT3; }
};

View file

@ -29,15 +29,18 @@ thread_local FrameAllocator::FrameAllocatorType FrameAllocator::smMainInstance
thread_local dsize_t FrameAllocator::smAllocatedBytes;
#endif
#if defined(TORQUE_DEBUG)
U32 FrameAllocator::smMaxFrameAllocation;
dsize_t FrameAllocator::smMaxFrameAllocation;
DefineEngineFunction(getMaxFrameAllocation, S32, (), , "")
U32 FrameAllocator::getMaxFrameAllocation()
{
return (S32)FrameAllocator::smMaxFrameAllocation;
}
#if defined(TORQUE_DEBUG)
DefineEngineFunction(getMaxFrameAllocation, S32, (), , "")
{
return (S32)FrameAllocator::getMaxFrameAllocation();
}
#endif

View file

@ -103,11 +103,21 @@ public:
return (U32)(numBytes / sizeof(T));
}
static inline U32 calcRequiredPaddedByteSize(const dsize_t numBytes)
{
return calcRequiredElementSize(numBytes) * sizeof(T);
}
inline T* getAlignedBuffer() const
{
return mBuffer;
}
inline T* getAlignedBufferEnd() const
{
return mBuffer + mHighWaterMark;
}
inline U32 getPosition() const
{
return mWaterMark;
@ -153,7 +163,7 @@ public:
class FrameAllocator
{
public:
static dsize_t smMaxFrameAllocation;
static U32 smMaxFrameAllocation;
#ifdef TORQUE_MEM_DEBUG
static thread_local dsize_t smAllocatedBytes;
#endif
@ -220,6 +230,8 @@ public:
return smMainInstance.getSizeBytes();
}
static U32 getMaxFrameAllocation();
static thread_local FrameAllocatorType smMainInstance;
};
@ -253,7 +265,7 @@ public:
FrameAllocator::setWaterMark(mMarker);
}
void* alloc(const U32 allocSize) const
void* alloc(const U32 allocSize)
{
return FrameAllocator::alloc(allocSize);
}
@ -336,7 +348,7 @@ public:
const T& operator *() const { return *mMemory; }
T** operator &() { return &mMemory; }
const T** operator &() const { return &mMemory; }
T* const * operator &() const { return &mMemory; }
operator T* () { return mMemory; }
operator const T* () const { return mMemory; }