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/components/kbaselib/kbaselib_h.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_DEFAULTALIGNEMENT 4
00055
00059 #define kMalloc( pSize ) malloc( pSize )
00060 #define kRealloc( pOldMemBlock, pNewSize ) realloc( pOldMemBlock, pNewSize )
00061 #define kFree(pAllocatedMemBlock) free(pAllocatedMemBlock)
00062
00067 KFBX_DLL void* kMallocAligned( kSizeT pSize, kUInt pAlignement = KMEMORY_DEFAULTALIGNEMENT);
00068
00075 KFBX_DLL void* kReallocAligned( void* pAlignedPtr, kSizeT pSize, kUInt pAlignement = KMEMORY_DEFAULTALIGNEMENT);
00076
00081 KFBX_DLL void kFreeAligned( void* pAlignedPtr );
00082
00083
00087
00088 #ifndef _INC_CRTDBG
00089 #define _NORMAL_BLOCK 1
00090 #define _malloc_dbg(size, blockType, filename, linenumber) malloc(size)
00091 #define _realloc_dbg(ptr, size, blockType, filename, linenumber) realloc(ptr, size)
00092 #define _free_dbg(ptr, blockType) free(ptr)
00093 #endif
00094 #define kMallocDbg( pSize, blockType, filename, linenumber ) _malloc_dbg( pSize, blockType, filename, linenumber )
00095 #define kReallocDbg( pOldMemBlock, pNewSize, blockType, filename, linenumber ) _realloc_dbg( pOldMemBlock, pNewSize, blockType, filename, linenumber )
00096 #define kFreeDbg(pAllocatedMemBlock, blockType, filename, linenumber) _free_dbg(pAllocatedMemBlock, blockType)
00097 KFBX_DLL void* kMallocAlignedDbg( kSizeT pSize, kUInt pAlignement, int blockType, const char *filename, int linenumber);
00098 KFBX_DLL void* kReallocAlignedDbg( void* pAlignedPtr, kSizeT pSize, kUInt pAlignement, int blockType, const char *filename, int linenumber);
00099 KFBX_DLL void kFreeAlignedDbg( void* pAlignedPtr, int blockType, const char *filename, int linenumber );
00100
00103 class KFBX_DLL KMemoryUsageCounter {
00104 public:
00105 KMemoryUsageCounter();
00106 ~KMemoryUsageCounter();
00107 #ifdef _DEBUG
00108 kInt64 GetMemoryUsage();
00109 kInt64 GetMallocCount();
00110 kInt64 GetReallocCount();
00111 kInt64 GetFreeCount();
00112 kInt64 GetMallocMemoryUsage();
00113 kInt64 GetReallocMemoryUsage();
00114 kInt64 GetFreeMemoryUsage();
00115
00116 void CounterFree( void *pData );
00117 void CounterMalloc( size_t pValue );
00118 void CounterRealloc( void *pData,size_t pValue );
00119 #else
00120
00121 inline kInt64 GetMemoryUsage() { return 0; }
00122 inline kInt64 GetMallocCount() { return 0; }
00123 inline kInt64 GetReallocCount() { return 0; }
00124 inline kInt64 GetFreeCount() { return 0; }
00125 inline kInt64 GetMallocMemoryUsage() { return 0; }
00126 inline kInt64 GetReallocMemoryUsage() { return 0; }
00127 inline kInt64 GetFreeMemoryUsage() { return 0; }
00128
00129 inline void CounterFree( void* ) {};
00130 inline void CounterMalloc( size_t ) {};
00131 inline void CounterRealloc( void* , size_t ) {};
00132 #endif
00133
00134 KMemoryUsageCounter operator + (KMemoryUsageCounter& pSrc);
00135 KMemoryUsageCounter operator - (KMemoryUsageCounter& pSrc);
00136
00137 private:
00138 #ifdef _DEBUG
00139 kInt64 mMemoryUsage;
00140 kInt64 mMallocCount;
00141 kInt64 mMallocMemoryUsage;
00142 kInt64 mReallocCount;
00143 kInt64 mReallocMemoryUsage;
00144 kInt64 mFreeCount;
00145 kInt64 mFreeMemoryUsage;
00146 #endif
00147 };
00148
00151 class KFBX_DLL KMemoryAllocator : public KMemoryUsageCounter
00152 {
00153 public:
00154 explicit KMemoryAllocator(const char *pId){}
00155 ~KMemoryAllocator();
00156 public:
00157 virtual void Purge() {}
00158
00159 virtual void * Malloc (size_t pSize);
00160 virtual void * Calloc (size_t pCount,size_t pSize);
00161 virtual void * Realloc (void* pData,size_t pSize);
00162 virtual void Free (void* pData);
00163 };
00164
00165 #define KMEMORY_DEFAULTALIGNEMENT 4
00166
00169 class KFBX_DLL KMemoryReusePool : public KMemoryAllocator{
00170
00171 public:
00172
00173 explicit KMemoryReusePool(size_t pSizeRange = 1024 * 128, int pStrideShift = 2, int pAlignmentShift = 0, size_t pTotalAllocatedMax = 0);
00174 ~KMemoryReusePool();
00175
00176
00177 public:
00178 virtual void Purge();
00179
00180 virtual void * Malloc (size_t pSize);
00181 virtual void * Calloc (size_t pCount,size_t pSize);
00182 virtual void * Realloc (void* pData,size_t pSize);
00183 virtual void Free (void* pData);
00184 protected:
00185
00186 bool SizeFitsInPool(size_t pSize) const { return (pSize >= GetHeaderSize() && pSize < mSizeRange); }
00187 bool PoolIsFull() const { return (0 != mTotalAllocatedMax && mTotalAllocated > mTotalAllocatedMax); }
00188 kUChar ComputeAlignmentOffset(void* pBuffer);
00189 static int ComputeRightShiftRemainder(int pStrideShift);
00190
00191 protected:
00192 struct KBlockHeader {
00193 size_t mSize;
00194 kUChar mAlignmentOffset;
00195 };
00196 void* AssignHeaderAndReturnBlock(void* pBuffer, size_t pBufferSize);
00197 size_t GetBufferAndSize(void* pBlock, void* & pBuffer);
00198 static size_t GetHeaderSize();
00199
00200 private:
00201 KMemoryReusePool(KMemoryReusePool const &);
00202 KMemoryReusePool & operator=(KMemoryReusePool const &);
00203
00204 protected:
00205
00206 KMpMutex * mLock;
00207 void* * mBuffer;
00208 size_t const mSizeRange;
00209 size_t const mBufferSize;
00210 int const mStrideShift;
00211 int const mStrideRemainder;
00212 int const mAlignmentShift;
00213 size_t const mTotalAllocatedMax;
00214 size_t mTotalAllocated;
00215 size_t mTotalBlocks;
00216 };
00217
00220 class KFBX_DLL KMemoryPool {
00221 public:
00228 explicit KMemoryPool( kSizeT pBlockSize = 0,kUInt pBlockCount = 0,kUInt pAlignment = 0,bool pResizable = true );
00229 ~KMemoryPool();
00230
00237 void Init( kSizeT pBlockSize,kUInt pBlockCount = 0,kUInt pAlignement = 0,bool pResizable = true );
00238
00240 void Clear();
00241
00245 void* Allocate();
00246
00251 void Free( void* pMemBlock );
00252
00253 private:
00254 void InitVars();
00255
00256 private:
00257 kAtomic mCountFreeBlock;
00258 KMpStack* mFreeBlocks;
00259 kSizeT mBlockSize;
00260 kUInt mAlignment;
00261 bool mResizable;
00262 };
00263
00264
00265
00268 #if ( (defined(MEMORY_DEBUG) || defined(MEMORY_DEBUG_FBXFILESDK_MEMORY)) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00269 KFBX_DLL void KMemoryLeakInit();
00270 KFBX_DLL void KMemoryLeakDumpReport();
00271 #endif
00272
00273 #include <fbxfilesdk/fbxfilesdk_nsend.h>
00274
00275 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KMEMORY_H
00276