fbxalloc.h

Go to the documentation of this file.
00001 /****************************************************************************************
00002  
00003    Copyright (C) 2013 Autodesk, Inc.
00004    All rights reserved.
00005  
00006    Use of this software is subject to the terms of the Autodesk license agreement
00007    provided at the time of installation or download, or which otherwise accompanies
00008    this software in either electronic or hard copy form.
00009  
00010 ****************************************************************************************/
00011 
00020 #ifndef _FBXSDK_CORE_ARCH_ALLOC_H_
00021 #define _FBXSDK_CORE_ARCH_ALLOC_H_
00022 
00023 #include <fbxsdk/fbxsdk_def.h>
00024 
00025 #if defined(_DEBUG) && defined(FBXSDK_ENV_WIN)
00026     #include <crtdbg.h>
00027 #endif
00028 
00029 #if defined(FBXSDK_ALLOC_DEBUG)
00030     #if defined(FBXSDK_ENV_MAC)
00031         #include <malloc/malloc.h>
00032     #else
00033         #include <malloc.h>
00034     #endif
00035 #endif
00036 
00037 #include <fbxsdk/fbxsdk_nsbegin.h>
00038 
00039 #define FBXSDK_MEMORY_ALIGNMENT ((size_t)8U)
00040 
00041 typedef void*   (*FbxMallocProc)(size_t);           
00042 typedef void*   (*FbxCallocProc)(size_t, size_t);   
00043 typedef void*   (*FbxReallocProc)(void*, size_t);   
00044 typedef void    (*FbxFreeProc)(void*);              
00045 
00048 FBXSDK_DLL void FbxSetMallocHandler(FbxMallocProc pHandler);
00049 
00052 FBXSDK_DLL void FbxSetCallocHandler(FbxCallocProc pHandler);
00053 
00056 FBXSDK_DLL void FbxSetReallocHandler(FbxReallocProc pHandler);
00057 
00060 FBXSDK_DLL void FbxSetFreeHandler(FbxFreeProc pHandler);
00061 
00064 FBXSDK_DLL FbxMallocProc FbxGetMallocHandler();
00065 
00068 FBXSDK_DLL FbxCallocProc FbxGetCallocHandler();
00069 
00072 FBXSDK_DLL FbxReallocProc FbxGetReallocHandler();
00073 
00076 FBXSDK_DLL FbxFreeProc FbxGetFreeHandler();
00077 
00080 FBXSDK_DLL FbxMallocProc FbxGetDefaultMallocHandler();
00081 
00084 FBXSDK_DLL FbxCallocProc FbxGetDefaultCallocHandler();
00085 
00088 FBXSDK_DLL FbxReallocProc FbxGetDefaultReallocHandler();
00089 
00092 FBXSDK_DLL FbxFreeProc FbxGetDefaultFreeHandler();
00093 
00094 /*****************************************************************************************************************************
00095 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
00096 *****************************************************************************************************************************/
00097 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00098     FBXSDK_DLL void* FbxMalloc(size_t pSize);
00099     FBXSDK_DLL void* FbxCalloc(size_t pCount, size_t pSize);
00100     FBXSDK_DLL void* FbxRealloc(void* pData, size_t pSize);
00101     FBXSDK_DLL void FbxFree(void* pData);
00102     FBXSDK_DLL void* FbxMallocAligned(size_t pSize);
00103     FBXSDK_DLL char* FbxStrDup(const char* pString);
00104     FBXSDK_DLL wchar_t* FbxStrDupWC(const wchar_t* pString);
00105 
00106     //These versions of allocators use the default system mallocs, and on Windows we also pass the debugging parameters.
00107     //If you define FBXSDK_ALLOC_DEBUG in your project, the FBX SDK will use these debug versions everywhere.
00108     FBXSDK_DLL void* FbxMallocDebug(size_t pSize, int pBlock, const char* pFile, int pLine);
00109     FBXSDK_DLL void* FbxCallocDebug(size_t pCount, size_t pSize, int pBlock, const char* pFile, int pLine);
00110     FBXSDK_DLL void* FbxReallocDebug(void* pData, size_t pSize, int pBlock, const char* pFile, int pLine);
00111     FBXSDK_DLL void FbxFreeDebug(void* pData, int pBlock);
00112 
00113     //When FBXSDK_ALLOC_DEBUG is defined, redirect allocation calls to the debug version.
00114     #if defined(FBXSDK_ALLOC_DEBUG)
00115         #define FbxMalloc(s) FbxMallocDebug(s, _NORMAL_BLOCK, __FILE__, __LINE__)
00116         #define FbxCalloc(c, s) FbxCallocDebug(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
00117         #define FbxRealloc(p, s) FbxReallocDebug(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
00118         #define FbxFree(p) FbxFreeDebug(p, _NORMAL_BLOCK)
00119     #endif
00120 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
00121 
00123 template <class Type> class FbxDeletionPolicyDefault
00124 {
00125 public:
00127     static inline void DeleteIt(Type** pPtr)
00128     {
00129         if( *pPtr )
00130         {
00131             delete *pPtr;
00132             *pPtr = NULL;
00133         }
00134     }
00135 };
00136 
00138 template <class Type> class FbxDeletionPolicyDelete
00139 {
00140 public:
00142     static inline void DeleteIt(Type** mPtr)
00143     {
00144         if( *mPtr )
00145         {
00146             FbxDelete(*mPtr);
00147             *mPtr = NULL;
00148         }
00149     }
00150 };
00151 
00153 template <class Type> class FbxDeletionPolicyFree
00154 {
00155 public:
00157     static inline void DeleteIt(Type** pPtr)
00158     {
00159         if( *pPtr )
00160         {
00161             FbxFree(*pPtr);
00162             *pPtr = NULL;
00163         }
00164     }
00165 };
00166 
00168 template <class Type> class FbxDeletionPolicyObject
00169 {
00170 public:
00172     static inline void DeleteIt(Type** pPtr)
00173     {
00174         if( *pPtr )
00175         {
00176             (*pPtr)->Destroy();
00177             *pPtr = NULL;
00178         }
00179     }
00180 };
00181 
00185 template<class Type, class Policy=FbxDeletionPolicyDefault<Type> > class FbxAutoPtr
00186 {
00187 public:
00189     explicit FbxAutoPtr(Type* pPtr=0) : mPtr(pPtr){}
00190 
00192     ~FbxAutoPtr() { Policy::DeleteIt(&mPtr); }
00193 
00195     inline Type* Get() const { return mPtr; }
00196 
00198     inline Type* operator->() const { return mPtr; }
00199 
00201     inline operator Type* () const { return mPtr; }
00202 
00204     inline Type& operator*() const { return *mPtr; }
00205 
00207     inline bool operator!() const { return mPtr == 0; }
00208 
00210     inline operator bool () const { return mPtr != 0; }
00211 
00213     inline void Reset(Type* pPtr=0)
00214     {
00215         FBX_ASSERT(pPtr == 0 || pPtr != mPtr);  //Catch self-reset errors
00216         FbxAutoPtr<Type, Policy>(pPtr).Swap(*this);
00217     }
00218 
00220     inline void Swap(FbxAutoPtr& pOther)
00221     {
00222         Type* TmpPtr = pOther.mPtr;
00223         pOther.mPtr = mPtr;
00224         mPtr = TmpPtr;
00225     }
00226 
00228     inline Type* Release()
00229     {
00230         Type* TmpPtr = mPtr;
00231         mPtr = NULL;
00232         return TmpPtr;
00233     }
00234 
00235 /*****************************************************************************************************************************
00236 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
00237 *****************************************************************************************************************************/
00238 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00239 private:
00240     FbxAutoPtr(const FbxAutoPtr&);
00241     FbxAutoPtr& operator=(const FbxAutoPtr&);
00242 
00243     Type* mPtr;
00244 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
00245 };
00246 
00248 template <class Type> class FbxAutoFreePtr : public FbxAutoPtr<Type, FbxDeletionPolicyFree<Type> >
00249 {
00250 public:
00252     explicit FbxAutoFreePtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyFree<Type> >(pPtr){}
00253 };
00254 
00256 template <class Type> class FbxAutoDeletePtr : public FbxAutoPtr<Type, FbxDeletionPolicyDelete<Type> >
00257 {
00258 public:
00260     explicit FbxAutoDeletePtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyDelete<Type> >(pPtr){}
00261 };
00262 
00264 template <class Type> class FbxAutoDestroyPtr : public FbxAutoPtr<Type, FbxDeletionPolicyObject<Type> >
00265 {
00266 public:
00268     explicit FbxAutoDestroyPtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyObject<Type> >(pPtr){}
00269 };
00270 
00271 #include <fbxsdk/fbxsdk_nsend.h>
00272 
00273 #endif /* _FBXSDK_CORE_ARCH_ALLOC_H_ */