Main Page Modules Class Hierarchy Data Structures File List Data Fields Globals Related Pages

# oscl_mem_mempool.h

Go to the documentation of this file.

    00001 // -*- c++ -*-
    00002 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    00003
    00004 //               O S C L _ M E M _ M E M P O O L
    00005
    00006 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    00007
    00019 #ifndef OSCL_MEM_MEMPOOL_H_INCLUDED
    00020 #define OSCL_MEM_MEMPOOL_H_INCLUDED
    00021
    00022 #ifndef OSCL_MEM_H_INCLUDED
    00023 #include "oscl_mem.h"
    00024 #endif
    00025
    00026 #ifndef OSCL_DEFALLOC_H_INCLUDED
    00027 #include "oscl_defalloc.h"
    00028 #endif
    00029
    00030 #ifndef OSCL_VECTOR_H_INCLUDED
    00031 #include "oscl_vector.h"
    00032 #endif
    00033
    00034
    00041 class OsclMemPoolFixedChunkAllocatorObserver
    00042 {
    00043     public:
    00044         virtual void freechunkavailable(OsclAny* aContextData) = 0;
    00045         virtual ~OsclMemPoolFixedChunkAllocatorObserver() {}
    00046 };
    00047
    00048
    00049 class OsclMemPoolFixedChunkAllocator : public Oscl_DefAlloc
    00050 {
    00051     public:
    00062         OSCL_IMPORT_REF OsclMemPoolFixedChunkAllocator(const uint32 numchunk = 1, const uint32 chunksize = 0, Oscl_DefAlloc* gen_alloc = NULL, const uint32 chunkalignment = 0);
    00063
    00071         OSCL_IMPORT_REF virtual void enablenullpointerreturn();
    00072
    00075         virtual ~OsclMemPoolFixedChunkAllocator()
    00076         {
    00077             // Decrement the ref count
    00078             --iRefCount;
    00079
    00080             // If ref count reaches 0 then destroy this object
    00081             if (iRefCount <= 0)
    00082             {
    00083                 destroymempool();
    00084             }
    00085         }
    00086
    00095         OSCL_IMPORT_REF virtual OsclAny* allocate(const uint32 n);
    00096
    00103         OSCL_IMPORT_REF virtual void deallocate(OsclAny* p);
    00104
    00111         OSCL_IMPORT_REF virtual void notifyfreechunkavailable(OsclMemPoolFixedChunkAllocatorObserver& obs, OsclAny* aContextData = NULL);
    00112
    00118         OSCL_IMPORT_REF virtual void CancelFreeChunkAvailableCallback();
    00119
    00125         OSCL_IMPORT_REF void addRef();
    00126
    00133         OSCL_IMPORT_REF void removeRef();
    00134
    00135     protected:
    00136         OSCL_IMPORT_REF virtual void createmempool();
    00137         OSCL_IMPORT_REF virtual void destroymempool();
    00138
    00139         uint32 iNumChunk;
    00140         uint32 iChunkSize;
    00141         uint32 iChunkSizeMemAligned;
    00142         uint32 iChunkAlignment;
    00143         Oscl_DefAlloc* iMemPoolAllocator;
    00144         OsclAny* iMemPool;
    00145         OsclAny* iMemPoolAligned;
    00146
    00147         Oscl_Vector<OsclAny*, OsclMemAllocator> iFreeMemChunkList;
    00148
    00149         bool iCheckNextAvailableFreeChunk;
    00150         OsclMemPoolFixedChunkAllocatorObserver* iObserver;
    00151         OsclAny* iNextAvailableContextData;
    00152
    00153         int32 iRefCount;
    00154         bool iEnableNullPtrReturn;
    00155 };
    00156
    00157
    00167 class OsclMemPoolResizableAllocatorObserver
    00168 {
    00169     public:
    00170         virtual void freeblockavailable(OsclAny* aContextData) = 0;
    00171         virtual ~OsclMemPoolResizableAllocatorObserver() {}
    00172 };
    00173
    00174 class OsclMemPoolResizableAllocatorMemoryObserver
    00175 {
    00176     public:
    00177         virtual void freememoryavailable(OsclAny* aContextData) = 0;
    00178         virtual ~OsclMemPoolResizableAllocatorMemoryObserver() {}
    00179 };
    00180
    00181 class OsclMemPoolResizableAllocator : public Oscl_DefAlloc
    00182 {
    00183     public:
    00196         OSCL_IMPORT_REF OsclMemPoolResizableAllocator(uint32 aMemPoolBufferSize, uint32 aMemPoolBufferNumLimit = 0, uint32 aExpectedNumBlocksPerBuffer = 0, Oscl_DefAlloc* gen_alloc = NULL);
    00197
    00205         OSCL_IMPORT_REF virtual void enablenullpointerreturn();
    00206
    00214         OSCL_IMPORT_REF virtual OsclAny* allocate(const uint32 aNumBytes);
    00215
    00222         OSCL_IMPORT_REF virtual void deallocate(OsclAny* aPtr);
    00223
    00236         OSCL_IMPORT_REF virtual bool trim(OsclAny* aPtr, uint32 aBytesToFree);
    00237
    00240         OSCL_IMPORT_REF uint32 getBufferSize() const;
    00241
    00245         OSCL_IMPORT_REF virtual uint32 getAllocatedSize() const;
    00246
    00249         OSCL_IMPORT_REF virtual uint32 getAvailableSize() const;
    00250
    00253         OSCL_IMPORT_REF virtual uint32 getLargestContiguousFreeBlockSize() const;
    00254
    00255         OSCL_IMPORT_REF virtual bool setMaxSzForNewMemPoolBuffer(uint32 aMaxNewMemPoolBufferSz);
    00256
    00268         OSCL_IMPORT_REF virtual void notifyfreeblockavailable(OsclMemPoolResizableAllocatorObserver& aObserver, uint32 aRequestedSize = 0, OsclAny* aContextData = NULL);
    00269
    00275         OSCL_IMPORT_REF virtual void CancelFreeChunkAvailableCallback();
    00276
    00277         OSCL_IMPORT_REF virtual void notifyfreememoryavailable(OsclMemPoolResizableAllocatorMemoryObserver& aObserver, uint32 aRequestedSize = 0, OsclAny* aContextData = NULL);
    00278         OSCL_IMPORT_REF void CancelFreeMemoryAvailableCallback();
    00279
    00285         OSCL_IMPORT_REF void addRef();
    00286
    00293         OSCL_IMPORT_REF void removeRef();
    00294
    00295
    00296         struct MemPoolBlockInfo;
    00297
    00298         struct MemPoolBufferInfo
    00299         {
    00300             uint32 iBufferPreFence;     // Pre-fence to check for memory corruption
    00301             OsclAny* iStartAddr;        // Starting memory address of the memory pool buffer after the buffer info
    00302             OsclAny* iEndAddr;          // Ending memory address of the memory pool buffer
    00303             uint32 iBufferSize;         // Total size of the memory pool buffer including the buffer info header
    00304             uint32 iNumOutstanding;     // Number of outstanding blocks from this memory pool buffer
    00305             MemPoolBlockInfo* iNextFreeBlock; // Pointer to the next free memory block
    00306             uint32 iAllocatedSz;        //Number of butes allocated from the mempool
    00307             uint32 iBufferPostFence;    // Post-fence to check for memory corruption
    00308         };
    00309
    00310         struct MemPoolBlockInfo
    00311         {
    00312             uint32 iBlockPreFence;      // Pre-fence to check for memory corruption
    00313             MemPoolBlockInfo* iNextFreeBlock; // Pointer to the next (right neighbor) free block in the buffer. NULL if none.
    00314             MemPoolBlockInfo* iPrevFreeBlock; // Pointer to the previous (left neighbor) free block in the buffer. NULL if first free block
    00315             uint32 iBlockSize;          // Total size of the block including the block info header
    00316             uint8* iBlockBuffer;        // Pointer to the buffer area of the block
    00317             MemPoolBufferInfo* iParentBuffer; // Pointer to the block's parent memory pool buffer
    00318             uint32 iBlockPostFence;     // Post-fence to check for memory corruption
    00319         };
    00320
    00321     protected:
    00322
    00325         virtual ~OsclMemPoolResizableAllocator()
    00326         {
    00327             destroyallmempoolbuffers();
    00328         }
    00329
    00330         MemPoolBufferInfo* addnewmempoolbuffer(uint32 aBufferSize);
    00331         void destroyallmempoolbuffers();
    00332         MemPoolBlockInfo* findfreeblock(uint32 aBlockSize);
    00333         OsclAny* allocateblock(MemPoolBlockInfo& aBlockPtr, uint32 aNumBytes);
    00334         void deallocateblock(MemPoolBlockInfo& aBlockPtr);
    00335         bool validateblock(OsclAny* aBlockBufPtr);
    00336
    00337         uint32 iMemPoolBufferSize;
    00338         uint32 iMemPoolBufferNumLimit;
    00339         uint32 iExpectedNumBlocksPerBuffer;
    00340         uint32 iMaxNewMemPoolBufferSz;
    00341         Oscl_DefAlloc* iMemPoolBufferAllocator;
    00342         Oscl_Vector<MemPoolBufferInfo*, OsclMemAllocator> iMemPoolBufferList;
    00343
    00344         uint32 iBufferInfoAlignedSize;
    00345         uint32 iBlockInfoAlignedSize;
    00346
    00347         bool iCheckNextAvailable;
    00348         uint32 iRequestedNextAvailableSize;
    00349         OsclAny* iNextAvailableContextData;
    00350         OsclMemPoolResizableAllocatorObserver* iObserver;
    00351
    00352         bool iCheckFreeMemoryAvailable;
    00353         uint32  iRequestedAvailableFreeMemSize;
    00354         OsclAny* iFreeMemContextData;
    00355         OsclMemPoolResizableAllocatorMemoryObserver* iFreeMemPoolObserver;
    00356
    00357         int32 iRefCount;
    00358         bool iEnableNullPtrReturn;
    00359         //To compute the size of the buffer, excluding the extra memory in the buffer for its management
    00360         uint32 getMemPoolBufferSize(MemPoolBufferInfo* aBufferInfo) const;
    00361         //To compute the number of bytes allocated from the buffer
    00362         uint32 getMemPoolBufferAllocatedSize(MemPoolBufferInfo* aBufferInfo) const;
    00363         //To compute the addition bytes which were allocated while createing the memory pool for the buffer.
    00364         uint32 memoryPoolBufferMgmtOverhead() const;
    00365 };
    00366
    00367 #endif
    00368