CodegenNumberAllocator.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "BackEnd.h"
  6. CodeGenNumberThreadAllocator::CodeGenNumberThreadAllocator(Recycler * recycler)
  7. : recycler(recycler), currentNumberSegment(nullptr), currentChunkSegment(nullptr),
  8. numberSegmentEnd(nullptr), currentNumberBlockEnd(nullptr), nextNumber(nullptr), chunkSegmentEnd(nullptr),
  9. currentChunkBlockEnd(nullptr), nextChunk(nullptr), hasNewNumberBlock(nullptr), hasNewChunkBlock(nullptr),
  10. pendingIntegrationNumberSegmentCount(0), pendingIntegrationChunkSegmentCount(0),
  11. pendingIntegrationNumberSegmentPageCount(0), pendingIntegrationChunkSegmentPageCount(0)
  12. {
  13. }
  14. CodeGenNumberThreadAllocator::~CodeGenNumberThreadAllocator()
  15. {
  16. pendingIntegrationNumberSegment.Clear(&NoThrowNoMemProtectHeapAllocator::Instance);
  17. pendingIntegrationChunkSegment.Clear(&NoThrowNoMemProtectHeapAllocator::Instance);
  18. pendingIntegrationNumberBlock.Clear(&NoThrowHeapAllocator::Instance);
  19. pendingIntegrationChunkBlock.Clear(&NoThrowHeapAllocator::Instance);
  20. pendingFlushNumberBlock.Clear(&NoThrowHeapAllocator::Instance);
  21. pendingFlushChunkBlock.Clear(&NoThrowHeapAllocator::Instance);
  22. pendingReferenceNumberBlock.Clear(&NoThrowHeapAllocator::Instance);
  23. }
  24. size_t
  25. CodeGenNumberThreadAllocator::GetNumberAllocSize()
  26. {
  27. #ifdef RECYCLER_MEMORY_VERIFY
  28. if (recycler->VerifyEnabled())
  29. {
  30. return HeapInfo::GetAlignedSize(AllocSizeMath::Add(sizeof(Js::JavascriptNumber) + sizeof(size_t), recycler->verifyPad));
  31. }
  32. #endif
  33. return HeapInfo::GetAlignedSizeNoCheck(sizeof(Js::JavascriptNumber));
  34. }
  35. size_t
  36. CodeGenNumberThreadAllocator::GetChunkAllocSize()
  37. {
  38. #ifdef RECYCLER_MEMORY_VERIFY
  39. if (recycler->VerifyEnabled())
  40. {
  41. return HeapInfo::GetAlignedSize(AllocSizeMath::Add(sizeof(CodeGenNumberChunk) + sizeof(size_t), recycler->verifyPad));
  42. }
  43. #endif
  44. return HeapInfo::GetAlignedSizeNoCheck(sizeof(CodeGenNumberChunk));
  45. }
  46. Js::JavascriptNumber *
  47. CodeGenNumberThreadAllocator::AllocNumber()
  48. {
  49. AutoCriticalSection autocs(&cs);
  50. size_t sizeCat = GetNumberAllocSize();
  51. if (nextNumber + sizeCat > currentNumberBlockEnd)
  52. {
  53. AllocNewNumberBlock();
  54. }
  55. Js::JavascriptNumber * newNumber = (Js::JavascriptNumber *)nextNumber;
  56. #ifdef RECYCLER_MEMORY_VERIFY
  57. recycler->FillCheckPad(newNumber, sizeof(Js::JavascriptNumber), sizeCat);
  58. #endif
  59. nextNumber += sizeCat;
  60. return newNumber;
  61. }
  62. CodeGenNumberChunk *
  63. CodeGenNumberThreadAllocator::AllocChunk()
  64. {
  65. AutoCriticalSection autocs(&cs);
  66. size_t sizeCat = GetChunkAllocSize();
  67. if (nextChunk + sizeCat > currentChunkBlockEnd)
  68. {
  69. AllocNewChunkBlock();
  70. }
  71. CodeGenNumberChunk * newChunk = (CodeGenNumberChunk *)nextChunk;
  72. #ifdef RECYCLER_MEMORY_VERIFY
  73. recycler->FillCheckPad(nextChunk, sizeof(CodeGenNumberChunk), sizeCat);
  74. #endif
  75. memset(newChunk, 0, sizeof(CodeGenNumberChunk));
  76. nextChunk += sizeCat;
  77. return newChunk;
  78. }
  79. void
  80. CodeGenNumberThreadAllocator::AllocNewNumberBlock()
  81. {
  82. Assert(cs.IsLocked());
  83. Assert(nextNumber + GetNumberAllocSize() > currentNumberBlockEnd);
  84. if (hasNewNumberBlock)
  85. {
  86. if (!pendingReferenceNumberBlock.PrependNode(&NoThrowHeapAllocator::Instance,
  87. currentNumberBlockEnd - BlockSize, currentNumberSegment))
  88. {
  89. Js::Throw::OutOfMemory();
  90. }
  91. hasNewNumberBlock = false;
  92. }
  93. if (currentNumberBlockEnd == numberSegmentEnd)
  94. {
  95. Assert(cs.IsLocked());
  96. // Reserve the segment, but not committing it
  97. currentNumberSegment = PageAllocator::AllocPageSegment(pendingIntegrationNumberSegment, this->recycler->GetRecyclerLeafPageAllocator(), true);
  98. if (currentNumberSegment == nullptr)
  99. {
  100. currentNumberBlockEnd = nullptr;
  101. numberSegmentEnd = nullptr;
  102. nextNumber = nullptr;
  103. Js::Throw::OutOfMemory();
  104. }
  105. pendingIntegrationNumberSegmentCount++;
  106. pendingIntegrationNumberSegmentPageCount += currentNumberSegment->GetPageCount();
  107. currentNumberBlockEnd = currentNumberSegment->GetAddress();
  108. numberSegmentEnd = currentNumberSegment->GetEndAddress();
  109. }
  110. // Commit the page.
  111. if (!::VirtualAlloc(currentNumberBlockEnd, BlockSize, MEM_COMMIT, PAGE_READWRITE))
  112. {
  113. Js::Throw::OutOfMemory();
  114. }
  115. nextNumber = currentNumberBlockEnd;
  116. currentNumberBlockEnd += BlockSize;
  117. hasNewNumberBlock = true;
  118. this->recycler->GetRecyclerLeafPageAllocator()->FillAllocPages(nextNumber, 1);
  119. }
  120. void
  121. CodeGenNumberThreadAllocator::AllocNewChunkBlock()
  122. {
  123. Assert(cs.IsLocked());
  124. Assert(nextChunk + GetChunkAllocSize() > currentChunkBlockEnd);
  125. if (hasNewChunkBlock)
  126. {
  127. if (!pendingFlushChunkBlock.PrependNode(&NoThrowHeapAllocator::Instance,
  128. currentChunkBlockEnd - BlockSize, currentChunkSegment))
  129. {
  130. Js::Throw::OutOfMemory();
  131. }
  132. // All integrated pages' object are all live initially, so don't need to rescan them
  133. ::ResetWriteWatch(currentChunkBlockEnd - BlockSize, BlockSize);
  134. pendingReferenceNumberBlock.MoveTo(&pendingFlushNumberBlock);
  135. hasNewChunkBlock = false;
  136. }
  137. if (currentChunkBlockEnd == chunkSegmentEnd)
  138. {
  139. Assert(cs.IsLocked());
  140. // Reserve the segment, but not committing it
  141. currentChunkSegment = PageAllocator::AllocPageSegment(pendingIntegrationChunkSegment, this->recycler->GetRecyclerPageAllocator(), true);
  142. if (currentChunkSegment == nullptr)
  143. {
  144. currentChunkBlockEnd = nullptr;
  145. chunkSegmentEnd = nullptr;
  146. nextChunk = nullptr;
  147. Js::Throw::OutOfMemory();
  148. }
  149. pendingIntegrationChunkSegmentCount++;
  150. pendingIntegrationChunkSegmentPageCount += currentChunkSegment->GetPageCount();
  151. currentChunkBlockEnd = currentChunkSegment->GetAddress();
  152. chunkSegmentEnd = currentChunkSegment->GetEndAddress();
  153. }
  154. // Commit the page.
  155. if (!::VirtualAlloc(currentChunkBlockEnd, BlockSize, MEM_COMMIT, PAGE_READWRITE))
  156. {
  157. Js::Throw::OutOfMemory();
  158. }
  159. nextChunk = currentChunkBlockEnd;
  160. currentChunkBlockEnd += BlockSize;
  161. hasNewChunkBlock = true;
  162. this->recycler->GetRecyclerLeafPageAllocator()->FillAllocPages(nextChunk, 1);
  163. }
  164. void
  165. CodeGenNumberThreadAllocator::Integrate()
  166. {
  167. AutoCriticalSection autocs(&cs);
  168. PageAllocator * leafPageAllocator = this->recycler->GetRecyclerLeafPageAllocator();
  169. leafPageAllocator->IntegrateSegments(pendingIntegrationNumberSegment, pendingIntegrationNumberSegmentCount, pendingIntegrationNumberSegmentPageCount);
  170. PageAllocator * recyclerPageAllocator = this->recycler->GetRecyclerPageAllocator();
  171. recyclerPageAllocator->IntegrateSegments(pendingIntegrationChunkSegment, pendingIntegrationChunkSegmentCount, pendingIntegrationChunkSegmentPageCount);
  172. pendingIntegrationNumberSegmentCount = 0;
  173. pendingIntegrationChunkSegmentCount = 0;
  174. pendingIntegrationNumberSegmentPageCount = 0;
  175. pendingIntegrationChunkSegmentPageCount = 0;
  176. #ifdef TRACK_ALLOC
  177. TrackAllocData oldAllocData = recycler->nextAllocData;
  178. recycler->nextAllocData.Clear();
  179. #endif
  180. while (!pendingIntegrationNumberBlock.Empty())
  181. {
  182. TRACK_ALLOC_INFO(recycler, Js::JavascriptNumber, Recycler, 0, (size_t)-1);
  183. BlockRecord& record = pendingIntegrationNumberBlock.Head();
  184. if (!recycler->IntegrateBlock<LeafBit>(record.blockAddress, record.segment, GetNumberAllocSize(), sizeof(Js::JavascriptNumber)))
  185. {
  186. Js::Throw::OutOfMemory();
  187. }
  188. pendingIntegrationNumberBlock.RemoveHead(&NoThrowHeapAllocator::Instance);
  189. }
  190. while (!pendingIntegrationChunkBlock.Empty())
  191. {
  192. TRACK_ALLOC_INFO(recycler, CodeGenNumberChunk, Recycler, 0, (size_t)-1);
  193. BlockRecord& record = pendingIntegrationChunkBlock.Head();
  194. if (!recycler->IntegrateBlock<NoBit>(record.blockAddress, record.segment, GetChunkAllocSize(), sizeof(CodeGenNumberChunk)))
  195. {
  196. Js::Throw::OutOfMemory();
  197. }
  198. pendingIntegrationChunkBlock.RemoveHead(&NoThrowHeapAllocator::Instance);
  199. }
  200. #ifdef TRACK_ALLOC
  201. Assert(recycler->nextAllocData.IsEmpty());
  202. recycler->nextAllocData = oldAllocData;
  203. #endif
  204. }
  205. void
  206. CodeGenNumberThreadAllocator::FlushAllocations()
  207. {
  208. AutoCriticalSection autocs(&cs);
  209. pendingFlushNumberBlock.MoveTo(&pendingIntegrationNumberBlock);
  210. pendingFlushChunkBlock.MoveTo(&pendingIntegrationChunkBlock);
  211. }
  212. CodeGenNumberAllocator::CodeGenNumberAllocator(CodeGenNumberThreadAllocator * threadAlloc, Recycler * recycler) :
  213. threadAlloc(threadAlloc), recycler(recycler), chunk(nullptr), chunkTail(nullptr), currentChunkNumberCount(CodeGenNumberChunk::MaxNumberCount)
  214. {
  215. #if DBG
  216. finalized = false;
  217. #endif
  218. }
  219. // We should never call this function if we are using tagged float
  220. #if !FLOATVAR
  221. Js::JavascriptNumber *
  222. CodeGenNumberAllocator::Alloc()
  223. {
  224. Assert(!finalized);
  225. if (currentChunkNumberCount == CodeGenNumberChunk::MaxNumberCount)
  226. {
  227. CodeGenNumberChunk * newChunk = threadAlloc? threadAlloc->AllocChunk()
  228. : RecyclerNewStructZ(recycler, CodeGenNumberChunk);
  229. // Need to always put the new chunk last, as when we flush
  230. // pages, new chunk's page might not be full yet, and won't
  231. // be flushed, and we will have a broken link in the link list.
  232. newChunk->next = nullptr;
  233. if (this->chunkTail != nullptr)
  234. {
  235. this->chunkTail->next = newChunk;
  236. }
  237. else
  238. {
  239. this->chunk = newChunk;
  240. }
  241. this->chunkTail = newChunk;
  242. this->currentChunkNumberCount = 0;
  243. }
  244. Js::JavascriptNumber * newNumber = threadAlloc? threadAlloc->AllocNumber()
  245. : Js::JavascriptNumber::NewUninitialized(recycler);
  246. this->chunkTail->numbers[this->currentChunkNumberCount++] = newNumber;
  247. return newNumber;
  248. }
  249. #endif
  250. CodeGenNumberChunk *
  251. CodeGenNumberAllocator::Finalize()
  252. {
  253. Assert(!finalized);
  254. #if DBG
  255. finalized = true;
  256. #endif
  257. CodeGenNumberChunk * finalizedChunk = this->chunk;
  258. this->chunk = nullptr;
  259. this->chunkTail = nullptr;
  260. this->currentChunkNumberCount = 0;
  261. return finalizedChunk;
  262. }