AkMemoryMgr.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. /// \file
  21. /// Memory Manager namespace.
  22. #ifndef _AKMEMORYMGR_H_
  23. #define _AKMEMORYMGR_H_
  24. #if !defined( AK_OPTIMIZED ) && !( defined AK_DISABLE_MEMDEBUG )
  25. #ifndef AK_MEMDEBUG
  26. #define AK_MEMDEBUG
  27. #endif
  28. #endif
  29. #include <AK/SoundEngine/Common/AkTypes.h>
  30. #include <AK/SoundEngine/Common/AkSoundEngineExport.h>
  31. struct AkMemSettings;
  32. /// Memory category IDs.
  33. enum AkMemID
  34. {
  35. AkMemID_Object, ///< Generic placeholder for allocations tied to the Wwise project.
  36. AkMemID_Event, ///< Events from the Wwise project.
  37. AkMemID_Structure, ///< Structures from the Wwise project.
  38. AkMemID_Media, ///< Media from the Wwise project.
  39. AkMemID_GameObject, ///< Game Objects and related.
  40. AkMemID_Processing, ///< Anything tied to instancing and processing of the DSP graph.
  41. AkMemID_ProcessingPlugin, ///< Plug-in allocations related to the DSP graph.
  42. AkMemID_Streaming, ///< Streaming Manager objects.
  43. AkMemID_StreamingIO, ///< Streaming Manager I/O memory.
  44. AkMemID_SpatialAudio, ///< Spatial audio.
  45. AkMemID_SpatialAudioGeometry, ///< Spatial audio geometry data.
  46. AkMemID_SpatialAudioPaths, ///< Spatial audio paths data.
  47. AkMemID_GameSim, ///< Game Simulator allocations.
  48. AkMemID_MonitorQueue, ///< Monitor Queue.
  49. AkMemID_Profiler, ///< Profiler.
  50. AkMemID_FilePackage, ///< File packager.
  51. AkMemID_SoundEngine, ///< Base sound engine allocations (managers, etc).
  52. AkMemID_Integration, ///< Game engine integration allocations.
  53. AkMemID_JobMgr, ///< Allocations for Sound Engine jobs and job dependencies.
  54. AkMemID_NUM, ///< Category count.
  55. AkMemID_MASK = 0x1FFFFFFF, ///< Mask for category IDs.
  56. AkMemType_Media = 0x20000000, ///< Media memory type bit.
  57. AkMemType_Device = 0x40000000, ///< Device memory type bit.
  58. AkMemType_NoTrack = 0x80000000 ///< Do not track this allocation.
  59. };
  60. namespace AK
  61. {
  62. /// Memory Manager namespace.
  63. /// \remarks The functions in this namespace are thread-safe, unless stated otherwise.
  64. /// \sa
  65. /// - \ref memorymanager
  66. namespace MemoryMgr
  67. {
  68. /// Memory category statistics.
  69. /// \remarks These statistics are not collected in the Release configuration of
  70. /// the default memory manager implementation.
  71. /// \sa
  72. /// - AK::MemoryMgr::GetCategoryStats()
  73. /// - \ref memorymanager
  74. struct CategoryStats
  75. {
  76. // Current state
  77. AkUInt64 uUsed; ///< Used memory (in bytes)
  78. // Statistics
  79. AkUInt64 uPeakUsed; ///< Peak used memory (in bytes)
  80. AkUInt32 uAllocs; ///< Number of allocation calls since initialization
  81. AkUInt32 uFrees; ///< Number of free calls since initialization
  82. };
  83. /// Memory global statistics.
  84. /// \remarks These statistics are not collected in the Release configuration of
  85. /// the default memory manager implementation.
  86. /// \sa
  87. /// - AK::MemoryMgr::GetGlobalStats()
  88. /// - \ref memorymanager
  89. struct GlobalStats
  90. {
  91. AkUInt64 uUsed; ///< Total memory used including all categories (in bytes)
  92. AkUInt64 uDeviceUsed; ///< Total device memory used including all categories (in bytes)
  93. AkUInt64 uReserved; ///< Total reserved memory. (Used and unused). Will return 0 if the reserved memory is not traceable.
  94. AkUInt64 uMax; ///< Maximum total allocation size, specified in the initialization settings through uMemAllocationSizeLimit. Will be 0 if no limit was set.
  95. };
  96. ////////////////////////////////////////////////////////////////////////
  97. /// @name Initialization
  98. //@{
  99. /// Query whether the Memory Manager has been sucessfully initialized.
  100. /// \warning This function is not thread-safe. It should not be called at the same time as MemoryMgr::Init or MemoryMgr::Term.
  101. /// \return True if the Memory Manager is initialized, False otherwise
  102. /// \sa
  103. /// - AK::MemoryMgr::Init()
  104. /// - \ref memorymanager
  105. AK_EXTERNAPIFUNC( bool, IsInitialized )();
  106. /// Terminate the Memory Manager.
  107. /// \warning This function is not thread-safe. It is not valid to allocate memory or otherwise interact with the memory manager during or after this call.
  108. /// \sa
  109. /// - \ref memorymanager
  110. AK_EXTERNAPIFUNC( void, Term )();
  111. /// Performs whatever steps are required to initialize a thread for use with the memory manager.
  112. /// For example initializing thread local storage that the allocator requires to work.
  113. /// The default implementation of the memory manager performs thread initialization automatically and therefore this call is optional.
  114. /// For implementations where the cost of automatically initializing a thread for use with an allocator would be prohibitively expensive
  115. /// this call allows you to perform the initialization once during, for example, thread creation.
  116. /// \sa
  117. /// - AkMemInitForThread
  118. AK_EXTERNAPIFUNC( void, InitForThread )();
  119. /// Allows you to manually terminate a thread for use with the memory manager.
  120. /// The default implementation of the memory manager requires that all threads that interact with the memory manager call this function prior
  121. /// to either their termination or the termination of the memory manager. Threads not created by the sound engine itself will not have this
  122. /// function called for them automatically.
  123. /// Take care to call this function for any thread, not owned by wwise, that may have interacted with the memory manager. For example job system workers.
  124. /// \sa
  125. /// - AkMemTermForThread
  126. AK_EXTERNAPIFUNC( void, TermForThread )();
  127. /// Allows you to "trim" a thread being used with the memory manager.
  128. /// This is a function that will be called periodically by some Wwise-owned threads,
  129. /// so that any thread-local state can be cleaned up in order to return memory for other systems to use.
  130. /// For example, this can be used to return thread-local heaps to global stores or to finalize other deferred operations.
  131. /// This function is only required for optimization purposes and does not have to be defined.
  132. /// Therefore, unlike TermForThread, this is not expected to be called in all scenarios by Wwise.
  133. /// It is also recommended to be called by game engine integrations in any worker threads that run Wwise jobs.
  134. /// Refer to \ref eventmgrthread_jobmgr_best_practices for more information.
  135. /// \sa
  136. /// - AkMemTrimForThread
  137. AK_EXTERNAPIFUNC( void, TrimForThread )();
  138. //@}
  139. ////////////////////////////////////////////////////////////////////////
  140. /// @name Memory Allocation
  141. //@{
  142. #ifdef AK_MEMDEBUG
  143. /// Allocate memory: debug version.
  144. /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
  145. /// \sa
  146. /// - \ref memorymanager
  147. AK_EXTERNAPIFUNC( void *, dMalloc )(
  148. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  149. size_t in_uSize, ///< Number of bytes to allocate
  150. const char *in_pszFile, ///< Debug file name
  151. AkUInt32 in_uLine ///< Debug line number
  152. );
  153. #endif // AK_MEMDEBUG
  154. /// Allocate memory.
  155. /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
  156. /// \sa
  157. /// - \ref memorymanager
  158. AK_EXTERNAPIFUNC( void *, Malloc )(
  159. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  160. size_t in_uSize ///< Number of bytes to allocate
  161. );
  162. #ifdef AK_MEMDEBUG
  163. /// Reallocate memory: debug version.
  164. /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
  165. /// \sa
  166. /// - \ref memorymanager
  167. AK_EXTERNAPIFUNC( void*, dRealloc )(
  168. AkMemPoolId in_poolId,
  169. void *in_pAlloc,
  170. size_t in_uSize,
  171. const char *in_pszFile,
  172. AkUInt32 in_uLine
  173. );
  174. #endif // AK_MEMDEBUG
  175. /// Reallocate memory.
  176. /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
  177. /// \sa
  178. /// - \ref memorymanager
  179. AK_EXTERNAPIFUNC( void *, Realloc )(
  180. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  181. void * in_pAlloc, ///< Pointer to the start of the allocated memory
  182. size_t in_uSize ///< Number of bytes to allocate
  183. );
  184. #ifdef AK_MEMDEBUG
  185. /// Reallocate memory: debug version.
  186. /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
  187. /// \sa
  188. /// - \ref memorymanager
  189. AK_EXTERNAPIFUNC( void*, dReallocAligned )(
  190. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  191. void *in_pAlloc, ///< Pointer to the start of the allocated memory
  192. size_t in_uSize, ///< Number of bytes to allocate
  193. AkUInt32 in_uAlignment, ///< Alignment (in bytes)
  194. const char *in_pszFile, ///< Debug file name
  195. AkUInt32 in_uLine ///< Debug line number
  196. );
  197. #endif // AK_MEMDEBUG
  198. /// Reallocate memory.
  199. /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
  200. /// \sa
  201. /// - \ref memorymanager
  202. AK_EXTERNAPIFUNC( void *, ReallocAligned )(
  203. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  204. void * in_pAlloc, ///< Pointer to the start of the allocated memory
  205. size_t in_uSize, ///< Number of bytes to allocate
  206. AkUInt32 in_uAlignment ///< Alignment (in bytes)
  207. );
  208. /// Free memory allocated with the memory manager.
  209. /// \sa
  210. /// - \ref memorymanager
  211. AK_EXTERNAPIFUNC( void, Free )(
  212. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  213. void * in_pMemAddress ///< Pointer to the start of memory
  214. );
  215. #ifdef AK_MEMDEBUG
  216. /// Allocate memory with a specific alignment. debug version.
  217. /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
  218. /// \sa
  219. /// - \ref memorymanager
  220. AK_EXTERNAPIFUNC( void *, dMalign )(
  221. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  222. size_t in_uSize, ///< Number of bytes to allocate
  223. AkUInt32 in_uAlignment, ///< Alignment (in bytes)
  224. const char* in_pszFile, ///< Debug file name
  225. AkUInt32 in_uLine ///< Debug line number
  226. );
  227. #endif // AK_MEMDEBUG
  228. /// Allocate memory with a specific alignment.
  229. /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
  230. /// \sa
  231. /// - \ref memorymanager
  232. AK_EXTERNAPIFUNC( void *, Malign )(
  233. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  234. size_t in_uSize, ///< Number of bytes to allocate
  235. AkUInt32 in_uAlignment ///< Alignment (in bytes)
  236. );
  237. //@}
  238. ////////////////////////////////////////////////////////////////////////
  239. /// @name Memory Profiling
  240. //@{
  241. /// Get statistics for a given memory category.
  242. /// \note Be aware of the potentially incoherent nature of reporting such information during concurrent modification by multiple threads.
  243. AK_EXTERNAPIFUNC( void, GetCategoryStats )(
  244. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  245. CategoryStats& out_poolStats ///< Returned statistics.
  246. );
  247. /// Get statistics for overall memory manager usage.
  248. /// \note Be aware of the potentially incoherent nature of reporting such information during concurrent modification by multiple threads.
  249. AK_EXTERNAPIFUNC( void, GetGlobalStats )(
  250. GlobalStats& out_stats ///< Returned statistics.
  251. );
  252. /// Called to start profiling memory usage for one thread (the calling thread).
  253. /// \note Not implementing this will result in the Soundbank tab of the Wwise Profiler to show 0 bytes for memory usage.
  254. AK_EXTERNAPIFUNC( void, StartProfileThreadUsage )(
  255. );
  256. /// Called to stop profiling memory usage for the current thread.
  257. /// \return The amount of memory allocated by this thread since StartProfileThreadUsage was called.
  258. /// \note Not implementing this will result in the Soundbank tab of the Wwise Profiler to show 0 bytes for memory usage.
  259. AK_EXTERNAPIFUNC( AkUInt64, StopProfileThreadUsage )(
  260. );
  261. /// Dumps the currently tracked allocations to a file
  262. /// \note AkMemSettings::uMemoryDebugLevel must be enabled and the build must define AK_MEMDEBUG for this to work
  263. AK_EXTERNAPIFUNC( void, DumpToFile )(
  264. const AkOSChar* pszFilename ///< Filename.
  265. );
  266. //@}
  267. }
  268. }
  269. #endif // _AKMEMORYMGR_H_