2
0

BsMemoryAllocator.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 Creates and constructs an array of "count" elements.
  169. */
  170. template<class T>
  171. inline T* bs_newN(UINT32 count)
  172. {
  173. T* ptr = (T*)MemoryAllocator<GenAlloc>::allocateArray(sizeof(T), count);
  174. for(unsigned int i = 0; i < count; i++)
  175. new ((void*)&ptr[i]) T;
  176. return ptr;
  177. }
  178. /**
  179. * @brief Create a new object with the specified allocator and the specified parameters.
  180. */
  181. template<class Type, class... Args>
  182. Type* bs_new(Args &&...args)
  183. {
  184. return new (bs_alloc<GenAlloc>(sizeof(Type))) Type(std::forward<Args>(args)...);
  185. }
  186. /**
  187. * @brief Frees all the bytes allocated at the specified location.
  188. */
  189. inline void bs_free(void* ptr)
  190. {
  191. MemoryAllocator<GenAlloc>::free(ptr);
  192. }
  193. /************************************************************************/
  194. /* MACRO VERSIONS */
  195. /* You will almost always want to use the template versions but in some */
  196. /* cases (private destructor) it is not possible. In which case you may */
  197. /* use these instead. */
  198. /************************************************************************/
  199. #define BS_PVT_DELETE(T, ptr) \
  200. (ptr)->~T(); \
  201. MemoryAllocator<GenAlloc>::free(ptr);
  202. #define BS_PVT_DELETE_A(T, ptr, Alloc) \
  203. (ptr)->~T(); \
  204. MemoryAllocator<Alloc>::free(ptr);
  205. }
  206. namespace BansheeEngine
  207. {
  208. /**
  209. * @brief Allocator for the standard library that internally uses Banshee
  210. * memory allocator.
  211. */
  212. template <class T, class Alloc = GenAlloc>
  213. class StdAlloc
  214. {
  215. public:
  216. // Type definitions
  217. typedef T value_type;
  218. typedef T* pointer;
  219. typedef const T* const_pointer;
  220. typedef T& reference;
  221. typedef const T& const_reference;
  222. typedef std::size_t size_type;
  223. typedef std::ptrdiff_t difference_type;
  224. /**
  225. * @brief Rebind allocator to type U
  226. */
  227. template <class U>
  228. struct rebind
  229. {
  230. typedef StdAlloc<U, Alloc> other;
  231. };
  232. StdAlloc() throw()
  233. { }
  234. StdAlloc(const StdAlloc&) throw()
  235. { }
  236. template <class U>
  237. StdAlloc (const StdAlloc<U, Alloc>&) throw()
  238. { }
  239. ~StdAlloc() throw()
  240. { }
  241. /**
  242. * @brief Return address of value.
  243. */
  244. pointer address (reference value) const
  245. {
  246. return &value;
  247. }
  248. /**
  249. * @brief Return address of value.
  250. */
  251. const_pointer address (const_reference value) const
  252. {
  253. return &value;
  254. }
  255. /**
  256. * @brief Return maximum number of elements that can be allocated.
  257. */
  258. size_type max_size () const throw()
  259. {
  260. return std::numeric_limits<std::size_t>::max() / sizeof(T);
  261. }
  262. /**
  263. * @brief Allocate but don't initialize number elements of type T.
  264. */
  265. pointer allocate (size_type num, const void* = 0)
  266. {
  267. pointer ret = (pointer)(bs_alloc<Alloc>((UINT32)num*sizeof(T)));
  268. return ret;
  269. }
  270. /**
  271. * @brief Initialize elements of allocated storage p with value "value".
  272. */
  273. void construct (pointer p, const T& value)
  274. {
  275. new((void*)p)T(value);
  276. }
  277. /**
  278. * @brief Destroy elements of initialized storage p.
  279. */
  280. void destroy (pointer p)
  281. {
  282. p->~T();
  283. }
  284. /**
  285. * @brief Deallocate storage p of deleted elements.
  286. */
  287. void deallocate (pointer p, size_type num)
  288. {
  289. bs_free<Alloc>((void*)p);
  290. }
  291. };
  292. /**
  293. * @brief Return that all specializations of this allocator are interchangeable.
  294. */
  295. template <class T1, class T2, class Alloc>
  296. bool operator== (const StdAlloc<T1, Alloc>&,
  297. const StdAlloc<T2, Alloc>&) throw() {
  298. return true;
  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 false;
  307. }
  308. }
  309. #include "BsMemStack.h"
  310. #include "BsGlobalFrameAlloc.h"
  311. #include "BsMemAllocProfiler.h"