Table of Contents

include/AK/Tools/Common/AkObject.h

Go to the documentation of this file.
00001 
00002 //
00003 // AkObject.h
00004 //
00005 // Base class for object that use dynamic allocation.
00006 // Overloads new and delete to call those of the memory manager.
00007 //
00008 // Copyright (c) 2006 Audiokinetic Inc. / All Rights Reserved
00009 //
00011 
00012 #ifndef _AK_OBJECT_H_
00013 #define _AK_OBJECT_H_
00014 
00015 #include <AK/SoundEngine/Common/AkMemoryMgr.h>
00016 
00017 extern AKSOUNDENGINE_API AkMemPoolId g_DefaultPoolId;
00018 extern AKSOUNDENGINE_API AkMemPoolId g_LEngineDefaultPoolId;
00019 
00020 //-----------------------------------------------------------------------------
00021 // Placement New definition. Use like this:
00022 // AkPlacementNew( memorybuffer ) T(); // where T is your type constructor
00023 //-----------------------------------------------------------------------------
00024 
00026 struct AkPlacementNewKey 
00027 { 
00029     AkForceInline AkPlacementNewKey(){} 
00030 };
00031 
00032 AkForceInline void * operator new( size_t /*size*/, void * memory, const AkPlacementNewKey & /*key*/ ) throw()
00033 {
00034       return memory;
00035 }
00036 
00037 #define AkPlacementNew(_memory) ::new( _memory, AkPlacementNewKey() )
00038 
00039 // Matching operator delete for AK placement new. This needs to be defined to avoid compiler warnings
00040 // with projects built with exceptions enabled.
00041 #ifndef AK_3DS
00042 AkForceInline void operator delete( void *, void *, const AkPlacementNewKey & ) throw() {}
00043 #endif
00044 
00045 //-----------------------------------------------------------------------------
00046 // Macros
00047 //-----------------------------------------------------------------------------
00048 
00050 struct AkPoolNewKey 
00051 { 
00053     AkForceInline AkPoolNewKey(){} 
00054 };
00055 
00056 // Important: Use these macros with appropriate delete.
00057 #if defined (AK_MEMDEBUG)
00058     #define AkNew(_pool,_what)              new((_pool),AkPoolNewKey(),__FILE__,__LINE__) _what
00059     #define AkAlloc(_pool,_size)            (AK::MemoryMgr::dMalloc((_pool),_size,__FILE__,__LINE__))
00060     #define AkNew2(_ptr,_pool,_type,_what)  { _ptr = (_type *) AK::MemoryMgr::dMalloc((_pool),sizeof(_type),__FILE__,__LINE__); if ( _ptr ) AkPlacementNew( _ptr ) _what; }
00061     #define AkMalign(_pool,_size,_align)    (AK::MemoryMgr::dMalign((_pool),_size,_align, __FILE__,__LINE__))
00062     #define AkNewAligned(_pool,_what,_align)    new((_pool),AkPoolNewKey(),(_align),__FILE__,__LINE__) _what
00063 #else
00064     #define AkNew(_pool,_what)              new((_pool),AkPoolNewKey()) _what
00065     #define AkAlloc(_pool,_size)            (AK::MemoryMgr::Malloc((_pool),_size))
00066     #define AkNew2(_ptr,_pool,_type,_what)  { _ptr = (_type *) AK::MemoryMgr::Malloc((_pool),sizeof(_type)); if ( _ptr ) AkPlacementNew( _ptr ) _what; }
00067     #define AkMalign(_pool,_size,_align)    (AK::MemoryMgr::Malign((_pool),_size,_align))
00068     #define AkNewAligned(_pool,_what,_align)    new((_pool),AkPoolNewKey(),(_align)) _what
00069 #endif
00070 
00071 #define AkFree(_pool,_pvmem)                (AK::MemoryMgr::Free((_pool),(_pvmem)))
00072 #define AkFalign(_pool,_pvmem)              (AK::MemoryMgr::Falign((_pool),(_pvmem)))
00073 
00074 #if defined (AK_MEMDEBUG)
00075 
00076     AkForceInline void * operator new(size_t size,AkMemPoolId in_PoolId,const AkPoolNewKey &,const char* szFile,AkUInt32 ulLine) throw()
00077     {
00078         return AK::MemoryMgr::dMalloc( in_PoolId, size, szFile, ulLine );
00079     }
00080 
00081     AkForceInline void * operator new(size_t size,AkMemPoolId in_PoolId,const AkPoolNewKey &,AkUInt32 in_align,const char* szFile,AkUInt32 ulLine) throw()
00082     {
00083         return AK::MemoryMgr::dMalign( in_PoolId, size, in_align, szFile, ulLine );
00084     }
00085     
00086     #ifndef AK_3DS
00087     AkForceInline void operator delete(void *,AkMemPoolId,const AkPoolNewKey &,const char*,AkUInt32) throw() {}
00088     AkForceInline void operator delete(void *,AkMemPoolId,const AkPoolNewKey &,AkUInt32,const char*,AkUInt32) throw() {}
00089     #endif
00090     
00091 #else
00092 
00093     AkForceInline void * operator new(size_t size,AkMemPoolId in_PoolId,const AkPoolNewKey &) throw()
00094     {
00095         return AK::MemoryMgr::Malloc( in_PoolId, size );
00096     }
00097 
00098     AkForceInline void * operator new(size_t size,AkMemPoolId in_PoolId,const AkPoolNewKey &,AkUInt32 in_align) throw()
00099     {
00100         return AK::MemoryMgr::Malign( in_PoolId, size, in_align );
00101     }
00102     
00103     #ifndef AK_3DS
00104     AkForceInline void operator delete(void *,AkMemPoolId,const AkPoolNewKey &) throw() {}
00105     AkForceInline void operator delete(void *,AkMemPoolId,const AkPoolNewKey &,AkUInt32) throw() {}
00106     #endif
00107 
00108 #endif
00109 
00110 //-----------------------------------------------------------------------------
00111 // Name: Class CAkObject
00112 // Desc: Base allocator object: DEPRECATED.
00113 //-----------------------------------------------------------------------------
00114 
00115 class CAkObject
00116 {
00117 public:
00119     virtual ~CAkObject( ) { }
00120 };
00121 
00122 template <class T>
00123 AkForceInline void AkDelete( AkMemPoolId in_PoolId, T * in_pObject )
00124 {
00125     if ( in_pObject )
00126     {
00127         in_pObject->~T();
00128         AK::MemoryMgr::Free( in_PoolId, in_pObject );
00129     }
00130 }
00131 
00132 template <class T>
00133 AkForceInline void AkDeleteAligned( AkMemPoolId in_PoolId, T * in_pObject )
00134 {
00135     if ( in_pObject )
00136     {
00137         in_pObject->~T();
00138         AK::MemoryMgr::Falign( in_PoolId, in_pObject );
00139     }
00140 }
00141 
00142 #endif // _AK_OBJECT_H_