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