00001
00004 #ifndef FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KMEMORY_H
00005 #define FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KMEMORY_H
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
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
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
00168 explicit KMemoryReusePool(size_t pSizeRange = 1024 * 128, int pStrideShift = 2, int pAlignmentShift = 0, size_t pTotalAllocatedMax = 0);
00169 virtual ~KMemoryReusePool();
00170
00171
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
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
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