BsMemoryAllocator.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. // Type definitions
  225. typedef T value_type;
  226. typedef T* pointer;
  227. typedef const T* const_pointer;
  228. typedef T& reference;
  229. typedef const T& const_reference;
  230. typedef std::size_t size_type;
  231. typedef std::ptrdiff_t difference_type;
  232. /**
  233. * @brief Rebind allocator to type U
  234. */
  235. template <class U>
  236. struct rebind
  237. {
  238. typedef StdAlloc<U, Alloc> other;
  239. };
  240. StdAlloc() throw()
  241. { }
  242. StdAlloc(const StdAlloc&) throw()
  243. { }
  244. template <class U>
  245. StdAlloc (const StdAlloc<U, Alloc>&) throw()
  246. { }
  247. ~StdAlloc() throw()
  248. { }
  249. /**
  250. * @brief Return address of value.
  251. */
  252. pointer address (reference value) const
  253. {
  254. return &value;
  255. }
  256. /**
  257. * @brief Return address of value.
  258. */
  259. const_pointer address (const_reference value) const
  260. {
  261. return &value;
  262. }
  263. /**
  264. * @brief Return maximum number of elements that can be allocated.
  265. */
  266. size_type max_size () const throw()
  267. {
  268. return std::numeric_limits<std::size_t>::max() / sizeof(T);
  269. }
  270. /**
  271. * @brief Allocate but don't initialize number elements of type T.
  272. */
  273. pointer allocate (size_type num, const void* = 0)
  274. {
  275. pointer ret = (pointer)(bs_alloc<Alloc>((UINT32)num*sizeof(T)));
  276. return ret;
  277. }
  278. /**
  279. * @brief Initialize elements of allocated storage p with value "value".
  280. */
  281. void construct (pointer p, const T& value)
  282. {
  283. new((void*)p)T(value);
  284. }
  285. /**
  286. * @brief Destroy elements of initialized storage p.
  287. */
  288. void destroy (pointer p)
  289. {
  290. p->~T();
  291. }
  292. /**
  293. * @brief Deallocate storage p of deleted elements.
  294. */
  295. void deallocate (pointer p, size_type num)
  296. {
  297. bs_free<Alloc>((void*)p);
  298. }
  299. };
  300. /**
  301. * @brief Return that all specializations of this allocator are interchangeable.
  302. */
  303. template <class T1, class T2, class Alloc>
  304. bool operator== (const StdAlloc<T1, Alloc>&,
  305. const StdAlloc<T2, Alloc>&) throw() {
  306. return true;
  307. }
  308. /**
  309. * @brief Return that all specializations of this allocator are interchangeable.
  310. */
  311. template <class T1, class T2, class Alloc>
  312. bool operator!= (const StdAlloc<T1, Alloc>&,
  313. const StdAlloc<T2, Alloc>&) throw() {
  314. return false;
  315. }
  316. }
  317. #include "BsMemStack.h"
  318. #include "BsGlobalFrameAlloc.h"
  319. #include "BsMemAllocProfiler.h"