IAkPluginMemAlloc.h 8.8 KB

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