Go to
the documentation of this file.
00001
00004 #ifndef FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KCONTAINERALLOCATORS_H
00005 #define FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KCONTAINERALLOCATORS_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 #include <fbxfilesdk/fbxfilesdk_def.h>
00042
00043 #include <fbxfilesdk/components/kbaselib/klib/kdebug.h>
00044
00045 #include <fbxfilesdk/fbxfilesdk_nsbegin.h>
00046
00051 class KFBX_DLL KBaseAllocator
00052 {
00053 public:
00060 KBaseAllocator(size_t const pRecordSize)
00061 : mRecordSize(pRecordSize)
00062 {
00063 }
00064
00072 void Reserve(size_t const pRecordCount)
00073 {
00074
00075 }
00076
00082 void* AllocateRecords(size_t const pRecordCount = 1)
00083 {
00084 return FbxSdkMalloc(pRecordCount * mRecordSize);
00085 }
00086
00090 void FreeMemory(void* pRecord)
00091 {
00092 FbxSdkFree(pRecord);
00093 }
00094
00097 size_t GetRecordSize() const
00098 {
00099 return mRecordSize;
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 private:
00109 size_t mRecordSize;
00110 };
00111
00117 class KHungryAllocator
00118 {
00119 public:
00120 KHungryAllocator(size_t pRecordSize)
00121 : mRecordSize(pRecordSize)
00122 , mData(NULL)
00123 , mRecordPoolSize(0)
00124 {
00125 }
00126
00127 KHungryAllocator(const KHungryAllocator& pOther)
00128 : mRecordSize(pOther.mRecordSize)
00129 , mData(0)
00130 , mRecordPoolSize(pOther.mRecordPoolSize)
00131 {
00132 }
00133
00134 ~KHungryAllocator()
00135 {
00136 MemoryBlock* lCurrent = mData;
00137 MemoryBlock* lNext = lCurrent ? lCurrent->mNextBlock : 0;
00138 while (lCurrent)
00139 {
00140 FbxSdkDelete(lCurrent);
00141 lCurrent = lNext;
00142 lNext = lCurrent ? lCurrent->mNextBlock : 0;
00143 }
00144 }
00145
00146 void Reserve(size_t const pRecordCount)
00147 {
00148 MemoryBlock* lMem = FbxSdkNew< MemoryBlock >(pRecordCount * mRecordSize);
00149 lMem->mNextBlock = mData;
00150 mData = lMem;
00151 mRecordPoolSize += pRecordCount;
00152 }
00153
00154 void* AllocateRecords(size_t const pRecordCount = 1)
00155 {
00156 MemoryBlock* lBlock = mData;
00157 void* lRecord = NULL;
00158
00159 while ((lBlock != NULL) &&
00160 ((lRecord = lBlock->GetChunk(pRecordCount * mRecordSize)) == NULL))
00161 {
00162 lBlock = lBlock->mNextBlock;
00163 }
00164
00165 if (lRecord == NULL)
00166 {
00167 size_t lNumRecordToAllocate = mRecordPoolSize / 8 == 0 ? 2 : mRecordPoolSize / 8;
00168 if (lNumRecordToAllocate < pRecordCount)
00169 {
00170 lNumRecordToAllocate = pRecordCount;
00171 }
00172 Reserve(lNumRecordToAllocate);
00173 lRecord = AllocateRecords(pRecordCount);
00174 }
00175
00176 return lRecord;
00177 }
00178
00179 void FreeMemory(void* pRecord)
00180 {
00181
00182 }
00183
00184 size_t GetRecordSize() const
00185 {
00186 return mRecordSize;
00187 }
00188
00189 KHungryAllocator& operator=(const KHungryAllocator& pOther)
00190 {
00191 if( this != &pOther )
00192 {
00193
00194
00195
00196 if( mRecordSize < pOther.mRecordSize )
00197 {
00198 mRecordPoolSize = 0;
00199 }
00200
00201 mRecordSize = pOther.mRecordSize;
00202 }
00203
00204 return(*this);
00205 }
00206
00207 private:
00208 class MemoryBlock
00209 {
00210 public:
00211 MemoryBlock(size_t pSize)
00212 : mNextBlock(NULL)
00213 , mData(NULL)
00214 , mFreeData(NULL)
00215 , mEnd(NULL)
00216 {
00217 mData = FbxSdkMalloc(pSize);
00218 mFreeData = mData;
00219 mEnd = reinterpret_cast<char*>(mData) + pSize;
00220 };
00221
00222 ~MemoryBlock()
00223 {
00224 FbxSdkFree(mData);
00225 }
00226
00227 void* GetChunk(size_t const pSize)
00228 {
00229 if (reinterpret_cast<char*>(mFreeData) + pSize < mEnd)
00230 {
00231 void* lChunk = mFreeData;
00232 mFreeData = reinterpret_cast<char*>(mFreeData) + pSize;
00233 return lChunk;
00234 }
00235
00236 return NULL;
00237 }
00238
00239 MemoryBlock* mNextBlock;
00240 void* mData;
00241 void* mFreeData;
00242 void* mEnd;
00243 };
00244
00245 size_t mRecordSize;
00246 MemoryBlock* mData;
00247 size_t mRecordPoolSize;
00248 };
00249
00250 #include <fbxfilesdk/fbxfilesdk_nsend.h>
00251
00252 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KCONTAINERALLOCATORS_H
00253