Version
menu

Wwise SDK 2024.1.9
IAkPluginMemAlloc.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) 2025 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 /// \file
28 /// Memory allocation macros for Wwise sound engine plug-ins.
29 
30 #ifndef _IAKPLUGINMEMALLOC_H_
31 #define _IAKPLUGINMEMALLOC_H_
32 
34 
35 namespace AK
36 {
37  /// Interface to memory allocation
38  /// \akcaution SDK users should never call these function directly, but use memory allocation macros instead. \endakcaution
39  /// \sa
40  /// - \ref fx_memory_alloc
42  {
43  protected:
44  /// Virtual destructor on interface to avoid warnings.
45  virtual ~IAkPluginMemAlloc(){}
46 
47  public:
48 
49  /// Allocate memory.
50  /// \return A pointer to the newly-allocated memory.
51  /// \sa
52  /// - \ref fx_memory_alloc
53  virtual void * Malloc(
54  size_t in_uSize, ///< Allocation size in bytes
55  const char* in_pszFile, ///< Allocation file name (for tracking purposes, unused in Release)
56  AkUInt32 in_uLine ///< Allocation line number (for tracking purposes, unused in Release)
57  ) = 0;
58 
59  /// Free allocated memory.
60  /// \sa
61  /// - \ref fx_memory_alloc
62  virtual void Free(
63  void * in_pMemAddress ///< Allocated memory start address
64  ) = 0;
65 
66  /// Allocate memory.
67  /// \return A pointer to the newly-allocated memory.
68  /// \sa
69  /// - \ref fx_memory_alloc
70  virtual void * Malign(
71  size_t in_uSize, ///< Allocation size in bytes
72  size_t in_uAlignment, ///< Required alignment in bytes
73  const char* in_pszFile, ///< Allocation file name (for tracking purposes, unused in Release)
74  AkUInt32 in_uLine ///< Allocation line number (for tracking purposes, unused in Release)
75  ) = 0;
76 
77  /// Reallocated memory.
78  /// \return A pointer to the reallocated memory.
79  /// \sa
80  /// - \ref fx_memory_alloc
81  virtual void * Realloc(
82  void * in_pMemAddress, ///< Allocated memory start address
83  size_t in_uSize, ///< Allocation size in bytes
84  const char* in_pszFile, ///< Allocation file name (for tracking purposes, unused in Release)
85  AkUInt32 in_uLine ///< Allocation line number (for tracking purposes, unused in Release)
86  ) = 0;
87 
88  /// Reallocated memory.
89  /// \return A pointer to the reallocated memory.
90  /// \sa
91  /// - \ref fx_memory_alloc
92  virtual void * ReallocAligned(
93  void * in_pMemAddress, ///< Allocated memory start address
94  size_t in_uSize, ///< Allocation size in bytes
95  size_t in_uAlignment, ///< Required alignment in bytes
96  const char* in_pszFile, ///< Allocation file name (for tracking purposes, unused in Release)
97  AkUInt32 in_uLine ///< Allocation line number (for tracking purposes, unused in Release)
98  ) = 0;
99  };
100 }
101 
102  AkForceInline void * operator new( size_t size, AK::IAkPluginMemAlloc * in_pAllocator, const char * szFile, AkUInt32 ulLine) throw()
103  {
104  return in_pAllocator->Malloc( size, szFile, ulLine );
105  }
106 
107  AkForceInline void* operator new(size_t size, AK::IAkPluginMemAlloc* in_pAllocator) throw()
108  {
109  return in_pAllocator->Malloc(size, NULL, 0);
110  }
111 
112  AkForceInline void operator delete( void *, AK::IAkPluginMemAlloc *, const char *, AkUInt32 ) throw() {}
113  AkForceInline void operator delete( void *, AK::IAkPluginMemAlloc * ) throw() {}
114 
115  /// Macro used to allocate objects.
116  /// \param _allocator Memory allocator interface.
117  /// \param _what Desired object type.
118  /// \return A pointer to the newly-allocated object.
119  /// \aknote Use AK_PLUGIN_DELETE() for memory allocated with this macro. \endaknote
120  /// \sa
121  /// - \ref fx_memory_alloc
122  /// - AK_PLUGIN_DELETE()
123  #define AK_PLUGIN_NEW( _allocator, _what ) new( ( _allocator ), __FILE__, __LINE__ ) _what
124 
125 
126  /// Macro used to allocate memory
127  /// \param _allocator Memory allocator interface.
128  /// \param _size Requested size in bytes.
129  /// \return A void pointer to the the allocated memory.
130  /// \aknote Use AK_PLUGIN_FREE() for memory allocated with this macro. \endaknote
131  /// \sa
132  /// - \ref fx_memory_alloc
133  /// - AK_PLUGIN_FREE()
134  #define AK_PLUGIN_ALLOC( _allocator, _size ) ( _allocator )->Malloc( ( _size ), __FILE__, __LINE__ )
135 
136  /// Macro used to allocate memory with an alignment
137  /// \param _allocator Memory allocator interface.
138  /// \param _size Requested size in bytes.
139  /// \param _align Requested alignment of allocation
140  /// \return A void pointer to the the allocated memory.
141  /// \aknote Use AK_PLUGIN_FREE() for memory allocated with this macro. \endaknote
142  /// \sa
143  /// - \ref fx_memory_alloc
144  /// - AK_PLUGIN_FREE()
145  #define AK_PLUGIN_ALLOC_ALIGN( _allocator, _size, _align ) ( _allocator )->Malign( ( _size ), ( _align ), __FILE__, __LINE__ )
146 
147  /// Macro used to reallocate memory
148  /// \param _allocator Memory allocator interface.
149  /// \param _pmem Start of allocated memory.
150  /// \param _size Requested size in bytes.
151  /// \return A void pointer to the the reallocated memory.
152  /// \aknote Use AK_PLUGIN_FREE() for memory allocated with this macro. \endaknote
153  /// \sa
154  /// - \ref fx_memory_alloc
155  /// - AK_PLUGIN_FREE()
156  #define AK_PLUGIN_REALLOC( _allocator, _pmem, _size ) ( _allocator )->Realloc( ( _pmem ), ( _size ), __FILE__, __LINE__ )
157 
158  /// Macro used to reallocate memory with an alignment
159  /// \param _allocator Memory allocator interface.
160  /// \param _pmem Start of allocated memory.
161  /// \param _size Requested size in bytes.
162  /// \param _align Requested alignment of allocation
163  /// \return A void pointer to the the reallocated memory.
164  /// \aknote Use AK_PLUGIN_FREE() for memory allocated with this macro. \endaknote
165  /// \sa
166  /// - \ref fx_memory_alloc
167  /// - AK_PLUGIN_FREE()
168  #define AK_PLUGIN_REALLOC_ALIGN( _allocator, _pmem, _size, _align ) ( _allocator )->ReallocAligned( ( _pmem ), ( _size ), ( _align ), __FILE__, __LINE__ )
169 
170 
171 /// Macro used to delete objects.
172 /// \param in_pAllocator Memory allocator interface.
173 /// \param in_pObject A pointer to the allocated object.
174 /// \sa
175 /// - \ref fx_memory_alloc
176 /// - AK_PLUGIN_NEW()
177 template <class T>
178 AkForceInline void AK_PLUGIN_DELETE( AK::IAkPluginMemAlloc * in_pAllocator, T * in_pObject )
179 {
180  if ( in_pObject )
181  {
182  in_pObject->~T();
183  in_pAllocator->Free( in_pObject );
184  }
185 }
186 
187 /// Macro used to free memory.
188 /// \param _allocator Memory allocator interface.
189 /// \param _pvmem A void pointer to the allocated memory.
190 /// \sa
191 /// - \ref fx_memory_alloc
192 /// - AK_PLUGIN_ALLOC()
193 #define AK_PLUGIN_FREE( _allocator, _pvmem ) ( _allocator )->Free( ( _pvmem ) )
194 
195 /// Allocator for plugin-friendly arrays (see AkArray).
196 /// Usage: Initialize the array with Init(AK::IAkPluginMemAlloc* in_pAllocator), passing it the memory allocator received from the host. Then use normally.
198 {
199 public:
201  AkForceInline void Init(AK::IAkPluginMemAlloc* in_pAllocator) { m_pAllocator = in_pAllocator; }
202 protected:
203  AkForceInline void* Alloc(size_t in_uSize) { return AK_PLUGIN_ALLOC(m_pAllocator, in_uSize); }
204  AkForceInline void* ReAlloc(void* in_pCurrent, size_t /*in_uOldSize*/, size_t in_uNewSize) { return AK_PLUGIN_REALLOC(m_pAllocator, in_pCurrent, in_uNewSize); }
205  AkForceInline void Free(void* in_pAddress) { AK_PLUGIN_FREE(m_pAllocator, in_pAddress); }
206  AkForceInline void TransferMem(void*& io_pDest, AkPluginArrayAllocator& in_src, void* in_pSrc)
207  {
208  // The expected io_pDest should be NULL here.
209  io_pDest = in_pSrc;
210  m_pAllocator = in_src.GetAllocator();
211  }
213 private:
214  AK::IAkPluginMemAlloc* m_pAllocator;
215 };
216 
217 #endif // _IAKPLUGINMEMALLOC_H_
AkForceInline void AK_PLUGIN_DELETE(AK::IAkPluginMemAlloc *in_pAllocator, T *in_pObject)
AkForceInline void * Alloc(size_t in_uSize)
AkForceInline AK::IAkPluginMemAlloc * GetAllocator()
Definition of data structures for AkAudioObject.
virtual void * Malloc(size_t in_uSize, const char *in_pszFile, AkUInt32 in_uLine)=0
AkForceInline void * ReAlloc(void *in_pCurrent, size_t, size_t in_uNewSize)
#define NULL
Definition: AkTypes.h:46
AkForceInline void TransferMem(void *&io_pDest, AkPluginArrayAllocator &in_src, void *in_pSrc)
AkForceInline void Init(AK::IAkPluginMemAlloc *in_pAllocator)
#define AK_PLUGIN_FREE(_allocator, _pvmem)
virtual void * ReallocAligned(void *in_pMemAddress, size_t in_uSize, size_t in_uAlignment, const char *in_pszFile, AkUInt32 in_uLine)=0
AkForceInline AkPluginArrayAllocator()
#define AK_PLUGIN_ALLOC(_allocator, _size)
virtual void * Realloc(void *in_pMemAddress, size_t in_uSize, const char *in_pszFile, AkUInt32 in_uLine)=0
virtual ~IAkPluginMemAlloc()
Virtual destructor on interface to avoid warnings.
virtual void * Malign(size_t in_uSize, size_t in_uAlignment, const char *in_pszFile, AkUInt32 in_uLine)=0
virtual void Free(void *in_pMemAddress)=0
#define AK_PLUGIN_REALLOC(_allocator, _pmem, _size)
uint32_t AkUInt32
Unsigned 32-bit integer.
#define AkForceInline
Definition: AkTypes.h:63
AkForceInline void Free(void *in_pAddress)

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