HeapInfo.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  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 "CommonMemoryPch.h"
  6. #include "Memory/PageHeapBlockTypeFilter.h"
  7. #if defined(TARGET_32)
  8. #include "ValidPointersMap/vpm.32b.h"
  9. #elif defined(TARGET_64)
  10. #include "ValidPointersMap/vpm.64b.h"
  11. #else
  12. #error "Platform is not handled"
  13. #endif
  14. template _ALWAYSINLINE char* HeapInfo::RealAlloc<NoBit, false>(Recycler * recycler, size_t sizeCat, size_t size);
  15. // The VS2013 linker treats this as a redefinition of an already
  16. // defined constant and complains. So skip the declaration if we're compiling
  17. // with VS2013 or below.
  18. #if !defined(_MSC_VER) || _MSC_VER >= 1900
  19. const uint SmallAllocationBlockAttributes::MaxSmallObjectCount;
  20. const uint MediumAllocationBlockAttributes::MaxSmallObjectCount;
  21. #endif
  22. HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes> HeapInfo::smallAllocValidPointersMap;
  23. HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes> HeapInfo::mediumAllocValidPointersMap;
  24. template <class TBlockAttributes>
  25. ValidPointers<TBlockAttributes>::ValidPointers(ushort const * validPointers, uint bucketIndex)
  26. #if USE_VPM_TABLE
  27. : validPointers(validPointers)
  28. #endif
  29. {
  30. #if !USE_VPM_TABLE
  31. Assert(validPointers == nullptr);
  32. maxObjectIndex = CalculateBucketInfo(bucketIndex, &indexPerObject);
  33. #endif
  34. #if DBG && USE_VPM_TABLE
  35. uint localIndexPerObject;
  36. uint localMaxObjectIndex = CalculateBucketInfo(bucketIndex, &localIndexPerObject);
  37. for (uint index = 0; index < TBlockAttributes::MaxSmallObjectCount; index++)
  38. {
  39. Assert(CalculateAddressIndex(index, localIndexPerObject, localMaxObjectIndex) == validPointers[index]);
  40. Assert(CalculateInteriorAddressIndex(index, localIndexPerObject, localMaxObjectIndex) == validPointers[index + TBlockAttributes::MaxSmallObjectCount]);
  41. }
  42. #endif
  43. }
  44. template <class TBlockAttributes>
  45. ushort ValidPointers<TBlockAttributes>::GetAddressIndex(uint index) const
  46. {
  47. Assert(index < TBlockAttributes::MaxSmallObjectCount);
  48. #if USE_VPM_TABLE
  49. return validPointers[index];
  50. #else
  51. return CalculateAddressIndex(index, indexPerObject, maxObjectIndex);
  52. #endif
  53. }
  54. template <class TBlockAttributes>
  55. ushort ValidPointers<TBlockAttributes>::GetInteriorAddressIndex(uint index) const
  56. {
  57. Assert(index < TBlockAttributes::MaxSmallObjectCount);
  58. #if USE_VPM_TABLE
  59. return validPointers[index + TBlockAttributes::MaxSmallObjectCount];
  60. #else
  61. return CalculateInteriorAddressIndex(index, indexPerObject, maxObjectIndex);
  62. #endif
  63. }
  64. #if !USE_VPM_TABLE || DBG
  65. template <class TBlockAttributes>
  66. uint ValidPointers<TBlockAttributes>::CalculateBucketInfo(uint bucketIndex, uint * indexPerObject)
  67. {
  68. uint bucketSize;
  69. if (TBlockAttributes::IsSmallBlock)
  70. {
  71. bucketSize = TBlockAttributes::MinObjectSize + HeapConstants::ObjectGranularity * bucketIndex;
  72. }
  73. else
  74. {
  75. bucketSize = TBlockAttributes::MinObjectSize + HeapConstants::MediumObjectGranularity * (bucketIndex + 1);
  76. }
  77. *indexPerObject = bucketSize / HeapConstants::ObjectGranularity;
  78. return ((TBlockAttributes::PageCount * AutoSystemInfo::PageSize) / bucketSize) * bucketSize / HeapConstants::ObjectGranularity;
  79. }
  80. template <class TBlockAttributes>
  81. ushort ValidPointers<TBlockAttributes>::CalculateAddressIndex(uint index, uint indexPerObject, uint maxObjectIndex)
  82. {
  83. Assert(index < TBlockAttributes::MaxSmallObjectCount);
  84. if (index >= maxObjectIndex)
  85. {
  86. return (ushort)-1;
  87. }
  88. uint addressIndex = index / indexPerObject;
  89. Assert(addressIndex < USHRT_MAX);
  90. if (addressIndex * indexPerObject != index)
  91. {
  92. return (ushort)-1;
  93. }
  94. return (ushort)addressIndex;
  95. }
  96. template <class TBlockAttributes>
  97. ushort ValidPointers<TBlockAttributes>::CalculateInteriorAddressIndex(uint index, uint indexPerObject, uint maxObjectIndex)
  98. {
  99. Assert(index < TBlockAttributes::MaxSmallObjectCount);
  100. if (index >= maxObjectIndex)
  101. {
  102. return (ushort)-1;
  103. }
  104. uint addressIndex = index / indexPerObject;
  105. Assert(addressIndex < USHRT_MAX);
  106. return (ushort)addressIndex;
  107. }
  108. #endif
  109. #if defined(ENABLE_TEST_HOOKS) || !USE_STATIC_VPM
  110. template <class TBlockAttributes>
  111. void HeapInfo::ValidPointersMap<TBlockAttributes>::GenerateValidPointersMap(ValidPointersMapTable * validTable, InvalidBitsTable& invalidTable, BlockInfoMapTable& blockInfoTable)
  112. {
  113. // Create the valid pointer map to be shared by the buckets.
  114. // Also create the invalid objects bit vector.
  115. ushort * buffer = &((*validTable)[0][0]);
  116. if (buffer)
  117. {
  118. memset(buffer, -1, sizeof(ushort) * 2 * TBlockAttributes::MaxSmallObjectCount * TBlockAttributes::BucketCount);
  119. }
  120. for (uint i = 0; i < TBlockAttributes::BucketCount; i++)
  121. {
  122. // Non-interior first
  123. ushort * validPointers = buffer;
  124. if (buffer)
  125. {
  126. buffer += TBlockAttributes::MaxSmallObjectCount;
  127. }
  128. typename SmallHeapBlockT<TBlockAttributes>::SmallHeapBlockBitVector * invalidBitVector = &invalidTable[i];
  129. invalidBitVector->SetAll();
  130. uint bucketSize;
  131. if (TBlockAttributes::IsSmallBlock)
  132. {
  133. bucketSize = TBlockAttributes::MinObjectSize + HeapConstants::ObjectGranularity * i;
  134. }
  135. else
  136. {
  137. bucketSize = TBlockAttributes::MinObjectSize + HeapConstants::MediumObjectGranularity * (i + 1);
  138. }
  139. uint stride = bucketSize / HeapConstants::ObjectGranularity;
  140. uint maxObjectCountForBucket = ((TBlockAttributes::PageCount * AutoSystemInfo::PageSize) / bucketSize);
  141. BlockInfoMapRow* blockInfoRow = &blockInfoTable[i];
  142. memset(blockInfoRow, 0, sizeof(BlockInfoMapRow));
  143. for (ushort j = 0; j < maxObjectCountForBucket; j++)
  144. {
  145. if (validPointers)
  146. {
  147. validPointers[j * stride] = j;
  148. }
  149. uintptr_t objectAddress = j * bucketSize;
  150. Assert(objectAddress / AutoSystemInfo::PageSize < USHRT_MAX);
  151. ushort pageIndex = (ushort)(objectAddress / AutoSystemInfo::PageSize);
  152. (*blockInfoRow)[pageIndex].pageObjectCount++;
  153. (*blockInfoRow)[pageIndex].lastObjectIndexOnPage = max(j, (*blockInfoRow)[pageIndex].lastObjectIndexOnPage);
  154. invalidBitVector->Clear(j * stride);
  155. }
  156. if (buffer)
  157. {
  158. // interior pointer
  159. ushort * validInteriorPointers = buffer;
  160. buffer += TBlockAttributes::MaxSmallObjectCount;
  161. for (ushort j = 0; j < maxObjectCountForBucket; j++)
  162. {
  163. uint start = j * stride;
  164. uint end = min(start + stride, TBlockAttributes::MaxSmallObjectCount);
  165. for (uint k = start; k < end; k++)
  166. {
  167. validInteriorPointers[k] = j;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. #endif
  174. #ifdef ENABLE_TEST_HOOKS
  175. template <>
  176. HRESULT HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::GenerateValidPointersMapForBlockType(FILE* file)
  177. {
  178. #define IfErrorGotoCleanup(result) if ((result) < 0) { hr = E_FAIL; goto cleanup; }
  179. Assert(file != nullptr);
  180. HRESULT hr = S_OK;
  181. // Use heap to allocate the table so we don't bloat the stack (~64k). We only use this function
  182. // to generate headers as part of testing.
  183. ValidPointersMapTable *valid = (ValidPointersMapTable *)malloc(sizeof(ValidPointersMapTable));
  184. InvalidBitsTable *invalid = (InvalidBitsTable *)malloc(sizeof(InvalidBitsTable));
  185. BlockInfoMapTable *blockMap = (BlockInfoMapTable*)malloc(sizeof(BlockInfoMapTable));
  186. if (valid == nullptr || invalid == nullptr || blockMap == nullptr)
  187. {
  188. hr = E_FAIL;
  189. goto cleanup;
  190. }
  191. GenerateValidPointersMap(valid, *invalid, *blockMap);
  192. IfErrorGotoCleanup(fwprintf(file, _u("#if USE_VPM_TABLE\n")))
  193. IfErrorGotoCleanup(fwprintf(file, _u("const ushort HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::validPointersBuffer[HeapConstants::BucketCount][HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::rowSize] = \n{\n")));
  194. // Generate the full buffer.
  195. for (unsigned i = 0; i < HeapConstants::BucketCount; ++i)
  196. {
  197. IfErrorGotoCleanup(fwprintf(file, _u(" {\n ")));
  198. for (unsigned j = 0; j < rowSize; ++j)
  199. {
  200. IfErrorGotoCleanup(fwprintf(
  201. file,
  202. (j < rowSize - 1) ? _u("0x%04hX, ") : _u("0x%04hX"),
  203. (*valid)[i][j]));
  204. }
  205. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::BucketCount - 1 ? _u("\n },\n") : _u("\n }\n"))));
  206. }
  207. IfErrorGotoCleanup(fwprintf(file, _u("};\n")));
  208. IfErrorGotoCleanup(fwprintf(file, _u("#endif // USE_VPM_TABLE\n\n")))
  209. // Generate the invalid bitvectors.
  210. IfErrorGotoCleanup(fwprintf(
  211. file,
  212. _u("const BVUnit HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::invalidBitsData[HeapConstants::BucketCount][SmallHeapBlockT<SmallAllocationBlockAttributes>::SmallHeapBlockBitVector::wordCount] = {\n")));
  213. for (unsigned i = 0; i < HeapConstants::BucketCount; ++i)
  214. {
  215. IfErrorGotoCleanup(fwprintf(file, _u(" {\n ")));
  216. for (unsigned j = 0; j < (*invalid)[i].wordCount; ++j)
  217. {
  218. const char16 *format = (j < (*invalid)[i].wordCount - 1) ?
  219. #if defined(TARGET_32)
  220. _u("0x%08X, ") : _u("0x%08X")
  221. #elif defined(TARGET_64)
  222. _u("0x%016I64X, ") : _u("0x%016I64X")
  223. #else
  224. #error "Platform is not handled"
  225. #endif
  226. ;
  227. IfErrorGotoCleanup(fwprintf(file, format, (*invalid)[i].GetRawData()[j]));
  228. }
  229. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::BucketCount - 1 ? _u("\n },\n") : _u("\n }\n"))));
  230. }
  231. IfErrorGotoCleanup(fwprintf(
  232. file,
  233. _u("};\n")
  234. _u("// The following is used to construct the InvalidBitsTable statically without forcing BVStatic to be an aggregate\n")
  235. _u("const HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::InvalidBitsTable * const HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::invalidBitsBuffers =\n")
  236. _u(" reinterpret_cast<const HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::InvalidBitsTable *>(&HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::invalidBitsData);\n")));
  237. // Generate the block map table
  238. IfErrorGotoCleanup(fwprintf(
  239. file,
  240. _u("const SmallHeapBlockT<SmallAllocationBlockAttributes>::BlockInfo HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::blockInfoBuffer[SmallAllocationBlockAttributes::BucketCount][SmallAllocationBlockAttributes::PageCount] = {\n")));
  241. for (unsigned i = 0; i < HeapConstants::BucketCount; ++i)
  242. {
  243. IfErrorGotoCleanup(fwprintf(file, _u(" // Bucket: %u, Size: %d\n"), i, (int) (HeapConstants::ObjectGranularity + (i * SmallAllocationBlockAttributes::BucketGranularity))));
  244. IfErrorGotoCleanup(fwprintf(file, _u(" {\n")));
  245. for (unsigned j = 0; j < SmallAllocationBlockAttributes::PageCount; ++j)
  246. {
  247. IfErrorGotoCleanup(fwprintf(file, _u(" { ")));
  248. const char16 *format = _u("0x%04hX, 0x%04hX");
  249. IfErrorGotoCleanup(fwprintf(file, format, (*blockMap)[i][j].lastObjectIndexOnPage, (*blockMap)[i][j].pageObjectCount));
  250. IfErrorGotoCleanup(fwprintf(file, (j < SmallAllocationBlockAttributes::PageCount - 1 ? _u(" },\n") : _u(" }\n"))));
  251. }
  252. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::BucketCount - 1 ? _u(" },\n") : _u(" }\n"))));
  253. }
  254. IfErrorGotoCleanup(fwprintf(file, _u("};\n")));
  255. cleanup:
  256. #undef IfErrorGotoCleanup
  257. free(valid);
  258. free(invalid);
  259. return hr;
  260. }
  261. template <>
  262. HRESULT HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::GenerateValidPointersMapForBlockType(FILE* file)
  263. {
  264. #define IfErrorGotoCleanup(result) if ((result) < 0) { hr = E_FAIL; goto cleanup; }
  265. Assert(file != nullptr);
  266. HRESULT hr = S_OK;
  267. // Use heap to allocate the table so we don't bloat the stack (~64k). We only use this function
  268. // to generate headers as part of testing.
  269. ValidPointersMapTable *valid = (ValidPointersMapTable *)malloc(sizeof(ValidPointersMapTable));
  270. InvalidBitsTable *invalid = (InvalidBitsTable *)malloc(sizeof(InvalidBitsTable));
  271. BlockInfoMapTable *blockMap = (BlockInfoMapTable *)malloc(sizeof(BlockInfoMapTable));
  272. if (valid == nullptr || invalid == nullptr || blockMap == nullptr)
  273. {
  274. hr = E_FAIL;
  275. goto cleanup;
  276. }
  277. GenerateValidPointersMap(valid, *invalid, *blockMap);
  278. IfErrorGotoCleanup(fwprintf(file, _u("#if USE_VPM_TABLE\n")))
  279. IfErrorGotoCleanup(fwprintf(file, _u("const ushort HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::validPointersBuffer[MediumAllocationBlockAttributes::BucketCount][HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::rowSize] = \n{\n")));
  280. // Generate the full buffer.
  281. for (unsigned i = 0; i < HeapConstants::MediumBucketCount; ++i)
  282. {
  283. IfErrorGotoCleanup(fwprintf(file, _u(" {\n ")));
  284. for (unsigned j = 0; j < rowSize; ++j)
  285. {
  286. IfErrorGotoCleanup(fwprintf(
  287. file,
  288. (j < rowSize - 1) ? _u("0x%04hX, ") : _u("0x%04hX"),
  289. (*valid)[i][j]));
  290. }
  291. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::MediumBucketCount - 1 ? _u("\n },\n") : _u("\n }\n"))));
  292. }
  293. IfErrorGotoCleanup(fwprintf(file, _u("};\n")));
  294. IfErrorGotoCleanup(fwprintf(file, _u("#endif // USE_VPM_TABLE\n\n")))
  295. // Generate the invalid bitvectors.
  296. IfErrorGotoCleanup(fwprintf(
  297. file,
  298. _u("const BVUnit HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::invalidBitsData[MediumAllocationBlockAttributes::BucketCount][SmallHeapBlockT<MediumAllocationBlockAttributes>::SmallHeapBlockBitVector::wordCount] = {\n")));
  299. for (unsigned i = 0; i < HeapConstants::MediumBucketCount; ++i)
  300. {
  301. IfErrorGotoCleanup(fwprintf(file, _u(" {\n ")));
  302. for (unsigned j = 0; j < (*invalid)[i].wordCount; ++j)
  303. {
  304. const char16 *format = (j < (*invalid)[i].wordCount - 1) ?
  305. #if defined(TARGET_32)
  306. _u("0x%08X, ") : _u("0x%08X")
  307. #elif defined(TARGET_64)
  308. _u("0x%016I64X, ") : _u("0x%016I64X")
  309. #else
  310. #error "Platform is not handled"
  311. #endif
  312. ;
  313. IfErrorGotoCleanup(fwprintf(file, format, (*invalid)[i].GetRawData()[j]));
  314. }
  315. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::MediumBucketCount - 1 ? _u("\n },\n") : _u("\n }\n"))));
  316. }
  317. IfErrorGotoCleanup(fwprintf(
  318. file,
  319. _u("};\n")
  320. _u("// The following is used to construct the InvalidBitsTable statically without forcing BVStatic to be an aggregate\n")
  321. _u("const HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::InvalidBitsTable * const HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::invalidBitsBuffers =\n")
  322. _u(" reinterpret_cast<const HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::InvalidBitsTable *>(&HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::invalidBitsData);\n")));
  323. // Generate the block map table
  324. IfErrorGotoCleanup(fwprintf(
  325. file,
  326. _u("const SmallHeapBlockT<MediumAllocationBlockAttributes>::BlockInfo HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::blockInfoBuffer[MediumAllocationBlockAttributes::BucketCount][MediumAllocationBlockAttributes::PageCount] = {\n")));
  327. for (unsigned i = 0; i < HeapConstants::MediumBucketCount; ++i)
  328. {
  329. IfErrorGotoCleanup(fwprintf(file, _u(" // Bucket: %u, Size: %d\n"), i, (int)(HeapConstants::MaxSmallObjectSize + ((i + 1) * MediumAllocationBlockAttributes::BucketGranularity))));
  330. IfErrorGotoCleanup(fwprintf(file, _u(" {\n")));
  331. for (unsigned j = 0; j < MediumAllocationBlockAttributes::PageCount; ++j)
  332. {
  333. IfErrorGotoCleanup(fwprintf(file, _u(" { ")));
  334. const char16 *format = _u("0x%04hX, 0x%04hX");
  335. IfErrorGotoCleanup(fwprintf(file, format, (*blockMap)[i][j].lastObjectIndexOnPage, (*blockMap)[i][j].pageObjectCount));
  336. IfErrorGotoCleanup(fwprintf(file, (j < MediumAllocationBlockAttributes::PageCount - 1 ? _u(" },\n") : _u(" }\n"))));
  337. }
  338. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::MediumBucketCount - 1 ? _u(" },\n") : _u(" }\n"))));
  339. }
  340. IfErrorGotoCleanup(fwprintf(file, _u("};\n")));
  341. cleanup:
  342. #undef IfErrorGotoCleanup
  343. free(valid);
  344. free(invalid);
  345. return hr;
  346. }
  347. template <class TBlockAttributes>
  348. HRESULT HeapInfo::ValidPointersMap<TBlockAttributes>::GenerateValidPointersMapHeader(LPCWSTR vpmFullPath)
  349. {
  350. Assert(vpmFullPath != nullptr);
  351. HRESULT hr = E_FAIL;
  352. FILE * file = nullptr;
  353. if (_wfopen_s(&file, vpmFullPath, _u("w")) == 0 && file != nullptr)
  354. {
  355. const char16 * header =
  356. _u("//-------------------------------------------------------------------------------------------------------\n")
  357. _u("// Copyright (C) Microsoft. All rights reserved.\n")
  358. _u("// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.\n")
  359. _u("//-------------------------------------------------------------------------------------------------------\n")
  360. _u("// Generated via jshost -GenerateValidPointersMapHeader\n")
  361. #if defined(TARGET_32)
  362. _u("// Target platforms: 32bit - x86 & arm\n")
  363. #elif defined(TARGET_64)
  364. _u("// Target platform: 64bit - amd64 & arm64\n")
  365. #else
  366. #error "Platform is not handled"
  367. #endif
  368. _u("#if USE_STATIC_VPM\n")
  369. _u("\n");
  370. if (fwprintf(file, header) >= 0)
  371. {
  372. hr = ValidPointersMap<SmallAllocationBlockAttributes>::GenerateValidPointersMapForBlockType(file);
  373. if (SUCCEEDED(hr))
  374. {
  375. hr = ValidPointersMap<MediumAllocationBlockAttributes>::GenerateValidPointersMapForBlockType(file);
  376. }
  377. fwprintf(file, _u("#endif // USE_STATIC_VPM\n"));
  378. }
  379. fclose(file);
  380. }
  381. return hr;
  382. }
  383. #endif
  384. HeapInfo::HeapInfo() :
  385. recycler(nullptr),
  386. #if ENABLE_CONCURRENT_GC
  387. newLeafHeapBlockList(nullptr),
  388. newNormalHeapBlockList(nullptr),
  389. #ifdef RECYCLER_WRITE_BARRIER
  390. newNormalWithBarrierHeapBlockList(nullptr),
  391. newFinalizableWithBarrierHeapBlockList(nullptr),
  392. #endif
  393. newFinalizableHeapBlockList(nullptr),
  394. #ifdef RECYCLER_VISITED_HOST
  395. newRecyclerVisitedHostHeapBlockList(nullptr),
  396. #endif
  397. newMediumLeafHeapBlockList(nullptr),
  398. newMediumNormalHeapBlockList(nullptr),
  399. #ifdef RECYCLER_WRITE_BARRIER
  400. newMediumNormalWithBarrierHeapBlockList(nullptr),
  401. newMediumFinalizableWithBarrierHeapBlockList(nullptr),
  402. #endif
  403. #ifdef RECYCLER_VISITED_HOST
  404. newMediumRecyclerVisitedHostHeapBlockList(nullptr),
  405. #endif
  406. newMediumFinalizableHeapBlockList(nullptr),
  407. #endif
  408. #ifdef RECYCLER_FINALIZE_CHECK
  409. liveFinalizableObjectCount(0),
  410. pendingDisposableObjectCount(0),
  411. newFinalizableObjectCount(0),
  412. #endif
  413. #if ENABLE_PARTIAL_GC
  414. uncollectedNewPageCount(0),
  415. unusedPartialCollectFreeBytes(0),
  416. #endif
  417. uncollectedAllocBytes(0),
  418. lastUncollectedAllocBytes(0),
  419. pendingZeroPageCount(0)
  420. #ifdef RECYCLER_PAGE_HEAP
  421. , pageHeapMode(PageHeapMode::PageHeapModeOff)
  422. , isPageHeapEnabled(false)
  423. , pageHeapBlockType(PageHeapBlockTypeFilter::PageHeapBlockTypeFilterAll)
  424. , captureAllocCallStack(false)
  425. , captureFreeCallStack(false)
  426. #endif
  427. {
  428. }
  429. HeapInfo::~HeapInfo()
  430. {
  431. RECYCLER_SLOW_CHECK(this->VerifySmallHeapBlockCount());
  432. // Finalize all finalizable object first
  433. for (uint i=0; i < HeapConstants::BucketCount; i++)
  434. {
  435. heapBuckets[i].FinalizeAllObjects();
  436. }
  437. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  438. for (uint i=0; i < HeapConstants::MediumBucketCount; i++)
  439. {
  440. mediumHeapBuckets[i].FinalizeAllObjects();
  441. }
  442. #endif
  443. largeObjectBucket.FinalizeAllObjects();
  444. #if ENABLE_CONCURRENT_GC
  445. SmallFinalizableHeapBucket::FinalizeHeapBlockList(this->newFinalizableHeapBlockList);
  446. MediumFinalizableHeapBucket::FinalizeHeapBlockList(this->newMediumFinalizableHeapBlockList);
  447. #ifdef RECYCLER_VISITED_HOST
  448. SmallRecyclerVisitedHostHeapBucket::FinalizeHeapBlockList(this->newRecyclerVisitedHostHeapBlockList);
  449. MediumRecyclerVisitedHostHeapBucket::FinalizeHeapBlockList(this->newMediumRecyclerVisitedHostHeapBlockList);
  450. #endif
  451. #ifdef RECYCLER_WRITE_BARRIER
  452. SmallFinalizableWithBarrierHeapBucket::FinalizeHeapBlockList(this->newFinalizableWithBarrierHeapBlockList);
  453. MediumFinalizableWithBarrierHeapBucket::FinalizeHeapBlockList(this->newMediumFinalizableWithBarrierHeapBlockList);
  454. #endif
  455. #endif
  456. #ifdef RECYCLER_FINALIZE_CHECK
  457. Assert(liveFinalizableObjectCount == 0);
  458. Assert(pendingDisposableObjectCount == 0);
  459. #endif
  460. // Delete the heap blocks
  461. Recycler * recycler = this->recycler;
  462. #ifdef RECYCLER_SLOW_CHECK_ENABLED
  463. size_t largeBlockCount = this->largeObjectBucket.GetLargeHeapBlockCount(false);
  464. uint mediumBlockCount = 0;
  465. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !SMALLBLOCK_MEDIUM_ALLOC
  466. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  467. {
  468. mediumBlockCount += mediumHeapBuckets[i].GetLargeHeapBlockCount(false);
  469. }
  470. #endif
  471. #endif
  472. RECYCLER_SLOW_CHECK(Assert(this->heapBlockCount[HeapBlock::HeapBlockType::LargeBlockType] - largeBlockCount - mediumBlockCount == 0));
  473. #if ENABLE_CONCURRENT_GC
  474. SmallLeafHeapBucket::DeleteHeapBlockList(this->newLeafHeapBlockList, recycler);
  475. SmallNormalHeapBucket::DeleteHeapBlockList(this->newNormalHeapBlockList, recycler);
  476. #ifdef RECYCLER_WRITE_BARRIER
  477. SmallNormalWithBarrierHeapBucket::DeleteHeapBlockList(this->newNormalWithBarrierHeapBlockList, recycler);
  478. SmallFinalizableWithBarrierHeapBucket::DeleteHeapBlockList(this->newFinalizableWithBarrierHeapBlockList, recycler);
  479. #endif
  480. SmallFinalizableHeapBucket::DeleteHeapBlockList(this->newFinalizableHeapBlockList, recycler);
  481. MediumLeafHeapBucket::DeleteHeapBlockList(this->newMediumLeafHeapBlockList, recycler);
  482. MediumNormalHeapBucket::DeleteHeapBlockList(this->newMediumNormalHeapBlockList, recycler);
  483. #ifdef RECYCLER_WRITE_BARRIER
  484. MediumNormalWithBarrierHeapBucket::DeleteHeapBlockList(this->newMediumNormalWithBarrierHeapBlockList, recycler);
  485. MediumFinalizableWithBarrierHeapBucket::DeleteHeapBlockList(this->newMediumFinalizableWithBarrierHeapBlockList, recycler);
  486. #endif
  487. MediumFinalizableHeapBucket::DeleteHeapBlockList(this->newMediumFinalizableHeapBlockList, recycler);
  488. #ifdef RECYCLER_VISITED_HOST
  489. SmallFinalizableHeapBucket::DeleteHeapBlockList(this->newRecyclerVisitedHostHeapBlockList, recycler);
  490. MediumFinalizableHeapBucket::DeleteHeapBlockList(this->newMediumRecyclerVisitedHostHeapBlockList, recycler);
  491. #endif
  492. #endif
  493. // We do this here, instead of in the Recycler destructor, because the above stuff may
  494. // generate additional tracking events, particularly ReportUnallocated.
  495. // Arguably we shouldn't report these things as ReportUnallocated...
  496. RecyclerMemoryTracking::ReportRecyclerDestroy(recycler);
  497. }
  498. void
  499. HeapInfo::Initialize(Recycler * recycler
  500. #ifdef RECYCLER_PAGE_HEAP
  501. , PageHeapMode pageheapmode
  502. , bool captureAllocCallStack
  503. , bool captureFreeCallStack
  504. #endif
  505. )
  506. {
  507. this->recycler = recycler;
  508. #ifdef DUMP_FRAGMENTATION_STATS
  509. if (recycler->GetRecyclerFlagsTable().DumpFragmentationStats)
  510. {
  511. Output::Print(_u("[FRAG %d] Start"), ::GetTickCount());
  512. }
  513. #endif
  514. #ifdef RECYCLER_PAGE_HEAP
  515. isPageHeapEnabled = false;
  516. PageHeapBlockTypeFilter blockTypeFilter = PageHeapBlockTypeFilter::PageHeapBlockTypeFilterAll;
  517. Js::NumberRange bucketNumberRange;
  518. Js::NumberRange* pBucketNumberRange = &bucketNumberRange;
  519. if (pageheapmode == PageHeapMode::PageHeapModeOff)
  520. {
  521. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  522. isPageHeapEnabled = recycler->GetRecyclerFlagsTable().PageHeap != PageHeapMode::PageHeapModeOff;
  523. pageheapmode = (PageHeapMode)recycler->GetRecyclerFlagsTable().PageHeap;
  524. blockTypeFilter = (PageHeapBlockTypeFilter)recycler->GetRecyclerFlagsTable().PageHeapBlockType;
  525. pBucketNumberRange = &recycler->GetRecyclerFlagsTable().PageHeapBucketNumber;
  526. #else
  527. // @TODO in free build, use environment var or other way to enable page heap
  528. // currently page heap build is enable in free build but has not implemented a way to input the page heap flags.
  529. // if we only need page heap in free test build, just move RECYCLER_PAGE_HEAP definition into ENABLE_DEBUG_CONFIG_OPTIONS
  530. // in CommonDefines.h it should work
  531. #endif
  532. }
  533. else
  534. {
  535. isPageHeapEnabled = true;
  536. }
  537. #ifdef RECYCLER_PAGE_HEAP
  538. if (isPageHeapEnabled)
  539. {
  540. this->captureAllocCallStack = captureAllocCallStack;
  541. this->captureFreeCallStack = captureFreeCallStack;
  542. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  543. this->captureAllocCallStack = captureAllocCallStack || recycler->GetRecyclerFlagsTable().PageHeapAllocStack;
  544. this->captureFreeCallStack = captureFreeCallStack || recycler->GetRecyclerFlagsTable().PageHeapFreeStack;
  545. #endif
  546. }
  547. #endif
  548. if (IsPageHeapEnabled())
  549. {
  550. this->pageHeapMode = pageheapmode;
  551. // Use one of the two modes with -PageHeap flag
  552. Assert(this->pageHeapMode == PageHeapMode::PageHeapModeBlockStart || this->pageHeapMode == PageHeapMode::PageHeapModeBlockEnd);
  553. this->pageHeapBlockType = blockTypeFilter;
  554. for (int i = 0; i < HeapConstants::BucketCount + HeapConstants::MediumBucketCount; i++)
  555. {
  556. if (pBucketNumberRange->InRange(i))
  557. {
  558. if (i < HeapConstants::BucketCount)
  559. {
  560. this->smallBlockPageHeapBucketFilter.Set(i);
  561. }
  562. else
  563. {
  564. this->mediumBlockPageHeapBucketFilter.Set(i - HeapConstants::BucketCount);
  565. }
  566. }
  567. }
  568. }
  569. else
  570. {
  571. // These should not be set if we're not in page heap mode
  572. Assert(!(captureAllocCallStack || captureFreeCallStack));
  573. }
  574. #endif
  575. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  576. {
  577. heapBuckets[i].Initialize(this, (i + 1) << HeapConstants::ObjectAllocationShift);
  578. }
  579. RECYCLER_SLOW_CHECK(memset(this->heapBlockCount, 0, sizeof(this->heapBlockCount)));
  580. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  581. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  582. {
  583. #if SMALLBLOCK_MEDIUM_ALLOC
  584. mediumHeapBuckets[i].Initialize(this, HeapConstants::MaxSmallObjectSize + ((i + 1) * HeapConstants::MediumObjectGranularity));
  585. #else
  586. mediumHeapBuckets[i].Initialize(this, HeapConstants::MaxSmallObjectSize + ((i + 1) * HeapConstants::MediumObjectGranularity), true);
  587. #endif
  588. }
  589. #endif
  590. largeObjectBucket.Initialize(this, HeapConstants::MaxMediumObjectSize);
  591. }
  592. #if defined(PROFILE_RECYCLER_ALLOC) || defined(RECYCLER_MEMORY_VERIFY) || defined(MEMSPECT_TRACKING) || defined(ETW_MEMORY_TRACKING)
  593. void
  594. HeapInfo::Initialize(Recycler * recycler, void(*trackNativeAllocCallBack)(Recycler *, void *, size_t)
  595. #ifdef RECYCLER_PAGE_HEAP
  596. , PageHeapMode pageheapmode
  597. , bool captureAllocCallStack
  598. , bool captureFreeCallStack
  599. #endif
  600. )
  601. {
  602. Initialize(recycler
  603. #ifdef RECYCLER_PAGE_HEAP
  604. , pageheapmode
  605. , captureAllocCallStack
  606. , captureFreeCallStack
  607. #endif
  608. );
  609. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  610. {
  611. heapBuckets[i].GetBucket<NoBit>().GetAllocator()->SetTrackNativeAllocatedObjectCallBack(trackNativeAllocCallBack);
  612. }
  613. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  614. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  615. {
  616. mediumHeapBuckets[i].GetBucket<NoBit>().GetAllocator()->SetTrackNativeAllocatedObjectCallBack(trackNativeAllocCallBack);
  617. }
  618. #endif
  619. }
  620. #endif
  621. #ifdef RECYCLER_PAGE_HEAP
  622. template bool HeapInfo::IsPageHeapEnabledForBlock<MediumAllocationBlockAttributes>(const size_t objectSize);
  623. template bool HeapInfo::IsPageHeapEnabledForBlock<SmallAllocationBlockAttributes>(const size_t objectSize);
  624. template bool HeapInfo::IsPageHeapEnabledForBlock<LargeAllocationBlockAttributes>(const size_t objectSize);
  625. template <typename TBlockAttributes>
  626. bool HeapInfo::IsPageHeapEnabledForBlock(const size_t objectSize)
  627. {
  628. if (IsPageHeapEnabled())
  629. {
  630. if (TBlockAttributes::IsSmallBlock)
  631. {
  632. return smallBlockPageHeapBucketFilter.Test(GetBucketIndex(objectSize)) != 0;
  633. }
  634. else if (TBlockAttributes::IsMediumBlock)
  635. {
  636. return mediumBlockPageHeapBucketFilter.Test(GetMediumBucketIndex(objectSize)) != 0;
  637. }
  638. else
  639. {
  640. return ((byte)this->pageHeapBlockType & (byte)PageHeapBlockTypeFilter::PageHeapBlockTypeFilterLarge) != 0;
  641. }
  642. }
  643. return false;
  644. }
  645. #endif
  646. void
  647. HeapInfo::ResetMarks(ResetMarkFlags flags)
  648. {
  649. for (uint i=0; i < HeapConstants::BucketCount; i++)
  650. {
  651. heapBuckets[i].ResetMarks(flags);
  652. }
  653. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  654. for (uint i=0; i < HeapConstants::MediumBucketCount; i++)
  655. {
  656. mediumHeapBuckets[i].ResetMarks(flags);
  657. }
  658. #endif
  659. largeObjectBucket.ResetMarks(flags);
  660. #if ENABLE_CONCURRENT_GC
  661. if ((flags & ResetMarkFlags_ScanImplicitRoot) != 0)
  662. {
  663. HeapBlockList::ForEach(newLeafHeapBlockList, [flags](SmallLeafHeapBlock * heapBlock)
  664. {
  665. heapBlock->MarkImplicitRoots();
  666. });
  667. HeapBlockList::ForEach(newNormalHeapBlockList, [flags](SmallNormalHeapBlock * heapBlock)
  668. {
  669. heapBlock->MarkImplicitRoots();
  670. });
  671. #ifdef RECYCLER_WRITE_BARRIER
  672. HeapBlockList::ForEach(newNormalWithBarrierHeapBlockList, [flags](SmallNormalWithBarrierHeapBlock * heapBlock)
  673. {
  674. heapBlock->MarkImplicitRoots();
  675. });
  676. HeapBlockList::ForEach(newFinalizableWithBarrierHeapBlockList, [flags](SmallFinalizableWithBarrierHeapBlock * heapBlock)
  677. {
  678. heapBlock->MarkImplicitRoots();
  679. });
  680. #endif
  681. HeapBlockList::ForEach(newFinalizableHeapBlockList, [flags](SmallNormalHeapBlock * heapBlock)
  682. {
  683. heapBlock->MarkImplicitRoots();
  684. });
  685. HeapBlockList::ForEach(newMediumLeafHeapBlockList, [flags](MediumLeafHeapBlock * heapBlock)
  686. {
  687. heapBlock->MarkImplicitRoots();
  688. });
  689. HeapBlockList::ForEach(newMediumNormalHeapBlockList, [flags](MediumNormalHeapBlock * heapBlock)
  690. {
  691. heapBlock->MarkImplicitRoots();
  692. });
  693. #ifdef RECYCLER_WRITE_BARRIER
  694. HeapBlockList::ForEach(newMediumNormalWithBarrierHeapBlockList, [flags](MediumNormalWithBarrierHeapBlock * heapBlock)
  695. {
  696. heapBlock->MarkImplicitRoots();
  697. });
  698. HeapBlockList::ForEach(newMediumFinalizableWithBarrierHeapBlockList, [flags](MediumFinalizableWithBarrierHeapBlock * heapBlock)
  699. {
  700. heapBlock->MarkImplicitRoots();
  701. });
  702. #endif
  703. HeapBlockList::ForEach(newMediumFinalizableHeapBlockList, [flags](MediumNormalHeapBlock * heapBlock)
  704. {
  705. heapBlock->MarkImplicitRoots();
  706. });
  707. }
  708. #endif
  709. }
  710. void
  711. HeapInfo::ScanInitialImplicitRoots()
  712. {
  713. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  714. {
  715. heapBuckets[i].ScanInitialImplicitRoots(recycler);
  716. }
  717. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  718. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  719. {
  720. mediumHeapBuckets[i].ScanInitialImplicitRoots(recycler);
  721. }
  722. #endif
  723. largeObjectBucket.ScanInitialImplicitRoots(recycler);
  724. #if ENABLE_CONCURRENT_GC
  725. // NOTE: Don't need to do newLeafHeapBlockList
  726. HeapBlockList::ForEach(newNormalHeapBlockList, [this](SmallNormalHeapBlock * heapBlock)
  727. {
  728. heapBlock->ScanInitialImplicitRoots(recycler);
  729. });
  730. #ifdef RECYCLER_WRITE_BARRIER
  731. HeapBlockList::ForEach(newNormalWithBarrierHeapBlockList, [this](SmallNormalWithBarrierHeapBlock * heapBlock)
  732. {
  733. heapBlock->ScanInitialImplicitRoots(recycler);
  734. });
  735. HeapBlockList::ForEach(newFinalizableWithBarrierHeapBlockList, [this](SmallFinalizableWithBarrierHeapBlock * heapBlock)
  736. {
  737. heapBlock->ScanInitialImplicitRoots(recycler);
  738. });
  739. #endif
  740. HeapBlockList::ForEach(newFinalizableHeapBlockList, [this](SmallNormalHeapBlock * heapBlock)
  741. {
  742. heapBlock->ScanInitialImplicitRoots(recycler);
  743. });
  744. #endif
  745. #if ENABLE_CONCURRENT_GC
  746. // NOTE: Don't need to do newLeafHeapBlockList
  747. HeapBlockList::ForEach(newMediumNormalHeapBlockList, [this](MediumNormalHeapBlock * heapBlock)
  748. {
  749. heapBlock->ScanInitialImplicitRoots(recycler);
  750. });
  751. #ifdef RECYCLER_WRITE_BARRIER
  752. HeapBlockList::ForEach(newMediumNormalWithBarrierHeapBlockList, [this](MediumNormalWithBarrierHeapBlock * heapBlock)
  753. {
  754. heapBlock->ScanInitialImplicitRoots(recycler);
  755. });
  756. HeapBlockList::ForEach(newMediumFinalizableWithBarrierHeapBlockList, [this](MediumFinalizableWithBarrierHeapBlock * heapBlock)
  757. {
  758. heapBlock->ScanInitialImplicitRoots(recycler);
  759. });
  760. #endif
  761. HeapBlockList::ForEach(newMediumFinalizableHeapBlockList, [this](MediumNormalHeapBlock * heapBlock)
  762. {
  763. heapBlock->ScanInitialImplicitRoots(recycler);
  764. });
  765. #endif
  766. }
  767. void
  768. HeapInfo::ScanNewImplicitRoots()
  769. {
  770. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  771. {
  772. heapBuckets[i].ScanNewImplicitRoots(recycler);
  773. }
  774. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  775. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  776. {
  777. mediumHeapBuckets[i].ScanNewImplicitRoots(recycler);
  778. }
  779. #endif
  780. largeObjectBucket.ScanNewImplicitRoots(recycler);
  781. #if ENABLE_CONCURRENT_GC
  782. // NOTE: need to do newLeafHeapBlockList to find new memory
  783. HeapBlockList::ForEach(newLeafHeapBlockList, [this](SmallLeafHeapBlock * heapBlock)
  784. {
  785. heapBlock->ScanNewImplicitRoots(recycler);
  786. });
  787. HeapBlockList::ForEach(newNormalHeapBlockList, [this](SmallNormalHeapBlock * heapBlock)
  788. {
  789. heapBlock->ScanNewImplicitRoots(recycler);
  790. });
  791. #ifdef RECYCLER_WRITE_BARRIER
  792. HeapBlockList::ForEach(newNormalWithBarrierHeapBlockList, [this](SmallNormalWithBarrierHeapBlock * heapBlock)
  793. {
  794. heapBlock->ScanNewImplicitRoots(recycler);
  795. });
  796. HeapBlockList::ForEach(newFinalizableWithBarrierHeapBlockList, [this](SmallFinalizableWithBarrierHeapBlock * heapBlock)
  797. {
  798. heapBlock->ScanNewImplicitRoots(recycler);
  799. });
  800. #endif
  801. HeapBlockList::ForEach(newFinalizableHeapBlockList, [this](SmallNormalHeapBlock * heapBlock)
  802. {
  803. heapBlock->ScanNewImplicitRoots(recycler);
  804. });
  805. // NOTE: need to do newLeafHeapBlockList to find new memory
  806. HeapBlockList::ForEach(newMediumLeafHeapBlockList, [this](MediumLeafHeapBlock * heapBlock)
  807. {
  808. heapBlock->ScanNewImplicitRoots(recycler);
  809. });
  810. HeapBlockList::ForEach(newMediumNormalHeapBlockList, [this](MediumNormalHeapBlock * heapBlock)
  811. {
  812. heapBlock->ScanNewImplicitRoots(recycler);
  813. });
  814. #ifdef RECYCLER_WRITE_BARRIER
  815. HeapBlockList::ForEach(newMediumNormalWithBarrierHeapBlockList, [this](MediumNormalWithBarrierHeapBlock * heapBlock)
  816. {
  817. heapBlock->ScanNewImplicitRoots(recycler);
  818. });
  819. HeapBlockList::ForEach(newMediumFinalizableWithBarrierHeapBlockList, [this](MediumFinalizableWithBarrierHeapBlock * heapBlock)
  820. {
  821. heapBlock->ScanNewImplicitRoots(recycler);
  822. });
  823. #endif
  824. HeapBlockList::ForEach(newMediumFinalizableHeapBlockList, [this](MediumNormalHeapBlock * heapBlock)
  825. {
  826. heapBlock->ScanNewImplicitRoots(recycler);
  827. });
  828. #endif
  829. }
  830. LargeHeapBlock *
  831. HeapInfo::AddLargeHeapBlock(size_t size)
  832. {
  833. // Do a no-throwing allocation here
  834. return largeObjectBucket.AddLargeHeapBlock(size, /* nothrow = */ true);
  835. }
  836. void HeapInfo::SweepBuckets(RecyclerSweep& recyclerSweep, bool concurrent)
  837. {
  838. Recycler * recycler = recyclerSweep.GetRecycler();
  839. // TODO: Remove below workaround for unreferenced local after enabled -profile for GC
  840. static_cast<Recycler*>(recycler);
  841. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  842. {
  843. heapBuckets[i].SweepFinalizableObjects(recyclerSweep);
  844. }
  845. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  846. // CONCURRENT-TODO: Allow this in the background as well
  847. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  848. {
  849. mediumHeapBuckets[i].SweepFinalizableObjects(recyclerSweep);
  850. }
  851. #endif
  852. #if ENABLE_CONCURRENT_GC
  853. if (concurrent)
  854. {
  855. RECYCLER_SLOW_CHECK(VerifySmallHeapBlockCount());
  856. RECYCLER_SLOW_CHECK(VerifyLargeHeapBlockCount());
  857. }
  858. if (concurrent)
  859. {
  860. this->SetupBackgroundSweep(recyclerSweep);
  861. }
  862. else
  863. #endif
  864. {
  865. this->SweepSmallNonFinalizable(recyclerSweep);
  866. }
  867. RECYCLER_PROFILE_EXEC_CHANGE(recycler, Js::SweepSmallPhase, Js::SweepLargePhase);
  868. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !(SMALLBLOCK_MEDIUM_ALLOC)
  869. // CONCURRENT-TODO: Allow this in the background as well
  870. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  871. {
  872. mediumHeapBuckets[i].Sweep(recyclerSweep);
  873. }
  874. #endif
  875. largeObjectBucket.Sweep(recyclerSweep);
  876. }
  877. void
  878. HeapInfo::Sweep(RecyclerSweep& recyclerSweep, bool concurrent)
  879. {
  880. #ifdef RECYCLER_FINALIZE_CHECK
  881. this->newFinalizableObjectCount = 0;
  882. #endif
  883. // Initialize this to false. Individual heap buckets can set it to true
  884. Recycler * recycler = recyclerSweep.GetRecycler();
  885. RECYCLER_PROFILE_EXEC_BEGIN(recycler, Js::SweepSmallPhase);
  886. #ifdef RECYCLER_STATS
  887. memset(&recycler->collectionStats.numEmptySmallBlocks, 0, sizeof(recycler->collectionStats.numEmptySmallBlocks));
  888. recycler->collectionStats.numZeroedOutSmallBlocks = 0;
  889. #endif
  890. RECYCLER_SLOW_CHECK(VerifySmallHeapBlockCount());
  891. // Call finalize before sweeping so that the finalizer can still access object it referenced
  892. largeObjectBucket.Finalize();
  893. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !(SMALLBLOCK_MEDIUM_ALLOC)
  894. for (uint i=0; i < HeapConstants::MediumBucketCount; i++)
  895. {
  896. mediumHeapBuckets[i].Finalize();
  897. }
  898. #endif
  899. #if ENABLE_CONCURRENT_GC
  900. // Merge the new blocks before we sweep the finalizable object in thread
  901. recyclerSweep.MergePendingNewHeapBlockList<SmallFinalizableHeapBlock>();
  902. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumFinalizableHeapBlock>();
  903. #ifdef RECYCLER_WRITE_BARRIER
  904. recyclerSweep.MergePendingNewHeapBlockList<SmallFinalizableWithBarrierHeapBlock>();
  905. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumFinalizableWithBarrierHeapBlock>();
  906. #endif
  907. #ifdef RECYCLER_VISITED_HOST
  908. recyclerSweep.MergePendingNewHeapBlockList<SmallRecyclerVisitedHostHeapBlock>();
  909. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumRecyclerVisitedHostHeapBlock>();
  910. #endif
  911. #endif
  912. SweepBuckets(recyclerSweep, concurrent);
  913. RECYCLER_PROFILE_EXEC_END(recycler, Js::SweepLargePhase);
  914. RECYCLER_SLOW_CHECK(VerifyLargeHeapBlockCount());
  915. RECYCLER_SLOW_CHECK(Assert(this->newFinalizableObjectCount == 0));
  916. }
  917. #if ENABLE_CONCURRENT_GC
  918. void
  919. HeapInfo::SetupBackgroundSweep(RecyclerSweep& recyclerSweep)
  920. {
  921. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  922. {
  923. this->heapBuckets[i].SetupBackgroundSweep(recyclerSweep);
  924. }
  925. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  926. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  927. {
  928. this->mediumHeapBuckets[i].SetupBackgroundSweep(recyclerSweep);
  929. }
  930. #endif
  931. }
  932. #endif
  933. void
  934. HeapInfo::SweepSmallNonFinalizable(RecyclerSweep& recyclerSweep)
  935. {
  936. #if ENABLE_CONCURRENT_GC
  937. recyclerSweep.MergePendingNewHeapBlockList<SmallLeafHeapBlock>();
  938. recyclerSweep.MergePendingNewHeapBlockList<SmallNormalHeapBlock>();
  939. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumLeafHeapBlock>();
  940. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumNormalHeapBlock>();
  941. #ifdef RECYCLER_WRITE_BARRIER
  942. recyclerSweep.MergePendingNewHeapBlockList<SmallNormalWithBarrierHeapBlock>();
  943. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumNormalWithBarrierHeapBlock>();
  944. #endif
  945. // Finalizable are already merge before in SweepHeap
  946. Assert(!recyclerSweep.HasPendingNewHeapBlocks());
  947. #endif
  948. if (!recyclerSweep.IsBackground())
  949. {
  950. // finalizer may trigger arena allocations, do don't suspend the leaf (thread) page allocator
  951. // until we are going to sweep leaf pages.
  952. recycler->GetRecyclerLeafPageAllocator()->SuspendIdleDecommit();
  953. }
  954. for (uint i=0; i<HeapConstants::BucketCount; i++)
  955. {
  956. heapBuckets[i].Sweep(recyclerSweep);
  957. }
  958. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  959. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  960. {
  961. mediumHeapBuckets[i].Sweep(recyclerSweep);
  962. }
  963. #endif
  964. if (!recyclerSweep.IsBackground())
  965. {
  966. // large block don't use the leaf page allocator, we can resume idle decommit now
  967. recycler->GetRecyclerLeafPageAllocator()->ResumeIdleDecommit();
  968. RECYCLER_SLOW_CHECK(VerifySmallHeapBlockCount());
  969. RECYCLER_SLOW_CHECK(VerifyLargeHeapBlockCount());
  970. }
  971. }
  972. size_t
  973. HeapInfo::Rescan(RescanFlags flags)
  974. {
  975. size_t scannedPageCount = 0;
  976. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  977. {
  978. scannedPageCount += heapBuckets[i].Rescan(recycler, flags);
  979. }
  980. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  981. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  982. {
  983. #if SMALLBLOCK_MEDIUM_ALLOC
  984. scannedPageCount += mediumHeapBuckets[i].Rescan(recycler, flags);
  985. #else
  986. scannedPageCount += mediumHeapBuckets[i].Rescan(flags);
  987. #endif
  988. }
  989. #endif
  990. scannedPageCount += largeObjectBucket.Rescan(flags);
  991. return scannedPageCount;
  992. }
  993. #if ENABLE_MEM_STATS
  994. #ifdef DUMP_FRAGMENTATION_STATS
  995. template <ObjectInfoBits TBucketType>
  996. struct DumpBucketTypeName { static char16 name[]; };
  997. template<> char16 DumpBucketTypeName<NoBit>::name[] = _u("Normal ");
  998. template<> char16 DumpBucketTypeName<LeafBit>::name[] = _u("Leaf ");
  999. template<> char16 DumpBucketTypeName<FinalizeBit>::name[] = _u("Fin ");
  1000. #ifdef RECYCLER_WRITE_BARRIER
  1001. template<> char16 DumpBucketTypeName<WithBarrierBit>::name[] = _u("NormWB ");
  1002. template<> char16 DumpBucketTypeName<FinalizableWithBarrierBit>::name[] = _u("FinWB ");
  1003. #endif
  1004. #ifdef RECYCLER_VISITED_HOST
  1005. template<> char16 DumpBucketTypeName<RecyclerVisitedHostBit>::name[] = _u("Visited");
  1006. #endif
  1007. template <typename TBlockType>
  1008. struct DumpBlockTypeName { static char16 name[]; };
  1009. template<> char16 DumpBlockTypeName<SmallAllocationBlockAttributes>::name[] = _u("(S)");
  1010. template<> char16 DumpBlockTypeName<MediumAllocationBlockAttributes>::name[] = _u("(M)");
  1011. #endif // DUMP_FRAGMENTATION_STATS
  1012. template <ObjectInfoBits TBucketType>
  1013. struct EtwBucketTypeEnum { static uint16 code; };
  1014. template<> uint16 EtwBucketTypeEnum<NoBit>::code = 0;
  1015. template<> uint16 EtwBucketTypeEnum<LeafBit>::code = 1;
  1016. template<> uint16 EtwBucketTypeEnum<FinalizeBit>::code = 2;
  1017. #ifdef RECYCLER_WRITE_BARRIER
  1018. template<> uint16 EtwBucketTypeEnum<WithBarrierBit>::code = 3;
  1019. template<> uint16 EtwBucketTypeEnum<FinalizableWithBarrierBit>::code = 4;
  1020. #endif
  1021. #ifdef RECYCLER_VISITED_HOST
  1022. template<> uint16 EtwBucketTypeEnum<RecyclerVisitedHostBit>::code = 5;
  1023. #endif
  1024. template <typename TBlockType>
  1025. struct EtwBlockTypeEnum { static uint16 code; };
  1026. template<> uint16 EtwBlockTypeEnum<SmallAllocationBlockAttributes>::code = 0;
  1027. template<> uint16 EtwBlockTypeEnum<MediumAllocationBlockAttributes>::code = 1;
  1028. class BucketStatsReporter
  1029. {
  1030. private:
  1031. static const uint16 LargeBucketNameCode = 2 << 8;
  1032. static const uint16 TotalBucketNameCode = 3 << 8;
  1033. Recycler* recycler;
  1034. HeapBucketStats total;
  1035. template <class TBlockAttributes, ObjectInfoBits TBucketType>
  1036. uint16 BucketNameCode() const
  1037. {
  1038. return EtwBucketTypeEnum<TBucketType>::code + (EtwBlockTypeEnum<TBlockAttributes>::code << 8);
  1039. }
  1040. bool IsMemProtectMode() const
  1041. {
  1042. return recycler->IsMemProtectMode();
  1043. }
  1044. public:
  1045. BucketStatsReporter(Recycler* recycler)
  1046. : recycler(recycler)
  1047. {
  1048. DUMP_FRAGMENTATION_STATS_ONLY(DumpHeader());
  1049. }
  1050. bool IsEtwEnabled() const
  1051. {
  1052. return IS_GCETW_Enabled(GC_BUCKET_STATS);
  1053. }
  1054. bool IsDumpEnabled() const
  1055. {
  1056. return DUMP_FRAGMENTATION_STATS_IS(!!recycler->GetRecyclerFlagsTable().DumpFragmentationStats);
  1057. }
  1058. bool IsEnabled() const
  1059. {
  1060. return IsEtwEnabled() || IsDumpEnabled();
  1061. }
  1062. template <class TBlockType>
  1063. void PreAggregateBucketStats(TBlockType* list)
  1064. {
  1065. HeapBlockList::ForEach(list, [](TBlockType* heapBlock)
  1066. {
  1067. // Process blocks not in allocator in pre-pass. They are not put into buckets yet.
  1068. if (!heapBlock->IsInAllocator())
  1069. {
  1070. heapBlock->heapBucket->PreAggregateBucketStats(heapBlock);
  1071. }
  1072. });
  1073. }
  1074. template <class TBlockAttributes, ObjectInfoBits TBucketType>
  1075. void GetBucketStats(HeapBucketGroup<TBlockAttributes>& group)
  1076. {
  1077. auto& bucket = group.template GetBucket<TBucketType>();
  1078. bucket.AggregateBucketStats();
  1079. const auto& stats = bucket.GetMemStats();
  1080. total.Aggregate(stats);
  1081. if (stats.totalByteCount > 0)
  1082. {
  1083. const uint16 bucketNameCode = BucketNameCode<TBlockAttributes, TBucketType>();
  1084. const uint16 sizeCat = static_cast<uint16>(bucket.GetSizeCat());
  1085. GCETW(GC_BUCKET_STATS, (recycler, bucketNameCode, sizeCat, stats.objectByteCount, stats.totalByteCount));
  1086. #ifdef DUMP_FRAGMENTATION_STATS
  1087. DumpStats<TBlockAttributes, TBucketType>(sizeCat, stats);
  1088. #endif
  1089. }
  1090. }
  1091. void GetBucketStats(LargeHeapBucket& bucket)
  1092. {
  1093. bucket.AggregateBucketStats();
  1094. const auto& stats = bucket.GetMemStats();
  1095. total.Aggregate(stats);
  1096. if (stats.totalByteCount > 0)
  1097. {
  1098. const uint16 sizeCat = static_cast<uint16>(bucket.GetSizeCat());
  1099. GCETW(GC_BUCKET_STATS, (recycler, LargeBucketNameCode, sizeCat, stats.objectByteCount, stats.totalByteCount));
  1100. DUMP_FRAGMENTATION_STATS_ONLY(DumpLarge(stats));
  1101. }
  1102. }
  1103. void Report()
  1104. {
  1105. GCETW(GC_BUCKET_STATS, (recycler, TotalBucketNameCode, 0, total.objectByteCount, total.totalByteCount));
  1106. DUMP_FRAGMENTATION_STATS_ONLY(DumpFooter());
  1107. }
  1108. #ifdef DUMP_FRAGMENTATION_STATS
  1109. void DumpHeader()
  1110. {
  1111. if (IsDumpEnabled())
  1112. {
  1113. Output::Print(_u("[FRAG %d] Post-Collection State\n"), ::GetTickCount());
  1114. Output::Print(_u("---------------------------------------------------------------------------------------\n"));
  1115. Output::Print(_u(" #Blk #Objs #Fin ObjBytes FreeBytes TotalBytes UsedPercent\n"));
  1116. Output::Print(_u("---------------------------------------------------------------------------------------\n"));
  1117. }
  1118. }
  1119. template <class TBlockAttributes, ObjectInfoBits TBucketType>
  1120. void DumpStats(uint sizeCat, const HeapBucketStats& stats)
  1121. {
  1122. if (IsDumpEnabled())
  1123. {
  1124. Output::Print(_u("%-7s%s %4d : "),
  1125. DumpBucketTypeName<TBucketType>::name, DumpBlockTypeName<TBlockAttributes>::name, sizeCat);
  1126. stats.Dump();
  1127. }
  1128. }
  1129. void DumpLarge(const HeapBucketStats& stats)
  1130. {
  1131. if (IsDumpEnabled())
  1132. {
  1133. Output::Print(_u("Large : "));
  1134. stats.Dump();
  1135. }
  1136. }
  1137. void DumpFooter()
  1138. {
  1139. if (IsDumpEnabled())
  1140. {
  1141. Output::Print(_u("---------------------------------------------------------------------------------------\n"));
  1142. Output::Print(_u("Total : "));
  1143. total.Dump();
  1144. }
  1145. }
  1146. #endif
  1147. };
  1148. void
  1149. HeapInfo::ReportMemStats()
  1150. {
  1151. BucketStatsReporter report(recycler);
  1152. if (!report.IsEnabled())
  1153. {
  1154. return;
  1155. }
  1156. #if ENABLE_CONCURRENT_GC
  1157. // Pre aggregate pass on all the heap blocks that are not merged into bucket's lists yet
  1158. report.PreAggregateBucketStats(this->newNormalHeapBlockList);
  1159. report.PreAggregateBucketStats(this->newLeafHeapBlockList);
  1160. report.PreAggregateBucketStats(this->newFinalizableHeapBlockList);
  1161. #ifdef RECYCLER_WRITE_BARRIER
  1162. report.PreAggregateBucketStats(this->newNormalWithBarrierHeapBlockList);
  1163. report.PreAggregateBucketStats(this->newFinalizableWithBarrierHeapBlockList);
  1164. #endif
  1165. #ifdef RECYCLER_VISITED_HOST
  1166. report.PreAggregateBucketStats(this->newRecyclerVisitedHostHeapBlockList);
  1167. #endif
  1168. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1169. report.PreAggregateBucketStats(this->newMediumNormalHeapBlockList);
  1170. report.PreAggregateBucketStats(this->newMediumLeafHeapBlockList);
  1171. report.PreAggregateBucketStats(this->newMediumFinalizableHeapBlockList);
  1172. #ifdef RECYCLER_WRITE_BARRIER
  1173. report.PreAggregateBucketStats(this->newMediumNormalWithBarrierHeapBlockList);
  1174. report.PreAggregateBucketStats(this->newMediumFinalizableWithBarrierHeapBlockList);
  1175. #endif
  1176. #ifdef RECYCLER_VISITED_HOST
  1177. report.PreAggregateBucketStats(this->newMediumRecyclerVisitedHostHeapBlockList);
  1178. #endif
  1179. #endif
  1180. #endif // ENABLE_CONCURRENT_GC
  1181. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1182. {
  1183. report.GetBucketStats<SmallAllocationBlockAttributes, NoBit>(heapBuckets[i]);
  1184. report.GetBucketStats<SmallAllocationBlockAttributes, LeafBit>(heapBuckets[i]);
  1185. report.GetBucketStats<SmallAllocationBlockAttributes, FinalizeBit>(heapBuckets[i]);
  1186. #ifdef RECYCLER_WRITE_BARRIER
  1187. report.GetBucketStats<SmallAllocationBlockAttributes, WithBarrierBit>(heapBuckets[i]);
  1188. report.GetBucketStats<SmallAllocationBlockAttributes, FinalizableWithBarrierBit>(heapBuckets[i]);
  1189. #endif
  1190. #ifdef RECYCLER_VISITED_HOST
  1191. report.GetBucketStats<SmallAllocationBlockAttributes, RecyclerVisitedHostBit>(heapBuckets[i]);
  1192. #endif
  1193. }
  1194. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1195. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1196. {
  1197. report.GetBucketStats<MediumAllocationBlockAttributes, NoBit>(mediumHeapBuckets[i]);
  1198. report.GetBucketStats<MediumAllocationBlockAttributes, LeafBit>(mediumHeapBuckets[i]);
  1199. report.GetBucketStats<MediumAllocationBlockAttributes, FinalizeBit>(mediumHeapBuckets[i]);
  1200. #ifdef RECYCLER_WRITE_BARRIER
  1201. report.GetBucketStats<MediumAllocationBlockAttributes, WithBarrierBit>(mediumHeapBuckets[i]);
  1202. report.GetBucketStats<MediumAllocationBlockAttributes, FinalizableWithBarrierBit>(mediumHeapBuckets[i]);
  1203. #endif
  1204. #ifdef RECYCLER_VISITED_HOST
  1205. report.GetBucketStats<MediumAllocationBlockAttributes, RecyclerVisitedHostBit>(mediumHeapBuckets[i]);
  1206. #endif
  1207. }
  1208. #endif
  1209. report.GetBucketStats(largeObjectBucket);
  1210. report.Report();
  1211. }
  1212. #endif // ENABLE_MEM_STATS
  1213. #if ENABLE_PARTIAL_GC
  1214. void
  1215. HeapInfo::SweepPartialReusePages(RecyclerSweep& recyclerSweep)
  1216. {
  1217. RECYCLER_PROFILE_EXEC_THREAD_BEGIN(recyclerSweep.IsBackground(), recyclerSweep.GetRecycler(), Js::SweepPartialReusePhase);
  1218. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1219. {
  1220. heapBuckets[i].SweepPartialReusePages(recyclerSweep);
  1221. }
  1222. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1223. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1224. {
  1225. mediumHeapBuckets[i].SweepPartialReusePages(recyclerSweep);
  1226. }
  1227. #endif
  1228. RECYCLER_PROFILE_EXEC_THREAD_END(recyclerSweep.IsBackground(), recyclerSweep.GetRecycler(), Js::SweepPartialReusePhase);
  1229. // GC-TODO: LargeHeapBlock don't reuse object, so we don't need to keep
  1230. // pages with low free space from being reused.
  1231. // Only count the byte that we would have freed but we are not reusing it if we are doing a partial GC
  1232. // This will increase the GC pressure and make partial less and less likely.
  1233. if (recyclerSweep.InPartialCollect())
  1234. {
  1235. this->unusedPartialCollectFreeBytes += recyclerSweep.GetPartialUnusedFreeByteCount();
  1236. }
  1237. }
  1238. void HeapInfo::FinishPartialCollect(RecyclerSweep * recyclerSweep)
  1239. {
  1240. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1241. {
  1242. heapBuckets[i].FinishPartialCollect(recyclerSweep);
  1243. }
  1244. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1245. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1246. {
  1247. mediumHeapBuckets[i].FinishPartialCollect(recyclerSweep);
  1248. }
  1249. #endif
  1250. largeObjectBucket.FinishPartialCollect(recyclerSweep);
  1251. }
  1252. #endif
  1253. #if ENABLE_CONCURRENT_GC
  1254. void
  1255. HeapInfo::PrepareSweep()
  1256. {
  1257. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1258. {
  1259. heapBuckets[i].PrepareSweep();
  1260. }
  1261. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1262. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1263. {
  1264. mediumHeapBuckets[i].PrepareSweep();
  1265. }
  1266. #endif
  1267. }
  1268. #endif
  1269. #if ENABLE_CONCURRENT_GC
  1270. void
  1271. HeapInfo::SweepPendingObjects(RecyclerSweep& recyclerSweep)
  1272. {
  1273. if (recyclerSweep.HasPendingSweepSmallHeapBlocks())
  1274. {
  1275. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1276. {
  1277. heapBuckets[i].SweepPendingObjects(recyclerSweep);
  1278. }
  1279. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1280. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1281. {
  1282. mediumHeapBuckets[i].SweepPendingObjects(recyclerSweep);
  1283. }
  1284. #endif
  1285. }
  1286. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !SMALLBLOCK_MEDIUM_ALLOC
  1287. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1288. {
  1289. mediumHeapBuckets[i].SweepPendingObjects(recyclerSweep);
  1290. }
  1291. #endif
  1292. largeObjectBucket.SweepPendingObjects(recyclerSweep);
  1293. }
  1294. #endif
  1295. #if ENABLE_CONCURRENT_GC && ENABLE_ALLOCATIONS_DURING_CONCURRENT_SWEEP
  1296. void HeapInfo::StartAllocationsDuringConcurrentSweep()
  1297. {
  1298. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1299. {
  1300. heapBuckets[i].StartAllocationDuringConcurrentSweep();
  1301. }
  1302. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1303. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1304. {
  1305. mediumHeapBuckets[i].StartAllocationDuringConcurrentSweep();
  1306. }
  1307. #endif
  1308. }
  1309. void
  1310. HeapInfo::FinishConcurrentSweep()
  1311. {
  1312. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1313. {
  1314. heapBuckets[i].FinishConcurrentSweep();
  1315. }
  1316. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1317. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1318. {
  1319. mediumHeapBuckets[i].FinishConcurrentSweep();
  1320. }
  1321. #endif
  1322. }
  1323. #endif
  1324. #if ENABLE_CONCURRENT_GC
  1325. void
  1326. HeapInfo::TransferPendingHeapBlocks(RecyclerSweep& recyclerSweep)
  1327. {
  1328. Assert(!recyclerSweep.IsBackground());
  1329. RECYCLER_SLOW_CHECK(VerifySmallHeapBlockCount());
  1330. if (recyclerSweep.HasPendingEmptyBlocks())
  1331. {
  1332. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1333. {
  1334. heapBuckets[i].TransferPendingEmptyHeapBlocks(recyclerSweep);
  1335. }
  1336. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1337. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1338. {
  1339. mediumHeapBuckets[i].TransferPendingEmptyHeapBlocks(recyclerSweep);
  1340. }
  1341. #endif
  1342. RECYCLER_SLOW_CHECK(VerifySmallHeapBlockCount());
  1343. }
  1344. // We might still have block that has been disposed but not made allocable
  1345. // which happens if we finish disposing object during concurrent sweep
  1346. // and can't modify the block lists
  1347. recyclerSweep.FlushPendingTransferDisposedObjects();
  1348. }
  1349. void
  1350. HeapInfo::ConcurrentTransferSweptObjects(RecyclerSweep& recyclerSweep)
  1351. {
  1352. #if ENABLE_PARTIAL_GC
  1353. Assert(!recyclerSweep.InPartialCollectMode());
  1354. #endif
  1355. Assert(!recyclerSweep.IsBackground());
  1356. TransferPendingHeapBlocks(recyclerSweep);
  1357. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !SMALLBLOCK_MEDIUM_ALLOC
  1358. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1359. {
  1360. mediumHeapBuckets[i].ConcurrentTransferSweptObjects(recyclerSweep);
  1361. }
  1362. #endif
  1363. largeObjectBucket.ConcurrentTransferSweptObjects(recyclerSweep);
  1364. }
  1365. #if ENABLE_PARTIAL_GC
  1366. void
  1367. HeapInfo::ConcurrentPartialTransferSweptObjects(RecyclerSweep& recyclerSweep)
  1368. {
  1369. Assert(recyclerSweep.InPartialCollectMode());
  1370. Assert(!recyclerSweep.IsBackground());
  1371. TransferPendingHeapBlocks(recyclerSweep);
  1372. RECYCLER_SLOW_CHECK(this->VerifyLargeHeapBlockCount());
  1373. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !SMALLBLOCK_MEDIUM_ALLOC
  1374. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1375. {
  1376. mediumHeapBuckets[i].ConcurrentPartialTransferSweptObjects(recyclerSweep);
  1377. }
  1378. #endif
  1379. largeObjectBucket.ConcurrentPartialTransferSweptObjects(recyclerSweep);
  1380. RECYCLER_SLOW_CHECK(this->VerifyLargeHeapBlockCount());
  1381. }
  1382. #endif
  1383. #endif
  1384. void
  1385. HeapInfo::DisposeObjects()
  1386. {
  1387. Recycler * recycler = this->recycler;
  1388. do
  1389. {
  1390. recycler->hasDisposableObject = false;
  1391. // finalizing the objects
  1392. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1393. {
  1394. heapBuckets[i].DisposeObjects();
  1395. }
  1396. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1397. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1398. {
  1399. mediumHeapBuckets[i].DisposeObjects();
  1400. }
  1401. #endif
  1402. largeObjectBucket.DisposeObjects();
  1403. }
  1404. // Calling dispose may enter the GC again and dispose more objects, loop until we don't have any more
  1405. while (recycler->hasDisposableObject);
  1406. recycler->hasPendingTransferDisposedObjects = true;
  1407. #if ENABLE_CONCURRENT_GC
  1408. if (!recycler->IsConcurrentExecutingState())
  1409. #endif
  1410. {
  1411. // Can't transfer disposed object when the background thread is walking the heap block list
  1412. // That includes reset mark, background rescan and concurrent sweep. Delay the transfer later.
  1413. // NOTE1: During concurrent sweep, we can't do this only if the bucket has "stopped" allocation
  1414. // After it resume allocation, we don't walk the list in the background thread any more
  1415. // (except for checking heap block count). But this is easier to detect via the collection state
  1416. // without walking all buckets.
  1417. // NOTE2: During transitive closure mark, we don't walk the heap block list, but we can continue
  1418. // to do background rescan. Since we don't have synchronization for that, we can't really enable
  1419. // able this just for the transitive closure, so just do all the background executing state.
  1420. TransferDisposedObjects();
  1421. }
  1422. }
  1423. void
  1424. HeapInfo::TransferDisposedObjects()
  1425. {
  1426. Recycler * recycler = this->recycler;
  1427. Assert(recycler->hasPendingTransferDisposedObjects);
  1428. #if ENABLE_CONCURRENT_GC
  1429. Assert(!recycler->IsConcurrentExecutingState());
  1430. #endif
  1431. recycler->hasPendingTransferDisposedObjects = false;
  1432. // move the disposed object back to the free lists
  1433. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1434. {
  1435. heapBuckets[i].TransferDisposedObjects();
  1436. }
  1437. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1438. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1439. {
  1440. mediumHeapBuckets[i].TransferDisposedObjects();
  1441. }
  1442. #endif
  1443. largeObjectBucket.TransferDisposedObjects();
  1444. }
  1445. void
  1446. HeapInfo::EnumerateObjects(ObjectInfoBits infoBits, void (*CallBackFunction)(void * address, size_t size))
  1447. {
  1448. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1449. {
  1450. heapBuckets[i].EnumerateObjects(infoBits, CallBackFunction);
  1451. }
  1452. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1453. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1454. {
  1455. mediumHeapBuckets[i].EnumerateObjects(infoBits, CallBackFunction);
  1456. }
  1457. #endif
  1458. largeObjectBucket.EnumerateObjects(infoBits, CallBackFunction);
  1459. #if ENABLE_CONCURRENT_GC
  1460. HeapBucket::EnumerateObjects(newLeafHeapBlockList, infoBits, CallBackFunction);
  1461. HeapBucket::EnumerateObjects(newNormalHeapBlockList, infoBits, CallBackFunction);
  1462. #ifdef RECYCLER_WRITE_BARRIER
  1463. HeapBucket::EnumerateObjects(newNormalWithBarrierHeapBlockList, infoBits, CallBackFunction);
  1464. HeapBucket::EnumerateObjects(newFinalizableWithBarrierHeapBlockList, infoBits, CallBackFunction);
  1465. #endif
  1466. #ifdef RECYCLER_VISITED_HOST
  1467. HeapBucket::EnumerateObjects(newRecyclerVisitedHostHeapBlockList, infoBits, CallBackFunction);
  1468. #endif
  1469. HeapBucket::EnumerateObjects(newFinalizableHeapBlockList, infoBits, CallBackFunction);
  1470. HeapBucket::EnumerateObjects(newMediumLeafHeapBlockList, infoBits, CallBackFunction);
  1471. HeapBucket::EnumerateObjects(newMediumNormalHeapBlockList, infoBits, CallBackFunction);
  1472. #ifdef RECYCLER_WRITE_BARRIER
  1473. HeapBucket::EnumerateObjects(newMediumNormalWithBarrierHeapBlockList, infoBits, CallBackFunction);
  1474. HeapBucket::EnumerateObjects(newMediumFinalizableWithBarrierHeapBlockList, infoBits, CallBackFunction);
  1475. #endif
  1476. #ifdef RECYCLER_VISITED_HOST
  1477. HeapBucket::EnumerateObjects(newMediumRecyclerVisitedHostHeapBlockList, infoBits, CallBackFunction);
  1478. #endif
  1479. HeapBucket::EnumerateObjects(newMediumFinalizableHeapBlockList, infoBits, CallBackFunction);
  1480. #endif
  1481. }
  1482. #if DBG || defined(RECYCLER_SLOW_CHECK_ENABLED)
  1483. size_t
  1484. HeapInfo::GetSmallHeapBlockCount(bool checkCount) const
  1485. {
  1486. size_t currentSmallHeapBlockCount = 0;
  1487. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1488. {
  1489. currentSmallHeapBlockCount += heapBuckets[i].GetNonEmptyHeapBlockCount(checkCount);
  1490. currentSmallHeapBlockCount += heapBuckets[i].GetEmptyHeapBlockCount();
  1491. }
  1492. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1493. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1494. {
  1495. currentSmallHeapBlockCount += mediumHeapBuckets[i].GetNonEmptyHeapBlockCount(checkCount);
  1496. currentSmallHeapBlockCount += mediumHeapBuckets[i].GetEmptyHeapBlockCount();
  1497. }
  1498. #endif
  1499. #if ENABLE_CONCURRENT_GC
  1500. currentSmallHeapBlockCount += HeapBlockList::Count(this->newLeafHeapBlockList);
  1501. currentSmallHeapBlockCount += HeapBlockList::Count(this->newNormalHeapBlockList);
  1502. currentSmallHeapBlockCount += HeapBlockList::Count(this->newFinalizableHeapBlockList);
  1503. #ifdef RECYCLER_VISITED_HOST
  1504. currentSmallHeapBlockCount += HeapBlockList::Count(this->newRecyclerVisitedHostHeapBlockList);
  1505. #endif
  1506. #ifdef RECYCLER_WRITE_BARRIER
  1507. currentSmallHeapBlockCount += HeapBlockList::Count(this->newNormalWithBarrierHeapBlockList);
  1508. currentSmallHeapBlockCount += HeapBlockList::Count(this->newFinalizableWithBarrierHeapBlockList);
  1509. #endif
  1510. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumLeafHeapBlockList);
  1511. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumNormalHeapBlockList);
  1512. #ifdef RECYCLER_VISITED_HOST
  1513. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumRecyclerVisitedHostHeapBlockList);
  1514. #endif
  1515. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumFinalizableHeapBlockList);
  1516. #ifdef RECYCLER_WRITE_BARRIER
  1517. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumNormalWithBarrierHeapBlockList);
  1518. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumFinalizableWithBarrierHeapBlockList);
  1519. #endif
  1520. // TODO: Update recycler sweep
  1521. // Recycler can be null if we have OOM in the ctor
  1522. if (this->recycler && this->recycler->recyclerSweep != nullptr)
  1523. {
  1524. // This function can't be called in the background
  1525. Assert(!this->recycler->recyclerSweep->IsBackground());
  1526. currentSmallHeapBlockCount += this->recycler->recyclerSweep->SetPendingMergeNewHeapBlockCount();
  1527. }
  1528. #endif
  1529. #ifdef RECYCLER_SLOW_CHECK_ENABLED
  1530. size_t expectedHeapBlockCount =
  1531. this->heapBlockCount[HeapBlock::HeapBlockType::SmallNormalBlockType]
  1532. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallLeafBlockType]
  1533. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallFinalizableBlockType]
  1534. #ifdef RECYCLER_VISITED_HOST
  1535. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallRecyclerVisitedHostBlockType]
  1536. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumRecyclerVisitedHostBlockType]
  1537. #endif
  1538. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumNormalBlockType]
  1539. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumLeafBlockType]
  1540. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumFinalizableBlockType];
  1541. #ifdef RECYCLER_WRITE_BARRIER
  1542. expectedHeapBlockCount +=
  1543. this->heapBlockCount[HeapBlock::HeapBlockType::SmallNormalBlockWithBarrierType]
  1544. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallFinalizableBlockWithBarrierType]
  1545. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumNormalBlockWithBarrierType]
  1546. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumFinalizableBlockWithBarrierType];
  1547. #endif
  1548. Assert(!checkCount || currentSmallHeapBlockCount == expectedHeapBlockCount);
  1549. #endif
  1550. return currentSmallHeapBlockCount;
  1551. }
  1552. size_t
  1553. HeapInfo::GetLargeHeapBlockCount(bool checkCount) const
  1554. {
  1555. size_t currentLargeHeapBlockCount = 0;
  1556. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !SMALLBLOCK_MEDIUM_ALLOC
  1557. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1558. {
  1559. currentLargeHeapBlockCount += mediumHeapBuckets[i].GetLargeHeapBlockCount(checkCount);
  1560. }
  1561. #endif
  1562. currentLargeHeapBlockCount += largeObjectBucket.GetLargeHeapBlockCount(checkCount);
  1563. RECYCLER_SLOW_CHECK(Assert(!checkCount || currentLargeHeapBlockCount == this->heapBlockCount[HeapBlock::HeapBlockType::LargeBlockType]));
  1564. return currentLargeHeapBlockCount;
  1565. }
  1566. #endif
  1567. #ifdef RECYCLER_SLOW_CHECK_ENABLED
  1568. void
  1569. HeapInfo::Check()
  1570. {
  1571. Assert(!this->recycler->CollectionInProgress());
  1572. size_t currentSmallHeapBlockCount = 0;
  1573. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1574. {
  1575. currentSmallHeapBlockCount += heapBuckets[i].Check();
  1576. currentSmallHeapBlockCount += heapBuckets[i].GetEmptyHeapBlockCount();
  1577. }
  1578. size_t currentLargeHeapBlockCount = 0;
  1579. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1580. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1581. {
  1582. #if SMALLBLOCK_MEDIUM_ALLOC
  1583. currentSmallHeapBlockCount += mediumHeapBuckets[i].Check();
  1584. currentSmallHeapBlockCount += mediumHeapBuckets[i].GetEmptyHeapBlockCount();
  1585. #else
  1586. currentLargeHeapBlockCount += mediumHeapBuckets[i].Check();
  1587. #endif
  1588. }
  1589. #endif
  1590. #if ENABLE_CONCURRENT_GC
  1591. currentSmallHeapBlockCount += Check(true, false, this->newLeafHeapBlockList);
  1592. currentSmallHeapBlockCount += Check(true, false, this->newNormalHeapBlockList);
  1593. #ifdef RECYCLER_WRITE_BARRIER
  1594. currentSmallHeapBlockCount += Check(true, false, this->newNormalWithBarrierHeapBlockList);
  1595. currentSmallHeapBlockCount += Check(true, false, this->newFinalizableWithBarrierHeapBlockList);
  1596. #endif
  1597. currentSmallHeapBlockCount += Check(true, false, this->newFinalizableHeapBlockList);
  1598. #ifdef RECYCLER_VISITED_HOST
  1599. currentSmallHeapBlockCount += Check(true, false, this->newRecyclerVisitedHostHeapBlockList);
  1600. currentSmallHeapBlockCount += Check(true, false, this->newMediumRecyclerVisitedHostHeapBlockList);
  1601. #endif
  1602. #endif
  1603. #if ENABLE_CONCURRENT_GC
  1604. currentSmallHeapBlockCount += Check(true, false, this->newMediumLeafHeapBlockList);
  1605. currentSmallHeapBlockCount += Check(true, false, this->newMediumNormalHeapBlockList);
  1606. #ifdef RECYCLER_WRITE_BARRIER
  1607. currentSmallHeapBlockCount += Check(true, false, this->newMediumNormalWithBarrierHeapBlockList);
  1608. currentSmallHeapBlockCount += Check(true, false, this->newMediumFinalizableWithBarrierHeapBlockList);
  1609. #endif
  1610. currentSmallHeapBlockCount += Check(true, false, this->newMediumFinalizableHeapBlockList);
  1611. #endif
  1612. size_t expectedHeapBlockCount =
  1613. this->heapBlockCount[HeapBlock::HeapBlockType::SmallNormalBlockType]
  1614. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallLeafBlockType]
  1615. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallFinalizableBlockType]
  1616. #ifdef RECYCLER_VISITED_HOST
  1617. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallRecyclerVisitedHostBlockType]
  1618. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumRecyclerVisitedHostBlockType]
  1619. #endif
  1620. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumNormalBlockType]
  1621. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumLeafBlockType]
  1622. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumFinalizableBlockType];
  1623. #ifdef RECYCLER_WRITE_BARRIER
  1624. expectedHeapBlockCount +=
  1625. this->heapBlockCount[HeapBlock::HeapBlockType::SmallNormalBlockWithBarrierType]
  1626. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallFinalizableBlockWithBarrierType]
  1627. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumNormalBlockWithBarrierType]
  1628. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumFinalizableBlockWithBarrierType];
  1629. #endif
  1630. Assert(currentSmallHeapBlockCount == expectedHeapBlockCount);
  1631. currentLargeHeapBlockCount += largeObjectBucket.Check();
  1632. Assert(currentLargeHeapBlockCount == this->heapBlockCount[HeapBlock::HeapBlockType::LargeBlockType]);
  1633. }
  1634. template <typename TBlockType>
  1635. size_t
  1636. HeapInfo::Check(bool expectFull, bool expectPending, TBlockType * list, TBlockType * tail)
  1637. {
  1638. size_t heapBlockCount = 0;
  1639. HeapBlockList::ForEach(list, tail, [&heapBlockCount, expectFull, expectPending](TBlockType * heapBlock)
  1640. {
  1641. heapBlock->Check(expectFull, expectPending);
  1642. heapBlockCount++;
  1643. });
  1644. return heapBlockCount;
  1645. }
  1646. template size_t HeapInfo::Check<SmallNormalHeapBlock>(bool expectFull, bool expectPending, SmallNormalHeapBlock * list, SmallNormalHeapBlock * tail);
  1647. template size_t HeapInfo::Check<SmallLeafHeapBlock>(bool expectFull, bool expectPending, SmallLeafHeapBlock * list, SmallLeafHeapBlock * tail);
  1648. template size_t HeapInfo::Check<SmallFinalizableHeapBlock>(bool expectFull, bool expectPending, SmallFinalizableHeapBlock * list, SmallFinalizableHeapBlock * tail);
  1649. #ifdef RECYCLER_VISITED_HOST
  1650. template size_t HeapInfo::Check<SmallRecyclerVisitedHostHeapBlock>(bool expectFull, bool expectPending, SmallRecyclerVisitedHostHeapBlock * list, SmallRecyclerVisitedHostHeapBlock * tail);
  1651. template size_t HeapInfo::Check<MediumRecyclerVisitedHostHeapBlock>(bool expectFull, bool expectPending, MediumRecyclerVisitedHostHeapBlock * list, MediumRecyclerVisitedHostHeapBlock * tail);
  1652. #endif
  1653. template size_t HeapInfo::Check<LargeHeapBlock>(bool expectFull, bool expectPending, LargeHeapBlock * list, LargeHeapBlock * tail);
  1654. #ifdef RECYCLER_WRITE_BARRIER
  1655. template size_t HeapInfo::Check<SmallNormalWithBarrierHeapBlock>(bool expectFull, bool expectPending, SmallNormalWithBarrierHeapBlock * list, SmallNormalWithBarrierHeapBlock * tail);
  1656. template size_t HeapInfo::Check<SmallFinalizableWithBarrierHeapBlock>(bool expectFull, bool expectPending, SmallFinalizableWithBarrierHeapBlock * list, SmallFinalizableWithBarrierHeapBlock * tail);
  1657. #endif
  1658. template size_t HeapInfo::Check<MediumNormalHeapBlock>(bool expectFull, bool expectPending, MediumNormalHeapBlock * list, MediumNormalHeapBlock * tail);
  1659. template size_t HeapInfo::Check<MediumLeafHeapBlock>(bool expectFull, bool expectPending, MediumLeafHeapBlock * list, MediumLeafHeapBlock * tail);
  1660. template size_t HeapInfo::Check<MediumFinalizableHeapBlock>(bool expectFull, bool expectPending, MediumFinalizableHeapBlock * list, MediumFinalizableHeapBlock * tail);
  1661. template size_t HeapInfo::Check<LargeHeapBlock>(bool expectFull, bool expectPending, LargeHeapBlock * list, LargeHeapBlock * tail);
  1662. #ifdef RECYCLER_WRITE_BARRIER
  1663. template size_t HeapInfo::Check<MediumNormalWithBarrierHeapBlock>(bool expectFull, bool expectPending, MediumNormalWithBarrierHeapBlock * list, MediumNormalWithBarrierHeapBlock * tail);
  1664. template size_t HeapInfo::Check<MediumFinalizableWithBarrierHeapBlock>(bool expectFull, bool expectPending, MediumFinalizableWithBarrierHeapBlock * list, MediumFinalizableWithBarrierHeapBlock * tail);
  1665. #endif
  1666. void
  1667. HeapInfo::VerifySmallHeapBlockCount()
  1668. {
  1669. GetSmallHeapBlockCount(true);
  1670. }
  1671. void
  1672. HeapInfo::VerifyLargeHeapBlockCount()
  1673. {
  1674. GetLargeHeapBlockCount(true);
  1675. }
  1676. #endif
  1677. #ifdef RECYCLER_MEMORY_VERIFY
  1678. void
  1679. HeapInfo::Verify()
  1680. {
  1681. Assert(!this->recycler->CollectionInProgress());
  1682. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1683. {
  1684. heapBuckets[i].Verify();
  1685. }
  1686. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1687. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1688. {
  1689. mediumHeapBuckets[i].Verify();
  1690. }
  1691. #endif
  1692. largeObjectBucket.Verify();
  1693. #if ENABLE_CONCURRENT_GC
  1694. HeapBlockList::ForEach(newLeafHeapBlockList, [](SmallLeafHeapBlock * heapBlock)
  1695. {
  1696. heapBlock->Verify();
  1697. });
  1698. HeapBlockList::ForEach(newNormalHeapBlockList, [](SmallNormalHeapBlock * heapBlock)
  1699. {
  1700. heapBlock->Verify();
  1701. });
  1702. #ifdef RECYCLER_WRITE_BARRIER
  1703. HeapBlockList::ForEach(newNormalWithBarrierHeapBlockList, [](SmallNormalWithBarrierHeapBlock * heapBlock)
  1704. {
  1705. heapBlock->Verify();
  1706. });
  1707. HeapBlockList::ForEach(newFinalizableWithBarrierHeapBlockList, [](SmallFinalizableWithBarrierHeapBlock * heapBlock)
  1708. {
  1709. heapBlock->Verify();
  1710. });
  1711. #endif
  1712. #ifdef RECYCLER_VISITED_HOST
  1713. HeapBlockList::ForEach(newRecyclerVisitedHostHeapBlockList, [](SmallFinalizableHeapBlock * heapBlock)
  1714. {
  1715. heapBlock->Verify();
  1716. });
  1717. #endif
  1718. HeapBlockList::ForEach(newFinalizableHeapBlockList, [](SmallFinalizableHeapBlock * heapBlock)
  1719. {
  1720. heapBlock->Verify();
  1721. });
  1722. #endif
  1723. #if ENABLE_CONCURRENT_GC
  1724. HeapBlockList::ForEach(newMediumLeafHeapBlockList, [](MediumLeafHeapBlock * heapBlock)
  1725. {
  1726. heapBlock->Verify();
  1727. });
  1728. HeapBlockList::ForEach(newMediumNormalHeapBlockList, [](MediumNormalHeapBlock * heapBlock)
  1729. {
  1730. heapBlock->Verify();
  1731. });
  1732. #ifdef RECYCLER_WRITE_BARRIER
  1733. HeapBlockList::ForEach(newMediumNormalWithBarrierHeapBlockList, [](MediumNormalWithBarrierHeapBlock * heapBlock)
  1734. {
  1735. heapBlock->Verify();
  1736. });
  1737. HeapBlockList::ForEach(newMediumFinalizableWithBarrierHeapBlockList, [](MediumFinalizableWithBarrierHeapBlock * heapBlock)
  1738. {
  1739. heapBlock->Verify();
  1740. });
  1741. #endif
  1742. #ifdef RECYCLER_VISITED_HOST
  1743. HeapBlockList::ForEach(newMediumRecyclerVisitedHostHeapBlockList, [](MediumFinalizableHeapBlock * heapBlock)
  1744. {
  1745. heapBlock->Verify();
  1746. });
  1747. #endif
  1748. HeapBlockList::ForEach(newMediumFinalizableHeapBlockList, [](MediumFinalizableHeapBlock * heapBlock)
  1749. {
  1750. heapBlock->Verify();
  1751. });
  1752. #endif
  1753. }
  1754. #endif
  1755. #ifdef RECYCLER_VERIFY_MARK
  1756. void
  1757. HeapInfo::VerifyMark()
  1758. {
  1759. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1760. {
  1761. heapBuckets[i].VerifyMark();
  1762. }
  1763. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1764. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1765. {
  1766. mediumHeapBuckets[i].VerifyMark();
  1767. }
  1768. #endif
  1769. largeObjectBucket.VerifyMark();
  1770. #if ENABLE_CONCURRENT_GC
  1771. HeapBlockList::ForEach(newLeafHeapBlockList, [](SmallLeafHeapBlock * heapBlock)
  1772. {
  1773. heapBlock->VerifyMark();
  1774. });
  1775. HeapBlockList::ForEach(newNormalHeapBlockList, [](SmallNormalHeapBlock * heapBlock)
  1776. {
  1777. heapBlock->VerifyMark();
  1778. });
  1779. #ifdef RECYCLER_WRITE_BARRIER
  1780. HeapBlockList::ForEach(newNormalWithBarrierHeapBlockList, [](SmallNormalWithBarrierHeapBlock * heapBlock)
  1781. {
  1782. heapBlock->VerifyMark();
  1783. });
  1784. HeapBlockList::ForEach(newFinalizableWithBarrierHeapBlockList, [](SmallFinalizableWithBarrierHeapBlock * heapBlock)
  1785. {
  1786. heapBlock->VerifyMark();
  1787. });
  1788. #endif
  1789. #ifdef RECYCLER_VISITED_HOST
  1790. HeapBlockList::ForEach(newRecyclerVisitedHostHeapBlockList, [](SmallFinalizableHeapBlock * heapBlock)
  1791. {
  1792. heapBlock->VerifyMark();
  1793. });
  1794. #endif
  1795. HeapBlockList::ForEach(newFinalizableHeapBlockList, [](SmallFinalizableHeapBlock * heapBlock)
  1796. {
  1797. heapBlock->VerifyMark();
  1798. });
  1799. #endif
  1800. #if ENABLE_CONCURRENT_GC
  1801. HeapBlockList::ForEach(newMediumLeafHeapBlockList, [](MediumLeafHeapBlock * heapBlock)
  1802. {
  1803. heapBlock->VerifyMark();
  1804. });
  1805. HeapBlockList::ForEach(newMediumNormalHeapBlockList, [](MediumNormalHeapBlock * heapBlock)
  1806. {
  1807. heapBlock->VerifyMark();
  1808. });
  1809. #ifdef RECYCLER_WRITE_BARRIER
  1810. HeapBlockList::ForEach(newMediumNormalWithBarrierHeapBlockList, [](MediumNormalWithBarrierHeapBlock * heapBlock)
  1811. {
  1812. heapBlock->VerifyMark();
  1813. });
  1814. HeapBlockList::ForEach(newMediumFinalizableWithBarrierHeapBlockList, [](MediumFinalizableWithBarrierHeapBlock * heapBlock)
  1815. {
  1816. heapBlock->VerifyMark();
  1817. });
  1818. #endif
  1819. #ifdef RECYCLER_VISITED_HOST
  1820. HeapBlockList::ForEach(newMediumRecyclerVisitedHostHeapBlockList, [](MediumFinalizableHeapBlock * heapBlock)
  1821. {
  1822. heapBlock->VerifyMark();
  1823. });
  1824. #endif
  1825. HeapBlockList::ForEach(newMediumFinalizableHeapBlockList, [](MediumFinalizableHeapBlock * heapBlock)
  1826. {
  1827. heapBlock->VerifyMark();
  1828. });
  1829. #endif
  1830. }
  1831. #endif
  1832. #ifdef RECYCLER_FINALIZE_CHECK
  1833. void
  1834. HeapInfo::VerifyFinalize()
  1835. {
  1836. // We can't check this if we are marking
  1837. Assert(!this->recycler->IsMarkState());
  1838. size_t currentFinalizableObjectCount = this->liveFinalizableObjectCount - this->newFinalizableObjectCount - this->pendingDisposableObjectCount;
  1839. #if DBG
  1840. Assert(currentFinalizableObjectCount == this->recycler->collectionStats.finalizeCount);
  1841. #else
  1842. if (currentFinalizableObjectCount != this->recycler->collectionStats.finalizeCount)
  1843. {
  1844. Output::Print(_u("ERROR: Recycler dropped some finalizable objects"));
  1845. DebugBreak();
  1846. }
  1847. #endif
  1848. }
  1849. #endif
  1850. #if DBG
  1851. bool
  1852. HeapInfo::AllocatorsAreEmpty()
  1853. {
  1854. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1855. {
  1856. if (!heapBuckets[i].AllocatorsAreEmpty())
  1857. {
  1858. return false;
  1859. }
  1860. }
  1861. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1862. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1863. {
  1864. if (!mediumHeapBuckets[i].AllocatorsAreEmpty())
  1865. {
  1866. return false;
  1867. }
  1868. }
  1869. #endif
  1870. return true;
  1871. }
  1872. #endif
  1873. // Block attribute functions
  1874. /* static */
  1875. BOOL SmallAllocationBlockAttributes::IsAlignedObjectSize(size_t sizeCat)
  1876. {
  1877. return HeapInfo::IsAlignedSmallObjectSize(sizeCat);
  1878. }
  1879. /* static */
  1880. BOOL MediumAllocationBlockAttributes::IsAlignedObjectSize(size_t sizeCat)
  1881. {
  1882. return HeapInfo::IsAlignedMediumObjectSize(sizeCat);
  1883. }
  1884. namespace Memory
  1885. {
  1886. template class HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>;
  1887. template class ValidPointers<SmallAllocationBlockAttributes>;
  1888. template class HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>;
  1889. template class ValidPointers<MediumAllocationBlockAttributes>;
  1890. };