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         OSCL_IMPORT_REF virtual ~OsclMemPoolFixedChunkAllocator();
    00076
    00085         OSCL_IMPORT_REF virtual OsclAny* allocate(const uint32 n);
    00086
    00093         OSCL_IMPORT_REF virtual void deallocate(OsclAny* p);
    00094
    00101         OSCL_IMPORT_REF virtual void notifyfreechunkavailable(OsclMemPoolFixedChunkAllocatorObserver& obs, OsclAny* aContextData = NULL);
    00102
    00108         OSCL_IMPORT_REF virtual void CancelFreeChunkAvailableCallback();
    00109
    00115         OSCL_IMPORT_REF void addRef();
    00116
    00123         OSCL_IMPORT_REF void removeRef();
    00124
    00125     protected:
    00126         OSCL_IMPORT_REF virtual void createmempool();
    00127         OSCL_IMPORT_REF virtual void destroymempool();
    00128
    00129         uint32 iNumChunk;
    00130         uint32 iChunkSize;
    00131         uint32 iChunkSizeMemAligned;
    00132         uint32 iChunkAlignment;
    00133         Oscl_DefAlloc* iMemPoolAllocator;
    00134         OsclAny* iMemPool;
    00135         OsclAny* iMemPoolAligned;
    00136
    00137         Oscl_Vector<OsclAny*, OsclMemAllocator> iFreeMemChunkList;
    00138
    00139         bool iCheckNextAvailableFreeChunk;
    00140         OsclMemPoolFixedChunkAllocatorObserver* iObserver;
    00141         OsclAny* iNextAvailableContextData;
    00142
    00143         int32 iRefCount;
    00144         bool iEnableNullPtrReturn;
    00145 };
    00146
    00147
    00157 class OsclMemPoolResizableAllocatorObserver
    00158 {
    00159     public:
    00160         virtual void freeblockavailable(OsclAny* aContextData) = 0;
    00161         virtual ~OsclMemPoolResizableAllocatorObserver() {}
    00162 };
    00163
    00164 class OsclMemPoolResizableAllocatorMemoryObserver
    00165 {
    00166     public:
    00167         virtual void freememoryavailable(OsclAny* aContextData) = 0;
    00168         virtual ~OsclMemPoolResizableAllocatorMemoryObserver() {}
    00169 };
    00170
    00171 class OsclMemPoolResizableAllocator : public Oscl_DefAlloc
    00172 {
    00173     public:
    00186         OSCL_IMPORT_REF OsclMemPoolResizableAllocator(uint32 aMemPoolBufferSize, uint32 aMemPoolBufferNumLimit = 0, uint32 aExpectedNumBlocksPerBuffer = 0, Oscl_DefAlloc* gen_alloc = NULL);
    00187
    00195         OSCL_IMPORT_REF virtual void enablenullpointerreturn();
    00196
    00204         OSCL_IMPORT_REF virtual OsclAny* allocate(const uint32 aNumBytes);
    00205
    00212         OSCL_IMPORT_REF virtual void deallocate(OsclAny* aPtr);
    00213
    00226         OSCL_IMPORT_REF virtual bool trim(OsclAny* aPtr, uint32 aBytesToFree);
    00227
    00230         OSCL_IMPORT_REF uint32 getBufferSize() const;
    00231
    00235         OSCL_IMPORT_REF virtual uint32 getAllocatedSize() const;
    00236
    00239         OSCL_IMPORT_REF virtual uint32 getAvailableSize() const;
    00240
    00243         OSCL_IMPORT_REF virtual uint32 getLargestContiguousFreeBlockSize() const;
    00244
    00245         OSCL_IMPORT_REF virtual bool setMaxSzForNewMemPoolBuffer(uint32 aMaxNewMemPoolBufferSz);
    00246
    00258         OSCL_IMPORT_REF virtual void notifyfreeblockavailable(OsclMemPoolResizableAllocatorObserver& aObserver, uint32 aRequestedSize = 0, OsclAny* aContextData = NULL);
    00259
    00265         OSCL_IMPORT_REF virtual void CancelFreeChunkAvailableCallback();
    00266
    00267         OSCL_IMPORT_REF virtual void notifyfreememoryavailable(OsclMemPoolResizableAllocatorMemoryObserver& aObserver, uint32 aRequestedSize = 0, OsclAny* aContextData = NULL);
    00268         OSCL_IMPORT_REF void CancelFreeMemoryAvailableCallback();
    00269
    00275         OSCL_IMPORT_REF void addRef();
    00276
    00283         OSCL_IMPORT_REF void removeRef();
    00284
    00285
    00286         struct MemPoolBlockInfo;
    00287
    00288         struct MemPoolBufferInfo
    00289         {
    00290             uint32 iBufferPreFence;     // Pre-fence to check for memory corruption
    00291             OsclAny* iStartAddr;        // Starting memory address of the memory pool buffer after the buffer info
    00292             OsclAny* iEndAddr;          // Ending memory address of the memory pool buffer
    00293             uint32 iBufferSize;         // Total size of the memory pool buffer including the buffer info header
    00294             uint32 iNumOutstanding;     // Number of outstanding blocks from this memory pool buffer
    00295             MemPoolBlockInfo* iNextFreeBlock; // Pointer to the next free memory block
    00296             uint32 iAllocatedSz;        //Number of butes allocated from the mempool
    00297             uint32 iBufferPostFence;    // Post-fence to check for memory corruption
    00298         };
    00299
    00300         struct MemPoolBlockInfo
    00301         {
    00302             uint32 iBlockPreFence;      // Pre-fence to check for memory corruption
    00303             MemPoolBlockInfo* iNextFreeBlock; // Pointer to the next (right neighbor) free block in the buffer. NULL if none.
    00304             MemPoolBlockInfo* iPrevFreeBlock; // Pointer to the previous (left neighbor) free block in the buffer. NULL if first free block
    00305             uint32 iBlockSize;          // Total size of the block including the block info header
    00306             uint8* iBlockBuffer;        // Pointer to the buffer area of the block
    00307             MemPoolBufferInfo* iParentBuffer; // Pointer to the block's parent memory pool buffer
    00308             uint32 iBlockPostFence;     // Post-fence to check for memory corruption
    00309         };
    00310
    00311     protected:
    00312
    00315         OSCL_IMPORT_REF virtual ~OsclMemPoolResizableAllocator();
    00316
    00317         MemPoolBufferInfo* addnewmempoolbuffer(uint32 aBufferSize);
    00318         void destroyallmempoolbuffers();
    00319         MemPoolBlockInfo* findfreeblock(uint32 aBlockSize);
    00320         OsclAny* allocateblock(MemPoolBlockInfo& aBlockPtr, uint32 aNumBytes);
    00321         void deallocateblock(MemPoolBlockInfo& aBlockPtr);
    00322         bool validateblock(OsclAny* aBlockBufPtr);
    00323
    00324         uint32 iMemPoolBufferSize;
    00325         uint32 iMemPoolBufferNumLimit;
    00326         uint32 iExpectedNumBlocksPerBuffer;
    00327         uint32 iMaxNewMemPoolBufferSz;
    00328         Oscl_DefAlloc* iMemPoolBufferAllocator;
    00329         Oscl_Vector<MemPoolBufferInfo*, OsclMemAllocator> iMemPoolBufferList;
    00330
    00331         uint32 iBufferInfoAlignedSize;
    00332         uint32 iBlockInfoAlignedSize;
    00333
    00334         bool iCheckNextAvailable;
    00335         uint32 iRequestedNextAvailableSize;
    00336         OsclAny* iNextAvailableContextData;
    00337         OsclMemPoolResizableAllocatorObserver* iObserver;
    00338
    00339         bool iCheckFreeMemoryAvailable;
    00340         uint32  iRequestedAvailableFreeMemSize;
    00341         OsclAny* iFreeMemContextData;
    00342         OsclMemPoolResizableAllocatorMemoryObserver* iFreeMemPoolObserver;
    00343
    00344         int32 iRefCount;
    00345         bool iEnableNullPtrReturn;
    00346         //To compute the size of the buffer, excluding the extra memory in the buffer for its management
    00347         uint32 getMemPoolBufferSize(MemPoolBufferInfo* aBufferInfo) const;
    00348         //To compute the number of bytes allocated from the buffer
    00349         uint32 getMemPoolBufferAllocatedSize(MemPoolBufferInfo* aBufferInfo) const;
    00350         //To compute the addition bytes which were allocated while createing the memory pool for the buffer.
    00351         uint32 memoryPoolBufferMgmtOverhead() const;
    00352 };
    00353
    00354 #endif
    00355