PageStack.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. template <typename T>
  6. class PageStack
  7. {
  8. private:
  9. struct Chunk : public PagePoolPage
  10. {
  11. Chunk * nextChunk;
  12. T entries[];
  13. };
  14. static const size_t EntriesPerChunk = (AutoSystemInfo::PageSize - sizeof(Chunk)) / sizeof(T);
  15. public:
  16. PageStack(PagePool * pagePool);
  17. ~PageStack();
  18. void Init(uint reservedPageCount = 0);
  19. void Clear();
  20. bool Pop(T * item);
  21. bool Push(T item);
  22. uint Split(uint targetCount, __in_ecount(targetCount) PageStack<T> ** targetStacks);
  23. void Abort();
  24. void Release();
  25. bool IsEmpty() const;
  26. #if DBG
  27. bool HasChunk() const
  28. {
  29. return this->currentChunk != nullptr;
  30. }
  31. #endif
  32. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  33. void SetMaxPageCount(size_t maxPageCount)
  34. {
  35. this->maxPageCount = maxPageCount > 1 ? maxPageCount : 1;
  36. }
  37. #endif
  38. static const uint MaxSplitTargets = 3; // Not counting original stack, so this supports 4-way parallel
  39. private:
  40. Chunk * CreateChunk();
  41. void FreeChunk(Chunk * chunk);
  42. private:
  43. T * nextEntry;
  44. T * chunkStart;
  45. T * chunkEnd;
  46. Chunk * currentChunk;
  47. PagePool * pagePool;
  48. bool usesReservedPages;
  49. #if DBG
  50. size_t count;
  51. #endif
  52. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  53. size_t pageCount;
  54. size_t maxPageCount;
  55. #endif
  56. };
  57. template <typename T>
  58. inline
  59. bool PageStack<T>::Pop(T * item)
  60. {
  61. Assert(currentChunk != nullptr);
  62. if (nextEntry == chunkStart)
  63. {
  64. // We're at the beginning of the chunk. Move to the previous chunk, if any
  65. if (currentChunk->nextChunk == nullptr)
  66. {
  67. // All done
  68. Assert(count == 0);
  69. return false;
  70. }
  71. Chunk * temp = currentChunk;
  72. currentChunk = currentChunk->nextChunk;
  73. FreeChunk(temp);
  74. chunkStart = currentChunk->entries;
  75. chunkEnd = &currentChunk->entries[EntriesPerChunk];
  76. nextEntry = chunkEnd;
  77. }
  78. Assert(nextEntry > chunkStart && nextEntry <= chunkEnd);
  79. nextEntry--;
  80. #if defined(_M_IX86) || defined(_M_X64)
  81. _mm_prefetch((char*) (nextEntry - 1), _MM_HINT_T0);
  82. #endif
  83. *item = *nextEntry;
  84. #if DBG
  85. count--;
  86. Assert(count == (nextEntry - chunkStart) + (pageCount - 1) * EntriesPerChunk);
  87. #endif
  88. return true;
  89. }
  90. template <typename T>
  91. inline
  92. bool PageStack<T>::Push(T item)
  93. {
  94. if (nextEntry == chunkEnd)
  95. {
  96. Chunk * newChunk = CreateChunk();
  97. if (newChunk == nullptr)
  98. {
  99. return false;
  100. }
  101. newChunk->nextChunk = currentChunk;
  102. currentChunk = newChunk;
  103. chunkStart = currentChunk->entries;
  104. chunkEnd = &currentChunk->entries[EntriesPerChunk];
  105. nextEntry = chunkStart;
  106. }
  107. Assert(nextEntry >= chunkStart && nextEntry < chunkEnd);
  108. *nextEntry = item;
  109. nextEntry++;
  110. #if DBG
  111. count++;
  112. Assert(count == (nextEntry - chunkStart) + (pageCount - 1) * EntriesPerChunk);
  113. #endif
  114. return true;
  115. }
  116. template <typename T>
  117. PageStack<T>::PageStack(PagePool * pagePool) :
  118. pagePool(pagePool),
  119. currentChunk(nullptr),
  120. nextEntry(nullptr),
  121. chunkStart(nullptr),
  122. chunkEnd(nullptr),
  123. usesReservedPages(false)
  124. {
  125. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  126. pageCount = 0;
  127. maxPageCount = (size_t)-1; // Default to no limit
  128. #endif
  129. #if DBG
  130. count = 0;
  131. #endif
  132. }
  133. template <typename T>
  134. PageStack<T>::~PageStack()
  135. {
  136. Assert(currentChunk == nullptr);
  137. Assert(nextEntry == nullptr);
  138. Assert(count == 0);
  139. Assert(pageCount == 0);
  140. }
  141. template <typename T>
  142. void PageStack<T>::Init(uint reservedPageCount)
  143. {
  144. if (reservedPageCount > 0)
  145. {
  146. this->usesReservedPages = true;
  147. this->pagePool->ReservePages(reservedPageCount);
  148. }
  149. // Preallocate one chunk.
  150. Assert(currentChunk == nullptr);
  151. currentChunk = CreateChunk();
  152. if (currentChunk == nullptr)
  153. {
  154. Js::Throw::OutOfMemory();
  155. }
  156. currentChunk->nextChunk = nullptr;
  157. chunkStart = currentChunk->entries;
  158. chunkEnd = &currentChunk->entries[EntriesPerChunk];
  159. nextEntry = chunkStart;
  160. }
  161. template <typename T>
  162. void PageStack<T>::Clear()
  163. {
  164. currentChunk = nullptr;
  165. nextEntry = nullptr;
  166. #if DBG
  167. count = 0;
  168. pageCount = 0;
  169. #endif
  170. }
  171. template <typename T>
  172. typename PageStack<T>::Chunk * PageStack<T>::CreateChunk()
  173. {
  174. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  175. if (pageCount >= maxPageCount)
  176. {
  177. return nullptr;
  178. }
  179. #endif
  180. Chunk * newChunk = (Chunk *)this->pagePool->GetPage(usesReservedPages);
  181. if (newChunk == nullptr)
  182. {
  183. return nullptr;
  184. }
  185. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  186. pageCount++;
  187. #endif
  188. return newChunk;
  189. }
  190. template <typename T>
  191. void PageStack<T>::FreeChunk(Chunk * chunk)
  192. {
  193. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  194. pageCount--;
  195. #endif
  196. this->pagePool->FreePage(chunk);
  197. }
  198. template <typename T>
  199. uint PageStack<T>::Split(uint targetCount, __in_ecount(targetCount) PageStack<T> ** targetStacks)
  200. {
  201. // Split the current stack up to [targetCount + 1] ways.
  202. // [targetStacks] contains the target stacks and must have [targetCount] elements.
  203. Assert(targetCount > 0 && targetCount <= MaxSplitTargets);
  204. Assert(targetStacks);
  205. __analysis_assume(targetCount <= MaxSplitTargets);
  206. Chunk * mainCurrent;
  207. Chunk * targetCurrents[MaxSplitTargets];
  208. // Do the initial split of first pages for each target stack.
  209. // During this, if we run out of pages, we will return a value < maxSplit to
  210. // indicate that the split was less than the maximum possible.
  211. Chunk * chunk = this->currentChunk;
  212. Assert(chunk != nullptr);
  213. // The first chunk is assigned to the main stack, and since it's already there,
  214. // we just advance to the next chunk and start assigning to each target stack.
  215. mainCurrent = chunk;
  216. chunk = chunk->nextChunk;
  217. uint targetIndex = 0;
  218. while (targetIndex < targetCount)
  219. {
  220. if (chunk == nullptr)
  221. {
  222. // No more pages. Adjust targetCount down to what we were actually able to do.
  223. // We'll return this number below so the caller knows.
  224. targetCount = targetIndex;
  225. break;
  226. }
  227. // Target stack should be empty.
  228. // If it has a free page currently, release it.
  229. Assert(targetStacks[targetIndex]->IsEmpty());
  230. targetStacks[targetIndex]->Release();
  231. targetStacks[targetIndex]->currentChunk = chunk;
  232. targetStacks[targetIndex]->chunkStart = chunk->entries;
  233. targetStacks[targetIndex]->chunkEnd = &chunk->entries[EntriesPerChunk];
  234. targetStacks[targetIndex]->nextEntry = targetStacks[targetIndex]->chunkEnd;
  235. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  236. this->pageCount--;
  237. targetStacks[targetIndex]->pageCount = 1;
  238. #endif
  239. #if DBG
  240. this->count -= EntriesPerChunk;
  241. targetStacks[targetIndex]->count = EntriesPerChunk;
  242. #endif
  243. targetCurrents[targetIndex] = chunk;
  244. chunk = chunk->nextChunk;
  245. targetIndex++;
  246. }
  247. // Loop through the remaining chunks (if any),
  248. // assigning each chunk to the main chunk and the target chunks in turn,
  249. // and linking each chunk to the end of the respective list.
  250. while (true)
  251. {
  252. if (chunk == nullptr)
  253. {
  254. break;
  255. }
  256. mainCurrent->nextChunk = chunk;
  257. mainCurrent = chunk;
  258. chunk = chunk->nextChunk;
  259. targetIndex = 0;
  260. while (targetIndex < targetCount)
  261. {
  262. if (chunk == nullptr)
  263. {
  264. break;
  265. }
  266. targetCurrents[targetIndex]->nextChunk = chunk;
  267. targetCurrents[targetIndex] = chunk;
  268. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  269. this->pageCount--;
  270. targetStacks[targetIndex]->pageCount++;
  271. #endif
  272. #if DBG
  273. this->count -= EntriesPerChunk;
  274. targetStacks[targetIndex]->count += EntriesPerChunk;
  275. #endif
  276. chunk = chunk->nextChunk;
  277. targetIndex++;
  278. }
  279. }
  280. // Terminate all the split chunk lists with null
  281. mainCurrent->nextChunk = nullptr;
  282. targetIndex = 0;
  283. while (targetIndex < targetCount)
  284. {
  285. targetCurrents[targetIndex]->nextChunk = nullptr;
  286. targetIndex++;
  287. }
  288. // Return the actual split count we were able to do, which may have been lowered above.
  289. return targetCount;
  290. }
  291. template <typename T>
  292. void PageStack<T>::Abort()
  293. {
  294. // Abandon the current entries in the stack and reset to initialized state.
  295. if (currentChunk == nullptr)
  296. {
  297. Assert(count == 0);
  298. return;
  299. }
  300. // Free all the chunks except the first one
  301. while (currentChunk->nextChunk != nullptr)
  302. {
  303. Chunk * temp = currentChunk;
  304. currentChunk = currentChunk->nextChunk;
  305. FreeChunk(temp);
  306. }
  307. chunkStart = currentChunk->entries;
  308. chunkEnd = &currentChunk->entries[EntriesPerChunk];
  309. nextEntry = chunkStart;
  310. #if DBG
  311. count = 0;
  312. #endif
  313. }
  314. template <typename T>
  315. void PageStack<T>::Release()
  316. {
  317. Assert(IsEmpty());
  318. // We may have a preallocated chunk still held; if so release it.
  319. if (currentChunk != nullptr)
  320. {
  321. Assert(currentChunk->nextChunk == nullptr);
  322. FreeChunk(currentChunk);
  323. currentChunk = nullptr;
  324. }
  325. nextEntry = nullptr;
  326. chunkStart = nullptr;
  327. chunkEnd = nullptr;
  328. }
  329. template <typename T>
  330. bool PageStack<T>::IsEmpty() const
  331. {
  332. if (currentChunk == nullptr)
  333. {
  334. Assert(count == 0);
  335. Assert(nextEntry == nullptr);
  336. return true;
  337. }
  338. if (nextEntry == chunkStart && currentChunk->nextChunk == nullptr)
  339. {
  340. Assert(count == 0);
  341. return true;
  342. }
  343. Assert(count != 0);
  344. return false;
  345. }