BfAstAllocator.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #pragma once
  2. //#define BF_USE_NEAR_NODE_REF
  3. #include "BeefySysLib/Common.h"
  4. #include "BeefySysLib/util/CritSect.h"
  5. #include "BeefySysLib/util/SLIList.h"
  6. #include "../Beef/BfCommon.h"
  7. #ifdef BF_PLATFORM_WINDOWS
  8. #define BF_AST_ALLOCATOR_USE_PAGES
  9. #endif
  10. /*#pragma warning(push)
  11. #pragma warning(disable:4141)
  12. #pragma warning(disable:4146)
  13. #pragma warning(disable:4291)
  14. #pragma warning(disable:4244)
  15. #pragma warning(disable:4267)
  16. #pragma warning(disable:4624)
  17. #pragma warning(disable:4800)
  18. #pragma warning(disable:4996)
  19. #include "llvm/ADT/SmallVector.h"
  20. #pragma warning(pop)*/
  21. NS_BF_BEGIN
  22. class BfSource;
  23. class BfSourceData;
  24. class BfBitSet
  25. {
  26. public:
  27. uint32* mBits;
  28. public:
  29. BfBitSet();
  30. ~BfBitSet();
  31. void Init(int numBits);
  32. bool IsSet(int idx);
  33. void Set(int idx);
  34. void Clear(int idx);
  35. };
  36. #ifdef BF_AST_ALLOCATOR_USE_PAGES
  37. class BfAstPageHeader
  38. {
  39. public:
  40. BfSourceData* mSourceData;
  41. };
  42. #endif
  43. class BfAstAllocChunk;
  44. class BfAstAllocManager;
  45. struct BfAstFreePage
  46. {
  47. BfAstFreePage* mNext;
  48. };
  49. class BfAstAllocManager
  50. {
  51. public:
  52. static const int CHUNK_SIZE = 1024*1024;
  53. static const int PAGE_SIZE = 4096;
  54. #ifdef BF_AST_ALLOCATOR_USE_PAGES
  55. CritSect mCritSect;
  56. SLIList<BfAstFreePage*> mFreePages;
  57. int mFreePageCount;
  58. Array<uint8*> mAllocChunks;
  59. #endif
  60. public:
  61. BfAstAllocManager();
  62. ~BfAstAllocManager();
  63. uint8* AllocPage();
  64. void FreePage(uint8* page);
  65. void FreePages(Array<uint8*> pages);
  66. void GetStats(int& allocPages, int& usedPages);
  67. };
  68. // #ifdef BUMPALLOC_TRACKALLOCS
  69. // struct BumpAllocTrackedEntry
  70. // {
  71. // int mCount;
  72. // int mSize;
  73. //
  74. // BumpAllocTrackedEntry()
  75. // {
  76. // mCount = 0;
  77. // mSize = 0;
  78. // }
  79. // };
  80. // #endif
  81. class BfAstAllocator
  82. {
  83. public:
  84. static const int LARGE_ALLOC_SIZE = 2048;
  85. BfSourceData* mSourceData;
  86. uint8* mCurPtr;
  87. uint8* mCurPageEnd;
  88. Array<void*> mLargeAllocs;
  89. Array<uint8*> mPages;
  90. int mLargeAllocSizes;
  91. int mNumPagesUsed;
  92. int mUsedSize;
  93. #ifdef BUMPALLOC_TRACKALLOCS
  94. Dictionary<String, BumpAllocTrackedEntry> mTrackedAllocs;
  95. #endif
  96. public:
  97. BfAstAllocator();
  98. ~BfAstAllocator();
  99. void InitChunkHead(int wantSize);
  100. int GetAllocSize() const
  101. {
  102. return (int)(((mPages.size() - 1) * BfAstAllocManager::PAGE_SIZE) + (BfAstAllocManager::PAGE_SIZE - (mCurPageEnd - mCurPtr)));
  103. }
  104. int GetTotalAllocSize() const
  105. {
  106. return (int)mPages.size() * BfAstAllocManager::PAGE_SIZE;
  107. }
  108. int CalcUsedSize() const
  109. {
  110. return GetAllocSize();
  111. }
  112. template <typename T>
  113. T* Alloc(int extraBytes = 0)
  114. {
  115. int alignSize = alignof(T);
  116. mCurPtr = (uint8*)(((intptr)mCurPtr + alignSize - 1) & ~(alignSize - 1));
  117. int wantSize = sizeof(T) + extraBytes;
  118. #ifdef BUMPALLOC_TRACKALLOCS
  119. const char* name = typeid(T).name();
  120. BumpAllocTrackedEntry* allocSizePtr;
  121. mTrackedAllocs.TryAdd(name, NULL, &allocSizePtr);
  122. allocSizePtr->mCount++;
  123. allocSizePtr->mSize += wantSize;
  124. #endif
  125. if (mCurPtr + wantSize >= mCurPageEnd)
  126. InitChunkHead(wantSize);
  127. memset(mCurPtr, 0, wantSize);
  128. T* retVal = new (mCurPtr) T();
  129. mCurPtr += wantSize;
  130. #ifndef BF_AST_ALLOCATOR_USE_PAGES
  131. retVal->mSourceData = this->mSourceData;
  132. #endif
  133. return retVal;
  134. }
  135. uint8* AllocBytes(int wantSize, int alignSize, const char* dbgName = "AllocBytes")
  136. {
  137. #ifdef BUMPALLOC_TRACKALLOCS
  138. BumpAllocTrackedEntry* allocSizePtr;
  139. mTrackedAllocs.TryAdd(dbgName, NULL, &allocSizePtr);
  140. allocSizePtr->mCount++;
  141. allocSizePtr->mSize += wantSize;
  142. #endif
  143. #ifndef BF_USE_NEAR_NODE_REF
  144. if (wantSize >= LARGE_ALLOC_SIZE)
  145. {
  146. mLargeAllocSizes += wantSize;
  147. uint8* addr = new uint8[wantSize];
  148. mLargeAllocs.push_back(addr);
  149. return addr;
  150. }
  151. #endif
  152. mCurPtr = (uint8*)(((intptr)mCurPtr + alignSize - 1) & ~(alignSize - 1));
  153. if (mCurPtr + wantSize >= mCurPageEnd)
  154. InitChunkHead(wantSize);
  155. memset(mCurPtr, 0, wantSize);
  156. uint8* retVal = mCurPtr;
  157. mCurPtr += wantSize;
  158. return retVal;
  159. }
  160. uint8* AllocBytes(int wantSize, const char* dbgName = "AllocBytes")
  161. {
  162. #ifdef BUMPALLOC_TRACKALLOCS
  163. BumpAllocTrackedEntry* allocSizePtr;
  164. mTrackedAllocs.TryAdd(dbgName, NULL, &allocSizePtr);
  165. allocSizePtr->mCount++;
  166. allocSizePtr->mSize += wantSize;
  167. #endif
  168. if (wantSize >= LARGE_ALLOC_SIZE)
  169. {
  170. uint8* addr = new uint8[wantSize];
  171. mLargeAllocs.push_back(addr);
  172. return addr;
  173. }
  174. if (mCurPtr + wantSize >= mCurPageEnd)
  175. InitChunkHead(wantSize);
  176. memset(mCurPtr, 0, wantSize);
  177. uint8* retVal = mCurPtr;
  178. mCurPtr += wantSize;
  179. return retVal;
  180. }
  181. };
  182. NS_BF_END