CmMemStack.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #pragma once
  2. #include <stack>
  3. #include <assert.h>
  4. #include "CmThreadDefines.h"
  5. namespace CamelotFramework
  6. {
  7. template <int BlockCapacity = 1024 * 1024>
  8. class MemStackInternal
  9. {
  10. private:
  11. class MemBlock
  12. {
  13. public:
  14. MemBlock(UINT32 size)
  15. :mData(nullptr), mFreePtr(0), mSize(size)
  16. {
  17. mData = static_cast<UINT8*>(cm_alloc(mSize));
  18. }
  19. ~MemBlock()
  20. {
  21. cm_free(mData);
  22. }
  23. UINT8* alloc(UINT8 amount)
  24. {
  25. UINT8* freePtr = &mData[mFreePtr];
  26. mFreePtr += amount;
  27. return freePtr;
  28. }
  29. void dealloc(UINT8* data, UINT8 amount)
  30. {
  31. mFreePtr -= amount;
  32. assert((&mData[mFreePtr]) == data && "Out of order stack deallocation detected. Deallocations need to happen in order opposite of allocations.");
  33. }
  34. UINT8* mData;
  35. UINT32 mFreePtr;
  36. UINT32 mSize;
  37. };
  38. public:
  39. MemStackInternal()
  40. { }
  41. ~MemStackInternal()
  42. {
  43. assert(mBlocks.size() == 0 && "Not all blocks were released before shutting down the stack allocator.");
  44. while(!mBlocks.empty())
  45. {
  46. MemBlock* curPtr = mBlocks.top();
  47. mBlocks.pop();
  48. cm_delete(curPtr);
  49. }
  50. }
  51. UINT8* alloc(UINT32 amount)
  52. {
  53. MemBlock* topBlock;
  54. if(mBlocks.size() == 0)
  55. topBlock = allocNewBlock(amount);
  56. else
  57. topBlock = mBlocks.top();
  58. mAllocSizes.push(amount);
  59. UINT32 freeMem = topBlock->mSize - topBlock->mFreePtr;
  60. if(amount <= freeMem)
  61. return topBlock->alloc(amount);
  62. MemBlock* newBlock = allocNewBlock(amount);
  63. return newBlock->alloc(amount);
  64. }
  65. void dealloc(UINT8* data)
  66. {
  67. assert(mAllocSizes.size() > 0 && "Out of order stack deallocation detected. Deallocations need to happen in order opposite of allocations.");
  68. UINT32 amount = mAllocSizes.top();
  69. mAllocSizes.pop();
  70. MemBlock* topBlock = mBlocks.top();
  71. topBlock->dealloc(data, amount);
  72. if(topBlock->mFreePtr == 0)
  73. {
  74. cm_delete(topBlock);
  75. mBlocks.pop();
  76. }
  77. }
  78. private:
  79. std::stack<MemBlock*> mBlocks;
  80. std::stack<UINT32> mAllocSizes;
  81. MemBlock* allocNewBlock(UINT32 wantedSize)
  82. {
  83. UINT32 blockSize = BlockCapacity;
  84. if(wantedSize > blockSize)
  85. blockSize = wantedSize;
  86. MemBlock* newBlock = cm_new<MemBlock>(blockSize);
  87. mBlocks.push(newBlock);
  88. return newBlock;
  89. }
  90. };
  91. /**
  92. * @brief Fastest, but also most limiting type of allocator. All deallocations
  93. * must happen in opposite order from allocations.
  94. *
  95. * @note It's mostly useful when you need to allocate something temporarily on the heap,
  96. * usually something that gets allocated and freed within the same function.
  97. *
  98. * Each allocation comes with a pretty hefty 4 byte memory overhead, so don't use it for small allocations.
  99. *
  100. * Operations done on a single heap are thread safe. Multiple threads are not allowed to access a heap that wasn't
  101. * created for them.
  102. *
  103. * @tparam BlockCapacity Minimum size of a block. Larger blocks mean less memory allocations, but also potentially
  104. * more wasted memory. If an allocation requests more bytes than BlockCapacity, first largest multiple is
  105. * used instead.
  106. * @tparam VectorAligned If true, all allocations will be aligned to 16bit boundaries.
  107. */
  108. class CM_UTILITY_EXPORT MemStack
  109. {
  110. public:
  111. /**
  112. * @brief Sets up the heap you can later use with alloc/dealloc calls. It is most common to have one heap
  113. * per thread.
  114. *
  115. * @param heapId Unique heap ID. Each heap can only be used from one thread, it cannot be shared.
  116. * You cannot have more than 256 heaps.
  117. */
  118. static void setupHeap(UINT8 heapId);
  119. static UINT8* alloc(UINT32 numBytes, UINT32 heapId);
  120. static void deallocLast(UINT8* data, UINT32 heapId);
  121. private:
  122. static MemStackInternal<1024 * 1024> mStacks[256];
  123. #if CM_DEBUG_MODE
  124. static CM_THREAD_ID_TYPE mThreadIds[256];
  125. #endif
  126. };
  127. CM_UTILITY_EXPORT inline UINT8* stackAlloc(UINT32 numBytes, UINT32 heapId);
  128. template<class T>
  129. T* stackAlloc(UINT32 heapId)
  130. {
  131. return (T*)MemStack::alloc(sizeof(T), heapId);
  132. }
  133. template<class T>
  134. T* stackAllocN(UINT32 count, UINT32 heapId)
  135. {
  136. return (T*)MemStack::alloc(sizeof(T) * count, heapId);
  137. }
  138. template<class T>
  139. T* stackConstructN(UINT32 count, UINT32 heapId)
  140. {
  141. T* data = stackAllocN<T>(count, heapId);
  142. for(unsigned int i = 0; i < count; i++)
  143. new ((void*)&data[i]) T;
  144. return data;
  145. }
  146. template<class T>
  147. void stackDestruct(T* data, UINT32 heapId)
  148. {
  149. data->~T();
  150. MemStack::deallocLast((UINT8*)data, heapId);
  151. }
  152. template<class T>
  153. void stackDestructN(T* data, UINT32 count, UINT32 heapId)
  154. {
  155. for(unsigned int i = 0; i < count; i++)
  156. data[i].~T();
  157. MemStack::deallocLast((UINT8*)data, heapId);
  158. }
  159. CM_UTILITY_EXPORT inline void stackDeallocLast(void* data, UINT32 heapId);
  160. }