WwiseMemoryMgrAPI.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. #pragma once
  16. #include "CoreMinimal.h"
  17. #include "AkInclude.h"
  18. #include "Wwise/WwiseSoundEngineModule.h"
  19. class IWwiseMemoryMgrAPI
  20. {
  21. public:
  22. static IWwiseMemoryMgrAPI* Get()
  23. {
  24. IWwiseSoundEngineModule::ForceLoadModule();
  25. return IWwiseSoundEngineModule::MemoryMgr;
  26. }
  27. UE_NONCOPYABLE(IWwiseMemoryMgrAPI);
  28. protected:
  29. inline IWwiseMemoryMgrAPI() = default;
  30. public:
  31. virtual ~IWwiseMemoryMgrAPI() {}
  32. /// Initialize the default implementation of the Memory Manager.
  33. /// \sa AK::MemoryMgr
  34. virtual AKRESULT Init(
  35. AkMemSettings* in_pSettings ///< Memory manager initialization settings.
  36. ) = 0;
  37. /// Obtain the default initialization settings for the default implementation of the Memory Manager.
  38. virtual void GetDefaultSettings(
  39. AkMemSettings& out_pMemSettings ///< Memory manager default initialization settings.
  40. ) = 0;
  41. ////////////////////////////////////////////////////////////////////////
  42. /// @name Initialization
  43. //@{
  44. /// Query whether the Memory Manager has been sucessfully initialized.
  45. /// \warning This function is not thread-safe. It should not be called at the same time as MemoryMgr::Init or MemoryMgr::Term.
  46. /// \return True if the Memory Manager is initialized, False otherwise
  47. /// \sa
  48. /// - AK::MemoryMgr::Init()
  49. /// - \ref memorymanager
  50. virtual bool IsInitialized() = 0;
  51. /// Terminate the Memory Manager.
  52. /// \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.
  53. /// \sa
  54. /// - \ref memorymanager
  55. virtual void Term() = 0;
  56. /// Performs whatever steps are required to initialize a thread for use with the memory manager.
  57. /// For example initializing thread local storage that the allocator requires to work.
  58. /// The default implementation of the memory manager performs thread initialization automatically and therefore this call is optional.
  59. /// For implementations where the cost of automatically initializing a thread for use with an allocator would be prohibitively expensive
  60. /// this call allows you to perform the initialization once during, for example, thread creation.
  61. /// \sa
  62. /// - AkMemInitForThread
  63. virtual void InitForThread() = 0;
  64. /// Allows you to manually terminate a thread for use with the memory manager.
  65. /// The default implementation of the memory manager requires that all threads that interact with the memory manager call this function prior
  66. /// to either their termination or the termination of the memory manager. Threads not created by the sound engine itself will not have this
  67. /// function called for them automatically.
  68. /// 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.
  69. /// \sa
  70. /// - AkMemTermForThread
  71. virtual void TermForThread() = 0;
  72. /// Allows you to "trim" a thread being used with the memory manager.
  73. /// This is a function that will be called periodically by some Wwise-owned threads,
  74. /// so that any thread-local state can be cleaned up in order to return memory for other systems to use.
  75. /// For example, this can be used to return thread-local heaps to global stores or to finalize other deferred operations.
  76. /// This function is only required for optimization purposes and does not have to be defined.
  77. /// Therefore, unlike TermForThread, this is not expected to be called in all scenarios by Wwise.
  78. /// It is also recommended to be called by game engine integrations in any worker threads that run Wwise jobs.
  79. /// Refer to \ref eventmgrthread_jobmgr_best_practices for more information.
  80. /// \sa
  81. /// - AkMemTrimForThread
  82. virtual void TrimForThread() = 0;
  83. //@}
  84. ////////////////////////////////////////////////////////////////////////
  85. /// @name Memory Allocation
  86. //@{
  87. /// Allocate memory: debug version.
  88. /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
  89. /// \sa
  90. /// - \ref memorymanager
  91. virtual void* dMalloc(
  92. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  93. size_t in_uSize, ///< Number of bytes to allocate
  94. const char* in_pszFile, ///< Debug file name
  95. AkUInt32 in_uLine ///< Debug line number
  96. ) = 0;
  97. /// Allocate memory.
  98. /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
  99. /// \sa
  100. /// - \ref memorymanager
  101. virtual void* Malloc(
  102. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  103. size_t in_uSize ///< Number of bytes to allocate
  104. ) = 0;
  105. /// Reallocate memory: debug version.
  106. /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
  107. /// \sa
  108. /// - \ref memorymanager
  109. virtual void* dRealloc(
  110. AkMemPoolId in_poolId,
  111. void* in_pAlloc,
  112. size_t in_uSize,
  113. const char* in_pszFile,
  114. AkUInt32 in_uLine
  115. ) = 0;
  116. /// Reallocate memory.
  117. /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
  118. /// \sa
  119. /// - \ref memorymanager
  120. virtual void* Realloc(
  121. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  122. void* in_pAlloc, ///< Pointer to the start of the allocated memory
  123. size_t in_uSize ///< Number of bytes to allocate
  124. ) = 0;
  125. /// Reallocate memory: debug version.
  126. /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
  127. /// \sa
  128. /// - \ref memorymanager
  129. virtual void* dReallocAligned(
  130. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  131. void* in_pAlloc, ///< Pointer to the start of the allocated memory
  132. size_t in_uSize, ///< Number of bytes to allocate
  133. AkUInt32 in_uAlignment, ///< Alignment (in bytes)
  134. const char* in_pszFile, ///< Debug file name
  135. AkUInt32 in_uLine ///< Debug line number
  136. ) = 0;
  137. /// Reallocate memory.
  138. /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
  139. /// \sa
  140. /// - \ref memorymanager
  141. virtual void* ReallocAligned(
  142. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  143. void* in_pAlloc, ///< Pointer to the start of the allocated memory
  144. size_t in_uSize, ///< Number of bytes to allocate
  145. AkUInt32 in_uAlignment ///< Alignment (in bytes)
  146. ) = 0;
  147. /// Free memory allocated with the memory manager.
  148. /// \sa
  149. /// - \ref memorymanager
  150. virtual void Free(
  151. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  152. void* in_pMemAddress ///< Pointer to the start of memory
  153. ) = 0;
  154. /// Allocate memory with a specific alignment. debug version.
  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. virtual void* dMalign(
  159. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  160. size_t in_uSize, ///< Number of bytes to allocate
  161. AkUInt32 in_uAlignment, ///< Alignment (in bytes)
  162. const char* in_pszFile, ///< Debug file name
  163. AkUInt32 in_uLine ///< Debug line number
  164. ) = 0;
  165. /// Allocate memory with a specific alignment.
  166. /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
  167. /// \sa
  168. /// - \ref memorymanager
  169. virtual void* Malign(
  170. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  171. size_t in_uSize, ///< Number of bytes to allocate
  172. AkUInt32 in_uAlignment ///< Alignment (in bytes)
  173. ) = 0;
  174. //@}
  175. ////////////////////////////////////////////////////////////////////////
  176. /// @name Memory Profiling
  177. //@{
  178. /// Get statistics for a given memory category.
  179. /// \note Be aware of the potentially incoherent nature of reporting such information during concurrent modification by multiple threads.
  180. virtual void GetCategoryStats(
  181. AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
  182. AK::MemoryMgr::CategoryStats& out_poolStats ///< Returned statistics.
  183. ) = 0;
  184. /// Get statistics for overall memory manager usage.
  185. /// \note Be aware of the potentially incoherent nature of reporting such information during concurrent modification by multiple threads.
  186. virtual void GetGlobalStats(
  187. AK::MemoryMgr::GlobalStats& out_stats ///< Returned statistics.
  188. ) = 0;
  189. /// Called to start profiling memory usage for one thread (the calling thread).
  190. /// \note Not implementing this will result in the Soundbank tab of the Wwise Profiler to show 0 bytes for memory usage.
  191. virtual void StartProfileThreadUsage(
  192. ) = 0;
  193. /// Called to stop profiling memory usage for the current thread.
  194. /// \return The amount of memory allocated by this thread since StartProfileThreadUsage was called.
  195. /// \note Not implementing this will result in the Soundbank tab of the Wwise Profiler to show 0 bytes for memory usage.
  196. virtual AkUInt64 StopProfileThreadUsage(
  197. ) = 0;
  198. /// Dumps the currently tracked allocations to a file
  199. /// \note AkMemSettings::uMemoryDebugLevel must be enabled and the build must define AK_MEMDEBUG for this to work
  200. virtual void DumpToFile(
  201. const AkOSChar* pszFilename ///< Filename.
  202. ) = 0;
  203. //@}
  204. };