Allocator.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/Assert.h>
  7. #include <AnKi/Util/Memory.h>
  8. #include <AnKi/Util/Logger.h>
  9. #include <AnKi/Util/Forward.h>
  10. #include <cstddef> // For ptrdiff_t
  11. #include <utility> // For forward
  12. #include <new> // For placement new
  13. #include <type_traits> // For some checks
  14. namespace anki {
  15. /// @addtogroup util_memory
  16. /// @{
  17. /// Pool based allocator
  18. ///
  19. /// This is a template that accepts memory pools with a specific interface
  20. ///
  21. /// @tparam T The type
  22. ///
  23. /// @note Don't ever EVER remove the double copy constructor and the double operator=. The compiler will create defaults
  24. template<typename T, typename TPool>
  25. class GenericPoolAllocator
  26. {
  27. template<typename, typename>
  28. friend class GenericPoolAllocator;
  29. public:
  30. // Typedefs
  31. using size_type = size_t;
  32. using difference_type = ptrdiff_t;
  33. using pointer = T*;
  34. using const_pointer = const T*;
  35. using reference = T&;
  36. using const_reference = const T&;
  37. using value_type = T;
  38. /// Move assignments between containers will copy the allocator as well. If propagate_on_container_move_assignment
  39. /// is not defined then not moves are going to happen.
  40. using propagate_on_container_move_assignment = std::true_type;
  41. /// A struct to rebind the allocator to another allocator of type Y
  42. template<typename Y>
  43. struct rebind
  44. {
  45. using other = GenericPoolAllocator<Y, TPool>;
  46. };
  47. /// Default constructor
  48. GenericPoolAllocator()
  49. {
  50. }
  51. /// Copy constructor
  52. GenericPoolAllocator(const GenericPoolAllocator& b)
  53. {
  54. *this = b;
  55. }
  56. /// Copy constructor
  57. template<typename Y>
  58. GenericPoolAllocator(const GenericPoolAllocator<Y, TPool>& b)
  59. {
  60. *this = b;
  61. }
  62. /// Copy constructor, uses another type of allocator
  63. template<typename Y, typename YPool>
  64. GenericPoolAllocator(const GenericPoolAllocator<Y, YPool>& b)
  65. {
  66. auto balloc = b;
  67. m_pool = balloc.m_pool;
  68. if(m_pool)
  69. {
  70. m_pool->retain();
  71. }
  72. }
  73. /// Constuctor that creates a pool
  74. template<typename... TArgs>
  75. GenericPoolAllocator(AllocAlignedCallback allocCb, void* allocCbUserData, TArgs&&... args)
  76. {
  77. m_pool = static_cast<TPool*>(allocCb(allocCbUserData, nullptr, sizeof(TPool), alignof(TPool)));
  78. if(ANKI_UNLIKELY(!m_pool))
  79. {
  80. ANKI_UTIL_LOGF("Out of memory");
  81. }
  82. ::new(m_pool) TPool(allocCb, allocCbUserData, std::forward<TArgs>(args)...);
  83. m_pool->retain();
  84. }
  85. /// Destructor
  86. ~GenericPoolAllocator()
  87. {
  88. clear();
  89. }
  90. /// Copy
  91. GenericPoolAllocator& operator=(const GenericPoolAllocator& b)
  92. {
  93. copy(b);
  94. return *this;
  95. }
  96. /// Copy
  97. template<typename Y>
  98. GenericPoolAllocator& operator=(const GenericPoolAllocator<Y, TPool>& b)
  99. {
  100. copy(b);
  101. return *this;
  102. }
  103. /// Get the address of a reference
  104. pointer address(reference x) const
  105. {
  106. return &x;
  107. }
  108. /// Get the const address of a const reference
  109. const_pointer address(const_reference x) const
  110. {
  111. return &x;
  112. }
  113. /// Allocate memory
  114. /// @param n The elements of type T to allocate
  115. /// @param hint It's been used to override the alignment. The type should be PtrSize.
  116. pointer allocate(size_type n, [[maybe_unused]] const void* hint = nullptr)
  117. {
  118. ANKI_ASSERT(m_pool);
  119. size_type size = n * sizeof(value_type);
  120. // Operator new doesn't respect alignment (in GCC at least) so use the type's alignment. If hint override the
  121. // alignment
  122. PtrSize alignment = (hint != nullptr) ? *static_cast<const PtrSize*>(hint) : alignof(value_type);
  123. void* out = m_pool->allocate(size, alignment);
  124. if(ANKI_UNLIKELY(out == nullptr))
  125. {
  126. ANKI_UTIL_LOGF("Out of memory");
  127. }
  128. return static_cast<pointer>(out);
  129. }
  130. /// Allocate memory
  131. /// @param n The elements of type T to allocate
  132. /// @param alignment The alignment of the allocation.
  133. ///
  134. /// @note It's not part of the STL interface
  135. pointer allocate(size_type n, U32 alignment)
  136. {
  137. PtrSize hint = alignment;
  138. return allocate(n, &hint);
  139. }
  140. /// Deallocate memory
  141. void deallocate(void* p, [[maybe_unused]] size_type n)
  142. {
  143. ANKI_ASSERT(m_pool);
  144. m_pool->free(p);
  145. }
  146. /// Call constructor
  147. void construct(pointer p, const T& val)
  148. {
  149. ::new(p) T(val);
  150. }
  151. /// Call constructor with many arguments
  152. template<typename Y, typename... Args>
  153. void construct(Y* p, Args&&... args)
  154. {
  155. // Placement new
  156. ::new(static_cast<void*>(p)) Y(std::forward<Args>(args)...);
  157. }
  158. /// Call default constructor only for non-trivially constructible types.
  159. template<typename Y>
  160. void construct(Y* p)
  161. {
  162. if(!std::is_trivially_constructible<Y>::value)
  163. {
  164. ::new(static_cast<void*>(p)) Y();
  165. }
  166. }
  167. /// Call destructor
  168. void destroy(pointer p)
  169. {
  170. static_assert(sizeof(T) > 0, "Incomplete type");
  171. ANKI_ASSERT(p != nullptr);
  172. p->~T();
  173. }
  174. /// Call destructor
  175. template<typename Y>
  176. void destroy(Y* p)
  177. {
  178. static_assert(sizeof(T) > 0, "Incomplete type");
  179. ANKI_ASSERT(p != nullptr);
  180. p->~Y();
  181. }
  182. /// Get the max allocation size
  183. size_type max_size() const
  184. {
  185. return MAX_PTR_SIZE;
  186. }
  187. /// Get the memory pool
  188. /// @note This is AnKi specific
  189. const TPool& getMemoryPool() const
  190. {
  191. ANKI_ASSERT(m_pool);
  192. return *m_pool;
  193. }
  194. /// Get the memory pool
  195. /// @note This is AnKi specific
  196. TPool& getMemoryPool()
  197. {
  198. ANKI_ASSERT(m_pool);
  199. return *m_pool;
  200. }
  201. /// Allocate a new object and call it's constructor
  202. /// @note This is AnKi specific
  203. template<typename Y, typename... Args>
  204. Y* newInstance(Args&&... args)
  205. {
  206. typename rebind<Y>::other alloc(*this);
  207. Y* ptr = alloc.allocate(1);
  208. if(ptr)
  209. {
  210. alloc.construct(ptr, std::forward<Args>(args)...);
  211. }
  212. return ptr;
  213. }
  214. /// Allocate a new array of objects and call their constructor
  215. /// @note This is AnKi specific
  216. template<typename Y>
  217. Y* newArray(size_type n)
  218. {
  219. typename rebind<Y>::other alloc(*this);
  220. Y* ptr = alloc.allocate(n);
  221. if(ptr)
  222. {
  223. // Call the constuctors
  224. for(size_type i = 0; i < n; i++)
  225. {
  226. alloc.construct(&ptr[i]);
  227. }
  228. }
  229. return ptr;
  230. }
  231. /// Allocate a new array of objects and call their constructor
  232. /// @note This is AnKi specific
  233. template<typename Y>
  234. Y* newArray(size_type n, const Y& v)
  235. {
  236. typename rebind<Y>::other alloc(*this);
  237. Y* ptr = alloc.allocate(n);
  238. if(ptr)
  239. {
  240. // Call the constuctors
  241. for(size_type i = 0; i < n; i++)
  242. {
  243. alloc.construct(&ptr[i], v);
  244. }
  245. }
  246. return ptr;
  247. }
  248. /// Allocate a new array of objects and call their constructor.
  249. /// @note This is AnKi specific.
  250. /// @note The output is a parameter to work with template deduction.
  251. template<typename TValue, typename TSize>
  252. void newArray(size_type n, WeakArray<TValue, TSize>& out);
  253. /// Allocate a new array of objects and call their constructor.
  254. /// @note This is AnKi specific.
  255. /// @note The output is a parameter to work with template deduction.
  256. template<typename TValue, typename TSize>
  257. void newArray(size_type n, const TValue& v, WeakArray<TValue, TSize>& out);
  258. /// Call the destructor and deallocate an object
  259. /// @note This is AnKi specific
  260. template<typename Y>
  261. void deleteInstance(Y* ptr)
  262. {
  263. if(ptr != nullptr)
  264. {
  265. typename rebind<Y>::other alloc(*this);
  266. alloc.destroy(ptr);
  267. alloc.deallocate(ptr, 1);
  268. }
  269. }
  270. /// Call the destructor and deallocate an array of objects
  271. /// @note This is AnKi specific
  272. template<typename Y>
  273. void deleteArray(Y* ptr, size_type n)
  274. {
  275. typename rebind<Y>::other alloc(*this);
  276. if(ptr != nullptr)
  277. {
  278. // Call the destructors
  279. for(size_type i = 0; i < n; i++)
  280. {
  281. alloc.destroy(&ptr[i]);
  282. }
  283. alloc.deallocate(ptr, n);
  284. }
  285. else
  286. {
  287. ANKI_ASSERT(n == 0);
  288. }
  289. }
  290. /// Call the destructor and deallocate an array of objects
  291. /// @note This is AnKi specific
  292. template<typename TValue, typename TSize>
  293. void deleteArray(WeakArray<TValue, TSize>& arr);
  294. private:
  295. TPool* m_pool = nullptr;
  296. template<typename Y>
  297. void copy(const GenericPoolAllocator<Y, TPool>& b)
  298. {
  299. clear();
  300. if(b.m_pool)
  301. {
  302. m_pool = b.m_pool;
  303. m_pool->retain();
  304. }
  305. }
  306. void clear()
  307. {
  308. if(m_pool)
  309. {
  310. auto count = m_pool->release();
  311. if(count == 1)
  312. {
  313. auto allocCb = m_pool->getAllocationCallback();
  314. auto ud = m_pool->getAllocationCallbackUserData();
  315. ANKI_ASSERT(allocCb);
  316. m_pool->~TPool();
  317. allocCb(ud, m_pool, 0, 0);
  318. }
  319. m_pool = nullptr;
  320. }
  321. }
  322. };
  323. /// @name GenericPoolAllocator global functions
  324. /// @{
  325. /// Another allocator of the same type can deallocate from this one
  326. template<typename T1, typename T2, typename TPool>
  327. inline Bool operator==(const GenericPoolAllocator<T1, TPool>&, const GenericPoolAllocator<T2, TPool>&)
  328. {
  329. return true;
  330. }
  331. /// Another allocator of the another type cannot deallocate from this one
  332. template<typename T1, typename AnotherAllocator, typename TPool>
  333. inline Bool operator==(const GenericPoolAllocator<T1, TPool>&, const AnotherAllocator&)
  334. {
  335. return false;
  336. }
  337. /// Another allocator of the same type can deallocate from this one
  338. template<typename T1, typename T2, typename TPool>
  339. inline Bool operator!=(const GenericPoolAllocator<T1, TPool>&, const GenericPoolAllocator<T2, TPool>&)
  340. {
  341. return false;
  342. }
  343. /// Another allocator of the another type cannot deallocate from this one
  344. template<typename T1, typename AnotherAllocator, typename TPool>
  345. inline Bool operator!=(const GenericPoolAllocator<T1, TPool>&, const AnotherAllocator&)
  346. {
  347. return true;
  348. }
  349. /// @}
  350. /// Allocator using the base memory pool.
  351. template<typename T>
  352. using GenericMemoryPoolAllocator = GenericPoolAllocator<T, BaseMemoryPool>;
  353. /// Heap based allocator. The default allocator. It uses malloc and free for allocations/deallocations
  354. template<typename T>
  355. using HeapAllocator = GenericPoolAllocator<T, HeapMemoryPool>;
  356. /// Allocator that uses a StackMemoryPool
  357. template<typename T>
  358. using StackAllocator = GenericPoolAllocator<T, StackMemoryPool>;
  359. /// Allocator that uses a ChainMemoryPool
  360. template<typename T>
  361. using ChainAllocator = GenericPoolAllocator<T, ChainMemoryPool>;
  362. #define ANKI_FRIEND_ALLOCATOR \
  363. template<typename, typename> \
  364. friend class GenericPoolAllocator;
  365. /// @}
  366. } // end namespace anki
  367. #include <AnKi/Util/Allocator.inl.h>