123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- #ifndef _AK_TOOLS_COMMON_AKALLOCATOR_H
- #define _AK_TOOLS_COMMON_AKALLOCATOR_H
- #include <AK/SoundEngine/Common/IAkPluginMemAlloc.h>
- #include <AK/Tools/Common/AkAssert.h>
- #include <AK/AkPlatforms.h>
- #ifdef AK_WIN
- #include <cstdlib>
- #endif
- namespace AK
- {
-
- namespace Wwise
- {
- class Mallocator
- : public AK::IAkPluginMemAlloc
- {
- public:
- virtual void* Malloc(
- size_t in_uSize,
- const char*,
- AkUInt32
- ) override
- {
- return malloc(in_uSize);
- }
- virtual void Free(void* in_pMemAddress) override
- {
- free(in_pMemAddress);
- }
- virtual void* Malign(
- size_t in_uSize,
- size_t in_uAlignment,
- const char*,
- AkUInt32
- ) override
- {
- #ifdef AK_WIN
- return _aligned_malloc(in_uSize, in_uAlignment);
- #else
- return aligned_alloc(in_uAlignment, in_uSize);
- #endif
- }
- virtual void* Realloc(
- void* in_pMemAddress,
- size_t in_uSize,
- const char*,
- AkUInt32
- ) override
- {
- return realloc(in_pMemAddress, in_uSize);
- }
- virtual void* ReallocAligned(
- void* in_pMemAddress,
- size_t in_uSize,
- size_t in_uAlignment,
- const char*,
- AkUInt32
- ) override
- {
- #ifdef AK_WIN
- return _aligned_realloc(in_pMemAddress, in_uSize, in_uAlignment);
- #else
- AKASSERT(!"ReallocAligned is not supported: using realloc");
- return realloc(in_pMemAddress, in_uSize);
- #endif
- }
- };
- template<typename T>
- class SafeAllocator
- {
- public:
- SafeAllocator(AK::IAkPluginMemAlloc* in_pAlloc)
- : m_pAlloc(in_pAlloc),
- m_pPtr(nullptr)
- {
- }
- ~SafeAllocator()
- {
- if (m_pPtr)
- {
- m_pAlloc->Free(m_pPtr);
- }
- }
- T* operator->() { return m_pPtr; }
- T& operator*() { return *m_pPtr; }
- explicit operator bool() const { return m_pPtr != nullptr; }
- operator T*&() { return m_pPtr; }
- private:
- AK::IAkPluginMemAlloc* m_pAlloc;
- T* m_pPtr;
- };
- }
- }
- #endif
|