SmallHeapBlockAllocator.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. template <typename TBlockType>
  7. SmallHeapBlockAllocator<TBlockType>::SmallHeapBlockAllocator() :
  8. freeObjectList(nullptr),
  9. endAddress(nullptr),
  10. heapBlock(nullptr),
  11. prev(nullptr),
  12. next(nullptr)
  13. {
  14. #ifdef RECYCLER_TRACK_NATIVE_ALLOCATED_OBJECTS
  15. this->lastNonNativeBumpAllocatedBlock = nullptr;
  16. #endif
  17. }
  18. template <typename TBlockType>
  19. void
  20. SmallHeapBlockAllocator<TBlockType>::Initialize()
  21. {
  22. Assert(this->freeObjectList == nullptr);
  23. Assert(this->endAddress == nullptr);
  24. Assert(this->heapBlock == nullptr);
  25. this->prev = this;
  26. this->next = this;
  27. #if ENABLE_ALLOCATIONS_DURING_CONCURRENT_SWEEP
  28. DebugOnly(this->isAllocatingFromNewBlock = false);
  29. #endif
  30. }
  31. template <typename TBlockType>
  32. void
  33. SmallHeapBlockAllocator<TBlockType>::UpdateHeapBlock()
  34. {
  35. if (heapBlock != nullptr)
  36. {
  37. if (this->endAddress == nullptr)
  38. {
  39. heapBlock->freeObjectList = this->freeObjectList;
  40. }
  41. else
  42. {
  43. Assert(heapBlock->freeObjectList == nullptr);
  44. }
  45. }
  46. }
  47. template <typename TBlockType>
  48. void
  49. SmallHeapBlockAllocator<TBlockType>::Clear()
  50. {
  51. TBlockType * heapBlock = this->heapBlock;
  52. if (heapBlock != nullptr)
  53. {
  54. Assert(heapBlock->isInAllocator);
  55. heapBlock->isInAllocator = false;
  56. FreeObject * remainingFreeObjectList = nullptr;
  57. if (this->endAddress != nullptr)
  58. {
  59. #ifdef RECYCLER_TRACK_NATIVE_ALLOCATED_OBJECTS
  60. TrackNativeAllocatedObjects();
  61. lastNonNativeBumpAllocatedBlock = nullptr;
  62. #endif
  63. #ifdef PROFILE_RECYCLER_ALLOC
  64. // Need to tell the tracker
  65. this->bucket->heapInfo->recycler->TrackUnallocated((char *)this->freeObjectList, this->endAddress, this->bucket->sizeCat);
  66. #endif
  67. RecyclerMemoryTracking::ReportUnallocated(this->heapBlock->heapBucket->heapInfo->recycler, (char *)this->freeObjectList, this->endAddress, heapBlock->heapBucket->sizeCat);
  68. #ifdef RECYCLER_PERF_COUNTERS
  69. size_t unallocatedObjects = heapBlock->objectCount - ((char *)this->freeObjectList - heapBlock->address) / heapBlock->objectSize;
  70. size_t unallocatedObjectBytes = unallocatedObjects * heapBlock->GetObjectSize();
  71. RECYCLER_PERF_COUNTER_ADD(LiveObject, unallocatedObjects);
  72. RECYCLER_PERF_COUNTER_ADD(LiveObjectSize, unallocatedObjectBytes);
  73. RECYCLER_PERF_COUNTER_SUB(FreeObjectSize, unallocatedObjectBytes);
  74. RECYCLER_PERF_COUNTER_ADD(SmallHeapBlockLiveObject, unallocatedObjects);
  75. RECYCLER_PERF_COUNTER_ADD(SmallHeapBlockLiveObjectSize, unallocatedObjectBytes);
  76. RECYCLER_PERF_COUNTER_SUB(SmallHeapBlockFreeObjectSize, unallocatedObjectBytes);
  77. #endif
  78. Assert(heapBlock->freeObjectList == nullptr);
  79. this->endAddress = nullptr;
  80. }
  81. else
  82. {
  83. remainingFreeObjectList = this->freeObjectList;
  84. heapBlock->freeObjectList = remainingFreeObjectList;
  85. }
  86. this->freeObjectList = nullptr;
  87. // this->freeObjectList and this->lastFreeCount are accessed in SmallHeapBlock::ResetMarks
  88. // the order of access there is first we see if lastFreeCount = 0, and if it is, we assert
  89. // that freeObjectList = null. Because of ARM's memory model, we need to insert barriers
  90. // so that the two variables can be accessed correctly across threads. Here, after we write
  91. // to this->freeObjectList, we insert a write barrier so that if this->lastFreeCount is 0,
  92. // this->freeObjectList must have been set to null. On the other end, we stick a read barrier
  93. // We use the MemoryBarrier macro because of ARMs lack of a separate read barrier
  94. #if defined(_M_ARM32_OR_ARM64)
  95. #if DBG
  96. MemoryBarrier();
  97. #endif
  98. #endif
  99. if (remainingFreeObjectList == nullptr)
  100. {
  101. uint lastFreeCount = heapBlock->GetAndClearLastFreeCount();
  102. heapBlock->heapBucket->heapInfo->recycler->autoHeap.uncollectedAllocBytes += lastFreeCount * heapBlock->GetObjectSize();
  103. Assert(heapBlock->lastUncollectedAllocBytes == 0);
  104. DebugOnly(heapBlock->lastUncollectedAllocBytes = lastFreeCount * heapBlock->GetObjectSize());
  105. }
  106. else
  107. {
  108. DebugOnly(heapBlock->SetIsClearedFromAllocator(true));
  109. }
  110. this->heapBlock = nullptr;
  111. RECYCLER_SLOW_CHECK(heapBlock->CheckDebugFreeBitVector(false));
  112. }
  113. else if (this->freeObjectList != nullptr)
  114. {
  115. // Explicit Free Object List
  116. #ifdef RECYCLER_MEMORY_VERIFY
  117. FreeObject* freeObject = this->freeObjectList;
  118. while (freeObject)
  119. {
  120. HeapBlock* heapBlockVerify = this->bucket->GetRecycler()->FindHeapBlock((void*) freeObject);
  121. Assert(heapBlockVerify != nullptr);
  122. Assert(!heapBlockVerify->IsLargeHeapBlock());
  123. TBlockType* smallBlock = (TBlockType*)heapBlockVerify;
  124. smallBlock->ClearExplicitFreeBitForObject((void*) freeObject);
  125. freeObject = freeObject->GetNext();
  126. }
  127. #endif
  128. this->freeObjectList = nullptr;
  129. }
  130. #if ENABLE_ALLOCATIONS_DURING_CONCURRENT_SWEEP
  131. DebugOnly(this->isAllocatingFromNewBlock = false);
  132. #endif
  133. }
  134. template <typename TBlockType>
  135. void
  136. SmallHeapBlockAllocator<TBlockType>::SetNew(BlockType * heapBlock)
  137. {
  138. Assert(this->endAddress == nullptr);
  139. Assert(this->heapBlock == nullptr);
  140. Assert(this->freeObjectList == nullptr);
  141. Assert(heapBlock != nullptr);
  142. Assert(heapBlock->freeObjectList == nullptr);
  143. Assert(heapBlock->lastFreeCount != 0);
  144. Assert(!heapBlock->isInAllocator);
  145. heapBlock->isInAllocator = true;
  146. this->heapBlock = heapBlock;
  147. this->freeObjectList = (FreeObject *)heapBlock->GetAddress();
  148. this->endAddress = heapBlock->GetEndAddress();
  149. #if ENABLE_ALLOCATIONS_DURING_CONCURRENT_SWEEP
  150. DebugOnly(this->isAllocatingFromNewBlock = true);
  151. #endif
  152. }
  153. template <typename TBlockType>
  154. void
  155. SmallHeapBlockAllocator<TBlockType>::Set(BlockType * heapBlock)
  156. {
  157. Assert(this->endAddress == nullptr);
  158. Assert(this->heapBlock == nullptr);
  159. Assert(this->freeObjectList == nullptr);
  160. Assert(heapBlock != nullptr);
  161. Assert(heapBlock->freeObjectList != nullptr);
  162. Assert(heapBlock->lastFreeCount != 0);
  163. Assert(!heapBlock->isInAllocator);
  164. heapBlock->isInAllocator = true;
  165. this->heapBlock = heapBlock;
  166. RECYCLER_SLOW_CHECK(this->heapBlock->CheckDebugFreeBitVector(true));
  167. this->freeObjectList = this->heapBlock->freeObjectList;
  168. #if ENABLE_ALLOCATIONS_DURING_CONCURRENT_SWEEP
  169. DebugOnly(this->isAllocatingFromNewBlock = false);
  170. #endif
  171. }
  172. template <typename TBlockType>
  173. void
  174. SmallHeapBlockAllocator<TBlockType>::SetExplicitFreeList(FreeObject* list)
  175. {
  176. Assert(list != nullptr);
  177. Assert(this->heapBlock == nullptr);
  178. Assert(this->freeObjectList == nullptr);
  179. this->freeObjectList = list;
  180. }
  181. #ifdef RECYCLER_TRACK_NATIVE_ALLOCATED_OBJECTS
  182. template <typename TBlockType>
  183. void
  184. SmallHeapBlockAllocator<TBlockType>::TrackNativeAllocatedObjects()
  185. {
  186. Assert(this->freeObjectList != nullptr && endAddress != nullptr);
  187. Assert(this->heapBlock != nullptr);
  188. #if defined(PROFILE_RECYCLER_ALLOC) || defined(RECYCLER_MEMORY_VERIFY) || defined(MEMSPECT_TRACKING) || defined(ETW_MEMORY_TRACKING)
  189. if (pfnTrackNativeAllocatedObjectCallBack == nullptr)
  190. {
  191. return;
  192. }
  193. if (lastNonNativeBumpAllocatedBlock == nullptr)
  194. {
  195. Assert((char *)this->freeObjectList == this->heapBlock->GetAddress());
  196. return;
  197. }
  198. Recycler * recycler = this->heapBlock->heapBucket->heapInfo->recycler;
  199. size_t sizeCat = this->heapBlock->heapBucket->sizeCat;
  200. char * curr = lastNonNativeBumpAllocatedBlock + sizeCat;
  201. Assert(curr <= (char *)this->freeObjectList);
  202. #if DBG_DUMP
  203. AllocationVerboseTrace(recycler->GetRecyclerFlagsTable(), _u("TrackNativeAllocatedObjects: recycler = 0x%p, sizeCat = %u, lastRuntimeAllocatedBlock = 0x%p, freeObjectList = 0x%p, nativeAllocatedObjectCount = %u\n"),
  204. recycler, sizeCat, this->lastNonNativeBumpAllocatedBlock, this->freeObjectList, ((char *)this->freeObjectList - curr) / sizeCat);
  205. #endif
  206. while (curr < (char *)this->freeObjectList)
  207. {
  208. pfnTrackNativeAllocatedObjectCallBack(recycler, curr, sizeCat);
  209. curr += sizeCat;
  210. }
  211. #elif defined(RECYCLER_PERF_COUNTERS)
  212. if (lastNonNativeBumpAllocatedBlock == nullptr)
  213. {
  214. return;
  215. }
  216. size_t sizeCat = this->heapBlock->heapBucket->sizeCat;
  217. char * curr = lastNonNativeBumpAllocatedBlock + sizeCat;
  218. Assert(curr <= (char *)this->freeObjectList);
  219. size_t byteCount = ((char *)this->freeObjectList - curr);
  220. #if DBG_DUMP
  221. AllocationVerboseTrace(_u("TrackNativeAllocatedObjects: recycler = 0x%p, sizeCat = %u, lastRuntimeAllocatedBlock = 0x%p, freeObjectList = 0x%p, nativeAllocatedObjectCount = %u\n"),
  222. recycler, sizeCat, this->lastNonNativeBumpAllocatedBlock, this->freeObjectList, ((char *)this->freeObjectList - curr) / sizeCat);
  223. #endif
  224. RECYCLER_PERF_COUNTER_ADD(LiveObject, byteCount / sizeCat);
  225. RECYCLER_PERF_COUNTER_ADD(LiveObjectSize, byteCount);
  226. RECYCLER_PERF_COUNTER_SUB(FreeObjectSize, byteCount);
  227. RECYCLER_PERF_COUNTER_ADD(SmallHeapBlockLiveObject, byteCount / sizeCat);
  228. RECYCLER_PERF_COUNTER_ADD(SmallHeapBlockLiveObjectSize, byteCount);
  229. RECYCLER_PERF_COUNTER_SUB(SmallHeapBlockFreeObjectSize, byteCount);
  230. #else
  231. #error Not implemented
  232. #endif
  233. }
  234. #endif
  235. namespace Memory
  236. {
  237. EXPLICIT_INSTANTIATE_WITH_SMALL_HEAP_BLOCK_TYPE(SmallHeapBlockAllocator)
  238. template _ALWAYSINLINE char* SmallHeapBlockAllocator<SmallNormalHeapBlock>::InlinedAllocImpl</*canFaultInject*/true>(Recycler * recycler, DECLSPEC_GUARD_OVERFLOW size_t sizeCat, ObjectInfoBits attributes);
  239. }