00001
00002
00003
00004
00005
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 #ifndef FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KMEMORY_H
00036 #define FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KMEMORY_H
00037
00038 #include <fbxfilesdk/components/kbaselib/kbaselib_h.h>
00039
00040 #include <string.h>
00041
00042 #ifndef KARCH_ENV_MACOSX
00043 #include <stdlib.h>
00044 #endif
00045
00046 #include <fbxfilesdk/fbxfilesdk_nsbegin.h>
00047
00048 class KMpStack;
00049 class KMpMutex;
00050 #define KMEMORY_DEFAULTALIGNEMENT 4
00051
00055 #define kMalloc( pSize ) malloc( pSize )
00056 #define kRealloc( pOldMemBlock, pNewSize ) realloc( pOldMemBlock, pNewSize )
00057 #define kFree(pAllocatedMemBlock) free(pAllocatedMemBlock)
00058
00063 KFBX_DLL void* kMallocAligned( kSizeT pSize, kUInt pAlignement = KMEMORY_DEFAULTALIGNEMENT);
00064
00071 KFBX_DLL void* kReallocAligned( void* pAlignedPtr, kSizeT pSize, kUInt pAlignement = KMEMORY_DEFAULTALIGNEMENT);
00072
00074 KFBX_DLL void kFreeAligned( void* pAlignedPtr );
00075
00076
00080
00081 #ifndef _INC_CRTDBG
00082 #define _NORMAL_BLOCK 1
00083 #define _malloc_dbg(size, blockType, filename, linenumber) malloc(size)
00084 #define _realloc_dbg(ptr, size, blockType, filename, linenumber) realloc(ptr, size)
00085 #define _free_dbg(ptr, blockType) free(ptr)
00086 #endif
00087 #define kMallocDbg( pSize, blockType, filename, linenumber ) _malloc_dbg( pSize, blockType, filename, linenumber )
00088 #define kReallocDbg( pOldMemBlock, pNewSize, blockType, filename, linenumber ) _realloc_dbg( pOldMemBlock, pNewSize, blockType, filename, linenumber )
00089 #define kFreeDbg(pAllocatedMemBlock, blockType, filename, linenumber) _free_dbg(pAllocatedMemBlock, blockType)
00090 KFBX_DLL void* kMallocAlignedDbg( kSizeT pSize, kUInt pAlignement, int blockType, const char *filename, int linenumber);
00091 KFBX_DLL void* kReallocAlignedDbg( void* pAlignedPtr, kSizeT pSize, kUInt pAlignement, int blockType, const char *filename, int linenumber);
00092 KFBX_DLL void kFreeAlignedDbg( void* pAlignedPtr, int blockType, const char *filename, int linenumber );
00093
00094
00095
00096
00097 class KFBX_DLL KMemoryUsageCounter {
00098 public:
00099 KMemoryUsageCounter();
00100 ~KMemoryUsageCounter();
00101
00102 kInt64 GetMemoryUsage();
00103 kInt64 GetMallocCount();
00104 kInt64 GetReallocCount();
00105 kInt64 GetFreeCount();
00106 kInt64 GetMallocMemoryUsage();
00107 kInt64 GetReallocMemoryUsage();
00108 kInt64 GetFreeMemoryUsage();
00109
00110 void CounterFree( void *pData );
00111 void CounterMalloc( size_t pValue );
00112 void CounterRealloc( void *pData,size_t pValue );
00113
00114 KMemoryUsageCounter operator + (KMemoryUsageCounter& pSrc);
00115 KMemoryUsageCounter operator - (KMemoryUsageCounter& pSrc);
00116
00117 private:
00118 kInt64 mMemoryUsage;
00119 kInt64 mMallocCount;
00120 kInt64 mMallocMemoryUsage;
00121 kInt64 mReallocCount;
00122 kInt64 mReallocMemoryUsage;
00123 kInt64 mFreeCount;
00124 kInt64 mFreeMemoryUsage;
00125 };
00126
00127
00128
00129
00130 class KFBX_DLL KMemoryAllocator : public KMemoryUsageCounter
00131 {
00132 public:
00133 explicit KMemoryAllocator(const char *pId){}
00134 ~KMemoryAllocator();
00135 public:
00136 virtual void Purge() {}
00137
00138 virtual void * Malloc (size_t pSize);
00139 virtual void * Calloc (size_t pCount,size_t pSize);
00140 virtual void * Realloc (void* pData,size_t pSize);
00141 virtual void Free (void* pData);
00142
00143
00144
00145
00146
00147
00148 };
00149
00150 #define KMEMORY_DEFAULTALIGNEMENT 4
00151
00152
00153
00154
00155
00156 class KFBX_DLL KMemoryReusePool : public KMemoryAllocator{
00157
00158 public:
00159
00160 explicit KMemoryReusePool(size_t pSizeRange = 1024 * 128, int pStrideShift = 2, int pAlignmentShift = 0, size_t pTotalAllocatedMax = 0);
00161 ~KMemoryReusePool();
00162
00163
00164 public:
00165 virtual void Purge();
00166
00167 virtual void * Malloc (size_t pSize);
00168 virtual void * Calloc (size_t pCount,size_t pSize);
00169 virtual void * Realloc (void* pData,size_t pSize);
00170 virtual void Free (void* pData);
00171
00172
00173
00174
00175
00176
00177
00178 protected:
00179
00180 bool SizeFitsInPool(size_t pSize) const { return (pSize >= GetHeaderSize() && pSize < mSizeRange); }
00181 bool PoolIsFull() const { return (0 != mTotalAllocatedMax && mTotalAllocated > mTotalAllocatedMax); }
00182 kUChar ComputeAlignmentOffset(void* pBuffer);
00183 static int ComputeRightShiftRemainder(int pStrideShift);
00184
00185 protected:
00186 struct KBlockHeader {
00187 size_t mSize;
00188 kUChar mAlignmentOffset;
00189 };
00190 void* AssignHeaderAndReturnBlock(void* pBuffer, size_t pBufferSize);
00191 size_t GetBufferAndSize(void* pBlock, void* & pBuffer);
00192 static size_t GetHeaderSize();
00193
00194 private:
00195 KMemoryReusePool(KMemoryReusePool const &);
00196 KMemoryReusePool & operator=(KMemoryReusePool const &);
00197
00198 protected:
00199
00200 KMpMutex * mLock;
00201 void* * mBuffer;
00202 size_t const mSizeRange;
00203 size_t const mBufferSize;
00204 int const mStrideShift;
00205 int const mStrideRemainder;
00206 int const mAlignmentShift;
00207 size_t const mTotalAllocatedMax;
00208 size_t mTotalAllocated;
00209 size_t mTotalBlocks;
00210 };
00211
00212
00213
00214
00215 class KFBX_DLL KMemoryPool {
00216 public:
00217 explicit KMemoryPool( kSizeT pBlockSize = 0,kUInt pBlockCount = 0,kUInt pAlignement = 0,bool pResizable = true );
00218 ~KMemoryPool();
00219
00225 void Init( kSizeT pBlockSize,kUInt pBlockCount = 0,kUInt pAlignement = 0,bool pResizable = true );
00226
00228 void Clear();
00229
00233 void* Allocate();
00234
00239 void Free( void* pMemBlock );
00240
00241 private:
00242 void InitVars();
00243
00244 private:
00245 kAtomic mCountFreeBlock;
00246 KMpStack* mFreeBlocks;
00247 kSizeT mBlockSize;
00248 kUInt mAlignement;
00249 bool mResizable;
00250 };
00251
00252
00253
00254
00255
00256
00257
00258 #if ( (defined(MEMORY_DEBUG) || defined(MEMORY_DEBUG_FBXFILESDK_MEMORY)) && defined(_DEBUG) && defined(KARCH_ENV_WIN32))
00259 KFBX_DLL void KMemoryLeakInit();
00260 KFBX_DLL void KMemoryLeakDumpReport();
00261 #endif
00262
00263 #include <fbxfilesdk/fbxfilesdk_nsend.h>
00264
00265 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KMEMORY_H
00266