MemoryPool.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Util/MemoryPool.h>
  6. #include <AnKi/Util/Functions.h>
  7. #include <AnKi/Util/Assert.h>
  8. #include <AnKi/Util/Thread.h>
  9. #include <AnKi/Util/Atomic.h>
  10. #include <AnKi/Util/Logger.h>
  11. #include <AnKi/Util/Tracer.h>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <cstdio>
  15. namespace anki {
  16. #if ANKI_MEM_EXTRA_CHECKS
  17. static PoolSignature computePoolSignature(void* ptr)
  18. {
  19. ANKI_ASSERT(ptr);
  20. PtrSize sig64 = ptrToNumber(ptr);
  21. PoolSignature sig = PoolSignature(sig64);
  22. sig ^= 0x5bd1e995;
  23. sig ^= sig << 24;
  24. ANKI_ASSERT(sig != 0);
  25. return sig;
  26. }
  27. class AllocationHeader
  28. {
  29. public:
  30. PtrSize m_allocationSize;
  31. PoolSignature m_signature;
  32. };
  33. constexpr U32 kExtraChecksMaxAlignment = 64;
  34. constexpr U32 kAllocationHeaderSize = getAlignedRoundUp(kExtraChecksMaxAlignment, U32(sizeof(AllocationHeader)));
  35. template<typename TPtr, typename TSize>
  36. static void invalidateMemory([[maybe_unused]] TPtr ptr, [[maybe_unused]] TSize size)
  37. {
  38. memset(static_cast<void*>(ptr), 0xCC, size);
  39. }
  40. #endif
  41. #define ANKI_OOM_ACTION() ANKI_UTIL_LOGE("Out of memory. Expect segfault")
  42. void* mallocAligned(PtrSize size, PtrSize alignmentBytes)
  43. {
  44. ANKI_ASSERT(size > 0);
  45. ANKI_ASSERT(alignmentBytes > 0);
  46. #if ANKI_OS_ANDROID
  47. void* out = memalign(getAlignedRoundUp(alignmentBytes, sizeof(void*)), size);
  48. if(out)
  49. {
  50. // Make sure it's aligned
  51. ANKI_ASSERT(isAligned(alignmentBytes, out));
  52. }
  53. else
  54. {
  55. ANKI_UTIL_LOGE("memalign() failed, Size %zu, alignment %zu", size, alignmentBytes);
  56. }
  57. return out;
  58. #elif ANKI_POSIX
  59. void* out = nullptr;
  60. PtrSize alignment = getAlignedRoundUp(alignmentBytes, sizeof(void*));
  61. int err = posix_memalign(&out, alignment, size);
  62. if(!err) [[likely]]
  63. {
  64. ANKI_ASSERT(out != nullptr);
  65. // Make sure it's aligned
  66. ANKI_ASSERT(isAligned(alignmentBytes, out));
  67. }
  68. else
  69. {
  70. ANKI_UTIL_LOGE("posix_memalign() failed, Size %zu, alignment %zu", size, alignmentBytes);
  71. }
  72. return out;
  73. #elif ANKI_OS_WINDOWS
  74. void* out = _aligned_malloc(size, alignmentBytes);
  75. if(out)
  76. {
  77. // Make sure it's aligned
  78. ANKI_ASSERT(isAligned(alignmentBytes, out));
  79. }
  80. else
  81. {
  82. ANKI_UTIL_LOGE("_aligned_malloc() failed. Size %zu, alignment %zu", size, alignmentBytes);
  83. }
  84. return out;
  85. #else
  86. # error "Unimplemented"
  87. #endif
  88. }
  89. void freeAligned(void* ptr)
  90. {
  91. #if ANKI_POSIX
  92. ::free(ptr);
  93. #elif ANKI_OS_WINDOWS
  94. _aligned_free(ptr);
  95. #else
  96. # error "Unimplemented"
  97. #endif
  98. }
  99. void* allocAligned([[maybe_unused]] void* userData, void* ptr, PtrSize size, PtrSize alignment)
  100. {
  101. void* out;
  102. if(ptr == nullptr)
  103. {
  104. // Allocate
  105. ANKI_ASSERT(size > 0);
  106. out = mallocAligned(size, alignment);
  107. }
  108. else
  109. {
  110. // Deallocate
  111. ANKI_ASSERT(size == 0);
  112. ANKI_ASSERT(alignment == 0);
  113. freeAligned(ptr);
  114. out = nullptr;
  115. }
  116. return out;
  117. }
  118. void BaseMemoryPool::init(AllocAlignedCallback allocCb, void* allocCbUserData, const Char* name)
  119. {
  120. ANKI_ASSERT(m_allocCb == nullptr && m_name == nullptr);
  121. ANKI_ASSERT(allocCb != nullptr);
  122. m_allocCb = allocCb;
  123. m_allocCbUserData = allocCbUserData;
  124. PtrSize len;
  125. if(name && (len = strlen(name)) > 0)
  126. {
  127. m_name = static_cast<char*>(m_allocCb(m_allocCbUserData, nullptr, len + 1, 1));
  128. memcpy(m_name, name, len + 1);
  129. }
  130. }
  131. void BaseMemoryPool::destroy()
  132. {
  133. if(m_name != nullptr)
  134. {
  135. m_allocCb(m_allocCbUserData, m_name, 0, 0);
  136. m_name = nullptr;
  137. }
  138. m_allocCb = nullptr;
  139. m_allocationCount.setNonAtomically(0);
  140. }
  141. void HeapMemoryPool::init(AllocAlignedCallback allocCb, void* allocCbUserData, const Char* name)
  142. {
  143. BaseMemoryPool::init(allocCb, allocCbUserData, name);
  144. #if ANKI_MEM_EXTRA_CHECKS
  145. m_signature = computePoolSignature(this);
  146. #endif
  147. }
  148. void HeapMemoryPool::destroy()
  149. {
  150. const U32 count = m_allocationCount.load();
  151. if(count != 0)
  152. {
  153. ANKI_UTIL_LOGE("Memory pool destroyed before all memory being released (%u deallocations missed): %s", count, getName());
  154. }
  155. BaseMemoryPool::destroy();
  156. }
  157. void* HeapMemoryPool::allocate(PtrSize size, PtrSize alignment)
  158. {
  159. ANKI_ASSERT(size > 0);
  160. #if ANKI_MEM_EXTRA_CHECKS
  161. ANKI_ASSERT(alignment <= kExtraChecksMaxAlignment && "Wrong assumption");
  162. size += kAllocationHeaderSize;
  163. #endif
  164. void* mem = m_allocCb(m_allocCbUserData, nullptr, size, alignment);
  165. if(mem != nullptr)
  166. {
  167. m_allocationCount.fetchAdd(1);
  168. #if ANKI_MEM_EXTRA_CHECKS
  169. memset(mem, 0, kAllocationHeaderSize);
  170. AllocationHeader& header = *static_cast<AllocationHeader*>(mem);
  171. header.m_signature = m_signature;
  172. header.m_allocationSize = size;
  173. mem = static_cast<void*>(static_cast<U8*>(mem) + kAllocationHeaderSize);
  174. #endif
  175. }
  176. else
  177. {
  178. ANKI_OOM_ACTION();
  179. }
  180. return mem;
  181. }
  182. void HeapMemoryPool::free(void* ptr)
  183. {
  184. if(ptr == nullptr) [[unlikely]]
  185. {
  186. return;
  187. }
  188. #if ANKI_MEM_EXTRA_CHECKS
  189. U8* memU8 = static_cast<U8*>(ptr) - kAllocationHeaderSize;
  190. AllocationHeader& header = *reinterpret_cast<AllocationHeader*>(memU8);
  191. if(header.m_signature != m_signature)
  192. {
  193. ANKI_UTIL_LOGE("Signature missmatch on free");
  194. }
  195. ptr = static_cast<void*>(memU8);
  196. invalidateMemory(ptr, header.m_allocationSize);
  197. #endif
  198. m_allocationCount.fetchSub(1);
  199. m_allocCb(m_allocCbUserData, ptr, 0, 0);
  200. }
  201. Error StackMemoryPool::StackAllocatorBuilderInterface::allocateChunk(PtrSize size, Chunk*& out)
  202. {
  203. ANKI_ASSERT(size > 0);
  204. ANKI_ASSERT(m_parent && m_parent->m_allocCb);
  205. const PtrSize fullChunkSize = offsetof(Chunk, m_memoryStart) + size;
  206. void* mem = m_parent->m_allocCb(m_parent->m_allocCbUserData, nullptr, fullChunkSize, kMaxAlignment);
  207. if(mem) [[likely]]
  208. {
  209. out = static_cast<Chunk*>(mem);
  210. #if ANKI_MEM_EXTRA_CHECKS
  211. invalidateMemory(&out->m_memoryStart[0], size);
  212. #endif
  213. }
  214. else
  215. {
  216. ANKI_OOM_ACTION();
  217. return Error::kOutOfMemory;
  218. }
  219. return Error::kNone;
  220. }
  221. void StackMemoryPool::StackAllocatorBuilderInterface::freeChunk(Chunk* chunk)
  222. {
  223. ANKI_ASSERT(chunk);
  224. ANKI_ASSERT(m_parent && m_parent->m_allocCb);
  225. m_parent->m_allocCb(m_parent->m_allocCbUserData, chunk, 0, 0);
  226. }
  227. void StackMemoryPool::StackAllocatorBuilderInterface::recycleChunk([[maybe_unused]] Chunk& chunk)
  228. {
  229. ANKI_ASSERT(chunk.m_chunkSize > 0);
  230. #if ANKI_MEM_EXTRA_CHECKS
  231. invalidateMemory(&chunk.m_memoryStart[0], chunk.m_chunkSize);
  232. #endif
  233. }
  234. void StackMemoryPool::init(AllocAlignedCallback allocCb, void* allocCbUserData, PtrSize initialChunkSize, F64 nextChunkScale, PtrSize nextChunkBias,
  235. Bool ignoreDeallocationErrors, const Char* name)
  236. {
  237. ANKI_ASSERT(initialChunkSize > 0);
  238. ANKI_ASSERT(nextChunkScale >= 1.0);
  239. BaseMemoryPool::init(allocCb, allocCbUserData, name);
  240. m_builder.getInterface().m_parent = this;
  241. m_builder.getInterface().m_ignoreDeallocationErrors = ignoreDeallocationErrors;
  242. m_builder.getInterface().m_initialChunkSize = initialChunkSize;
  243. m_builder.getInterface().m_nextChunkScale = nextChunkScale;
  244. m_builder.getInterface().m_nextChunkBias = nextChunkBias;
  245. }
  246. void StackMemoryPool::destroy()
  247. {
  248. m_builder.destroy();
  249. m_builder.getInterface() = {};
  250. BaseMemoryPool::destroy();
  251. }
  252. void* StackMemoryPool::allocate(PtrSize size, PtrSize alignment)
  253. {
  254. ANKI_ASSERT(size > 0);
  255. ANKI_ASSERT(alignment > 0 && alignment <= kMaxAlignment);
  256. Chunk* chunk;
  257. PtrSize offset;
  258. if(m_builder.allocate(size, alignment, chunk, offset))
  259. {
  260. return nullptr;
  261. }
  262. const PtrSize address = ptrToNumber(&chunk->m_memoryStart[0]) + offset;
  263. return numberToPtr<void*>(address);
  264. }
  265. void StackMemoryPool::free(void* ptr)
  266. {
  267. if(ptr == nullptr) [[unlikely]]
  268. {
  269. return;
  270. }
  271. m_builder.free();
  272. }
  273. void StackMemoryPool::reset()
  274. {
  275. m_builder.reset();
  276. m_allocationCount.store(0);
  277. }
  278. } // end namespace anki