1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-09-19 12:06:07 +08:00

Update CUtlMemory_RawAllocator & CUtlVectorRawAllocator

This commit is contained in:
GAMMACASE
2025-08-01 21:47:27 +03:00
parent 470f8576f1
commit f24a7dd343
2 changed files with 32 additions and 32 deletions

View File

@ -1027,9 +1027,11 @@ void CUtlMemoryAligned<T, nAlignment>::Purge()
} }
#pragma pack(push, 1) #pragma pack(push, 1)
template< class T > template< class T, class A >
class CUtlMemory_RawAllocator class CUtlMemory_RawAllocator
{ {
typedef A CAllocator;
public: public:
// constructor, destructor // constructor, destructor
CUtlMemory_RawAllocator( int nGrowSize = 0, int nInitSize = 0 ); CUtlMemory_RawAllocator( int nGrowSize = 0, int nInitSize = 0 );
@ -1057,7 +1059,7 @@ public:
void *DetachMemory(); void *DetachMemory();
// Fast swap // Fast swap
void Swap( CUtlMemory_RawAllocator< T > &mem ); void Swap( CUtlMemory_RawAllocator< T, A > &mem );
// Size // Size
int NumAllocated() const { return m_nAllocationCount; } int NumAllocated() const { return m_nAllocationCount; }
@ -1104,15 +1106,15 @@ private:
// constructor, destructor // constructor, destructor
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T > template< class T, class A >
CUtlMemory_RawAllocator<T>::CUtlMemory_RawAllocator( int nGrowSize, int nInitSize ) CUtlMemory_RawAllocator<T, A>::CUtlMemory_RawAllocator( int nGrowSize, int nInitSize )
: m_nAllocationCount( 0 ), m_pMemory( nullptr ) : m_nAllocationCount( 0 ), m_pMemory( nullptr )
{ {
EnsureCapacity( nInitSize ); EnsureCapacity( nInitSize );
} }
template< class T > template< class T, class A >
CUtlMemory_RawAllocator<T>::~CUtlMemory_RawAllocator() CUtlMemory_RawAllocator<T, A>::~CUtlMemory_RawAllocator()
{ {
Purge(); Purge();
} }
@ -1120,15 +1122,15 @@ CUtlMemory_RawAllocator<T>::~CUtlMemory_RawAllocator()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Fast swap // Fast swap
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T > template< class T, class A >
void CUtlMemory_RawAllocator<T>::Swap( CUtlMemory_RawAllocator<T> &mem ) void CUtlMemory_RawAllocator<T, A>::Swap( CUtlMemory_RawAllocator<T, A> &mem )
{ {
V_swap( m_pMemory, mem.m_pMemory ); V_swap( m_pMemory, mem.m_pMemory );
V_swap( m_nAllocationCount, mem.m_nAllocationCount ); V_swap( m_nAllocationCount, mem.m_nAllocationCount );
} }
template< class T > template< class T, class A >
void CUtlMemory_RawAllocator<T>::AssumeMemory( T* pMemory, int numElements ) void CUtlMemory_RawAllocator<T, A>::AssumeMemory( T* pMemory, int numElements )
{ {
// Blow away any existing allocated memory // Blow away any existing allocated memory
Purge(); Purge();
@ -1138,8 +1140,8 @@ void CUtlMemory_RawAllocator<T>::AssumeMemory( T* pMemory, int numElements )
m_nAllocationCount = numElements; m_nAllocationCount = numElements;
} }
template< class T > template< class T, class A >
void *CUtlMemory_RawAllocator<T>::DetachMemory() void *CUtlMemory_RawAllocator<T, A>::DetachMemory()
{ {
void *pMemory = m_pMemory; void *pMemory = m_pMemory;
m_pMemory = 0; m_pMemory = 0;
@ -1147,8 +1149,8 @@ void *CUtlMemory_RawAllocator<T>::DetachMemory()
return pMemory; return pMemory;
} }
template< class T > template< class T, class A >
inline T* CUtlMemory_RawAllocator<T>::Detach() inline T* CUtlMemory_RawAllocator<T, A>::Detach()
{ {
return (T*)DetachMemory(); return (T*)DetachMemory();
} }
@ -1156,8 +1158,8 @@ inline T* CUtlMemory_RawAllocator<T>::Detach()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Grows the memory // Grows the memory
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T > template< class T, class A >
void CUtlMemory_RawAllocator<T>::Grow( int num ) void CUtlMemory_RawAllocator<T, A>::Grow( int num )
{ {
Assert( num > 0 ); Assert( num > 0 );
EnsureCapacity( m_nAllocationCount + num ); EnsureCapacity( m_nAllocationCount + num );
@ -1166,18 +1168,16 @@ void CUtlMemory_RawAllocator<T>::Grow( int num )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Makes sure we've got at least this much memory // Makes sure we've got at least this much memory
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T > template< class T, class A >
inline void CUtlMemory_RawAllocator<T>::EnsureCapacity( int num ) inline void CUtlMemory_RawAllocator<T, A>::EnsureCapacity( int num )
{ {
if(m_nAllocationCount >= num) if(m_nAllocationCount >= num)
return; return;
int new_alloc_size = CalcNewDoublingCount( m_nAllocationCount, num, 2, INT_MAX ); int new_alloc_size = CalcNewDoublingCount( m_nAllocationCount, num, 2, INT_MAX );
size_t adjusted_size = 0;
MEM_ALLOC_CREDIT_CLASS(); MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T *)CRawAllocator::Realloc( m_pMemory, new_alloc_size * sizeof( T ), &adjusted_size ); m_pMemory = CAllocator::Realloc( m_pMemory, new_alloc_size, m_nAllocationCount );
m_nAllocationCount = clamp( (int)(adjusted_size / sizeof( T )), new_alloc_size, INT_MAX );
UTLMEMORY_TRACK_ALLOC(); UTLMEMORY_TRACK_ALLOC();
} }
@ -1185,21 +1185,21 @@ inline void CUtlMemory_RawAllocator<T>::EnsureCapacity( int num )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Memory deallocation // Memory deallocation
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template< class T > template< class T, class A >
void CUtlMemory_RawAllocator<T>::Purge() void CUtlMemory_RawAllocator<T, A>::Purge()
{ {
if (m_nAllocationCount > 0) if (m_nAllocationCount > 0)
{ {
UTLMEMORY_TRACK_FREE(); UTLMEMORY_TRACK_FREE();
CRawAllocator::Free( m_pMemory ); CAllocator::Free( m_pMemory );
m_pMemory = 0; m_pMemory = 0;
} }
m_nAllocationCount = 0; m_nAllocationCount = 0;
} }
template< class T > template< class T, class A >
void CUtlMemory_RawAllocator<T>::Purge( int numElements ) void CUtlMemory_RawAllocator<T, A>::Purge( int numElements )
{ {
Assert( numElements >= 0 ); Assert( numElements >= 0 );
@ -1232,11 +1232,9 @@ void CUtlMemory_RawAllocator<T>::Purge( int numElements )
} }
UTLMEMORY_TRACK_FREE(); UTLMEMORY_TRACK_FREE();
size_t adjusted_size = 0;
MEM_ALLOC_CREDIT_CLASS(); MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T *)CRawAllocator::Realloc( m_pMemory, numElements * sizeof( T ), &adjusted_size ); m_pMemory = CAllocator::Realloc( m_pMemory, numElements, m_nAllocationCount );
m_nAllocationCount = clamp( (int)(adjusted_size / sizeof( T )), numElements, INT_MAX );
UTLMEMORY_TRACK_ALLOC(); UTLMEMORY_TRACK_ALLOC();
} }

View File

@ -247,10 +247,12 @@ public:
CUtlVectorConservative( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {} CUtlVectorConservative( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
}; };
template< class T > template< class T, class A = CMemAllocAllocator >
class CUtlVectorRawAllocator : public CUtlVector< T, CUtlMemory_RawAllocator<T> > class CUtlVectorRawAllocator : public CUtlVector< T, CUtlMemory_RawAllocator<T, A> >
{ {
typedef CUtlVector< T, CUtlMemory_RawAllocator<T> > BaseClass; typedef CUtlVector< T, CUtlMemory_RawAllocator<T, A> > BaseClass;
typedef A CAllocator;
public: public:
// constructor, destructor // constructor, destructor