Allocator.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #pragma once
  6. #include <AnKi/Util/Assert.h>
  7. #include <AnKi/Util/MemoryPool.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(!m_pool) [[unlikely]]
  79. {
  80. ANKI_UTIL_LOGF("Out of memory");
  81. }
  82. ::new(m_pool) TPool();
  83. m_pool->init(allocCb, allocCbUserData, std::forward<TArgs>(args)...);
  84. m_pool->retain();
  85. }
  86. /// Destructor
  87. ~GenericPoolAllocator()
  88. {
  89. clear();
  90. }
  91. /// Copy
  92. GenericPoolAllocator& operator=(const GenericPoolAllocator& b)
  93. {
  94. copy(b);
  95. return *this;
  96. }
  97. /// Copy
  98. template<typename Y>
  99. GenericPoolAllocator& operator=(const GenericPoolAllocator<Y, TPool>& b)
  100. {
  101. copy(b);
  102. return *this;
  103. }
  104. /// Get the address of a reference
  105. pointer address(reference x) const
  106. {
  107. return &x;
  108. }
  109. /// Get the const address of a const reference
  110. const_pointer address(const_reference x) const
  111. {
  112. return &x;
  113. }
  114. /// Allocate memory
  115. /// @param n The elements of type T to allocate
  116. /// @param hint It's been used to override the alignment. The type should be PtrSize.
  117. pointer allocate(size_type n, [[maybe_unused]] const void* hint = nullptr)
  118. {
  119. ANKI_ASSERT(m_pool);
  120. size_type size = n * sizeof(value_type);
  121. // Operator new doesn't respect alignment (in GCC at least) so use the type's alignment. If hint override the
  122. // alignment
  123. PtrSize alignment = (hint != nullptr) ? *static_cast<const PtrSize*>(hint) : alignof(value_type);
  124. void* out = m_pool->allocate(size, alignment);
  125. if(out == nullptr) [[unlikely]]
  126. {
  127. ANKI_UTIL_LOGF("Out of memory");
  128. }
  129. return static_cast<pointer>(out);
  130. }
  131. /// Allocate memory
  132. /// @param n The elements of type T to allocate
  133. /// @param alignment The alignment of the allocation.
  134. ///
  135. /// @note It's not part of the STL interface
  136. pointer allocate(size_type n, U32 alignment)
  137. {
  138. PtrSize hint = alignment;
  139. return allocate(n, &hint);
  140. }
  141. /// Deallocate memory
  142. void deallocate(void* p, [[maybe_unused]] size_type n)
  143. {
  144. ANKI_ASSERT(m_pool);
  145. m_pool->free(p);
  146. }
  147. /// Call constructor
  148. void construct(pointer p, const T& val)
  149. {
  150. ::new(p) T(val);
  151. }
  152. /// Call constructor with many arguments
  153. template<typename Y, typename... Args>
  154. void construct(Y* p, Args&&... args)
  155. {
  156. // Placement new
  157. ::new(static_cast<void*>(p)) Y(std::forward<Args>(args)...);
  158. }
  159. /// Call default constructor only for non-trivially constructible types.
  160. template<typename Y>
  161. void construct(Y* p)
  162. {
  163. if(!std::is_trivially_constructible<Y>::value)
  164. {
  165. ::new(static_cast<void*>(p)) Y();
  166. }
  167. }
  168. /// Call destructor
  169. void destroy(pointer p)
  170. {
  171. static_assert(sizeof(T) > 0, "Incomplete type");
  172. ANKI_ASSERT(p != nullptr);
  173. p->~T();
  174. }
  175. /// Call destructor
  176. template<typename Y>
  177. void destroy(Y* p)
  178. {
  179. static_assert(sizeof(T) > 0, "Incomplete type");
  180. ANKI_ASSERT(p != nullptr);
  181. p->~Y();
  182. }
  183. /// Get the max allocation size
  184. size_type max_size() const
  185. {
  186. return kMaxPtrSize;
  187. }
  188. /// Get the memory pool
  189. /// @note This is AnKi specific
  190. const TPool& getMemoryPool() const
  191. {
  192. ANKI_ASSERT(m_pool);
  193. return *m_pool;
  194. }
  195. /// Get the memory pool
  196. /// @note This is AnKi specific
  197. TPool& getMemoryPool()
  198. {
  199. ANKI_ASSERT(m_pool);
  200. return *m_pool;
  201. }
  202. /// Allocate a new object and call it's constructor
  203. /// @note This is AnKi specific
  204. template<typename Y, typename... Args>
  205. Y* newInstance(Args&&... args)
  206. {
  207. typename rebind<Y>::other alloc(*this);
  208. Y* ptr = alloc.allocate(1);
  209. if(ptr)
  210. {
  211. alloc.construct(ptr, std::forward<Args>(args)...);
  212. }
  213. return ptr;
  214. }
  215. /// Allocate a new array of objects and call their constructor
  216. /// @note This is AnKi specific
  217. template<typename Y>
  218. Y* newArray(size_type n)
  219. {
  220. typename rebind<Y>::other alloc(*this);
  221. Y* ptr = alloc.allocate(n);
  222. if(ptr)
  223. {
  224. // Call the constuctors
  225. for(size_type i = 0; i < n; i++)
  226. {
  227. alloc.construct(&ptr[i]);
  228. }
  229. }
  230. return ptr;
  231. }
  232. /// Allocate a new array of objects and call their constructor
  233. /// @note This is AnKi specific
  234. template<typename Y>
  235. Y* newArray(size_type n, const Y& v)
  236. {
  237. typename rebind<Y>::other alloc(*this);
  238. Y* ptr = alloc.allocate(n);
  239. if(ptr)
  240. {
  241. // Call the constuctors
  242. for(size_type i = 0; i < n; i++)
  243. {
  244. alloc.construct(&ptr[i], v);
  245. }
  246. }
  247. return ptr;
  248. }
  249. /// Allocate a new array of objects and call their constructor.
  250. /// @note This is AnKi specific.
  251. /// @note The output is a parameter to work with template deduction.
  252. template<typename TValue, typename TSize>
  253. void newArray(size_type n, WeakArray<TValue, TSize>& out)
  254. {
  255. TValue* arr = newArray<TValue>(n);
  256. ANKI_ASSERT(n < std::numeric_limits<TSize>::max());
  257. out.setArray(arr, TSize(n));
  258. }
  259. /// Allocate a new array of objects and call their constructor.
  260. /// @note This is AnKi specific.
  261. /// @note The output is a parameter to work with template deduction.
  262. template<typename TValue, typename TSize>
  263. void newArray(size_type n, const TValue& v, WeakArray<TValue, TSize>& out)
  264. {
  265. TValue* arr = newArray<TValue>(n, v);
  266. ANKI_ASSERT(n < std::numeric_limits<TSize>::max());
  267. out.setArray(arr, TSize(n));
  268. }
  269. /// Call the destructor and deallocate an object
  270. /// @note This is AnKi specific
  271. template<typename Y>
  272. void deleteInstance(Y* ptr)
  273. {
  274. if(ptr != nullptr)
  275. {
  276. typename rebind<Y>::other alloc(*this);
  277. alloc.destroy(ptr);
  278. alloc.deallocate(ptr, 1);
  279. }
  280. }
  281. /// Call the destructor and deallocate an array of objects
  282. /// @note This is AnKi specific
  283. template<typename Y>
  284. void deleteArray(Y* ptr, size_type n)
  285. {
  286. typename rebind<Y>::other alloc(*this);
  287. if(ptr != nullptr)
  288. {
  289. // Call the destructors
  290. for(size_type i = 0; i < n; i++)
  291. {
  292. alloc.destroy(&ptr[i]);
  293. }
  294. alloc.deallocate(ptr, n);
  295. }
  296. else
  297. {
  298. ANKI_ASSERT(n == 0);
  299. }
  300. }
  301. /// Call the destructor and deallocate an array of objects
  302. /// @note This is AnKi specific
  303. template<typename TValue, typename TSize>
  304. void deleteArray(WeakArray<TValue, TSize>& arr)
  305. {
  306. deleteArray(arr.getBegin(), arr.getSize());
  307. arr.setArray(nullptr, 0);
  308. }
  309. private:
  310. TPool* m_pool = nullptr;
  311. template<typename Y>
  312. void copy(const GenericPoolAllocator<Y, TPool>& b)
  313. {
  314. clear();
  315. if(b.m_pool)
  316. {
  317. m_pool = b.m_pool;
  318. m_pool->retain();
  319. }
  320. }
  321. void clear()
  322. {
  323. if(m_pool)
  324. {
  325. auto count = m_pool->release();
  326. if(count == 1)
  327. {
  328. auto allocCb = m_pool->getAllocationCallback();
  329. auto ud = m_pool->getAllocationCallbackUserData();
  330. ANKI_ASSERT(allocCb);
  331. m_pool->~TPool();
  332. allocCb(ud, m_pool, 0, 0);
  333. }
  334. m_pool = nullptr;
  335. }
  336. }
  337. };
  338. /// @name GenericPoolAllocator global functions
  339. /// @{
  340. /// Another allocator of the same type can deallocate from this one
  341. template<typename T1, typename T2, typename TPool>
  342. inline Bool operator==(const GenericPoolAllocator<T1, TPool>&, const GenericPoolAllocator<T2, TPool>&)
  343. {
  344. return true;
  345. }
  346. /// Another allocator of the another type cannot deallocate from this one
  347. template<typename T1, typename AnotherAllocator, typename TPool>
  348. inline Bool operator==(const GenericPoolAllocator<T1, TPool>&, const AnotherAllocator&)
  349. {
  350. return false;
  351. }
  352. /// Another allocator of the same type can deallocate from this one
  353. template<typename T1, typename T2, typename TPool>
  354. inline Bool operator!=(const GenericPoolAllocator<T1, TPool>&, const GenericPoolAllocator<T2, TPool>&)
  355. {
  356. return false;
  357. }
  358. /// Another allocator of the another type cannot deallocate from this one
  359. template<typename T1, typename AnotherAllocator, typename TPool>
  360. inline Bool operator!=(const GenericPoolAllocator<T1, TPool>&, const AnotherAllocator&)
  361. {
  362. return true;
  363. }
  364. /// @}
  365. /// Allocator using the base memory pool.
  366. template<typename T>
  367. using GenericMemoryPoolAllocator = GenericPoolAllocator<T, RefCountedMemoryPool<BaseMemoryPool>>;
  368. /// Heap based allocator. The default allocator. It uses malloc and free for allocations/deallocations
  369. template<typename T>
  370. using HeapAllocator = GenericPoolAllocator<T, RefCountedMemoryPool<HeapMemoryPool>>;
  371. /// Allocator that uses a StackMemoryPool
  372. template<typename T>
  373. using StackAllocator = GenericPoolAllocator<T, RefCountedMemoryPool<StackMemoryPool>>;
  374. #define ANKI_FRIEND_ALLOCATOR \
  375. template<typename, typename> \
  376. friend class GenericPoolAllocator;
  377. /// @}
  378. } // end namespace anki