AkModule.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. /// Audiokinetic's definitions and factory of overridable Memory Manager module.
  22. #pragma once
  23. #include <AK/SoundEngine/Common/AkTypes.h>
  24. #include <AK/SoundEngine/Common/AkMemoryMgr.h>
  25. /// \name Audiokinetic Memory Manager's implementation-specific definitions.
  26. //@{
  27. typedef void ( AKSOUNDENGINE_CALL *AkMemInitForThread ) (
  28. );
  29. typedef void ( AKSOUNDENGINE_CALL *AkMemTermForThread ) (
  30. );
  31. typedef void ( AKSOUNDENGINE_CALL *AkMemTrimForThread ) (
  32. );
  33. typedef void* ( AKSOUNDENGINE_CALL *AkMemMalloc ) (
  34. AkMemPoolId poolId,
  35. size_t uSize
  36. );
  37. typedef void* ( AKSOUNDENGINE_CALL *AkMemMalign ) (
  38. AkMemPoolId poolId,
  39. size_t uSize,
  40. AkUInt32 uAlignment
  41. );
  42. typedef void* ( AKSOUNDENGINE_CALL *AkMemRealloc ) (
  43. AkMemPoolId poolId,
  44. void* pAddress,
  45. size_t uSize
  46. );
  47. typedef void* ( AKSOUNDENGINE_CALL *AkMemReallocAligned ) (
  48. AkMemPoolId poolId,
  49. void* pAddress,
  50. size_t uSize,
  51. AkUInt32 uAlignment
  52. );
  53. typedef void ( AKSOUNDENGINE_CALL *AkMemFree ) (
  54. AkMemPoolId poolId,
  55. void* pAddress
  56. );
  57. typedef size_t ( AKSOUNDENGINE_CALL *AkMemTotalReservedMemorySize ) (
  58. );
  59. typedef size_t ( AKSOUNDENGINE_CALL *AkMemSizeOfMemory ) (
  60. AkMemPoolId poolId,
  61. void* pAddress
  62. );
  63. typedef void ( AKSOUNDENGINE_CALL *AkMemDebugMalloc ) (
  64. AkMemPoolId poolId,
  65. size_t uSize,
  66. void* pAddress,
  67. char const* pszFile,
  68. AkUInt32 uLine
  69. );
  70. typedef void ( AKSOUNDENGINE_CALL *AkMemDebugMalign ) (
  71. AkMemPoolId poolId,
  72. size_t uSize,
  73. AkUInt32 uAlignment,
  74. void* pAddress,
  75. char const* pszFile,
  76. AkUInt32 uLine
  77. );
  78. typedef void ( AKSOUNDENGINE_CALL *AkMemDebugRealloc ) (
  79. AkMemPoolId poolId,
  80. void* pOldAddress,
  81. size_t uSize,
  82. void* pNewAddress,
  83. char const* pszFile,
  84. AkUInt32 uLine
  85. );
  86. typedef void ( AKSOUNDENGINE_CALL *AkMemDebugReallocAligned ) (
  87. AkMemPoolId poolId,
  88. void* pOldAddress,
  89. size_t uSize,
  90. AkUInt32 uAlignment,
  91. void* pNewAddress,
  92. char const* pszFile,
  93. AkUInt32 uLine
  94. );
  95. typedef void ( AKSOUNDENGINE_CALL *AkMemDebugFree ) (
  96. AkMemPoolId poolId,
  97. void* pAddress
  98. );
  99. typedef void* ( *AkMemAllocVM ) (
  100. size_t size,
  101. size_t* extra
  102. );
  103. typedef void ( *AkMemFreeVM ) (
  104. void* address,
  105. size_t size,
  106. size_t extra,
  107. size_t release
  108. );
  109. /// Initialization settings for the default implementation of the Memory Manager. For more details, see \ref memorymanager_init.
  110. /// \sa AK::MemoryMgr
  111. struct AkMemSettings
  112. {
  113. /// @name High-level memory allocation hooks. When not NULL, redirect allocations normally forwarded to rpmalloc.
  114. //@{
  115. AkMemInitForThread pfInitForThread; ///< (Optional) Thread-specific allocator initialization hook.
  116. AkMemTermForThread pfTermForThread; ///< (Optional) Thread-specific allocator termination hook.
  117. AkMemTrimForThread pfTrimForThread; ///< (Optional) Thread-specific allocator "trimming" hook. Used to relinquish memory resources when threads enter a period of inactivity.
  118. AkMemMalloc pfMalloc; ///< (Optional) Memory allocation hook.
  119. AkMemMalign pfMalign; ///< (Optional) Memory allocation hook.
  120. AkMemRealloc pfRealloc; ///< (Optional) Memory allocation hook.
  121. AkMemReallocAligned pfReallocAligned; ///< (Optional) Memory allocation hook.
  122. AkMemFree pfFree; ///< (Optional) Memory allocation hook.
  123. AkMemTotalReservedMemorySize pfTotalReservedMemorySize; ///< (Optional) Memory allocation statistics hook.
  124. AkMemSizeOfMemory pfSizeOfMemory; ///< (Optional) Memory allocation statistics hook.
  125. //@}
  126. /// @name Configuration.
  127. //@{
  128. AkUInt64 uMemAllocationSizeLimit; ///< When non-zero, limits the total amount of virtual and device memory allocated by AK::MemoryMgr.
  129. bool bEnableSeparateDeviceHeap; ///< Enable use of device memory heap for all allocations (on applicable platforms).
  130. AK::TempAlloc::InitSettings tempAllocSettings[AK::TempAlloc::Type_NUM]; ///< Configuration of behavior for the temporary-memory pools.
  131. //@}
  132. /// @name Page allocation hooks, used by rpmalloc. Default to AKPLATFORM::AllocVM et al.
  133. //@{
  134. AkMemAllocVM pfAllocVM; ///< Virtual page allocation hook.
  135. AkMemFreeVM pfFreeVM; ///< Virtual page allocation hook.
  136. AkMemAllocVM pfAllocDevice; ///< Device page allocation hook.
  137. AkMemFreeVM pfFreeDevice; ///< Device page allocation hook.
  138. AkUInt32 uVMPageSize; ///< Virtual memory page size. Defaults to 0 which means auto-detect.
  139. AkUInt32 uDevicePageSize; ///< Device memory page size. Defaults to 0 which means auto-detect.
  140. AkUInt32 uMaxThreadLocalHeapAllocSize; ///< All memory allocations of sizes larger than this value will go to a global heap shared across all threads. Defaults to 0 which means all allocations go to a global heap.
  141. //@}
  142. /// @name Memory allocation debugging.
  143. //@{
  144. AkMemDebugMalloc pfDebugMalloc; ///< (Optional) Memory allocation debugging hook. Used for tracking calls to pfMalloc.
  145. AkMemDebugMalign pfDebugMalign; ///< (Optional) Memory allocation debugging hook. Used for tracking calls to pfMalign.
  146. AkMemDebugRealloc pfDebugRealloc; ///< (Optional) Memory allocation debugging hook. Used for tracking calls to pfRealloc.
  147. AkMemDebugReallocAligned pfDebugReallocAligned; ///< (Optional) Memory allocation debugging hook. Used for tracking calls to pfReallocAligned.
  148. AkMemDebugFree pfDebugFree; ///< (Optional) Memory allocation debugging hook. Used for tracking calls to pfFree.
  149. AkUInt32 uMemoryDebugLevel; ///< Default 0 disabled. 1 debug enabled. 2 stomp allocator enabled. 3 stomp allocator and debug enabled. User implementations may use multiple non-zero values to offer different features.
  150. //@}
  151. };
  152. //@}
  153. namespace AK
  154. {
  155. namespace MemoryMgr
  156. {
  157. /// Initialize the default implementation of the Memory Manager.
  158. /// \sa AK::MemoryMgr
  159. AK_EXTERNAPIFUNC( AKRESULT, Init ) (
  160. AkMemSettings * in_pSettings ///< Memory manager initialization settings.
  161. );
  162. /// Obtain the default initialization settings for the default implementation of the Memory Manager.
  163. AK_EXTERNAPIFUNC( void, GetDefaultSettings ) (
  164. AkMemSettings & out_pMemSettings ///< Memory manager default initialization settings.
  165. );
  166. }
  167. }
  168. #if defined AK_WIN || defined AK_LINUX || defined AK_SONY || defined AK_XBOX || defined AK_APPLE || defined AK_EMSCRIPTEN
  169. #define AK_CRT_ALLOCATOR_SUPPORTED
  170. #endif
  171. #ifdef AK_CRT_ALLOCATOR_SUPPORTED
  172. namespace AK
  173. {
  174. namespace MemoryMgr
  175. {
  176. /// @name C runtime allocator functions for the default implementation of the Memory Manager.
  177. //@{
  178. AK_EXTERNAPIFUNC( void, AkCrtAllocatorInitForThread )();
  179. AK_EXTERNAPIFUNC( void, AkCrtAllocatorTermForThread )();
  180. AK_EXTERNAPIFUNC( void, AkCrtAllocatorTrimForThread )();
  181. AK_EXTERNAPIFUNC( void*, AkCrtAllocatorMalloc )(
  182. AkMemPoolId poolId,
  183. size_t uSize
  184. );
  185. AK_EXTERNAPIFUNC( void*, AkCrtAllocatorMalign )(
  186. AkMemPoolId poolId,
  187. size_t uSize,
  188. AkUInt32 uAlignment
  189. );
  190. AK_EXTERNAPIFUNC( void*, AkCrtAllocatorRealloc )(
  191. AkMemPoolId poolId,
  192. void* pAddress,
  193. size_t uSize
  194. );
  195. AK_EXTERNAPIFUNC( void*, AkCrtAllocatorReallocAligned )(
  196. AkMemPoolId poolId,
  197. void* pAddress,
  198. size_t uSize,
  199. AkUInt32 uAlignment
  200. );
  201. AK_EXTERNAPIFUNC( void, AkCrtAllocatorFree )(
  202. AkMemPoolId poolId,
  203. void* pAddress
  204. );
  205. AK_EXTERNAPIFUNC( size_t, AkCrtAllocatorTotalReservedMemorySize )();
  206. AK_EXTERNAPIFUNC( size_t, AkCrtAllocatorSizeOfMemory )(
  207. AkMemPoolId poolID,
  208. void* pAddress
  209. );
  210. //@}
  211. }
  212. }
  213. #endif // AK_CRT_ALLOCATOR_SUPPORTED
  214. #if !defined AK_EMSCRIPTEN
  215. #define AK_STOMP_ALLOCATOR_SUPPORTED
  216. #endif
  217. #ifdef AK_STOMP_ALLOCATOR_SUPPORTED
  218. namespace AK
  219. {
  220. namespace MemoryMgr
  221. {
  222. /// @name Stomp allocator functions for the default implementation of the Memory Manager.
  223. //@{
  224. AK_EXTERNAPIFUNC( void, AkStompAllocatorInitForThread )();
  225. AK_EXTERNAPIFUNC( void, AkStompAllocatorTermForThread )();
  226. AK_EXTERNAPIFUNC( void, AkStompAllocatorTrimForThread )();
  227. AK_EXTERNAPIFUNC( void*, AkStompAllocatorMalloc )(
  228. AkMemPoolId poolId,
  229. size_t uSize
  230. );
  231. AK_EXTERNAPIFUNC( void*, AkStompAllocatorMalign )(
  232. AkMemPoolId poolId,
  233. size_t uSize,
  234. AkUInt32 uAlignment
  235. );
  236. AK_EXTERNAPIFUNC( void*, AkStompAllocatorRealloc )(
  237. AkMemPoolId poolId,
  238. void* pAddress,
  239. size_t uSize
  240. );
  241. AK_EXTERNAPIFUNC( void*, AkStompAllocatorReallocAligned )(
  242. AkMemPoolId poolId,
  243. void* pAddress,
  244. size_t uSize,
  245. AkUInt32 uAlignment
  246. );
  247. AK_EXTERNAPIFUNC( void, AkStompAllocatorFree )(
  248. AkMemPoolId poolId,
  249. void* pAddress
  250. );
  251. AK_EXTERNAPIFUNC( size_t, AkStompAllocatorTotalReservedMemorySize )();
  252. AK_EXTERNAPIFUNC( size_t, AkStompAllocatorSizeOfMemory )(
  253. AkMemPoolId poolID,
  254. void* pAddress
  255. );
  256. //@}
  257. }
  258. }
  259. #endif // AK_STOMP_ALLOCATOR_SUPPORTED
  260. namespace AK
  261. {
  262. namespace MemoryMgr
  263. {
  264. /// @name Debug tool hooks for the default implementation of the Memory Manager.
  265. //@{
  266. AK_EXTERNAPIFUNC( AKRESULT, AkMemDebugToolInit )();
  267. AK_EXTERNAPIFUNC( void, AkMemDebugToolTerm )();
  268. AK_EXTERNAPIFUNC( void, AkMemDebugToolMalloc ) (
  269. AkMemPoolId poolId,
  270. size_t uSize,
  271. void* pAddress,
  272. char const* pszFile,
  273. AkUInt32 uLine
  274. );
  275. AK_EXTERNAPIFUNC( void, AkMemDebugToolMalign ) (
  276. AkMemPoolId poolId,
  277. size_t uSize,
  278. AkUInt32 uAlignment,
  279. void* pAddress,
  280. char const* pszFile,
  281. AkUInt32 uLine
  282. );
  283. AK_EXTERNAPIFUNC( void, AkMemDebugToolRealloc ) (
  284. AkMemPoolId poolId,
  285. void* pOldAddress,
  286. size_t uSize,
  287. void* pNewAddress,
  288. char const* pszFile,
  289. AkUInt32 uLine
  290. );
  291. AK_EXTERNAPIFUNC( void, AkMemDebugToolReallocAligned ) (
  292. AkMemPoolId poolId,
  293. void* pOldAddress,
  294. size_t uSize,
  295. AkUInt32 uAlignment,
  296. void* pNewAddress,
  297. char const* pszFile,
  298. AkUInt32 uLine
  299. );
  300. AK_EXTERNAPIFUNC( void, AkMemDebugToolFree ) (
  301. AkMemPoolId poolId,
  302. void* pAddress
  303. );
  304. //@}
  305. }
  306. }