True Axis Physics SDK 1.2.0.1 Beta Documentation
www.trueaxis.com

MemoryMgr.h

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------------
00002 // File Name: MemoryMgr.h
00003 // Description:
00006 //
00007 // Copyright (C) 2004 - 2006 True Axis Pty Ltd, Australia. 
00008 // All Rights Reserved.
00009 //
00010 // History:
00011 //      Created File.
00012 //---------------------------------------------------------------------------------
00013 
00014 #ifndef TA_MEMORYMGR_H
00015 #define TA_MEMORYMGR_H
00016 
00017 #ifndef TA_COMMON_H
00018 #include "Common.h"
00019 #endif // TA_COMMON_H
00020 
00021 TA_OBFUSCATION_SKIP_PASS_2
00022 
00023 #include <new>
00024 
00025 #ifdef _DEBUG
00026 
00027 #define TA_MEMORY_MGR_NEW_ARRAY(pPtr, Type, nNumber)                                        \
00028 {                                                                                           \
00029     TA::s_nMemoryAllocLine = __LINE__;                                                      \
00030     TA::s_szMemoryAllocFileName = __FILE__;                                                 \
00031     (pPtr) = (Type*)(new TA::AllocateWrapper< Type >[nNumber]);                             \
00032     TA::s_nMemoryAllocLine = 0;                                                             \
00033     TA::s_szMemoryAllocFileName = 0;                                                        \
00034 }
00035 
00036 #define TA_MEMORY_MGR_DELETE_ARRAY(pPtr, Type)                                              \
00037 {                                                                                           \
00038     TA::s_nMemoryAllocLine = __LINE__;                                                      \
00039     TA::s_szMemoryAllocFileName = __FILE__;                                                 \
00040     delete []((TA::AllocateWrapper< Type >*)pPtr);                                          \
00041     TA::s_nMemoryAllocLine = 0;                                                             \
00042     TA::s_szMemoryAllocFileName = 0;                                                        \
00043 }
00044 
00045 #define TA_MEMORY_MGR_NEW(pPtr, Type)                                                       \
00046 {                                                                                           \
00047     (pPtr) = (Type*)TA::MemoryMgr::DbgAlloc(sizeof (Type), 16, __FILE__, __LINE__);         \
00048     new (pPtr) Type;                                                                        \
00049 }
00050 
00051 #define TA_MEMORY_MGR_DELETE(pPtr, Type)                                                    \
00052 {                                                                                           \
00053     (pPtr)->~Type();                                                                        \
00054     TA::MemoryMgr::DbgFree((pPtr), __FILE__, __LINE__);                                     \
00055 }
00056 
00057 #else
00058 
00059 #define TA_MEMORY_MGR_NEW_ARRAY(pPtr, Type, nNumber)                                        \
00060 {                                                                                           \
00061     (pPtr) = (Type*)(new TA::AllocateWrapper< Type >[nNumber]);                             \
00062 }
00063 
00064 #define TA_MEMORY_MGR_DELETE_ARRAY(pPtr, Type)                                              \
00065 {                                                                                           \
00066     delete []((TA::AllocateWrapper< Type >*)pPtr);                                          \
00067 }
00068 
00069 #define TA_MEMORY_MGR_NEW(pPtr, Type)                                                       \
00070 {                                                                                           \
00071     (pPtr) = (Type*)TA::MemoryMgr::Alloc(sizeof (Type), 16);                                \
00072     new (pPtr) Type;                                                                        \
00073 }
00074 
00075 #define TA_MEMORY_MGR_DELETE(pPtr, Type)                                                    \
00076 {                                                                                           \
00077     (pPtr)->~Type();                                                                        \
00078     TA::MemoryMgr::Free((pPtr));                                                            \
00079 }
00080 
00081 #endif
00082 
00083 namespace TA
00084 {
00085 
00086 TA_OBFUSCATION_RESERVED_ON
00087 class TACOMMON_CLASS MemoryMgr
00088 {
00089 public:
00090     typedef void* (TAC_CALL *FnAllocCallBack)(unsigned int nSize, unsigned int nAlignment);
00091     typedef void (TAC_CALL *FnFreeCallBack)(void* pData);
00092     typedef void* (TAC_CALL *FnDbgAllocCallBack)(unsigned int nSize, unsigned int nAlignment, const char * szFileName, int nLine);
00093     typedef void (TAC_CALL *FnDbgFreeCallBack)(void* pData, const char * szFileName, int nLine);
00094     
00095     static bool TAC_CALL SetAlloc(FnAllocCallBack pAllocCallBack);
00096     static bool TAC_CALL SetFree(FnFreeCallBack pFreeCallBack);
00097     static bool TAC_CALL SetDbgAlloc(FnDbgAllocCallBack pDbgAllocCallBack);
00098     static bool TAC_CALL SetDbgFree(FnDbgFreeCallBack pDbgFreeCallBack);
00099 
00100     static void* TAC_CALL DbgAlloc(unsigned int nSize, unsigned int nAlignment, const char * szFileName, int nLine);
00101     static void TAC_CALL DbgFree(void* pPtr, const char * szFileName, int nLine);
00102     static void* TAC_CALL Alloc(unsigned int nSize, unsigned int nAlignment);
00103     static void TAC_CALL Free(void* pPtr);
00104 
00105     static int TAC_CALL GetNumAllocationsUnFreed();
00106 TA_OBFUSCATION_RESERVED_OFF
00107 
00108 #ifdef TA_INTERNAL
00109     static void TAC_CALL Lock();
00110     static void TAC_CALL Unlock();
00111 #endif // TA_INTERNAL
00112 
00113 };
00114 
00115 #ifndef DOXYGEN
00116 #ifdef _DEBUG
00117     extern int s_nMemoryAllocLine;
00118     extern char* s_szMemoryAllocFileName;
00119 #endif // _DEBUG
00120 
00121 template <class Type>
00122 struct AllocateWrapper
00123 {
00124 public:
00125     Type m_data;
00126     void * TAC_CALL operator new[](unsigned int nSize)
00127     {
00128 #ifdef _DEBUG
00129         return TA::MemoryMgr::DbgAlloc(nSize, 16, s_szMemoryAllocFileName, s_nMemoryAllocLine);
00130 #else
00131         return TA::MemoryMgr::Alloc(nSize, 16);
00132 #endif
00133     }
00134     void TAC_CALL operator delete[](void* pData)
00135     {
00136 #ifdef _DEBUG
00137         TA::MemoryMgr::DbgFree(pData, s_szMemoryAllocFileName, s_nMemoryAllocLine);
00138 #else
00139         TA::MemoryMgr::Free(pData);
00140 #endif
00141     }
00142 };
00143 
00144 #endif // DOXYGEN
00145 
00146 }
00147 
00148 #endif // TA_MEMORYMGR_H


© Copyright 2004-2006 TRUE AXIS PTY LTD Australia. All rights reserved.