SmallFinalizableHeapBucket.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #ifdef DUMP_FRAGMENTATION_STATS
  20. void AggregateBucketStats(HeapBucketStats& stats);
  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_WRITE_BARRIER
  57. template <class TBlockAttributes>
  58. class SmallFinalizableWithBarrierHeapBucketT : public SmallFinalizableHeapBucketBaseT<SmallFinalizableWithBarrierHeapBlockT<TBlockAttributes> >
  59. {
  60. };
  61. #endif
  62. typedef SmallFinalizableHeapBucketT<MediumAllocationBlockAttributes> MediumFinalizableHeapBucket;
  63. typedef SmallFinalizableHeapBucketT<SmallAllocationBlockAttributes> SmallFinalizableHeapBucket;
  64. #ifdef RECYCLER_WRITE_BARRIER
  65. typedef SmallFinalizableWithBarrierHeapBucketT<MediumAllocationBlockAttributes> MediumFinalizableWithBarrierHeapBucket;
  66. typedef SmallFinalizableWithBarrierHeapBucketT<SmallAllocationBlockAttributes> SmallFinalizableWithBarrierHeapBucket;
  67. #endif
  68. // GC-TODO: Move this away
  69. template <ObjectInfoBits attributes, class TBlockAttributes>
  70. class SmallHeapBlockType
  71. {
  72. public:
  73. CompileAssert(attributes & FinalizeBit);
  74. typedef SmallFinalizableHeapBlockT<TBlockAttributes> BlockType;
  75. typedef SmallFinalizableHeapBucketT<TBlockAttributes> BucketType;
  76. };
  77. template <>
  78. class SmallHeapBlockType<LeafBit, SmallAllocationBlockAttributes>
  79. {
  80. public:
  81. typedef SmallLeafHeapBlock BlockType;
  82. typedef SmallLeafHeapBucketT<SmallAllocationBlockAttributes> BucketType;
  83. };
  84. template <>
  85. class SmallHeapBlockType<NoBit, SmallAllocationBlockAttributes>
  86. {
  87. public:
  88. typedef SmallNormalHeapBlock BlockType;
  89. typedef SmallNormalHeapBucket BucketType;
  90. };
  91. #ifdef RECYCLER_WRITE_BARRIER
  92. template <>
  93. class SmallHeapBlockType<WithBarrierBit, SmallAllocationBlockAttributes>
  94. {
  95. public:
  96. typedef SmallNormalWithBarrierHeapBlock BlockType;
  97. typedef SmallNormalWithBarrierHeapBucket BucketType;
  98. };
  99. template <>
  100. class SmallHeapBlockType<(ObjectInfoBits)(WithBarrierBit|LeafBit), SmallAllocationBlockAttributes>
  101. {
  102. public:
  103. typedef SmallLeafHeapBlock BlockType;
  104. typedef SmallLeafHeapBucketT<SmallAllocationBlockAttributes> BucketType;
  105. };
  106. template <>
  107. class SmallHeapBlockType<FinalizableWithBarrierBit, SmallAllocationBlockAttributes>
  108. {
  109. public:
  110. typedef SmallFinalizableWithBarrierHeapBlock BlockType;
  111. typedef SmallFinalizableWithBarrierHeapBucket BucketType;
  112. };
  113. #endif
  114. #if SMALLBLOCK_MEDIUM_ALLOC
  115. template <>
  116. class SmallHeapBlockType<FinalizeBit, MediumAllocationBlockAttributes>
  117. {
  118. public:
  119. typedef MediumFinalizableHeapBlock BlockType;
  120. typedef MediumFinalizableHeapBucket BucketType;
  121. };
  122. template <>
  123. class SmallHeapBlockType<LeafBit, MediumAllocationBlockAttributes>
  124. {
  125. public:
  126. typedef MediumLeafHeapBlock BlockType;
  127. typedef SmallLeafHeapBucketT<MediumAllocationBlockAttributes> BucketType;
  128. };
  129. template <>
  130. class SmallHeapBlockType<NoBit, MediumAllocationBlockAttributes>
  131. {
  132. public:
  133. typedef MediumNormalHeapBlock BlockType;
  134. typedef MediumNormalHeapBucket BucketType;
  135. };
  136. #ifdef RECYCLER_WRITE_BARRIER
  137. template <>
  138. class SmallHeapBlockType<WithBarrierBit, MediumAllocationBlockAttributes>
  139. {
  140. public:
  141. typedef MediumNormalWithBarrierHeapBlock BlockType;
  142. typedef MediumNormalWithBarrierHeapBucket BucketType;
  143. };
  144. template <>
  145. class SmallHeapBlockType<(ObjectInfoBits)(WithBarrierBit | LeafBit), MediumAllocationBlockAttributes>
  146. {
  147. public:
  148. typedef MediumLeafHeapBlock BlockType;
  149. typedef SmallLeafHeapBucketT<MediumAllocationBlockAttributes> BucketType;
  150. };
  151. template <>
  152. class SmallHeapBlockType<FinalizableWithBarrierBit, MediumAllocationBlockAttributes>
  153. {
  154. public:
  155. typedef MediumFinalizableWithBarrierHeapBlock BlockType;
  156. typedef MediumFinalizableWithBarrierHeapBucket BucketType;
  157. };
  158. #endif
  159. #endif
  160. template <class TBlockAttributes>
  161. class HeapBucketGroup
  162. {
  163. template <ObjectInfoBits objectAttributes>
  164. class BucketGetter
  165. {
  166. public:
  167. typedef typename SmallHeapBlockType<objectAttributes, TBlockAttributes>::BucketType BucketType;
  168. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  169. {
  170. CompileAssert(objectAttributes & FinalizeBit);
  171. return heapBucketGroup->finalizableHeapBucket;
  172. }
  173. };
  174. template <>
  175. class BucketGetter<LeafBit>
  176. {
  177. public:
  178. typedef typename SmallHeapBlockType<LeafBit, TBlockAttributes>::BucketType BucketType;
  179. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  180. {
  181. return heapBucketGroup->leafHeapBucket;
  182. }
  183. };
  184. template <>
  185. class BucketGetter<NoBit>
  186. {
  187. public:
  188. typedef typename SmallHeapBlockType<NoBit, TBlockAttributes>::BucketType BucketType;
  189. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  190. {
  191. return heapBucketGroup->heapBucket;
  192. }
  193. };
  194. template <>
  195. class BucketGetter<(ObjectInfoBits)(FinalizeBit | LeafBit)>
  196. {
  197. public:
  198. typedef typename SmallHeapBlockType<(ObjectInfoBits)(FinalizeBit | LeafBit), TBlockAttributes>::BucketType BucketType;
  199. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  200. {
  201. // TODO: SWB implemente finalizable leaf bucket
  202. return heapBucketGroup->finalizableHeapBucket;
  203. }
  204. };
  205. #ifdef RECYCLER_WRITE_BARRIER
  206. template <>
  207. class BucketGetter<WithBarrierBit>
  208. {
  209. public:
  210. typedef typename SmallHeapBlockType<WithBarrierBit, TBlockAttributes>::BucketType BucketType;
  211. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  212. {
  213. return heapBucketGroup->smallNormalWithBarrierHeapBucket;
  214. }
  215. };
  216. template <>
  217. class BucketGetter<(ObjectInfoBits)(WithBarrierBit | LeafBit)>
  218. {
  219. public:
  220. typedef typename SmallHeapBlockType<(ObjectInfoBits)(WithBarrierBit | LeafBit), TBlockAttributes>::BucketType BucketType;
  221. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  222. {
  223. // WithBarrierBit | LeafBit combination should not exist, this is only for compilation purpose
  224. Assert(false);
  225. return heapBucketGroup->leafHeapBucket;
  226. }
  227. };
  228. template <>
  229. class BucketGetter<FinalizableWithBarrierBit>
  230. {
  231. public:
  232. typedef typename SmallHeapBlockType<FinalizableWithBarrierBit, TBlockAttributes>::BucketType BucketType;
  233. static BucketType& GetBucket(HeapBucketGroup<TBlockAttributes> * heapBucketGroup)
  234. {
  235. return heapBucketGroup->smallFinalizableWithBarrierHeapBucket;
  236. }
  237. };
  238. #endif
  239. public:
  240. template <ObjectInfoBits objectAttributes>
  241. typename SmallHeapBlockType<objectAttributes, TBlockAttributes>::BucketType& GetBucket()
  242. {
  243. return BucketGetter<objectAttributes>::GetBucket(this);
  244. }
  245. void Initialize(HeapInfo * heapInfo, uint sizeCat);
  246. void ResetMarks(ResetMarkFlags flags);
  247. void ScanInitialImplicitRoots(Recycler * recycler);
  248. void ScanNewImplicitRoots(Recycler * recycler);
  249. void Sweep(RecyclerSweep& recyclerSweep);
  250. uint Rescan(Recycler * recycler, RescanFlags flags);
  251. #if ENABLE_CONCURRENT_GC
  252. void SweepPendingObjects(RecyclerSweep& recyclerSweep);
  253. #endif
  254. #if ENABLE_PARTIAL_GC
  255. void SweepPartialReusePages(RecyclerSweep& recyclerSweep);
  256. void FinishPartialCollect(RecyclerSweep * recyclerSweep);
  257. #endif
  258. #if ENABLE_CONCURRENT_GC
  259. void PrepareSweep();
  260. void SetupBackgroundSweep(RecyclerSweep& recyclerSweep);
  261. void TransferPendingEmptyHeapBlocks(RecyclerSweep& recyclerSweep);
  262. #endif
  263. void SweepFinalizableObjects(RecyclerSweep& recyclerSweep);
  264. void DisposeObjects();
  265. void TransferDisposedObjects();
  266. void EnumerateObjects(ObjectInfoBits infoBits, void (*CallBackFunction)(void * address, size_t size));
  267. void FinalizeAllObjects();
  268. static unsigned int GetHeapBucketOffset() { return offsetof(HeapBucketGroup<TBlockAttributes>, heapBucket); }
  269. #if DBG || defined(RECYCLER_SLOW_CHECK_ENABLED)
  270. size_t GetNonEmptyHeapBlockCount(bool checkCount) const;
  271. size_t GetEmptyHeapBlockCount() const;
  272. #endif
  273. #ifdef RECYCLER_SLOW_CHECK_ENABLED
  274. size_t Check();
  275. #endif
  276. #ifdef RECYCLER_MEMORY_VERIFY
  277. void Verify();
  278. #endif
  279. #ifdef RECYCLER_VERIFY_MARK
  280. void VerifyMark();
  281. #endif
  282. #if DBG
  283. bool AllocatorsAreEmpty();
  284. #endif
  285. private:
  286. SmallNormalHeapBucketT<TBlockAttributes> heapBucket;
  287. SmallLeafHeapBucketT<TBlockAttributes> leafHeapBucket;
  288. SmallFinalizableHeapBucketT<TBlockAttributes> finalizableHeapBucket;
  289. #ifdef RECYCLER_WRITE_BARRIER
  290. SmallNormalWithBarrierHeapBucketT<TBlockAttributes> smallNormalWithBarrierHeapBucket;
  291. SmallFinalizableWithBarrierHeapBucketT<TBlockAttributes> smallFinalizableWithBarrierHeapBucket;
  292. #endif
  293. };
  294. extern template class HeapBucketGroup<SmallAllocationBlockAttributes>;
  295. extern template class HeapBucketGroup<MediumAllocationBlockAttributes>;
  296. }