BsGlobalFrameAlloc.h 4.6 KB

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