AkDynaBlkPool.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. #ifndef _AKBLOCKPOOL_H
  21. #define _AKBLOCKPOOL_H
  22. #include <AK/Tools/Common/AkObject.h>
  23. #include <AK/Tools/Common/AkAssert.h>
  24. #include <AK/Tools/Common/AkListBareLight.h>
  25. //
  26. // AkDynaBlkPool - A dynamic block pool allocator which will grow (and shrink) in size in contiguous chunks of 'uPoolChunkSize' objects.
  27. // - Fragmentation in the pool will prevent it from shrinking, but in many use cases this is acceptable.
  28. //
  29. #ifdef _DEBUG
  30. //#define AK_DYNA_BLK_STATS
  31. #define AK_DYNA_BLK_SCRUB_MEM
  32. #endif
  33. #ifdef AK_DYNA_BLK_STATS
  34. #define STATS_ALLOC() Stats_Alloc()
  35. #define STATS_NEWCHUNK() Stats_NewChunk()
  36. #define STATS_FREE() Stats_Free()
  37. #define STATS_DELCHUNK() Stats_DelChunk()
  38. #else
  39. #define STATS_ALLOC()
  40. #define STATS_NEWCHUNK()
  41. #define STATS_FREE()
  42. #define STATS_DELCHUNK()
  43. #endif
  44. #ifdef AK_DYNA_BLK_SCRUB_MEM
  45. #define SCRUB_NEW_CHUNK() memset(&memory, 0xCC, sizeof(T)*uPoolChunkSize)
  46. #define SCRUB_NEW_ALLOC(pItem) memset(pItem, 0xAA, sizeof(T))
  47. #define SCRUB_FREE_BLOCK(pObj) memset(pObj, 0xDD, sizeof(T))
  48. #else
  49. #define SCRUB_NEW_CHUNK()
  50. #define SCRUB_NEW_ALLOC(pItem)
  51. #define SCRUB_FREE_BLOCK(pObj)
  52. #endif
  53. template < typename T, AkUInt32 uPoolChunkSize, class TAlloc = ArrayPoolDefault>
  54. class AkDynaBlkPool: public TAlloc
  55. {
  56. enum { kChunkMemoryBytes = sizeof(T)*uPoolChunkSize };
  57. struct FreeBlock
  58. {
  59. FreeBlock* pNextItem;
  60. char padding[ sizeof(T) - sizeof(FreeBlock*) ];
  61. };
  62. typedef AkListBare< FreeBlock, AkListBareNextItem, AkCountPolicyWithCount, AkLastPolicyNoLast > tFreeList;
  63. struct PoolChunk
  64. {
  65. PoolChunk() : pNextLightItem(NULL)
  66. {
  67. SCRUB_NEW_CHUNK();
  68. for( AkUInt32 i=0; i<uPoolChunkSize; ++i )
  69. {
  70. FreeBlock* pBlk = reinterpret_cast<FreeBlock*>( &memory ) + i;
  71. freeList.AddFirst(pBlk);
  72. }
  73. }
  74. inline bool BelongsTo( FreeBlock* pMem ) const { return (AkUInt8*)pMem >= memory && (AkUInt8*)pMem < (memory+kChunkMemoryBytes); }
  75. inline bool AllFree() const { return freeList.Length() == uPoolChunkSize; }
  76. inline bool AllAllocd() const { return freeList.IsEmpty(); }
  77. AkUInt8 memory[ kChunkMemoryBytes ];
  78. PoolChunk* pNextLightItem;
  79. tFreeList freeList;
  80. };
  81. typedef AkListBareLight< PoolChunk > tChunkList;
  82. public:
  83. T* New()
  84. {
  85. T* ptr = Alloc();
  86. if (ptr) AkPlacementNew(ptr) T;
  87. return ptr;
  88. }
  89. template<typename A1>
  90. T* New(A1 a1)
  91. {
  92. T* ptr = Alloc();
  93. if (ptr) AkPlacementNew(ptr) T(a1);
  94. return ptr;
  95. }
  96. template<typename A1, typename A2>
  97. T* New(A1 a1, A2 a2)
  98. {
  99. T* ptr = Alloc();
  100. if (ptr) AkPlacementNew(ptr) T(a1, a2);
  101. return ptr;
  102. }
  103. template<typename A1, typename A2, typename A3>
  104. T* New(A1 a1, A2 a2, A3 a3)
  105. {
  106. T* ptr = Alloc();
  107. if (ptr) AkPlacementNew(ptr) T(a1, a2, a3);
  108. return ptr;
  109. }
  110. template<typename A1, typename A2, typename A3, typename A4>
  111. T* New(A1 a1, A2 a2, A3 a3, A4 a4)
  112. {
  113. T* ptr = Alloc();
  114. if (ptr) AkPlacementNew(ptr) T(a1,a2,a3,a4);
  115. return ptr;
  116. }
  117. void Delete(T* ptr)
  118. {
  119. ptr->~T();
  120. Free(ptr);
  121. }
  122. void Transfer(AkDynaBlkPool<T, uPoolChunkSize, TAlloc>& in_src)
  123. {
  124. m_chunkList.Transfer(in_src.m_chunkList);
  125. #ifdef AK_DYNA_BLK_STATS
  126. uPeakUsedBytes = in_src.uPeakUsedBytes;
  127. uPeakAllocdBytes = in_src.uPeakAllocdBytes;
  128. uCurrentAllocdBytes = in_src.uCurrentAllocdBytes;
  129. uCurrentUsedBytes = in_src.uCurrentUsedBytes;
  130. in_src.uPeakUsedBytes = 0;
  131. in_src.uPeakAllocdBytes = 0;
  132. in_src.uCurrentAllocdBytes = 0;
  133. in_src.uCurrentUsedBytes = 0;
  134. #endif
  135. }
  136. private:
  137. T* Alloc()
  138. {
  139. FreeBlock* pItem = NULL;
  140. PoolChunk* pChunk = NULL;
  141. pChunk = m_chunkList.First();
  142. while (pChunk != NULL && pChunk->AllAllocd())
  143. pChunk = pChunk->pNextLightItem;
  144. if (pChunk == NULL)
  145. {
  146. pChunk = (PoolChunk *) TAlloc::Alloc( sizeof( PoolChunk ) );
  147. AkPlacementNew(pChunk) PoolChunk();
  148. STATS_NEWCHUNK();
  149. if (pChunk != NULL)
  150. m_chunkList.AddFirst(pChunk);
  151. }
  152. if (pChunk != NULL)
  153. {
  154. pItem = pChunk->freeList.First();
  155. AKASSERT(pItem != NULL);
  156. pChunk->freeList.RemoveFirst();
  157. SCRUB_NEW_ALLOC(pItem);
  158. STATS_ALLOC();
  159. }
  160. return reinterpret_cast<T*>(pItem);
  161. }
  162. void Free( T* pObj )
  163. {
  164. SCRUB_FREE_BLOCK((void*)pObj);
  165. FreeBlock* pItem = reinterpret_cast<FreeBlock*>(pObj);
  166. PoolChunk* pPrevChunk = NULL;
  167. PoolChunk* pChunk = m_chunkList.First();
  168. while (pChunk != NULL && !pChunk->BelongsTo(pItem))
  169. {
  170. pPrevChunk = pChunk;
  171. pChunk = pChunk->pNextLightItem;
  172. }
  173. AKASSERT(pChunk != NULL);
  174. pChunk->freeList.AddFirst(pItem);
  175. STATS_FREE();
  176. if (pChunk->AllFree())
  177. {
  178. m_chunkList.RemoveItem(pChunk, pPrevChunk);
  179. pChunk->~PoolChunk();
  180. TAlloc::Free( pChunk );
  181. STATS_DELCHUNK();
  182. }
  183. }
  184. tChunkList m_chunkList;
  185. #ifdef AK_DYNA_BLK_STATS
  186. void Stats_Alloc()
  187. {
  188. uCurrentUsedBytes += sizeof(T);
  189. uPeakUsedBytes = AkMax(uCurrentUsedBytes, uPeakUsedBytes);
  190. }
  191. void Stats_NewChunk()
  192. {
  193. uCurrentAllocdBytes += sizeof(PoolChunk);
  194. uPeakAllocdBytes = AkMax(uCurrentAllocdBytes, uPeakAllocdBytes);
  195. }
  196. void Stats_Free()
  197. {
  198. uCurrentUsedBytes -= sizeof(T);
  199. }
  200. void Stats_DelChunk()
  201. {
  202. uCurrentAllocdBytes -= sizeof(PoolChunk);
  203. }
  204. public:
  205. AkUInt32 uPeakUsedBytes;
  206. AkUInt32 uPeakAllocdBytes;
  207. AkUInt32 uCurrentAllocdBytes;
  208. AkUInt32 uCurrentUsedBytes;
  209. #endif
  210. };
  211. #endif