SmallFinalizableHeapBucket.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. namespace Memory
  6. {
  7. template <class THeapBlockType>
  8. class SmallFinalizableHeapBucketBaseT : public SmallNormalHeapBucketBase<THeapBlockType>
  9. {
  10. typedef SmallNormalHeapBucketBase<THeapBlockType> BaseT;
  11. public:
  12. typedef typename THeapBlockType::HeapBlockAttributes TBlockAttributes;
  13. SmallFinalizableHeapBucketBaseT();
  14. ~SmallFinalizableHeapBucketBaseT();
  15. void DisposeObjects();
  16. void TransferDisposedObjects();
  17. void FinalizeAllObjects();
  18. static void FinalizeHeapBlockList(THeapBlockType * list);
  19. #if ENABLE_MEM_STATS
  20. void AggregateBucketStats();
  21. #endif
  22. protected:
  23. void EnumerateObjects(ObjectInfoBits infoBits, void (*CallBackFunction)(void * address, size_t size));
  24. friend class HeapBucket;
  25. template <class TBlockAttributes>
  26. friend class HeapBucketGroup;
  27. void ResetMarks(ResetMarkFlags flags);
  28. void Sweep(RecyclerSweep& recyclerSweep);
  29. #if DBG || defined(RECYCLER_SLOW_CHECK_ENABLED)
  30. size_t GetNonEmptyHeapBlockCount(bool checkCount) const;
  31. #endif
  32. #ifdef RECYCLER_SLOW_CHECK_ENABLED
  33. size_t Check();
  34. #endif
  35. #ifdef RECYCLER_MEMORY_VERIFY
  36. void Verify();
  37. #endif
  38. #ifdef RECYCLER_VERIFY_MARK
  39. void VerifyMark();
  40. #endif
  41. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  42. friend class ::ScriptMemoryDumper;
  43. #endif
  44. template <typename TBlockType>
  45. friend class HeapBucketT;
  46. protected:
  47. THeapBlockType * pendingDisposeList; // list of block that has finalizable object that needs to be disposed
  48. #if DBG || defined(RECYCLER_SLOW_CHECK_ENABLED)
  49. THeapBlockType * tempPendingDisposeList;
  50. #endif
  51. };
  52. template <class TBlockAttributes>
  53. class SmallFinalizableHeapBucketT : public SmallFinalizableHeapBucketBaseT<SmallFinalizableHeapBlockT<TBlockAttributes> >
  54. {
  55. };
  56. #ifdef RECYCLER_VISITED_HOST
  57. template <class TBlockAttributes>
  58. class SmallRecyclerVisitedHostHeapBucketT : public SmallFinalizableHeapBucketBaseT<SmallRecyclerVisitedHostHeapBlockT<TBlockAttributes> >
  59. {
  60. };
  61. #endif
  62. #ifdef RECYCLER_WRITE_BARRIER
  63. template <class TBlockAttributes>
  64. class SmallFinalizableWithBarrierHeapBucketT : public SmallFinalizableHeapBucketBaseT<SmallFinalizableWithBarrierHeapBlockT<TBlockAttributes> >
  65. {
  66. };
  67. #endif
  68. typedef SmallFinalizableHeapBucketT<MediumAllocationBlockAttributes> MediumFinalizableHeapBucket;
  69. typedef SmallFinalizableHeapBucketT<SmallAllocationBlockAttributes> SmallFinalizableHeapBucket;
  70. #ifdef RECYCLER_VISITED_HOST
  71. typedef SmallRecyclerVisitedHostHeapBucketT<MediumAllocationBlockAttributes> MediumRecyclerVisitedHostHeapBucket;
  72. typedef SmallRecyclerVisitedHostHeapBucketT<SmallAllocationBlockAttributes> SmallRecyclerVisitedHostHeapBucket;
  73. #endif
  74. #ifdef RECYCLER_WRITE_BARRIER
  75. typedef SmallFinalizableWithBarrierHeapBucketT<MediumAllocationBlockAttributes> MediumFinalizableWithBarrierHeapBucket;
  76. typedef SmallFinalizableWithBarrierHeapBucketT<SmallAllocationBlockAttributes> SmallFinalizableWithBarrierHeapBucket;
  77. #endif
  78. // GC-TODO: Move this away
  79. template <ObjectInfoBits attributes, class TBlockAttributes>
  80. class SmallHeapBlockType
  81. {
  82. public:
  83. CompileAssert((attributes & FinalizeBit) != 0);
  84. #ifdef RECYCLER_VISITED_HOST
  85. // attributes with RecyclerVisitedHostBit must use SmallRecyclerVisitedHostHeap{Bucket|Block}T
  86. CompileAssert((attributes & RecyclerVisitedHostBit) == 0);
  87. #endif
  88. typedef SmallFinalizableHeapBlockT<TBlockAttributes> BlockType;
  89. typedef SmallFinalizableHeapBucketT<TBlockAttributes> BucketType;
  90. };
  91. #ifdef RECYCLER_VISITED_HOST
  92. template <>
  93. class SmallHeapBlockType<(ObjectInfoBits)(RecyclerVisitedHostBit), SmallAllocationBlockAttributes>
  94. {
  95. public:
  96. typedef SmallRecyclerVisitedHostHeapBlock BlockType;
  97. typedef SmallRecyclerVisitedHostHeapBucket BucketType;
  98. };
  99. template <>
  100. class SmallHeapBlockType<(ObjectInfoBits)(RecyclerVisitedHostTracedFinalizableBlockTypeBits), SmallAllocationBlockAttributes>
  101. {
  102. public:
  103. typedef SmallRecyclerVisitedHostHeapBlock BlockType;
  104. typedef SmallRecyclerVisitedHostHeapBucket BucketType;
  105. };
  106. template <>
  107. class SmallHeapBlockType<(ObjectInfoBits)(RecyclerVisitedHostFinalizableBlockTypeBits), SmallAllocationBlockAttributes>
  108. {
  109. public:
  110. typedef SmallRecyclerVisitedHostHeapBlock BlockType;
  111. typedef SmallRecyclerVisitedHostHeapBucket BucketType;
  112. };
  113. #endif
  114. template <>
  115. class SmallHeapBlockType<LeafBit, SmallAllocationBlockAttributes>
  116. {
  117. public:
  118. typedef SmallLeafHeapBlock BlockType;
  119. typedef SmallLeafHeapBucketT<SmallAllocationBlockAttributes> BucketType;
  120. };
  121. template <>
  122. class SmallHeapBlockType<NoBit, SmallAllocationBlockAttributes>
  123. {
  124. public:
  125. typedef SmallNormalHeapBlock BlockType;
  126. typedef SmallNormalHeapBucket BucketType;
  127. };
  128. #ifdef RECYCLER_WRITE_BARRIER
  129. template <>
  130. class SmallHeapBlockType<WithBarrierBit, SmallAllocationBlockAttributes>
  131. {
  132. public:
  133. typedef SmallNormalWithBarrierHeapBlock BlockType;
  134. typedef SmallNormalWithBarrierHeapBucket BucketType;
  135. };
  136. template <>
  137. class SmallHeapBlockType<(ObjectInfoBits)(WithBarrierBit|LeafBit), SmallAllocationBlockAttributes>
  138. {
  139. public:
  140. typedef SmallLeafHeapBlock BlockType;
  141. typedef SmallLeafHeapBucketT<SmallAllocationBlockAttributes> BucketType;
  142. };
  143. template <>
  144. class SmallHeapBlockType<FinalizableWithBarrierBit, SmallAllocationBlockAttributes>
  145. {
  146. public:
  147. typedef SmallFinalizableWithBarrierHeapBlock BlockType;
  148. typedef SmallFinalizableWithBarrierHeapBucket BucketType;
  149. };
  150. #endif
  151. #if SMALLBLOCK_MEDIUM_ALLOC
  152. template <>
  153. class SmallHeapBlockType<FinalizeBit, MediumAllocationBlockAttributes>
  154. {
  155. public:
  156. typedef MediumFinalizableHeapBlock BlockType;
  157. typedef MediumFinalizableHeapBucket BucketType;
  158. };
  159. #ifdef RECYCLER_VISITED_HOST
  160. template <>
  161. class SmallHeapBlockType<(ObjectInfoBits)(RecyclerVisitedHostBit), MediumAllocationBlockAttributes>
  162. {
  163. public:
  164. typedef MediumRecyclerVisitedHostHeapBlock BlockType;
  165. typedef MediumRecyclerVisitedHostHeapBucket BucketType;
  166. };
  167. template <>
  168. class SmallHeapBlockType<(ObjectInfoBits)(RecyclerVisitedHostTracedFinalizableBlockTypeBits), MediumAllocationBlockAttributes>
  169. {
  170. public:
  171. typedef MediumRecyclerVisitedHostHeapBlock BlockType;
  172. typedef MediumRecyclerVisitedHostHeapBucket BucketType;
  173. };
  174. template <>
  175. class SmallHeapBlockType<(ObjectInfoBits)(RecyclerVisitedHostFinalizableBlockTypeBits), MediumAllocationBlockAttributes>
  176. {
  177. public:
  178. typedef MediumRecyclerVisitedHostHeapBlock BlockType;
  179. typedef MediumRecyclerVisitedHostHeapBucket BucketType;
  180. };
  181. #endif
  182. template <>
  183. class SmallHeapBlockType<LeafBit, MediumAllocationBlockAttributes>
  184. {
  185. public:
  186. typedef MediumLeafHeapBlock BlockType;
  187. typedef SmallLeafHeapBucketT<MediumAllocationBlockAttributes> BucketType;
  188. };
  189. template <>
  190. class SmallHeapBlockType<NoBit, MediumAllocationBlockAttributes>
  191. {
  192. public:
  193. typedef MediumNormalHeapBlock BlockType;
  194. typedef MediumNormalHeapBucket BucketType;
  195. };
  196. #ifdef RECYCLER_WRITE_BARRIER
  197. template <>
  198. class SmallHeapBlockType<WithBarrierBit, MediumAllocationBlockAttributes>
  199. {
  200. public:
  201. typedef MediumNormalWithBarrierHeapBlock BlockType;
  202. typedef MediumNormalWithBarrierHeapBucket BucketType;
  203. };
  204. template <>
  205. class SmallHeapBlockType<(ObjectInfoBits)(WithBarrierBit | LeafBit), MediumAllocationBlockAttributes>
  206. {
  207. public:
  208. typedef MediumLeafHeapBlock BlockType;
  209. typedef SmallLeafHeapBucketT<MediumAllocationBlockAttributes> BucketType;
  210. };
  211. template <>
  212. class SmallHeapBlockType<FinalizableWithBarrierBit, MediumAllocationBlockAttributes>
  213. {
  214. public:
  215. typedef MediumFinalizableWithBarrierHeapBlock BlockType;
  216. typedef MediumFinalizableWithBarrierHeapBucket BucketType;
  217. };
  218. #endif
  219. #endif
  220. template <class TBlockAttributes>
  221. class HeapBucketGroup
  222. {
  223. template <ObjectInfoBits objectAttributes>
  224. class BucketGetter
  225. {
  226. public:
  227. typedef typename SmallHeapBlockType<objectAttributes, TBlockAttributes>::BucketType BucketType;
  228. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  229. {
  230. CompileAssert(objectAttributes & FinalizeBit);
  231. return heapBucketGroup->finalizableHeapBucket;
  232. }
  233. };
  234. template <>
  235. class BucketGetter<LeafBit>
  236. {
  237. public:
  238. typedef typename SmallHeapBlockType<LeafBit, TBlockAttributes>::BucketType BucketType;
  239. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  240. {
  241. return heapBucketGroup->leafHeapBucket;
  242. }
  243. };
  244. template <>
  245. class BucketGetter<NoBit>
  246. {
  247. public:
  248. typedef typename SmallHeapBlockType<NoBit, TBlockAttributes>::BucketType BucketType;
  249. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  250. {
  251. return heapBucketGroup->heapBucket;
  252. }
  253. };
  254. template <>
  255. class BucketGetter<(ObjectInfoBits)(FinalizeBit | LeafBit)>
  256. {
  257. public:
  258. typedef typename SmallHeapBlockType<(ObjectInfoBits)(FinalizeBit | LeafBit), TBlockAttributes>::BucketType BucketType;
  259. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  260. {
  261. // TODO: SWB implemente finalizable leaf bucket
  262. return heapBucketGroup->finalizableHeapBucket;
  263. }
  264. };
  265. #ifdef RECYCLER_VISITED_HOST
  266. template <>
  267. class BucketGetter<(ObjectInfoBits)(RecyclerVisitedHostBit)>
  268. {
  269. public:
  270. typedef typename SmallHeapBlockType<(ObjectInfoBits)(RecyclerVisitedHostBit), TBlockAttributes>::BucketType BucketType;
  271. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * HeapBucketGroup)
  272. {
  273. return HeapBucketGroup->recyclerVisitedHostHeapBucket;
  274. }
  275. };
  276. template <>
  277. class BucketGetter<(ObjectInfoBits)(RecyclerVisitedHostTracedFinalizableBlockTypeBits)>
  278. {
  279. public:
  280. typedef typename SmallHeapBlockType<(ObjectInfoBits)(RecyclerVisitedHostTracedFinalizableBlockTypeBits), TBlockAttributes>::BucketType BucketType;
  281. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * HeapBucketGroup)
  282. {
  283. return HeapBucketGroup->recyclerVisitedHostHeapBucket;
  284. }
  285. };
  286. template <>
  287. class BucketGetter<(ObjectInfoBits)(RecyclerVisitedHostFinalizableBlockTypeBits)>
  288. {
  289. public:
  290. typedef typename SmallHeapBlockType<(ObjectInfoBits)(RecyclerVisitedHostFinalizableBlockTypeBits), TBlockAttributes>::BucketType BucketType;
  291. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * HeapBucketGroup)
  292. {
  293. return HeapBucketGroup->recyclerVisitedHostHeapBucket;
  294. }
  295. };
  296. #endif
  297. #ifdef RECYCLER_WRITE_BARRIER
  298. template <>
  299. class BucketGetter<WithBarrierBit>
  300. {
  301. public:
  302. typedef typename SmallHeapBlockType<WithBarrierBit, TBlockAttributes>::BucketType BucketType;
  303. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  304. {
  305. return heapBucketGroup->smallNormalWithBarrierHeapBucket;
  306. }
  307. };
  308. template <>
  309. class BucketGetter<(ObjectInfoBits)(WithBarrierBit | LeafBit)>
  310. {
  311. public:
  312. typedef typename SmallHeapBlockType<(ObjectInfoBits)(WithBarrierBit | LeafBit), TBlockAttributes>::BucketType BucketType;
  313. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  314. {
  315. // WithBarrierBit | LeafBit combination should not exist, this is only for compilation purpose
  316. Assert(false);
  317. return heapBucketGroup->leafHeapBucket;
  318. }
  319. };
  320. template <>
  321. class BucketGetter<FinalizableWithBarrierBit>
  322. {
  323. public:
  324. typedef typename SmallHeapBlockType<FinalizableWithBarrierBit, TBlockAttributes>::BucketType BucketType;
  325. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  326. {
  327. return heapBucketGroup->smallFinalizableWithBarrierHeapBucket;
  328. }
  329. };
  330. #endif
  331. public:
  332. template <ObjectInfoBits objectAttributes>
  333. typename SmallHeapBlockType<objectAttributes, TBlockAttributes>::BucketType& GetBucket()
  334. {
  335. return BucketGetter<objectAttributes>::GetBucket(this);
  336. }
  337. void Initialize(HeapInfo * heapInfo, uint sizeCat);
  338. void ResetMarks(ResetMarkFlags flags);
  339. void ScanInitialImplicitRoots(Recycler * recycler);
  340. void ScanNewImplicitRoots(Recycler * recycler);
  341. void Sweep(RecyclerSweep& recyclerSweep);
  342. uint Rescan(Recycler * recycler, RescanFlags flags);
  343. #if ENABLE_CONCURRENT_GC
  344. void SweepPendingObjects(RecyclerSweep& recyclerSweep);
  345. #endif
  346. #if ENABLE_PARTIAL_GC
  347. void SweepPartialReusePages(RecyclerSweep& recyclerSweep);
  348. void FinishPartialCollect(RecyclerSweep * recyclerSweep);
  349. #endif
  350. #if ENABLE_CONCURRENT_GC
  351. void PrepareSweep();
  352. void SetupBackgroundSweep(RecyclerSweep& recyclerSweep);
  353. void TransferPendingEmptyHeapBlocks(RecyclerSweep& recyclerSweep);
  354. #endif
  355. void SweepFinalizableObjects(RecyclerSweep& recyclerSweep);
  356. void DisposeObjects();
  357. void TransferDisposedObjects();
  358. void EnumerateObjects(ObjectInfoBits infoBits, void (*CallBackFunction)(void * address, size_t size));
  359. void FinalizeAllObjects();
  360. static unsigned int GetHeapBucketOffset() { return offsetof(HeapBucketGroup<TBlockAttributes>, heapBucket); }
  361. #if DBG || defined(RECYCLER_SLOW_CHECK_ENABLED)
  362. size_t GetNonEmptyHeapBlockCount(bool checkCount) const;
  363. size_t GetEmptyHeapBlockCount() const;
  364. #endif
  365. #ifdef RECYCLER_SLOW_CHECK_ENABLED
  366. size_t Check();
  367. #endif
  368. #ifdef RECYCLER_MEMORY_VERIFY
  369. void Verify();
  370. #endif
  371. #ifdef RECYCLER_VERIFY_MARK
  372. void VerifyMark();
  373. #endif
  374. #if ENABLE_ALLOCATIONS_DURING_CONCURRENT_SWEEP
  375. void StartAllocationDuringConcurrentSweep();
  376. bool DoTwoPassConcurrentSweepPreCheck();
  377. void FinishSweepPrep(RecyclerSweep& recyclerSweep);
  378. void FinishConcurrentSweepPass1(RecyclerSweep& recyclerSweep);
  379. void FinishConcurrentSweep();
  380. #endif
  381. #if DBG
  382. bool AllocatorsAreEmpty();
  383. #endif
  384. private:
  385. SmallNormalHeapBucketT<TBlockAttributes> heapBucket;
  386. SmallLeafHeapBucketT<TBlockAttributes> leafHeapBucket;
  387. SmallFinalizableHeapBucketT<TBlockAttributes> finalizableHeapBucket;
  388. #ifdef RECYCLER_VISITED_HOST
  389. SmallRecyclerVisitedHostHeapBucketT<TBlockAttributes> recyclerVisitedHostHeapBucket;
  390. #endif
  391. #ifdef RECYCLER_WRITE_BARRIER
  392. SmallNormalWithBarrierHeapBucketT<TBlockAttributes> smallNormalWithBarrierHeapBucket;
  393. SmallFinalizableWithBarrierHeapBucketT<TBlockAttributes> smallFinalizableWithBarrierHeapBucket;
  394. #endif
  395. };
  396. extern template class HeapBucketGroup<SmallAllocationBlockAttributes>;
  397. extern template class HeapBucketGroup<MediumAllocationBlockAttributes>;
  398. }