BsMemoryAllocator.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #undef min
  6. #undef max
  7. #include <atomic>
  8. namespace BansheeEngine
  9. {
  10. class MemoryAllocatorBase;
  11. /**
  12. * @brief Thread safe class used for storing total number of memory allocations and deallocations,
  13. * primarily for statistic purposes.
  14. */
  15. class MemoryCounter
  16. {
  17. public:
  18. static BS_UTILITY_EXPORT UINT64 getNumAllocs()
  19. {
  20. return Allocs;
  21. }
  22. static BS_UTILITY_EXPORT UINT64 getNumFrees()
  23. {
  24. return Frees;
  25. }
  26. private:
  27. friend class MemoryAllocatorBase;
  28. // Threadlocal data can't be exported, so some magic to make it accessible from MemoryAllocator
  29. static BS_UTILITY_EXPORT void incAllocCount() { Allocs++; }
  30. static BS_UTILITY_EXPORT void incFreeCount() { Frees++; }
  31. static BS_THREADLOCAL UINT64 Allocs;
  32. static BS_THREADLOCAL UINT64 Frees;
  33. };
  34. /**
  35. * @brief Base class all memory allocators need to inherit. Provides
  36. * allocation and free counting.
  37. */
  38. class MemoryAllocatorBase
  39. {
  40. protected:
  41. static void incAllocCount() { MemoryCounter::incAllocCount(); }
  42. static void incFreeCount() { MemoryCounter::incFreeCount(); }
  43. };
  44. /**
  45. * @brief Memory allocator providing a generic implementation.
  46. * Specialize for specific categories as needed.
  47. *
  48. * @note For example you might implement a pool allocator for specific types in order
  49. * to reduce allocation overhead. By default standard malloc/free are used.
  50. */
  51. template<class T>
  52. class MemoryAllocator : public MemoryAllocatorBase
  53. {
  54. public:
  55. static inline void* allocate(size_t bytes)
  56. {
  57. #if BS_PROFILING_ENABLED
  58. incAllocCount();
  59. #endif
  60. return malloc(bytes);
  61. }
  62. static inline void* allocateArray(size_t bytes, UINT32 count)
  63. {
  64. #if BS_PROFILING_ENABLED
  65. incAllocCount();
  66. #endif
  67. return malloc(bytes * count);
  68. }
  69. static inline void free(void* ptr)
  70. {
  71. #if BS_PROFILING_ENABLED
  72. incFreeCount();
  73. #endif
  74. ::free(ptr);
  75. }
  76. static inline void freeArray(void* ptr, UINT32 count)
  77. {
  78. #if BS_PROFILING_ENABLED
  79. incFreeCount();
  80. #endif
  81. ::free(ptr);
  82. }
  83. };
  84. /**
  85. * @brief General allocator provided by the OS. Use for persistent long term allocations,
  86. * and allocations that don't happen often.
  87. */
  88. class GenAlloc
  89. { };
  90. /**
  91. * @brief Allocator used for allocating small amounts of temporary memory that
  92. * used and then quickly released
  93. *
  94. * @note Currently not used.
  95. */
  96. class ScratchAlloc
  97. { };
  98. /**
  99. * @brief Pool allocator that is only suited for allocating one specific type of data. Most useful when you are
  100. * often allocating one certain data type, with no specific allocation or deallocation order.
  101. *
  102. * @note Currently not used.
  103. */
  104. class PoolAlloc
  105. { };
  106. /**
  107. * @brief Allocates the specified number of bytes.
  108. */
  109. template<class Alloc>
  110. inline void* bs_alloc(size_t count)
  111. {
  112. return MemoryAllocator<Alloc>::allocate(count);
  113. }
  114. /**
  115. * @brief Allocates enough bytes to hold the specified type, but doesn't construct it.
  116. */
  117. template<class T, class Alloc>
  118. inline T* bs_alloc()
  119. {
  120. return (T*)MemoryAllocator<Alloc>::allocate(sizeof(T));
  121. }
  122. /**
  123. * @brief Creates and constructs an array of "count" elements.
  124. */
  125. template<class T, class Alloc>
  126. inline T* bs_newN(UINT32 count)
  127. {
  128. T* ptr = (T*)MemoryAllocator<Alloc>::allocateArray(sizeof(T), count);
  129. for(unsigned int i = 0; i < count; i++)
  130. new ((void*)&ptr[i]) T;
  131. return ptr;
  132. }
  133. /**
  134. * @brief Create a new object with the specified allocator and the specified parameters.
  135. */
  136. template<class Type, class Alloc, class... Args>
  137. Type* bs_new(Args &&...args)
  138. {
  139. return new (bs_alloc<Alloc>(sizeof(Type))) Type(std::forward<Args>(args)...);
  140. }
  141. /**
  142. * @brief Frees all the bytes allocated at the specified location.
  143. */
  144. template<class Alloc>
  145. inline void bs_free(void* ptr)
  146. {
  147. MemoryAllocator<Alloc>::free(ptr);
  148. }
  149. /**
  150. * @brief Destructs and frees the specified object.
  151. */
  152. template<class Alloc, class T>
  153. inline void bs_delete(T* ptr)
  154. {
  155. (ptr)->~T();
  156. MemoryAllocator<Alloc>::free(ptr);
  157. }
  158. /**
  159. * @brief Destructs and frees the specified array of objects.
  160. */
  161. template<class Alloc, class T>
  162. inline void bs_deleteN(T* ptr, UINT32 count)
  163. {
  164. for(unsigned int i = 0; i < count; i++)
  165. ptr[i].~T();
  166. MemoryAllocator<Alloc>::freeArray(ptr, count);
  167. }
  168. /*****************************************************************************/
  169. /* Default versions of all alloc/free/new/delete methods which call GenAlloc */
  170. /*****************************************************************************/
  171. /**
  172. * @brief Allocates the specified number of bytes.
  173. */
  174. inline void* bs_alloc(size_t count)
  175. {
  176. return MemoryAllocator<GenAlloc>::allocate(count);
  177. }
  178. /**
  179. * @brief Allocates enough bytes to hold the specified type, but doesn't construct it.
  180. */
  181. template<class T>
  182. inline T* bs_alloc()
  183. {
  184. return (T*)MemoryAllocator<GenAlloc>::allocate(sizeof(T));
  185. }
  186. /**
  187. * @brief Creates and constructs an array of "count" elements.
  188. */
  189. template<class T>
  190. inline T* bs_newN(UINT32 count)
  191. {
  192. T* ptr = (T*)MemoryAllocator<GenAlloc>::allocateArray(sizeof(T), count);
  193. for(unsigned int i = 0; i < count; i++)
  194. new ((void*)&ptr[i]) T;
  195. return ptr;
  196. }
  197. /**
  198. * @brief Create a new object with the specified allocator and the specified parameters.
  199. */
  200. template<class Type, class... Args>
  201. Type* bs_new(Args &&...args)
  202. {
  203. return new (bs_alloc<GenAlloc>(sizeof(Type))) Type(std::forward<Args>(args)...);
  204. }
  205. /**
  206. * @brief Frees all the bytes allocated at the specified location.
  207. */
  208. inline void bs_free(void* ptr)
  209. {
  210. MemoryAllocator<GenAlloc>::free(ptr);
  211. }
  212. /**
  213. * @brief Destructs and frees the specified object.
  214. */
  215. template<class T>
  216. inline void bs_delete(T* ptr)
  217. {
  218. (ptr)->~T();
  219. MemoryAllocator<GenAlloc>::free(ptr);
  220. }
  221. /**
  222. * @brief Destructs and frees the specified array of objects.
  223. */
  224. template<class T>
  225. inline void bs_deleteN(T* ptr, UINT32 count)
  226. {
  227. for(unsigned int i = 0; i < count; i++)
  228. ptr[i].~T();
  229. MemoryAllocator<GenAlloc>::freeArray(ptr, count);
  230. }
  231. /************************************************************************/
  232. /* MACRO VERSIONS */
  233. /* You will almost always want to use the template versions but in some */
  234. /* cases (private destructor) it is not possible. In which case you may */
  235. /* use these instead. */
  236. /************************************************************************/
  237. #define BS_PVT_DELETE(T, ptr) \
  238. (ptr)->~T(); \
  239. MemoryAllocator<GenAlloc>::free(ptr);
  240. #define BS_PVT_DELETE_A(T, ptr, Alloc) \
  241. (ptr)->~T(); \
  242. MemoryAllocator<Alloc>::free(ptr);
  243. }
  244. namespace BansheeEngine
  245. {
  246. /**
  247. * @brief Allocator for the standard library that internally uses Banshee
  248. * memory allocator.
  249. */
  250. template <class T, class Alloc = GenAlloc>
  251. class StdAlloc
  252. {
  253. public:
  254. // Type definitions
  255. typedef T value_type;
  256. typedef T* pointer;
  257. typedef const T* const_pointer;
  258. typedef T& reference;
  259. typedef const T& const_reference;
  260. typedef std::size_t size_type;
  261. typedef std::ptrdiff_t difference_type;
  262. /**
  263. * @brief Rebind allocator to type U
  264. */
  265. template <class U>
  266. struct rebind
  267. {
  268. typedef StdAlloc<U, Alloc> other;
  269. };
  270. StdAlloc() throw()
  271. { }
  272. StdAlloc(const StdAlloc&) throw()
  273. { }
  274. template <class U>
  275. StdAlloc (const StdAlloc<U, Alloc>&) throw()
  276. { }
  277. ~StdAlloc() throw()
  278. { }
  279. /**
  280. * @brief Return address of value.
  281. */
  282. pointer address (reference value) const
  283. {
  284. return &value;
  285. }
  286. /**
  287. * @brief Return address of value.
  288. */
  289. const_pointer address (const_reference value) const
  290. {
  291. return &value;
  292. }
  293. /**
  294. * @brief Return maximum number of elements that can be allocated.
  295. */
  296. size_type max_size () const throw()
  297. {
  298. return std::numeric_limits<std::size_t>::max() / sizeof(T);
  299. }
  300. /**
  301. * @brief Allocate but don't initialize number elements of type T.
  302. */
  303. pointer allocate (size_type num, const void* = 0)
  304. {
  305. pointer ret = (pointer)(bs_alloc<Alloc>((size_t)num*sizeof(T)));
  306. return ret;
  307. }
  308. /**
  309. * @brief Initialize elements of allocated storage p with value "value".
  310. */
  311. void construct (pointer p, const T& value)
  312. {
  313. new((void*)p)T(value);
  314. }
  315. /**
  316. * @brief Destroy elements of initialized storage p.
  317. */
  318. void destroy (pointer p)
  319. {
  320. p->~T();
  321. }
  322. /**
  323. * @brief Deallocate storage p of deleted elements.
  324. */
  325. void deallocate (pointer p, size_type num)
  326. {
  327. bs_free<Alloc>((void*)p);
  328. }
  329. };
  330. /**
  331. * @brief Return that all specializations of this allocator are interchangeable.
  332. */
  333. template <class T1, class T2, class Alloc>
  334. bool operator== (const StdAlloc<T1, Alloc>&,
  335. const StdAlloc<T2, Alloc>&) throw() {
  336. return true;
  337. }
  338. /**
  339. * @brief Return that all specializations of this allocator are interchangeable.
  340. */
  341. template <class T1, class T2, class Alloc>
  342. bool operator!= (const StdAlloc<T1, Alloc>&,
  343. const StdAlloc<T2, Alloc>&) throw() {
  344. return false;
  345. }
  346. }
  347. #include "BsMemStack.h"
  348. #include "BsMemAllocProfiler.h"