Version
menu_open
link
Wwise SDK 2022.1.12
AkMemoryMgr.h
Go to the documentation of this file.
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 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Copyright (c) 2024 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 /// \file
28 /// Memory Manager namespace.
29 
30 #ifndef _AKMEMORYMGR_H_
31 #define _AKMEMORYMGR_H_
32 
33 #if !defined( AK_OPTIMIZED ) && !( defined AK_DISABLE_MEMDEBUG )
34  #ifndef AK_MEMDEBUG
35  #define AK_MEMDEBUG
36  #endif
37 #endif
38 
41 
42 struct AkMemSettings;
43 
44 /// Memory category IDs.
45 enum AkMemID
46 {
47  AkMemID_Object, ///< Generic placeholder for allocations tied to the Wwise project.
48  AkMemID_Event, ///< Events from the Wwise project.
49  AkMemID_Structure, ///< Structures from the Wwise project.
50  AkMemID_Media, ///< Media from the Wwise project.
51  AkMemID_GameObject, ///< Game Objects and related.
52  AkMemID_Processing, ///< Anything tied to instancing and processing of the DSP graph.
53  AkMemID_ProcessingPlugin, ///< Plug-in allocations related to the DSP graph.
54  AkMemID_Streaming, ///< Streaming Manager objects.
55  AkMemID_StreamingIO, ///< Streaming Manager I/O memory.
56  AkMemID_SpatialAudio, ///< Spatial audio.
57  AkMemID_SpatialAudioGeometry, ///< Spatial audio geometry data.
58  AkMemID_SpatialAudioPaths, ///< Spatial audio paths data.
59  AkMemID_GameSim, ///< Game Simulator allocations.
60  AkMemID_MonitorQueue, ///< Monitor Queue.
61  AkMemID_Profiler, ///< Profiler.
62  AkMemID_FilePackage, ///< File packager.
63  AkMemID_SoundEngine, ///< Base sound engine allocations (managers, etc).
64  AkMemID_Integration, ///< Game engine integration allocations.
65  AkMemID_JobMgr, ///< Allocations for Sound Engine jobs and job dependencies.
66 
67  AkMemID_NUM, ///< Category count.
68  AkMemID_MASK = 0x1FFFFFFF, ///< Mask for category IDs.
69 
70  AkMemType_Media = 0x20000000, ///< Media memory type bit.
71  AkMemType_Device = 0x40000000, ///< Device memory type bit.
72  AkMemType_NoTrack = 0x80000000 ///< Do not track this allocation.
73 };
74 
75 namespace AK
76 {
77  /// Memory Manager namespace.
78  /// \remarks The functions in this namespace are thread-safe, unless stated otherwise.
79  /// \sa
80  /// - \ref memorymanager
81  namespace MemoryMgr
82  {
83  /// Memory category statistics.
84  /// \remarks These statistics are not collected in the Release configuration of
85  /// the default memory manager implementation.
86  /// \sa
87  /// - AK::MemoryMgr::GetCategoryStats()
88  /// - \ref memorymanager
90  {
91  // Current state
92  AkUInt64 uUsed; ///< Used memory (in bytes)
93 
94  // Statistics
95  AkUInt64 uPeakUsed; ///< Peak used memory (in bytes)
96  AkUInt32 uAllocs; ///< Number of allocation calls since initialization
97  AkUInt32 uFrees; ///< Number of free calls since initialization
98  };
99 
100  /// Memory global statistics.
101  /// \remarks These statistics are not collected in the Release configuration of
102  /// the default memory manager implementation.
103  /// \sa
104  /// - AK::MemoryMgr::GetGlobalStats()
105  /// - \ref memorymanager
106  struct GlobalStats
107  {
108  AkUInt64 uUsed; ///< Total memory used including all categories (in bytes)
109  AkUInt64 uDeviceUsed; ///< Total device memory used including all categories (in bytes)
110  AkUInt64 uReserved; ///< Total reserved memory. (Used and unused). Will return 0 if the reserved memory is not traceable.
111  AkUInt64 uMax; ///< Maximum total allocation size, specified in the initialization settings through uMemAllocationSizeLimit. Will be 0 if no limit was set.
112  };
113 
114  ////////////////////////////////////////////////////////////////////////
115  /// @name Initialization
116  //@{
117 
118  /// Query whether the Memory Manager has been sucessfully initialized.
119  /// \warning This function is not thread-safe. It should not be called at the same time as MemoryMgr::Init or MemoryMgr::Term.
120  /// \return True if the Memory Manager is initialized, False otherwise
121  /// \sa
122  /// - AK::MemoryMgr::Init()
123  /// - \ref memorymanager
125 
126  /// Terminate the Memory Manager.
127  /// \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.
128  /// \sa
129  /// - \ref memorymanager
130  AK_EXTERNAPIFUNC( void, Term )();
131 
132  /// Performs whatever steps are required to initialize a thread for use with the memory manager.
133  /// For example initializing thread local storage that the allocator requires to work.
134  /// The default implementation of the memory manager performs thread initialization automatically and therefore this call is optional.
135  /// For implementations where the cost of automatically initializing a thread for use with an allocator would be prohibitively expensive
136  /// this call allows you to perform the initialization once during, for example, thread creation.
137  /// \sa
138  /// - AkMemInitForThread
140 
141  /// Allows you to manually terminate a thread for use with the memory manager.
142  /// The default implementation of the memory manager requires that all threads that interact with the memory manager call this function prior
143  /// to either their termination or the termination of the memory manager. Threads not created by the sound engine itself will not have this
144  /// function called for them automatically.
145  /// 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.
146  /// \sa
147  /// - AkMemTermForThread
149 
150  /// Allows you to "trim" a thread being used with the memory manager.
151  /// This is a function that will be called periodically by some Wwise-owned threads,
152  /// so that any thread-local state can be cleaned up in order to return memory for other systems to use.
153  /// For example, this can be used to return thread-local heaps to global stores or to finalize other deferred operations.
154  /// This function is only required for optimization purposes and does not have to be defined.
155  /// Therefore, unlike TermForThread, this is not expected to be called in all scenarios by Wwise.
156  /// It is also recommended to be called by game engine integrations in any worker threads that run Wwise jobs.
157  /// Refer to \ref eventmgrthread_jobmgr_best_practices for more information.
158  /// \sa
159  /// - AkMemTrimForThread
161 
162  //@}
163 
164  ////////////////////////////////////////////////////////////////////////
165  /// @name Memory Allocation
166  //@{
167 
168 #ifdef AK_MEMDEBUG
169  /// Allocate memory: debug version.
170  /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
171  /// \sa
172  /// - \ref memorymanager
174  AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
175  size_t in_uSize, ///< Number of bytes to allocate
176  const char *in_pszFile, ///< Debug file name
177  AkUInt32 in_uLine ///< Debug line number
178  );
179 #endif // AK_MEMDEBUG
180 
181  /// Allocate memory.
182  /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
183  /// \sa
184  /// - \ref memorymanager
186  AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
187  size_t in_uSize ///< Number of bytes to allocate
188  );
189 
190 #ifdef AK_MEMDEBUG
191  /// Reallocate memory: debug version.
192  /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
193  /// \sa
194  /// - \ref memorymanager
196  AkMemPoolId in_poolId,
197  void *in_pAlloc,
198  size_t in_uSize,
199  const char *in_pszFile,
200  AkUInt32 in_uLine
201  );
202 #endif // AK_MEMDEBUG
203 
204  /// Reallocate memory.
205  /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
206  /// \sa
207  /// - \ref memorymanager
209  AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
210  void * in_pAlloc, ///< Pointer to the start of the allocated memory
211  size_t in_uSize ///< Number of bytes to allocate
212  );
213 
214 #ifdef AK_MEMDEBUG
215  /// Reallocate memory: debug version.
216  /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
217  /// \sa
218  /// - \ref memorymanager
220  AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
221  void *in_pAlloc, ///< Pointer to the start of the allocated memory
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 
229  /// Reallocate memory.
230  /// \return A pointer to the start of the reallocated memory (NULL if the allocation could not be completed)
231  /// \sa
232  /// - \ref memorymanager
234  AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
235  void * in_pAlloc, ///< Pointer to the start of the allocated memory
236  size_t in_uSize, ///< Number of bytes to allocate
237  AkUInt32 in_uAlignment ///< Alignment (in bytes)
238  );
239 
240  /// Free memory allocated with the memory manager.
241  /// \sa
242  /// - \ref memorymanager
244  AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
245  void * in_pMemAddress ///< Pointer to the start of memory
246  );
247 
248 #ifdef AK_MEMDEBUG
249  /// Allocate memory with a specific alignment. debug version.
250  /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
251  /// \sa
252  /// - \ref memorymanager
254  AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
255  size_t in_uSize, ///< Number of bytes to allocate
256  AkUInt32 in_uAlignment, ///< Alignment (in bytes)
257  const char* in_pszFile, ///< Debug file name
258  AkUInt32 in_uLine ///< Debug line number
259  );
260 #endif // AK_MEMDEBUG
261 
262  /// Allocate memory with a specific alignment.
263  /// \return A pointer to the start of the allocated memory (NULL if the allocation could not be completed)
264  /// \sa
265  /// - \ref memorymanager
267  AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
268  size_t in_uSize, ///< Number of bytes to allocate
269  AkUInt32 in_uAlignment ///< Alignment (in bytes)
270  );
271 
272  //@}
273 
274  ////////////////////////////////////////////////////////////////////////
275  /// @name Memory Profiling
276  //@{
277 
278  /// Get statistics for a given memory category.
279  /// \note Be aware of the potentially incoherent nature of reporting such information during concurrent modification by multiple threads.
281  AkMemPoolId in_poolId, ///< ID of the memory category (AkMemID)
282  CategoryStats& out_poolStats ///< Returned statistics.
283  );
284 
285  /// Get statistics for overall memory manager usage.
286  /// \note Be aware of the potentially incoherent nature of reporting such information during concurrent modification by multiple threads.
288  GlobalStats& out_stats ///< Returned statistics.
289  );
290 
291  /// Called to start profiling memory usage for one thread (the calling thread).
292  /// \note Not implementing this will result in the Soundbank tab of the Wwise Profiler to show 0 bytes for memory usage.
294  );
295 
296  /// Called to stop profiling memory usage for the current thread.
297  /// \return The amount of memory allocated by this thread since StartProfileThreadUsage was called.
298  /// \note Not implementing this will result in the Soundbank tab of the Wwise Profiler to show 0 bytes for memory usage.
300  );
301 
302  /// Dumps the currently tracked allocations to a file
303  /// \note AkMemSettings::uMemoryDebugLevel must be enabled and the build must define AK_MEMDEBUG for this to work
305  const AkOSChar* pszFilename ///< Filename.
306  );
307 
308  //@}
309  }
310 }
311 
312 #endif // _AKMEMORYMGR_H_
@ AkMemID_Processing
Anything tied to instancing and processing of the DSP graph.
Definition: AkMemoryMgr.h:52
AKSOUNDENGINE_API void DumpToFile(const AkOSChar *pszFilename)
@ AkMemID_SpatialAudioPaths
Spatial audio paths data.
Definition: AkMemoryMgr.h:58
@ AkMemID_MonitorQueue
Monitor Queue.
Definition: AkMemoryMgr.h:60
@ AkMemID_FilePackage
File packager.
Definition: AkMemoryMgr.h:62
AKSOUNDENGINE_API void TermForThread()
AKSOUNDENGINE_API void * dRealloc(AkMemPoolId in_poolId, void *in_pAlloc, size_t in_uSize, const char *in_pszFile, AkUInt32 in_uLine)
AkUInt64 uPeakUsed
Peak used memory (in bytes)
Definition: AkMemoryMgr.h:95
AkUInt64 uDeviceUsed
Total device memory used including all categories (in bytes)
Definition: AkMemoryMgr.h:109
Audiokinetic namespace.
@ AkMemID_StreamingIO
Streaming Manager I/O memory.
Definition: AkMemoryMgr.h:55
AKSOUNDENGINE_API void * dMalloc(AkMemPoolId in_poolId, size_t in_uSize, const char *in_pszFile, AkUInt32 in_uLine)
@ AkMemID_Media
Media from the Wwise project.
Definition: AkMemoryMgr.h:50
@ AkMemID_Integration
Game engine integration allocations.
Definition: AkMemoryMgr.h:64
@ AkMemID_Object
Generic placeholder for allocations tied to the Wwise project.
Definition: AkMemoryMgr.h:47
AKSOUNDENGINE_API void * dMalign(AkMemPoolId in_poolId, size_t in_uSize, AkUInt32 in_uAlignment, const char *in_pszFile, AkUInt32 in_uLine)
AKSOUNDENGINE_API void Free(AkMemPoolId in_poolId, void *in_pMemAddress)
#define AK_EXTERNAPIFUNC(_type, _name)
AKSOUNDENGINE_API void * Malloc(AkMemPoolId in_poolId, size_t in_uSize)
AKSOUNDENGINE_API void TrimForThread()
AkInt32 AkMemPoolId
Memory pool ID.
Definition: AkTypes.h:130
@ AkMemID_Profiler
Profiler.
Definition: AkMemoryMgr.h:61
AkMemID
Memory category IDs.
Definition: AkMemoryMgr.h:46
char AkOSChar
Generic character string.
Definition: AkTypes.h:60
AkUInt32 uAllocs
Number of allocation calls since initialization.
Definition: AkMemoryMgr.h:96
AKSOUNDENGINE_API void Term()
AKSOUNDENGINE_API void GetGlobalStats(GlobalStats &out_stats)
AKSOUNDENGINE_API void InitForThread()
@ AkMemID_SpatialAudioGeometry
Spatial audio geometry data.
Definition: AkMemoryMgr.h:57
AkUInt64 uReserved
Total reserved memory. (Used and unused). Will return 0 if the reserved memory is not traceable.
Definition: AkMemoryMgr.h:110
AkUInt64 uMax
Maximum total allocation size, specified in the initialization settings through uMemAllocationSizeLim...
Definition: AkMemoryMgr.h:111
AKSOUNDENGINE_API void * dReallocAligned(AkMemPoolId in_poolId, void *in_pAlloc, size_t in_uSize, AkUInt32 in_uAlignment, const char *in_pszFile, AkUInt32 in_uLine)
@ AkMemID_JobMgr
Allocations for Sound Engine jobs and job dependencies.
Definition: AkMemoryMgr.h:65
AKSOUNDENGINE_API void * Malign(AkMemPoolId in_poolId, size_t in_uSize, AkUInt32 in_uAlignment)
@ AkMemID_SoundEngine
Base sound engine allocations (managers, etc).
Definition: AkMemoryMgr.h:63
AKSOUNDENGINE_API void StartProfileThreadUsage()
@ AkMemID_GameSim
Game Simulator allocations.
Definition: AkMemoryMgr.h:59
@ AkMemID_GameObject
Game Objects and related.
Definition: AkMemoryMgr.h:51
@ AkMemID_Streaming
Streaming Manager objects.
Definition: AkMemoryMgr.h:54
@ AkMemID_NUM
Category count.
Definition: AkMemoryMgr.h:67
@ AkMemID_MASK
Mask for category IDs.
Definition: AkMemoryMgr.h:68
AKSOUNDENGINE_API void * Realloc(AkMemPoolId in_poolId, void *in_pAlloc, size_t in_uSize)
@ AkMemID_Structure
Structures from the Wwise project.
Definition: AkMemoryMgr.h:49
@ AkMemID_Event
Events from the Wwise project.
Definition: AkMemoryMgr.h:48
AKSOUNDENGINE_API AkUInt64 StopProfileThreadUsage()
uint64_t AkUInt64
Unsigned 64-bit integer.
AkUInt64 uUsed
Total memory used including all categories (in bytes)
Definition: AkMemoryMgr.h:108
AkUInt64 uUsed
Used memory (in bytes)
Definition: AkMemoryMgr.h:92
@ AkMemID_ProcessingPlugin
Plug-in allocations related to the DSP graph.
Definition: AkMemoryMgr.h:53
AKSOUNDENGINE_API void GetCategoryStats(AkMemPoolId in_poolId, CategoryStats &out_poolStats)
uint32_t AkUInt32
Unsigned 32-bit integer.
@ AkMemType_NoTrack
Do not track this allocation.
Definition: AkMemoryMgr.h:72
AKSOUNDENGINE_API bool IsInitialized()
@ AkMemID_SpatialAudio
Spatial audio.
Definition: AkMemoryMgr.h:56
AKSOUNDENGINE_API void * ReallocAligned(AkMemPoolId in_poolId, void *in_pAlloc, size_t in_uSize, AkUInt32 in_uAlignment)
@ AkMemType_Device
Device memory type bit.
Definition: AkMemoryMgr.h:71
@ AkMemType_Media
Media memory type bit.
Definition: AkMemoryMgr.h:70
AkUInt32 uFrees
Number of free calls since initialization.
Definition: AkMemoryMgr.h:97

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise