void *CMemoryPool::Alloc(unsigned int amount) { void *returnBlock; if (amount >(unsigned int)_blockSize) { return NULL; } _blocksAllocated++; _peakAlloc = max(_peakAlloc, _blocksAllocated); if (_blocksAllocated >= _numElements) { AddNewBlob(); } if (!_headOfFreeList) { DebugBreak(); } returnBlock = _headOfFreeList; _headOfFreeList = *((void **)_headOfFreeList); return returnBlock; }
/* <3fea40> ../public/MemPool.cpp:35 */ CMemoryPool::CMemoryPool(int blockSize, int numElements) { _blocksPerBlob = numElements; _blockSize = blockSize; _numBlobs = 0; _numElements = 0; AddNewBlob(); _peakAlloc = 0; _blocksAllocated = 0; }
/* <3fea72> ../public/MemPool.cpp:157 */ void *CMemoryPool::Alloc(unsigned int amount) { void *returnBlock; if (amount > (unsigned int)_blockSize) return NULL; ++_blocksAllocated; _peakAlloc = Q_max(_peakAlloc, _blocksAllocated); if (_blocksAllocated >= _numElements) AddNewBlob(); #ifdef _WIN32 if (!_headOfFreeList) DebugBreak(); #endif // _WIN32 returnBlock = _headOfFreeList; _headOfFreeList = *((void **)_headOfFreeList); return returnBlock; }
//----------------------------------------------------------------------------- // Purpose: Constructor //----------------------------------------------------------------------------- CMemoryPool::CMemoryPool( int blockSize, int numElements, int growMode, const char *pszAllocOwner, int nAlignment ) { #ifdef _X360 if( numElements > 0 && growMode != GROW_NONE ) { numElements = 1; } #endif m_nAlignment = ( nAlignment != 0 ) ? nAlignment : 1; Assert( IsPowerOfTwo( m_nAlignment ) ); m_BlockSize = blockSize < sizeof(void*) ? sizeof(void*) : blockSize; m_BlockSize = AlignValue( m_BlockSize, m_nAlignment ); m_BlocksPerBlob = numElements; m_PeakAlloc = 0; m_GrowMode = growMode; if ( !pszAllocOwner ) { pszAllocOwner = __FILE__; } m_pszAllocOwner = pszAllocOwner; Init(); AddNewBlob(); }
//----------------------------------------------------------------------------- // Purpose: Allocs a single block of memory from the pool. // Input : amount - //----------------------------------------------------------------------------- void *CMemoryPool::Alloc( unsigned int amount ) { void *returnBlock; if ( amount > (unsigned int)m_BlockSize ) return NULL; if( !m_pHeadOfFreeList ) { // returning NULL is fine in GROW_NONE if( m_GrowMode == GROW_NONE ) { //Assert( !"CMemoryPool::Alloc: tried to make new blob with GROW_NONE" ); return NULL; } // overflow AddNewBlob(); // still failure, error out if( !m_pHeadOfFreeList ) { Assert( !"CMemoryPool::Alloc: ran out of memory" ); return NULL; } } m_BlocksAllocated++; m_PeakAlloc = max(m_PeakAlloc, m_BlocksAllocated); returnBlock = m_pHeadOfFreeList; // move the pointer the next block m_pHeadOfFreeList = *((void**)m_pHeadOfFreeList); return returnBlock; }