BsGlobalFrameAlloc.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #include "BsStdHeaders.h"
  3. #include "BsThreadDefines.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Memory
  7. * @{
  8. */
  9. class FrameAlloc;
  10. /**
  11. * Returns a global, application wide frame allocator. Each thread gets its own frame allocator.
  12. *
  13. * @note Thread safe.
  14. */
  15. inline BS_UTILITY_EXPORT FrameAlloc& gFrameAlloc();
  16. /**
  17. * Allocates some memory using the global frame allocator.
  18. *
  19. * @param[in] numBytes Number of bytes to allocate.
  20. */
  21. inline BS_UTILITY_EXPORT UINT8* bs_frame_alloc(UINT32 numBytes);
  22. /**
  23. * Deallocates memory allocated with the global frame allocator.
  24. *
  25. * @note Must be called on the same thread the memory was allocated on.
  26. */
  27. inline BS_UTILITY_EXPORT void bs_frame_free(void* data);
  28. /**
  29. * Allocates enough memory to hold the object of specified type using the global frame allocator, but does not
  30. * construct the object.
  31. */
  32. template<class T>
  33. T* bs_frame_alloc()
  34. {
  35. return (T*)bs_frame_alloc(sizeof(T));
  36. }
  37. /**
  38. * Allocates enough memory to hold N objects of specified type using the global frame allocator, but does not
  39. * construct the object.
  40. */
  41. template<class T>
  42. T* bs_frame_alloc(UINT32 count)
  43. {
  44. return (T*)bs_frame_alloc(sizeof(T) * count);
  45. }
  46. /**
  47. * Allocates enough memory to hold the object(s) of specified type using the global frame allocator,
  48. * and constructs them.
  49. */
  50. template<class T>
  51. T* bs_frame_new(UINT32 count = 0)
  52. {
  53. T* data = bs_frame_alloc<T>(count);
  54. for(unsigned int i = 0; i < count; i++)
  55. new ((void*)&data[i]) T;
  56. return data;
  57. }
  58. /**
  59. * Allocates enough memory to hold the object(s) of specified type using the global frame allocator, and constructs them.
  60. */
  61. template<class T, class... Args>
  62. T* bs_frame_new(Args &&...args, UINT32 count = 0)
  63. {
  64. T* data = bs_frame_alloc<T>(count);
  65. for(unsigned int i = 0; i < count; i++)
  66. new ((void*)&data[i]) T(std::forward<Args>(args)...);
  67. return data;
  68. }
  69. /**
  70. * Destructs and deallocates an object allocated with the global frame allocator.
  71. *
  72. * @note Must be called on the same thread the memory was allocated on.
  73. */
  74. template<class T>
  75. void bs_frame_delete(T* data)
  76. {
  77. data->~T();
  78. bs_frame_free((UINT8*)data);
  79. }
  80. /**
  81. * Destructs and deallocates an array of objects allocated with the global frame allocator.
  82. *
  83. * @note Must be called on the same thread the memory was allocated on.
  84. */
  85. template<class T>
  86. void bs_frame_delete(T* data, UINT32 count)
  87. {
  88. for(unsigned int i = 0; i < count; i++)
  89. data[i].~T();
  90. bs_frame_free((UINT8*)data);
  91. }
  92. /** @copydoc FrameAlloc::markFrame */
  93. inline BS_UTILITY_EXPORT void bs_frame_mark();
  94. /** @copydoc FrameAlloc::clear */
  95. inline BS_UTILITY_EXPORT void bs_frame_clear();
  96. /** String allocated with a frame allocator. */
  97. typedef std::basic_string<char, std::char_traits<char>, StdAlloc<char, FrameAlloc>> FrameString;
  98. /** WString allocated with a frame allocator. */
  99. typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, StdAlloc<wchar_t, FrameAlloc>> FrameWString;
  100. /** Vector allocated with a frame allocator. */
  101. template <typename T, typename A = StdAlloc<T, FrameAlloc>>
  102. using FrameVector = std::vector < T, A > ;
  103. /** Stack allocated with a frame allocator. */
  104. template <typename T, typename A = StdAlloc<T, FrameAlloc>>
  105. using FrameStack = std::stack < T, std::deque<T, A> > ;
  106. /** Set allocated with a frame allocator. */
  107. template <typename T, typename P = std::less<T>, typename A = StdAlloc<T, FrameAlloc>>
  108. using FrameSet = std::set < T, P, A > ;
  109. /** Map allocated with a frame allocator. */
  110. template <typename K, typename V, typename P = std::less<K>, typename A = StdAlloc<std::pair<const K, V>, FrameAlloc>>
  111. using FrameMap = std::map < K, V, P, A >;
  112. /** UnorderedSet allocated with a frame allocator. */
  113. template <typename T, typename H = std::hash<T>, typename C = std::equal_to<T>, typename A = StdAlloc<T, FrameAlloc>>
  114. using FrameUnorderedSet = std::unordered_set < T, H, C, A >;
  115. /** UnorderedMap allocated with a frame allocator. */
  116. template <typename K, typename V, typename H = std::hash<K>, typename C = std::equal_to<K>, typename A = StdAlloc<std::pair<const K, V>, FrameAlloc>>
  117. using FrameUnorderedMap = std::unordered_map < K, V, H, C, A >;
  118. /** @cond INTERNAL */
  119. extern BS_THREADLOCAL FrameAlloc* _GlobalFrameAlloc;
  120. /**
  121. * Specialized memory allocator implementations that allows use of a global frame allocator in normal
  122. * new/delete/free/dealloc operators.
  123. */
  124. template<>
  125. class MemoryAllocator<FrameAlloc> : public MemoryAllocatorBase
  126. {
  127. public:
  128. static void* allocate(size_t bytes)
  129. {
  130. return bs_frame_alloc((UINT32)bytes);
  131. }
  132. static void* allocateArray(size_t bytes, UINT32 count)
  133. {
  134. return bs_frame_alloc((UINT32)(bytes * count));
  135. }
  136. static void free(void* ptr)
  137. {
  138. bs_frame_free(ptr);
  139. }
  140. static void freeArray(void* ptr, UINT32 count)
  141. {
  142. bs_frame_free(ptr);
  143. }
  144. };
  145. /** @endcond */
  146. /** @} */
  147. }