kmemory.h

Go to the documentation of this file.
00001 
00004 #ifndef FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KMEMORY_H
00005 #define FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KMEMORY_H
00006 
00007 /**************************************************************************************
00008 
00009  Copyright (C) 2001 - 2009 Autodesk, Inc. and/or its licensors.
00010  All Rights Reserved.
00011 
00012  The coded instructions, statements, computer programs, and/or related material 
00013  (collectively the "Data") in these files contain unpublished information 
00014  proprietary to Autodesk, Inc. and/or its licensors, which is protected by 
00015  Canada and United States of America federal copyright law and by international 
00016  treaties. 
00017  
00018  The Data may not be disclosed or distributed to third parties, in whole or in
00019  part, without the prior written consent of Autodesk, Inc. ("Autodesk").
00020 
00021  THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY.
00022  ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO
00023  WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR ARISING
00024  BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES OF TITLE, 
00025  NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR USE. 
00026  WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT WARRANT THAT THE OPERATION
00027  OF THE DATA WILL BE UNINTERRUPTED OR ERROR FREE. 
00028  
00029  IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS
00030  OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR EXPENSES
00031  OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE DAMAGES OR OTHER
00032  SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS OF PROFITS, REVENUE
00033  OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR DAMAGES OF ANY KIND),
00034  HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF LIABILITY, WHETHER DERIVED
00035  FROM CONTRACT, TORT (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE), OR OTHERWISE,
00036  ARISING OUT OF OR RELATING TO THE DATA OR ITS USE OR ANY OTHER PERFORMANCE,
00037  WHETHER OR NOT AUTODESK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS
00038  OR DAMAGE. 
00039 
00040 **************************************************************************************/
00041 
00042 #include <fbxfilesdk/fbxfilesdk_def.h>
00043 
00044 #include <string.h>
00045 
00046 #ifndef KARCH_ENV_MACOSX
00047     #include <stdlib.h>
00048 #endif
00049 
00050 #include <fbxfilesdk/fbxfilesdk_nsbegin.h>
00051 
00052 class KMpStack;
00053 class KMpMutex;
00054 #define KMEMORY_DEFAULTALIGNMENT    4
00055 
00059 #define kMalloc( pSize )                                                        FbxSdkMalloc( pSize )
00060 #define kRealloc( pOldMemBlock, pNewSize )                                      FbxSdkRealloc( pOldMemBlock, pNewSize )
00061 #define kFree(pAllocatedMemBlock)                                               FbxSdkFree(pAllocatedMemBlock)
00062 
00067 KFBX_DLL void* kMallocAligned( kSizeT pSize, kUInt pAlignment = KMEMORY_DEFAULTALIGNMENT);
00068 
00075 KFBX_DLL void* kReallocAligned( void* pAlignedPtr, kSizeT pSize, kUInt pAlignment = KMEMORY_DEFAULTALIGNMENT);
00076 
00081 KFBX_DLL void kFreeAligned( void* pAlignedPtr );
00082 
00083 
00087 // for debugging portability
00088 #define kMallocDbg( pSize, blockType, filename, linenumber )                    FbxSdkMalloc_Debug( pSize, blockType, filename, linenumber )
00089 #define kReallocDbg( pOldMemBlock, pNewSize, blockType, filename, linenumber )  FbxSdkRealloc_Debug( pOldMemBlock, pNewSize, blockType, filename, linenumber )
00090 #define kFreeDbg(pAllocatedMemBlock, blockType, filename, linenumber)           FbxSdkFree_Debug(pAllocatedMemBlock, blockType)
00091 
00092 KFBX_DLL void* kMallocAlignedDbg( kSizeT pSize, kUInt pAlignment, int blockType, const char *filename, int linenumber);
00093 KFBX_DLL void* kReallocAlignedDbg( void* pAlignedPtr, kSizeT pSize, kUInt pAlignment, int blockType, const char *filename, int linenumber);
00094 KFBX_DLL void  kFreeAlignedDbg( void* pAlignedPtr, int blockType, const char *filename, int linenumber );
00095 
00098 class KFBX_DLL KMemoryUsageCounter {
00099 public:
00100     KMemoryUsageCounter();
00101     ~KMemoryUsageCounter();
00102 #ifdef _DEBUG
00103     kInt64  GetMemoryUsage();
00104     kInt64  GetMallocCount();
00105     kInt64  GetReallocCount();
00106     kInt64  GetFreeCount();
00107     kInt64  GetMallocMemoryUsage();
00108     kInt64  GetReallocMemoryUsage();
00109     kInt64  GetFreeMemoryUsage();
00110 
00111     void CounterFree( void *pData );
00112     void CounterMalloc( size_t pValue );
00113     void CounterRealloc( void *pData,size_t pValue );
00114 #else
00115     // in Release the MemoryUsageCounter is not available
00116     inline kInt64 GetMemoryUsage()          { return 0; }
00117     inline kInt64 GetMallocCount()          { return 0; }
00118     inline kInt64 GetReallocCount()         { return 0; }
00119     inline kInt64 GetFreeCount()            { return 0; }
00120     inline kInt64 GetMallocMemoryUsage()    { return 0; }
00121     inline kInt64 GetReallocMemoryUsage()   { return 0; }
00122     inline kInt64 GetFreeMemoryUsage()      { return 0; }
00123 
00124     inline void CounterFree( void* )                {};
00125     inline void CounterMalloc( size_t )             {};
00126     inline void CounterRealloc( void* , size_t )    {};
00127 #endif
00128 
00129     KMemoryUsageCounter operator + (KMemoryUsageCounter& pSrc);
00130     KMemoryUsageCounter operator - (KMemoryUsageCounter& pSrc);
00131 
00132 private:
00133 #ifdef _DEBUG
00134     kInt64  mMemoryUsage;
00135     kInt64  mMallocCount;
00136     kInt64  mMallocMemoryUsage;
00137     kInt64  mReallocCount;
00138     kInt64  mReallocMemoryUsage;
00139     kInt64  mFreeCount;
00140     kInt64  mFreeMemoryUsage;
00141 #endif
00142 };
00143 
00146 class KFBX_DLL KMemoryAllocator : public KMemoryUsageCounter 
00147 {
00148 public:
00149     explicit KMemoryAllocator(const char *pId){}
00150     virtual ~KMemoryAllocator();
00151 public:
00152     virtual void    Purge() {}
00153 
00154     virtual void *  Malloc  (size_t pSize);
00155     virtual void *  Calloc  (size_t pCount,size_t pSize);
00156     virtual void *  Realloc (void* pData,size_t pSize);
00157     virtual void    Free    (void* pData);
00158 };
00159 
00160 #define KMEMORY_DEFAULTALIGNMENT    4
00161 
00164 class KFBX_DLL KMemoryReusePool : public KMemoryAllocator{
00165 
00166 public:
00167     // Public interface.
00168     explicit KMemoryReusePool(size_t pSizeRange = 1024 * 128, int pStrideShift = 2, int pAlignmentShift = 0, size_t pTotalAllocatedMax = 0);
00169     virtual ~KMemoryReusePool();
00170 
00171     // Allocation
00172 public:
00173     virtual void    Purge();
00174 
00175     virtual void *  Malloc  (size_t pSize);
00176     virtual void *  Calloc  (size_t pCount,size_t pSize);
00177     virtual void *  Realloc (void* pData,size_t pSize);
00178     virtual void    Free    (void* pData);
00179 protected:
00180     // Implementation.
00181     bool        SizeFitsInPool(size_t pSize) const { return (pSize >= GetHeaderSize() && pSize < mSizeRange); }
00182     bool        PoolIsFull() const { return (0 != mTotalAllocatedMax && mTotalAllocated > mTotalAllocatedMax); }
00183     kUChar      ComputeAlignmentOffset(void* pBuffer);
00184     static int  ComputeRightShiftRemainder(int pStrideShift);
00185 
00186 protected:
00187     struct KBlockHeader {
00188         size_t  mSize;
00189         kUChar  mAlignmentOffset;
00190     };
00191     void*           AssignHeaderAndReturnBlock(void* pBuffer, size_t pBufferSize);
00192     size_t          GetBufferAndSize(void* pBlock, void* & pBuffer);
00193     static size_t   GetHeaderSize();
00194 
00195 private:
00196     KMemoryReusePool(KMemoryReusePool const &);
00197     KMemoryReusePool & operator=(KMemoryReusePool const &);
00198 
00199 protected:
00200     // Fundamental data.
00201     KMpMutex *      mLock;              
00202     void* *         mBuffer;            
00203     size_t const    mSizeRange;         
00204     size_t const    mBufferSize;        
00205     int const       mStrideShift;       
00206     int const       mStrideRemainder;   
00207     int const       mAlignmentShift;    
00208     size_t const    mTotalAllocatedMax; 
00209     size_t          mTotalAllocated;    
00210     size_t          mTotalBlocks;       
00211 };
00212 
00215 class KFBX_DLL KMemoryPool {
00216 public:
00224     explicit KMemoryPool(kSizeT pBlockSize = 0,kUInt pBlockCount = 0,kUInt pAlignment = 0,bool pResizable = true, bool pSupportConcurrentAccess = true);
00225     ~KMemoryPool();
00226 
00233     void Init(  kSizeT pBlockSize,kUInt pBlockCount = 0,kUInt pAlignment = 0,bool pResizable = true );
00234 
00236     void Clear();
00237 
00241     void* Allocate();
00242 
00247     void Free( void* pMemBlock );
00248 
00249 private:
00250     void InitVars();
00251     void* Pop();
00252 
00253 private:
00254     kAtomic     mCountFreeBlock;
00255     void*       mFreeBlocks;
00256     kSizeT      mBlockSize;
00257     kUInt       mAlignment;
00258     bool        mResizable;
00259     bool        mSupportConcurrentAccess;
00260 };
00261 
00262 
00263 
00266 #if ( (defined(MEMORY_DEBUG) || defined(MEMORY_DEBUG_FBXFILESDK_MEMORY)) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00267 KFBX_DLL void KMemoryLeakInit();
00268 KFBX_DLL int KMemoryLeakDumpReport(_CrtMemState*);
00269 #endif
00270 
00271 #include <fbxfilesdk/fbxfilesdk_nsend.h>
00272 
00273 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KMEMORY_H
00274