BsGlobalFrameAlloc.h 5.1 KB

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