void CUtlMemoryBase::Purge( int numElements, bool bRealloc ) { Assert( numElements >= 0 ); if( numElements > m_nAllocationCount ) { // Ensure this isn't a grow request in disguise. Assert( numElements <= m_nAllocationCount ); return; } // If we have zero elements, simply do a purge: if( numElements == 0 ) { Purge(); return; } if ( IsExternallyAllocated() ) { // Can't shrink a buffer whose memory was externally allocated, fail silently like purge return; } // If the number of elements is the same as the allocation count, we are done. if( numElements == m_nAllocationCount ) { return; } if( !m_pMemory ) { // Allocation count is non zero, but memory is null. Assert( m_pMemory ); return; } if ( bRealloc ) { UTLMEMORY_TRACK_FREE(); m_nAllocationCount = numElements; UTLMEMORY_TRACK_ALLOC(); // Allocation count > 0, shrink it down. MEM_ALLOC_CREDIT_CLASS(); m_pMemory = PvRealloc( m_pMemory, m_nAllocationCount * m_unSizeOfElements ); } else { // Some of the tracking may be wrong as we are changing the size but are not reallocating. m_nAllocationCount = numElements; } }
//----------------------------------------------------------------------------- // Switches the buffer from an external memory buffer to a reallocatable buffer //----------------------------------------------------------------------------- void CUtlMemoryBase::ConvertToGrowableMemory( int nGrowSize ) { if ( !IsExternallyAllocated() ) return; m_nGrowSize = nGrowSize; if (m_nAllocationCount) { UTLMEMORY_TRACK_ALLOC(); MEM_ALLOC_CREDIT_CLASS(); int nNumBytes = m_nAllocationCount * m_unSizeOfElements; void *pMemory = PvAlloc( nNumBytes ); memcpy( pMemory, m_pMemory, nNumBytes ); m_pMemory = pMemory; } else { m_pMemory = NULL; } }
void CUtlVector<T, A>::EnsureCapacity(int num) { MEM_ALLOC_CREDIT_CLASS(); m_Memory.EnsureCapacity(num); ResetDbgInfo(); }