OSCL_EXPORT_REF OsclAny* ThreadSafeMemPoolFixedChunkAllocator::allocate(const uint32 n) { // Create the memory pool if it hasn't been created yet. // Use the allocation size, n, as the chunk size for memory pool iMemPoolMutex.Lock(); if (iChunkSize == 0) { iChunkSize = n; createmempool(); } else if (n > iChunkSize) { OSCL_LEAVE(OsclErrArgument); } if (iFreeMemChunkList.empty()) { // No free chunk is available iMemPoolMutex.Unlock(); return NULL; } // Return the next available chunk from the pool OsclAny* freechunk = iFreeMemChunkList.back(); // Remove the chunk from the free list iFreeMemChunkList.pop_back(); addRef_internal(); iMemPoolMutex.Unlock(); return freechunk; }
OSCL_EXPORT_REF OsclMemPoolFixedChunkAllocator::OsclMemPoolFixedChunkAllocator(const uint32 numchunk, const uint32 chunksize, Oscl_DefAlloc* gen_alloc, const uint32 chunkalignment) : iNumChunk(1), iChunkSize(0), iChunkSizeMemAligned(0), iChunkAlignment(chunkalignment), iMemPoolAllocator(gen_alloc), iMemPool(NULL), iMemPoolAligned(NULL), iCheckNextAvailableFreeChunk(false), iObserver(NULL), iNextAvailableContextData(NULL), iRefCount(1), iEnableNullPtrReturn(false) { iNumChunk = numchunk; iChunkSize = chunksize; iChunkAlignment = chunkalignment; if (iNumChunk == 0) { iNumChunk = 1; } // if alignment paramater is used - set it to the nearest larger power of 2 if ((iChunkAlignment > 0) && (iChunkAlignment < 0x80000000)) { uint32 align = 1; while (align < iChunkAlignment) { align <<= 1; } iChunkAlignment = align; } // 8 byte alignment is the default. It shouldn't be larger than 1k if ((iChunkAlignment < 8) || (iChunkAlignment > 1024)) { iChunkAlignment = 0; } if (iChunkSize > 0) { createmempool(); } }
OSCL_EXPORT_REF ThreadSafeMemPoolFixedChunkAllocator::ThreadSafeMemPoolFixedChunkAllocator(const uint32 numchunk, const uint32 chunksize, Oscl_DefAlloc* gen_alloc) : iNumChunk(1), iChunkSize(0), iChunkSizeMemAligned(0), iMemPoolAllocator(gen_alloc), iMemPool(NULL), iCheckNextAvailableFreeChunk(false), iObserver(NULL), iNextAvailableContextData(NULL), iRefCount(1) { iNumChunk = numchunk; iChunkSize = chunksize; if (iNumChunk == 0) { iNumChunk = 1; } if (iChunkSize > 0) { createmempool(); } iMemPoolMutex.Create(); }
OSCL_EXPORT_REF OsclAny* OsclMemPoolFixedChunkAllocator::allocate(const uint32 n) { // Create the memory pool if it hasn't been created yet. // Use the allocation size, n, as the chunk size for memory pool if (iChunkSize == 0) { iChunkSize = n; createmempool(); } else if (n > iChunkSize) { OSCL_LEAVE(OsclErrArgument); // OSCL_UNUSED_RETURN(NULL); This statement was removed to avoid compiler warning for Unreachable Code } if (iFreeMemChunkList.empty()) { // No free chunk is available if (iEnableNullPtrReturn) { return NULL; } else { OSCL_LEAVE(OsclErrNoResources); } } // Return the next available chunk from the pool OsclAny* freechunk = iFreeMemChunkList.back(); // Remove the chunk from the free list iFreeMemChunkList.pop_back(); addRef(); return freechunk; }