kcontainerallocators.h

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  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 #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         // By default, ignore all preallocating requests.
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     // Use default copy / assignment
00103     /*    operator=()
00104         *        The allocator must have appropriate copy semantics; does not need to
00105         *        copy its allocated blocks, but it should adjust its record size.
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         // "Hungry": release memory only when the allocator is destroyed.
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             // The next call to AllocateRecords() may skip over currently reserved
00194             // records if the size changes drastically, but otherwise GetChunk()
00195             // is size-oblivious.
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