SmallFinalizableHeapBlock.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 TBlockAttributes> class SmallFinalizableHeapBucketT;
  8. #ifdef RECYCLER_WRITE_BARRIER
  9. template <class TBlockAttributes> class SmallFinalizableWithBarrierHeapBlockT;
  10. #endif
  11. template <class TBlockAttributes>
  12. class SmallFinalizableHeapBlockT : public SmallNormalHeapBlockT<TBlockAttributes>
  13. {
  14. friend class HeapBucketT<SmallFinalizableHeapBlockT>;
  15. public:
  16. typedef TBlockAttributes HeapBlockAttributes;
  17. static const ObjectInfoBits RequiredAttributes = FinalizeBit;
  18. static SmallFinalizableHeapBlockT * New(HeapBucketT<SmallFinalizableHeapBlockT> * bucket);
  19. static void Delete(SmallFinalizableHeapBlockT * block);
  20. SmallFinalizableHeapBlockT * GetNextBlock() const { return SmallHeapBlockT<TBlockAttributes>::GetNextBlock()->AsFinalizableBlock<TBlockAttributes>(); }
  21. void SetNextBlock(SmallFinalizableHeapBlockT * next) { __super::SetNextBlock(next); }
  22. void ProcessMarkedObject(void* candidate, MarkContext * markContext);
  23. void SetAttributes(void * address, unsigned char attributes);
  24. #if ENABLE_PARTIAL_GC || ENABLE_CONCURRENT_GC
  25. static bool CanRescanFullBlock();
  26. static bool RescanObject(SmallFinalizableHeapBlockT<TBlockAttributes> * block, __in_ecount(localObjectSize) char * objectAddress, uint localObjectSize, uint objectIndex, Recycler * recycler);
  27. bool RescanTrackedObject(FinalizableObject * object, uint objectIndex, Recycler * recycler);
  28. #endif
  29. template<bool pageheap>
  30. SweepState Sweep(RecyclerSweep& sweepeData, bool queuePendingSweep, bool allocable);
  31. void DisposeObjects();
  32. void TransferDisposedObjects();
  33. bool HasPendingDisposeObjects() const
  34. {
  35. return (this->pendingDisposeCount != 0);
  36. }
  37. void AddPendingDisposeObject()
  38. {
  39. this->pendingDisposeCount++;
  40. Assert(this->pendingDisposeCount <= this->objectCount);
  41. }
  42. bool HasDisposedObjects() const { return this->disposedObjectList != nullptr; }
  43. bool HasAnyDisposeObjects() const { return this->HasPendingDisposeObjects() || this->HasDisposedObjects(); }
  44. template <typename Fn>
  45. void ForEachPendingDisposeObject(Fn fn)
  46. {
  47. if (this->HasPendingDisposeObjects())
  48. {
  49. for (uint i = 0; i < this->objectCount; i++)
  50. {
  51. if ((ObjectInfo(i) & PendingDisposeBit) != 0)
  52. {
  53. // When pending dispose, exactly the PendingDisposeBits should be set
  54. Assert(ObjectInfo(i) == PendingDisposeObjectBits);
  55. fn(i);
  56. }
  57. }
  58. }
  59. else
  60. {
  61. #if DBG
  62. for (uint i = 0; i < this->objectCount; i++)
  63. {
  64. Assert((ObjectInfo(i) & PendingDisposeBit) == 0);
  65. }
  66. #endif
  67. }
  68. }
  69. ushort AddDisposedObjectFreeBitVector(SmallHeapBlockBitVector * free);
  70. #ifdef RECYCLER_SLOW_CHECK_ENABLED
  71. uint CheckDisposedObjectFreeBitVector();
  72. virtual bool GetFreeObjectListOnAllocator(FreeObject ** freeObjectList) override;
  73. #endif
  74. #if DBG
  75. #if ENABLE_PARTIAL_GC
  76. void FinishPartialCollect();
  77. #endif
  78. bool IsPendingDispose() const { return isPendingDispose; }
  79. void SetIsPendingDispose() { isPendingDispose = true; }
  80. #endif
  81. void FinalizeAllObjects();
  82. #ifdef DUMP_FRAGMENTATION_STATS
  83. ushort GetFinalizeCount() {
  84. return finalizeCount;
  85. }
  86. #endif
  87. virtual bool FindHeapObject(void* objectAddress, Recycler * recycler, FindHeapObjectFlags flags, RecyclerHeapObjectInfo& heapObject) override
  88. {
  89. return FindHeapObjectImpl<SmallFinalizableHeapBlockT>(objectAddress, recycler, flags, heapObject);
  90. }
  91. protected:
  92. SmallFinalizableHeapBlockT(HeapBucketT<SmallFinalizableHeapBlockT> * bucket, ushort objectSize, ushort objectCount);
  93. #ifdef RECYCLER_WRITE_BARRIER
  94. SmallFinalizableHeapBlockT(HeapBucketT<SmallFinalizableWithBarrierHeapBlockT<TBlockAttributes>> * bucket, ushort objectSize, ushort objectCount, HeapBlockType blockType);
  95. #endif
  96. #if DBG
  97. void Init(ushort objectSize, ushort objectCount);
  98. bool isPendingDispose;
  99. #endif
  100. ushort finalizeCount;
  101. ushort pendingDisposeCount;
  102. FreeObject* disposedObjectList;
  103. FreeObject* disposedObjectListTail;
  104. friend class ScriptMemoryDumper;
  105. #ifdef RECYCLER_MEMORY_VERIFY
  106. friend void SmallHeapBlockT<TBlockAttributes>::Verify(bool pendingDispose);
  107. #endif
  108. };
  109. #ifdef RECYCLER_WRITE_BARRIER
  110. template <class TBlockAttributes>
  111. class SmallFinalizableWithBarrierHeapBlockT : public SmallFinalizableHeapBlockT<TBlockAttributes>
  112. {
  113. friend class HeapBucketT<SmallFinalizableWithBarrierHeapBlockT>;
  114. public:
  115. typedef TBlockAttributes HeapBlockAttributes;
  116. static const ObjectInfoBits RequiredAttributes = FinalizableWithBarrierBit;
  117. static const bool IsLeafOnly = false;
  118. static SmallFinalizableWithBarrierHeapBlockT * New(HeapBucketT<SmallFinalizableWithBarrierHeapBlockT> * bucket);
  119. static void Delete(SmallFinalizableWithBarrierHeapBlockT * block);
  120. SmallFinalizableWithBarrierHeapBlockT * GetNextBlock() const { return ((SmallHeapBlock*) this)->GetNextBlock()->AsFinalizableWriteBarrierBlock<TBlockAttributes>(); }
  121. virtual bool FindHeapObject(void* objectAddress, Recycler * recycler, FindHeapObjectFlags flags, RecyclerHeapObjectInfo& heapObject) override sealed
  122. {
  123. return FindHeapObjectImpl<SmallFinalizableWithBarrierHeapBlockT>(objectAddress, recycler, flags, heapObject);
  124. }
  125. protected:
  126. SmallFinalizableWithBarrierHeapBlockT(HeapBucketT<SmallFinalizableWithBarrierHeapBlockT> * bucket, ushort objectSize, ushort objectCount)
  127. : SmallFinalizableHeapBlockT(bucket, objectSize, objectCount, TBlockAttributes::IsSmallBlock ? SmallFinalizableBlockWithBarrierType : MediumFinalizableBlockWithBarrierType)
  128. {
  129. }
  130. };
  131. #endif
  132. extern template class SmallFinalizableHeapBlockT<SmallAllocationBlockAttributes>;
  133. extern template class SmallFinalizableHeapBlockT<MediumAllocationBlockAttributes>;
  134. typedef SmallFinalizableHeapBlockT<SmallAllocationBlockAttributes> SmallFinalizableHeapBlock;
  135. typedef SmallFinalizableHeapBlockT<MediumAllocationBlockAttributes> MediumFinalizableHeapBlock;
  136. #ifdef RECYCLER_WRITE_BARRIER
  137. extern template class SmallFinalizableWithBarrierHeapBlockT<SmallAllocationBlockAttributes>;
  138. extern template class SmallFinalizableWithBarrierHeapBlockT<MediumAllocationBlockAttributes>;
  139. typedef SmallFinalizableWithBarrierHeapBlockT<SmallAllocationBlockAttributes> SmallFinalizableWithBarrierHeapBlock;
  140. typedef SmallFinalizableWithBarrierHeapBlockT<MediumAllocationBlockAttributes> MediumFinalizableWithBarrierHeapBlock;
  141. #endif
  142. }