Table of Contents

include/AK/Tools/Common/AkObject.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 The content of this file includes portions of the AUDIOKINETIC Wwise Technology
00003 released in source code form as part of the SDK installer package.
00004 
00005 Commercial License Usage
00006 
00007 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
00008 may use this file in accordance with the end user license agreement provided 
00009 with the software or, alternatively, in accordance with the terms contained in a
00010 written agreement between you and Audiokinetic Inc.
00011 
00012 Apache License Usage
00013 
00014 Alternatively, this file may be used under the Apache License, Version 2.0 (the 
00015 "Apache License"); you may not use this file except in compliance with the 
00016 Apache License. You may obtain a copy of the Apache License at 
00017 http://www.apache.org/licenses/LICENSE-2.0.
00018 
00019 Unless required by applicable law or agreed to in writing, software distributed
00020 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
00021 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
00022 the specific language governing permissions and limitations under the License.
00023 
00024   Version: <VERSION>  Build: <BUILDNUMBER>
00025   Copyright (c) <COPYRIGHTYEAR> Audiokinetic Inc.
00026 *******************************************************************************/
00027 
00028 #ifndef _AK_OBJECT_H_
00029 #define _AK_OBJECT_H_
00030 
00031 #include <AK/SoundEngine/Common/AkMemoryMgr.h>
00032 
00033 extern AKSOUNDENGINE_API AkMemPoolId g_DefaultPoolId;
00034 extern AKSOUNDENGINE_API AkMemPoolId g_LEngineDefaultPoolId;
00035 
00036 //-----------------------------------------------------------------------------
00037 // Placement New definition. Use like this:
00038 // AkPlacementNew( memorybuffer ) T(); // where T is your type constructor
00039 //-----------------------------------------------------------------------------
00040 
00041 /// Unique structure identifier for AkPlacementNew. 
00042 struct AkPlacementNewKey 
00043 { 
00044     /// ctor 
00045     AkForceInline AkPlacementNewKey(){} 
00046 };
00047 
00048 AkForceInline void * operator new( size_t /*size*/, void * memory, const AkPlacementNewKey & /*key*/ ) throw()
00049 {
00050       return memory;
00051 }
00052 
00053 #define AkPlacementNew(_memory) ::new( _memory, AkPlacementNewKey() )
00054 
00055 // Matching operator delete for AK placement new. This needs to be defined to avoid compiler warnings
00056 // with projects built with exceptions enabled.
00057 AkForceInline void operator delete( void *, void *, const AkPlacementNewKey & ) throw() {}
00058 
00059 //-----------------------------------------------------------------------------
00060 // Macros
00061 //-----------------------------------------------------------------------------
00062 
00063 /// Unique structure identifier for AkNew. 
00064 struct AkPoolNewKey 
00065 { 
00066     /// ctor 
00067     AkForceInline AkPoolNewKey(){} 
00068 };
00069 
00070 // Important: Use these macros with appropriate delete.
00071 #if defined (AK_MEMDEBUG)
00072     #define AkNew(_pool,_what)              new((_pool),AkPoolNewKey(),__FILE__,__LINE__) _what
00073     #define AkAlloc(_pool,_size)            (AK::MemoryMgr::dMalloc((_pool),_size,__FILE__,__LINE__))
00074     #define AkNew2(_ptr,_pool,_type,_what)  { _ptr = (_type *) AK::MemoryMgr::dMalloc((_pool),sizeof(_type),__FILE__,__LINE__); if ( _ptr ) AkPlacementNew( _ptr ) _what; }
00075     #define AkMalign(_pool,_size,_align)    (AK::MemoryMgr::dMalign((_pool),_size,_align, __FILE__,__LINE__))
00076     #define AkNewAligned(_pool,_what,_align)    new((_pool),AkPoolNewKey(),(_align),__FILE__,__LINE__) _what
00077 #else
00078     #define AkNew(_pool,_what)              new((_pool),AkPoolNewKey()) _what
00079     #define AkAlloc(_pool,_size)            (AK::MemoryMgr::Malloc((_pool),_size))
00080     #define AkNew2(_ptr,_pool,_type,_what)  { _ptr = (_type *) AK::MemoryMgr::Malloc((_pool),sizeof(_type)); if ( _ptr ) AkPlacementNew( _ptr ) _what; }
00081     #define AkMalign(_pool,_size,_align)    (AK::MemoryMgr::Malign((_pool),_size,_align))
00082     #define AkNewAligned(_pool,_what,_align)    new((_pool),AkPoolNewKey(),(_align)) _what
00083 #endif
00084 
00085 #define AkFree(_pool,_pvmem)                (AK::MemoryMgr::Free((_pool),(_pvmem)))
00086 #define AkFalign(_pool,_pvmem)              (AK::MemoryMgr::Falign((_pool),(_pvmem)))
00087 #define AkRealloc(_pool, _pvmem, _size)     (AK::MemoryMgr::Realloc((_pool), _pvmem, _size))
00088 
00089 #if defined (AK_MEMDEBUG)
00090 
00091     AkForceInline void * operator new(size_t size,AkMemPoolId in_PoolId,const AkPoolNewKey &,const char* szFile,AkUInt32 ulLine) throw()
00092     {
00093         return AK::MemoryMgr::dMalloc( in_PoolId, size, szFile, ulLine );
00094     }
00095 
00096     AkForceInline void * operator new(size_t size,AkMemPoolId in_PoolId,const AkPoolNewKey &,AkUInt32 in_align,const char* szFile,AkUInt32 ulLine) throw()
00097     {
00098         return AK::MemoryMgr::dMalign( in_PoolId, size, in_align, szFile, ulLine );
00099     }
00100     
00101     AkForceInline void operator delete(void *,AkMemPoolId,const AkPoolNewKey &,const char*,AkUInt32) throw() {}
00102     AkForceInline void operator delete(void *,AkMemPoolId,const AkPoolNewKey &,AkUInt32,const char*,AkUInt32) throw() {}
00103     
00104 #else
00105 
00106     AkForceInline void * operator new(size_t size,AkMemPoolId in_PoolId,const AkPoolNewKey &) throw()
00107     {
00108         return AK::MemoryMgr::Malloc( in_PoolId, size );
00109     }
00110 
00111     AkForceInline void * operator new(size_t size,AkMemPoolId in_PoolId,const AkPoolNewKey &,AkUInt32 in_align) throw()
00112     {
00113         return AK::MemoryMgr::Malign( in_PoolId, size, in_align );
00114     }
00115     
00116     AkForceInline void operator delete(void *,AkMemPoolId,const AkPoolNewKey &) throw() {}
00117     AkForceInline void operator delete(void *,AkMemPoolId,const AkPoolNewKey &,AkUInt32) throw() {}
00118 
00119 #endif
00120 
00121 template <class T>
00122 AkForceInline void AkDelete( AkMemPoolId in_PoolId, T * in_pObject )
00123 {
00124     if ( in_pObject )
00125     {
00126         in_pObject->~T();
00127         AK::MemoryMgr::Free( in_PoolId, in_pObject );
00128     }
00129 }
00130 
00131 template <class T>
00132 AkForceInline void AkDeleteAligned( AkMemPoolId in_PoolId, T * in_pObject )
00133 {
00134     if ( in_pObject )
00135     {
00136         in_pObject->~T();
00137         AK::MemoryMgr::Falign( in_PoolId, in_pObject );
00138     }
00139 }
00140 
00141 #endif // _AK_OBJECT_H_