void CUtlMemoryBase::Grow( int num ) { Assert( num > 0 ); if ( IsExternallyAllocated() ) { // Can't grow a buffer whose memory was externally allocated Assert(0); return; } // Make sure we have at least numallocated + num allocations. // Use the grow rules specified for this memory (in m_nGrowSize) int nAllocationRequested = m_nAllocationCount + num; UTLMEMORY_TRACK_FREE(); m_nAllocationCount = UtlMemory_CalcNewAllocationCount( m_nAllocationCount, m_nGrowSize, nAllocationRequested, m_unSizeOfElements ); UTLMEMORY_TRACK_ALLOC(); if (m_pMemory) { m_pMemory = PvRealloc( m_pMemory, m_nAllocationCount * m_unSizeOfElements ); } else { m_pMemory = PvAlloc( m_nAllocationCount * m_unSizeOfElements ); } }
//----------------------------------------------------------------------------- // Makes sure we've got at least this much memory //----------------------------------------------------------------------------- void CUtlMemoryBase::EnsureCapacity( int num ) { if (m_nAllocationCount >= num) return; if ( IsExternallyAllocated() ) { // Can't grow a buffer whose memory was externally allocated Assert(0); return; } UTLMEMORY_TRACK_FREE(); m_nAllocationCount = num; UTLMEMORY_TRACK_ALLOC(); if (m_pMemory) { m_pMemory = PvRealloc( m_pMemory, m_nAllocationCount * m_unSizeOfElements ); } else { m_pMemory = PvAlloc( m_nAllocationCount * m_unSizeOfElements ); } }
//----------------------------------------------------------------------------- // Sets the priority level for a spew group //----------------------------------------------------------------------------- void SpewActivate( const tchar* pGroupName, int level ) { Assert( pGroupName ); // check for the default group first... if ((pGroupName[0] == '*') && (pGroupName[1] == '\0')) { s_DefaultLevel = level; return; } // Normal case, search in group list using binary search. // If not found, grow the list of groups and insert it into the // right place to maintain sorted order. Then set the level. int ind; if ( !FindSpewGroup( pGroupName, &ind ) ) { // not defined yet, insert an entry. ++s_GroupCount; if ( s_pSpewGroups ) { s_pSpewGroups = (SpewGroup_t*)PvRealloc( s_pSpewGroups, s_GroupCount * sizeof(SpewGroup_t) ); // shift elements down to preserve order int numToMove = s_GroupCount - ind - 1; memmove( &s_pSpewGroups[ind+1], &s_pSpewGroups[ind], numToMove * sizeof(SpewGroup_t) ); // Update standard groups for ( int i = 0; i < GROUP_COUNT; ++i ) { if ( ( ind <= s_pGroupIndices[i] ) && ( s_pGroupIndices[i] >= 0 ) ) { ++s_pGroupIndices[i]; } } } else { s_pSpewGroups = (SpewGroup_t*)PvAlloc( s_GroupCount * sizeof(SpewGroup_t) ); } Assert( _tcslen( pGroupName ) < MAX_GROUP_NAME_LENGTH ); _tcscpy( s_pSpewGroups[ind].m_GroupName, pGroupName ); // Update standard groups for ( int i = 0; i < GROUP_COUNT; ++i ) { if ( ( s_pGroupIndices[i] < 0 ) && !_tcsicmp( s_pGroupNames[i], pGroupName ) ) { s_pGroupIndices[i] = ind; break; } } } s_pSpewGroups[ind].m_Level = level; }
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; } }