Version

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

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