AkObject.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef _AK_OBJECT_H_
  21. #define _AK_OBJECT_H_
  22. #include <AK/SoundEngine/Common/AkMemoryMgr.h>
  23. //-----------------------------------------------------------------------------
  24. // Placement New definition. Use like this:
  25. // AkPlacementNew( memorybuffer ) T(); // where T is your type constructor
  26. //-----------------------------------------------------------------------------
  27. /// Unique structure identifier for AkPlacementNew.
  28. struct AkPlacementNewKey
  29. {
  30. /// ctor
  31. AkForceInline AkPlacementNewKey(){}
  32. };
  33. AkForceInline void * operator new( size_t /*size*/, void * memory, const AkPlacementNewKey & /*key*/ ) throw()
  34. {
  35. return memory;
  36. }
  37. #define AkPlacementNew(_memory) ::new( _memory, AkPlacementNewKey() )
  38. // Matching operator delete for AK placement new. This needs to be defined to avoid compiler warnings
  39. // with projects built with exceptions enabled.
  40. AkForceInline void operator delete( void *, void *, const AkPlacementNewKey & ) throw() {}
  41. //-----------------------------------------------------------------------------
  42. // Macros
  43. //-----------------------------------------------------------------------------
  44. /// Unique structure identifier for AkNew.
  45. struct AkPoolNewKey
  46. {
  47. /// ctor
  48. AkForceInline AkPoolNewKey() {}
  49. };
  50. #ifdef AK_MEMDEBUG
  51. #define AkNew( _pool, _what ) new( ( _pool ), AkPoolNewKey(), __FILE__, __LINE__ ) _what
  52. #define AkAlloc( _pool, _size ) ( AK::MemoryMgr::dMalloc( ( _pool ), _size, __FILE__, __LINE__ ) )
  53. #define AkMalign( _pool, _size, _align ) ( AK::MemoryMgr::dMalign( ( _pool ), _size, _align, __FILE__, __LINE__ ) )
  54. #define AkNewAligned( _pool, _what, _align ) new( ( _pool ), AkPoolNewKey(), ( _align ), __FILE__ , __LINE__ ) _what
  55. #define AkRealloc( _pool, _pvmem, _size ) ( AK::MemoryMgr::dRealloc( ( _pool ), _pvmem, _size, __FILE__, __LINE__ ) )
  56. #define AkReallocAligned( _pool, _pvmem, _size, _align ) ( AK::MemoryMgr::dReallocAligned( ( _pool ), _pvmem, _size, _align, __FILE__, __LINE__ ) )
  57. #else
  58. #define AkNew( _pool, _what ) new( ( _pool ), AkPoolNewKey() ) _what
  59. #define AkAlloc( _pool, _size ) ( AK::MemoryMgr::Malloc( ( _pool ), _size ) )
  60. #define AkMalign( _pool, _size, _align ) ( AK::MemoryMgr::Malign( ( _pool ), _size, _align ) )
  61. #define AkNewAligned( _pool, _what, _align ) new( ( _pool ), AkPoolNewKey(), ( _align ) ) _what
  62. #define AkRealloc( _pool, _pvmem, _size ) ( AK::MemoryMgr::Realloc( ( _pool ), _pvmem, _size ) )
  63. #define AkReallocAligned( _pool, _pvmem, _size, _align ) ( AK::MemoryMgr::ReallocAligned( ( _pool ), _pvmem, _size, _align ) )
  64. #endif
  65. #define AkFree( _pool, _pvmem ) ( AK::MemoryMgr::Free( ( _pool ), ( _pvmem ) ) )
  66. #ifdef AK_MEMDEBUG
  67. AkForceInline void * operator new( size_t size, AkMemPoolId in_PoolId, const AkPoolNewKey & , const char* szFile, AkUInt32 ulLine ) throw()
  68. {
  69. return AK::MemoryMgr::dMalloc( in_PoolId, size, szFile, ulLine );
  70. }
  71. AkForceInline void * operator new( size_t size, AkMemPoolId in_PoolId, const AkPoolNewKey &, AkUInt32 in_align, const char* szFile, AkUInt32 ulLine ) throw()
  72. {
  73. return AK::MemoryMgr::dMalign( in_PoolId, size, in_align, szFile, ulLine );
  74. }
  75. AkForceInline void operator delete( void *, AkMemPoolId, const AkPoolNewKey &, const char*, AkUInt32 ) throw() {}
  76. AkForceInline void operator delete( void *, AkMemPoolId, const AkPoolNewKey &, AkUInt32, const char*, AkUInt32 ) throw() {}
  77. #else
  78. AkForceInline void * operator new( size_t size, AkMemPoolId in_PoolId, const AkPoolNewKey & ) throw()
  79. {
  80. return AK::MemoryMgr::Malloc( in_PoolId, size );
  81. }
  82. AkForceInline void * operator new( size_t size, AkMemPoolId in_PoolId, const AkPoolNewKey &, AkUInt32 in_align ) throw()
  83. {
  84. return AK::MemoryMgr::Malign( in_PoolId, size, in_align );
  85. }
  86. AkForceInline void operator delete( void *, AkMemPoolId, const AkPoolNewKey & ) throw() {}
  87. AkForceInline void operator delete( void *, AkMemPoolId, const AkPoolNewKey &, AkUInt32 ) throw() {}
  88. #endif
  89. template <class T>
  90. AkForceInline void AkDelete( AkMemPoolId in_PoolId, T * in_pObject )
  91. {
  92. if ( in_pObject )
  93. {
  94. in_pObject->~T();
  95. AK::MemoryMgr::Free( in_PoolId, in_pObject );
  96. }
  97. }
  98. #endif // _AK_OBJECT_H_