AkAllocator.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // AkAllocator.h
  21. #ifndef _AK_TOOLS_COMMON_AKALLOCATOR_H
  22. #define _AK_TOOLS_COMMON_AKALLOCATOR_H
  23. #include <AK/SoundEngine/Common/IAkPluginMemAlloc.h>
  24. #include <AK/Tools/Common/AkAssert.h>
  25. #include <AK/AkPlatforms.h>
  26. #ifdef AK_WIN
  27. #include <cstdlib>
  28. #endif
  29. namespace AK
  30. {
  31. // Audiokinetic Wwise namespace
  32. namespace Wwise
  33. {
  34. class Mallocator
  35. : public AK::IAkPluginMemAlloc
  36. {
  37. public:
  38. virtual void* Malloc(
  39. size_t in_uSize,
  40. const char*,
  41. AkUInt32
  42. ) override
  43. {
  44. return malloc(in_uSize);
  45. }
  46. virtual void Free(void* in_pMemAddress) override
  47. {
  48. free(in_pMemAddress);
  49. }
  50. virtual void* Malign(
  51. size_t in_uSize,
  52. size_t in_uAlignment,
  53. const char*,
  54. AkUInt32
  55. ) override
  56. {
  57. #ifdef AK_WIN
  58. return _aligned_malloc(in_uSize, in_uAlignment);
  59. #else
  60. return aligned_alloc(in_uAlignment, in_uSize);
  61. #endif
  62. }
  63. virtual void* Realloc(
  64. void* in_pMemAddress,
  65. size_t in_uSize,
  66. const char*,
  67. AkUInt32
  68. ) override
  69. {
  70. return realloc(in_pMemAddress, in_uSize);
  71. }
  72. virtual void* ReallocAligned(
  73. void* in_pMemAddress,
  74. size_t in_uSize,
  75. size_t in_uAlignment,
  76. const char*,
  77. AkUInt32
  78. ) override
  79. {
  80. #ifdef AK_WIN
  81. return _aligned_realloc(in_pMemAddress, in_uSize, in_uAlignment);
  82. #else
  83. AKASSERT(!"ReallocAligned is not supported: using realloc");
  84. return realloc(in_pMemAddress, in_uSize);
  85. #endif
  86. }
  87. };
  88. template<typename T>
  89. class SafeAllocator
  90. {
  91. public:
  92. SafeAllocator(AK::IAkPluginMemAlloc* in_pAlloc)
  93. : m_pAlloc(in_pAlloc),
  94. m_pPtr(nullptr)
  95. {
  96. }
  97. ~SafeAllocator()
  98. {
  99. if (m_pPtr)
  100. {
  101. m_pAlloc->Free(m_pPtr);
  102. }
  103. }
  104. T* operator->() { return m_pPtr; }
  105. T& operator*() { return *m_pPtr; }
  106. explicit operator bool() const { return m_pPtr != nullptr; }
  107. operator T*&() { return m_pPtr; }
  108. private:
  109. AK::IAkPluginMemAlloc* m_pAlloc;
  110. T* m_pPtr;
  111. };
  112. }
  113. }
  114. #endif // _AK_TOOLS_COMMON_AKALLOCATOR_H