BsMemoryAllocator.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 Allocates the specified number of bytes.
  89. */
  90. template<class Alloc>
  91. inline void* bs_alloc(UINT32 count)
  92. {
  93. return MemoryAllocator<Alloc>::allocate(count);
  94. }
  95. /**
  96. * @brief Allocates enough bytes to hold the specified type, but doesn't construct it.
  97. */
  98. template<class T, class Alloc>
  99. inline T* bs_alloc()
  100. {
  101. return (T*)MemoryAllocator<Alloc>::allocate(sizeof(T));
  102. }
  103. /**
  104. * @brief Creates and constructs an array of "count" elements.
  105. */
  106. template<class T, class Alloc>
  107. inline T* bs_newN(UINT32 count)
  108. {
  109. T* ptr = (T*)MemoryAllocator<Alloc>::allocateArray(sizeof(T), count);
  110. for(unsigned int i = 0; i < count; i++)
  111. new ((void*)&ptr[i]) T;
  112. return ptr;
  113. }
  114. /**
  115. * @brief Create a new object with the specified allocator and the specified parameters.
  116. */
  117. template<class Type, class Alloc, class... Args>
  118. Type* bs_new(Args &&...args)
  119. {
  120. return new (bs_alloc<Alloc>(sizeof(Type))) Type(std::forward<Args>(args)...);
  121. }
  122. /**
  123. * @brief Frees all the bytes allocated at the specified location.
  124. */
  125. template<class Alloc>
  126. inline void bs_free(void* ptr)
  127. {
  128. MemoryAllocator<Alloc>::free(ptr);
  129. }
  130. /**
  131. * @brief Destructs and frees the specified object.
  132. */
  133. template<class T, class Alloc = GenAlloc>
  134. inline void bs_delete(T* ptr)
  135. {
  136. (ptr)->~T();
  137. MemoryAllocator<Alloc>::free(ptr);
  138. }
  139. /**
  140. * @brief Destructs and frees the specified array of objects.
  141. */
  142. template<class T, class Alloc = GenAlloc>
  143. inline void bs_deleteN(T* ptr, UINT32 count)
  144. {
  145. for(unsigned int i = 0; i < count; i++)
  146. ptr[i].~T();
  147. MemoryAllocator<Alloc>::freeArray(ptr, count);
  148. }
  149. /*****************************************************************************/
  150. /* Default versions of all alloc/free/new/delete methods which call GenAlloc */
  151. /*****************************************************************************/
  152. /**
  153. * @brief Allocates the specified number of bytes.
  154. */
  155. inline void* bs_alloc(UINT32 count)
  156. {
  157. return MemoryAllocator<GenAlloc>::allocate(count);
  158. }
  159. /**
  160. * @brief Allocates enough bytes to hold the specified type, but doesn't construct it.
  161. */
  162. template<class T>
  163. inline T* bs_alloc()
  164. {
  165. return (T*)MemoryAllocator<GenAlloc>::allocate(sizeof(T));
  166. }
  167. /**
  168. * @brief Allocates enough bytes to hold an array of \p count elements the specified type, but doesn't construct them.
  169. */
  170. template<class T>
  171. inline T* bs_allocN(UINT32 count)
  172. {
  173. return (T*)MemoryAllocator<GenAlloc>::allocate(count * sizeof(T));
  174. }
  175. /**
  176. * @brief Creates and constructs an array of \p count elements.
  177. */
  178. template<class T>
  179. inline T* bs_newN(UINT32 count)
  180. {
  181. T* ptr = (T*)MemoryAllocator<GenAlloc>::allocateArray(sizeof(T), count);
  182. for(unsigned int i = 0; i < count; i++)
  183. new ((void*)&ptr[i]) T;
  184. return ptr;
  185. }
  186. /**
  187. * @brief Create a new object with the specified allocator and the specified parameters.
  188. */
  189. template<class Type, class... Args>
  190. Type* bs_new(Args &&...args)
  191. {
  192. return new (bs_alloc<GenAlloc>(sizeof(Type))) Type(std::forward<Args>(args)...);
  193. }
  194. /**
  195. * @brief Frees all the bytes allocated at the specified location.
  196. */
  197. inline void bs_free(void* ptr)
  198. {
  199. MemoryAllocator<GenAlloc>::free(ptr);
  200. }
  201. /************************************************************************/
  202. /* MACRO VERSIONS */
  203. /* You will almost always want to use the template versions but in some */
  204. /* cases (private destructor) it is not possible. In which case you may */
  205. /* use these instead. */
  206. /************************************************************************/
  207. #define BS_PVT_DELETE(T, ptr) \
  208. (ptr)->~T(); \
  209. MemoryAllocator<GenAlloc>::free(ptr);
  210. #define BS_PVT_DELETE_A(T, ptr, Alloc) \
  211. (ptr)->~T(); \
  212. MemoryAllocator<Alloc>::free(ptr);
  213. }
  214. namespace BansheeEngine
  215. {
  216. /**
  217. * @brief Allocator for the standard library that internally uses Banshee
  218. * memory allocator.
  219. */
  220. template <class T, class Alloc = GenAlloc>
  221. class StdAlloc
  222. {
  223. public:
  224. typedef T value_type;
  225. StdAlloc() noexcept {}
  226. template<class T, class Alloc> StdAlloc(const StdAlloc<T, Alloc>&) noexcept {}
  227. template<class T, class Alloc> bool operator==(const StdAlloc<T, Alloc>&) const noexcept { return true; }
  228. template<class T, class Alloc> bool operator!=(const StdAlloc<T, Alloc>&) const noexcept { return false; }
  229. /**
  230. * @brief Allocate but don't initialize number elements of type T.
  231. */
  232. T* allocate(const size_t num) const
  233. {
  234. if (num == 0)
  235. return nullptr;
  236. if (num > static_cast<size_t>(-1) / sizeof(T))
  237. throw std::bad_array_new_length();
  238. void* const pv = bs_alloc<Alloc>((UINT32)(num * sizeof(T)));
  239. if (!pv)
  240. throw std::bad_alloc();
  241. return static_cast<T*>(pv);
  242. }
  243. /**
  244. * @brief Deallocate storage p of deleted elements.
  245. */
  246. void deallocate(T* p, size_t num) const noexcept
  247. {
  248. bs_free<Alloc>((void*)p);
  249. }
  250. };
  251. }
  252. #include "BsMemStack.h"
  253. #include "BsGlobalFrameAlloc.h"
  254. #include "BsMemAllocProfiler.h"