BsMemoryAllocator.h 8.9 KB

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