| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #pragma once
- #include <stack>
- #include <assert.h>
- #include "CmThreadDefines.h"
- namespace CamelotFramework
- {
- template <int BlockCapacity = 1024 * 1024>
- class MemStackInternal
- {
- private:
- class MemBlock
- {
- public:
- MemBlock(UINT32 size)
- :mData(nullptr), mFreePtr(0), mSize(size)
- {
- mData = static_cast<UINT8*>(cm_alloc(mSize));
- }
- ~MemBlock()
- {
- cm_free(mData);
- }
- UINT8* alloc(UINT8 amount)
- {
- UINT8* freePtr = &mData[mFreePtr];
- mFreePtr += amount;
- return freePtr;
- }
- void dealloc(UINT8* data, UINT8 amount)
- {
- mFreePtr -= amount;
- assert((&mData[mFreePtr]) == data && "Out of order stack deallocation detected. Deallocations need to happen in order opposite of allocations.");
- }
- UINT8* mData;
- UINT32 mFreePtr;
- UINT32 mSize;
- };
- public:
- MemStackInternal()
- { }
- ~MemStackInternal()
- {
- assert(mBlocks.size() == 0 && "Not all blocks were released before shutting down the stack allocator.");
- while(!mBlocks.empty())
- {
- MemBlock* curPtr = mBlocks.top();
- mBlocks.pop();
- cm_delete(curPtr);
- }
- }
- UINT8* alloc(UINT32 amount)
- {
- MemBlock* topBlock;
- if(mBlocks.size() == 0)
- topBlock = allocNewBlock(amount);
- else
- topBlock = mBlocks.top();
- mAllocSizes.push(amount);
- UINT32 freeMem = topBlock->mSize - topBlock->mFreePtr;
- if(amount <= freeMem)
- return topBlock->alloc(amount);
- MemBlock* newBlock = allocNewBlock(amount);
- return newBlock->alloc(amount);
- }
- void dealloc(UINT8* data)
- {
- assert(mAllocSizes.size() > 0 && "Out of order stack deallocation detected. Deallocations need to happen in order opposite of allocations.");
- UINT32 amount = mAllocSizes.top();
- mAllocSizes.pop();
- MemBlock* topBlock = mBlocks.top();
- topBlock->dealloc(data, amount);
- if(topBlock->mFreePtr == 0)
- {
- cm_delete(topBlock);
- mBlocks.pop();
- }
- }
- private:
- std::stack<MemBlock*> mBlocks;
- std::stack<UINT32> mAllocSizes;
- MemBlock* allocNewBlock(UINT32 wantedSize)
- {
- UINT32 blockSize = BlockCapacity;
- if(wantedSize > blockSize)
- blockSize = wantedSize;
- MemBlock* newBlock = cm_new<MemBlock>(blockSize);
- mBlocks.push(newBlock);
- return newBlock;
- }
- };
- /**
- * @brief Fastest, but also most limiting type of allocator. All deallocations
- * must happen in opposite order from allocations.
- *
- * @note It's mostly useful when you need to allocate something temporarily on the heap,
- * usually something that gets allocated and freed within the same function.
- *
- * Each allocation comes with a pretty hefty 4 byte memory overhead, so don't use it for small allocations.
- *
- * Operations done on a single heap are thread safe. Multiple threads are not allowed to access a heap that wasn't
- * created for them.
- *
- * @tparam BlockCapacity Minimum size of a block. Larger blocks mean less memory allocations, but also potentially
- * more wasted memory. If an allocation requests more bytes than BlockCapacity, first largest multiple is
- * used instead.
- * @tparam VectorAligned If true, all allocations will be aligned to 16bit boundaries.
- */
- class CM_UTILITY_EXPORT MemStack
- {
- public:
- /**
- * @brief Sets up the heap you can later use with alloc/dealloc calls. It is most common to have one heap
- * per thread.
- *
- * @param heapId Unique heap ID. Each heap can only be used from one thread, it cannot be shared.
- * You cannot have more than 256 heaps.
- */
- static void setupHeap(UINT8 heapId);
- static UINT8* alloc(UINT32 numBytes, UINT32 heapId);
- static void deallocLast(UINT8* data, UINT32 heapId);
- private:
- static MemStackInternal<1024 * 1024> mStacks[256];
- #if CM_DEBUG_MODE
- static CM_THREAD_ID_TYPE mThreadIds[256];
- #endif
- };
- CM_UTILITY_EXPORT inline UINT8* stackAlloc(UINT32 numBytes, UINT32 heapId);
- template<class T>
- T* stackAlloc(UINT32 heapId)
- {
- return (T*)MemStack::alloc(sizeof(T), heapId);
- }
- template<class T>
- T* stackAllocN(UINT32 count, UINT32 heapId)
- {
- return (T*)MemStack::alloc(sizeof(T) * count, heapId);
- }
- template<class T>
- T* stackConstructN(UINT32 count, UINT32 heapId)
- {
- T* data = stackAllocN<T>(count, heapId);
- for(unsigned int i = 0; i < count; i++)
- new ((void*)&data[i]) T;
- return data;
- }
- template<class T>
- void stackDestruct(T* data, UINT32 heapId)
- {
- data->~T();
- MemStack::deallocLast((UINT8*)data, heapId);
- }
- template<class T>
- void stackDestructN(T* data, UINT32 count, UINT32 heapId)
- {
- for(unsigned int i = 0; i < count; i++)
- data[i].~T();
- MemStack::deallocLast((UINT8*)data, heapId);
- }
- CM_UTILITY_EXPORT inline void stackDeallocLast(void* data, UINT32 heapId);
- }
|