Skip to content

Commit

Permalink
Replace free/malloc/realloc calls with the games allocator
Browse files Browse the repository at this point in the history
Fixes a crash with autocomplete in 2007 sdk
  • Loading branch information
lipsanen committed Oct 2, 2024
1 parent 8fbb3a3 commit 109a3c7
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 47 deletions.
22 changes: 11 additions & 11 deletions SDK/bms_new/include/public/tier1/utlmemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class CUtlMemoryConservative

}
CUtlMemoryConservative( T* pMemory, int numElements ) { Assert( 0 ); }
~CUtlMemoryConservative() { if ( m_pMemory ) free( m_pMemory ); }
~CUtlMemoryConservative() { if ( m_pMemory ) g_pMemAlloc->Free( m_pMemory ); }

// Can we use this index?
bool IsIdxValid( int i ) const { return ( IsDebug() ) ? ( i >= 0 && i < NumAllocated() ) : ( i >= 0 ); }
Expand Down Expand Up @@ -348,7 +348,7 @@ class CUtlMemoryConservative

FORCEINLINE void ReAlloc( size_t sz )
{
m_pMemory = (T*)realloc( m_pMemory, sz );
m_pMemory = (T*)g_pMemAlloc->Realloc( m_pMemory, sz );
RememberAllocSize( sz );
}
// Grows the memory, so that at least allocated + num elements are allocated
Expand All @@ -368,7 +368,7 @@ class CUtlMemoryConservative
// Memory deallocation
void Purge()
{
free( m_pMemory );
g_pMemAlloc->Free( m_pMemory );
RememberAllocSize( 0 );
m_pMemory = NULL;
}
Expand Down Expand Up @@ -421,7 +421,7 @@ CUtlMemory<T,I>::CUtlMemory( int nGrowSize, int nInitAllocationCount ) : m_pMemo
{
UTLMEMORY_TRACK_ALLOC();
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
}
}

Expand Down Expand Up @@ -460,7 +460,7 @@ void CUtlMemory<T,I>::Init( int nGrowSize /*= 0*/, int nInitSize /*= 0*/ )
{
UTLMEMORY_TRACK_ALLOC();
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
}
}

Expand Down Expand Up @@ -492,7 +492,7 @@ void CUtlMemory<T,I>::ConvertToGrowableMemory( int nGrowSize )
MEM_ALLOC_CREDIT_CLASS();

int nNumBytes = m_nAllocationCount * sizeof(T);
T *pMemory = (T*)malloc( nNumBytes );
T *pMemory = (T*)g_pMemAlloc->Alloc( nNumBytes );
memcpy( (void*)pMemory, (void*)m_pMemory, nNumBytes );
m_pMemory = pMemory;
}
Expand Down Expand Up @@ -740,13 +740,13 @@ void CUtlMemory<T,I>::Grow( int num )
if (m_pMemory)
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
Assert( m_pMemory );
}
else
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
Assert( m_pMemory );
}
}
Expand Down Expand Up @@ -777,12 +777,12 @@ inline void CUtlMemory<T,I>::EnsureCapacity( int num )
if (m_pMemory)
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
}
else
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
}
}

Expand Down Expand Up @@ -852,7 +852,7 @@ void CUtlMemory<T,I>::Purge( int numElements )

// Allocation count > 0, shrink it down.
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
}

//-----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions SDK/bms_new/src/all_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "tier1/utlbuffer.h"
#include <KeyValues.h>
#include <vstdlib/IKeyValuesSystem.h>
#include "memdbgon.h"


#include "characterset.cpp"
Expand Down
28 changes: 14 additions & 14 deletions SDK/episode1_new/include/public/tier1/utlmemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ CUtlMemory<T>::CUtlMemory( int nGrowSize, int nInitAllocationCount ) : m_pMemory
{
UTLMEMORY_TRACK_ALLOC();
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
}
}

Expand Down Expand Up @@ -250,7 +250,7 @@ void CUtlMemory<T>::ConvertToGrowableMemory( int nGrowSize )
MEM_ALLOC_CREDIT_CLASS();

int nNumBytes = m_nAllocationCount * sizeof(T);
T *pMemory = (T*)malloc( nNumBytes );
T *pMemory = (T*)g_pMemAlloc->Alloc( nNumBytes );
memcpy( pMemory, m_pMemory, nNumBytes );
m_pMemory = pMemory;
}
Expand Down Expand Up @@ -457,13 +457,13 @@ void CUtlMemory<T>::Grow( int num )
if (m_pMemory)
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
Assert( m_pMemory );
}
else
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
Assert( m_pMemory );
}
}
Expand Down Expand Up @@ -494,12 +494,12 @@ inline void CUtlMemory<T>::EnsureCapacity( int num )
if (m_pMemory)
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
}
else
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
}
}

Expand All @@ -515,7 +515,7 @@ void CUtlMemory<T>::Purge()
if (m_pMemory)
{
UTLMEMORY_TRACK_FREE();
free( (void*)m_pMemory );
g_pMemAlloc->Free( (void*)m_pMemory );
m_pMemory = 0;
}
m_nAllocationCount = 0;
Expand Down Expand Up @@ -569,7 +569,7 @@ void CUtlMemory<T>::Purge( int numElements )

// Allocation count > 0, shrink it down.
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -638,7 +638,7 @@ CUtlMemoryAligned<T, nAlignment>::CUtlMemoryAligned( int nGrowSize, int nInitAll
{
UTLMEMORY_TRACK_ALLOC();
MEM_ALLOC_CREDIT_CLASS();
m_pMemoryBase = malloc( nInitAllocationCount * sizeof(T) + (nAlignment - 1) );
m_pMemoryBase = g_pMemAlloc->Alloc( nInitAllocationCount * sizeof(T) + (nAlignment - 1) );
CUtlMemory<T>::m_pMemory = (T*)Align( m_pMemoryBase );
}
}
Expand Down Expand Up @@ -730,13 +730,13 @@ void CUtlMemoryAligned<T, nAlignment>::Grow( int num )
if (m_pMemoryBase)
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemoryBase = realloc( m_pMemoryBase, CUtlMemory<T>::m_nAllocationCount * sizeof(T) + (nAlignment - 1) );
m_pMemoryBase = g_pMemAlloc->Realloc( m_pMemoryBase, CUtlMemory<T>::m_nAllocationCount * sizeof(T) + (nAlignment - 1) );
Assert( m_pMemoryBase );
}
else
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemoryBase = malloc( CUtlMemory<T>::m_nAllocationCount * sizeof(T) + (nAlignment - 1) );
m_pMemoryBase = g_pMemAlloc->Alloc( CUtlMemory<T>::m_nAllocationCount * sizeof(T) + (nAlignment - 1) );
Assert( m_pMemoryBase );
}
CUtlMemory<T>::m_pMemory = (T*)Align( m_pMemoryBase );
Expand Down Expand Up @@ -768,12 +768,12 @@ inline void CUtlMemoryAligned<T, nAlignment>::EnsureCapacity( int num )
if (m_pMemoryBase)
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemoryBase = realloc( m_pMemoryBase, CUtlMemory<T>::m_nAllocationCount * sizeof(T) + (nAlignment - 1) );
m_pMemoryBase = g_pMemAlloc->Realloc( m_pMemoryBase, CUtlMemory<T>::m_nAllocationCount * sizeof(T) + (nAlignment - 1) );
}
else
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemoryBase = malloc( CUtlMemory<T>::m_nAllocationCount * sizeof(T) + (nAlignment - 1) );
m_pMemoryBase = g_pMemAlloc->Alloc( CUtlMemory<T>::m_nAllocationCount * sizeof(T) + (nAlignment - 1) );
}
CUtlMemory<T>::m_pMemory = (T*)Align( m_pMemoryBase );
}
Expand All @@ -790,7 +790,7 @@ void CUtlMemoryAligned<T, nAlignment>::Purge()
if (m_pMemoryBase)
{
UTLMEMORY_TRACK_FREE();
free( (void*)m_pMemoryBase );
g_pMemAlloc->Free( (void*)m_pMemoryBase );
m_pMemoryBase = 0;
CUtlMemory<T>::m_pMemory = 0;
}
Expand Down
1 change: 1 addition & 0 deletions SDK/episode1_new/src/all_oe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define NOMINMAX
#include <direct.h>
#include <windows.h>
#include "memdbgon.h"

#include "checksum_md5.cpp"
#include "cpu.cpp"
Expand Down
18 changes: 9 additions & 9 deletions SDK/orangebox_new/include/public/tier1/utlmemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ CUtlMemory<T,I>::CUtlMemory( int nGrowSize, int nInitAllocationCount ) : m_pMemo
{
UTLMEMORY_TRACK_ALLOC();
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
}
}

Expand Down Expand Up @@ -317,7 +317,7 @@ void CUtlMemory<T,I>::Init( int nGrowSize /*= 0*/, int nInitSize /*= 0*/ )
{
UTLMEMORY_TRACK_ALLOC();
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
}
}

Expand Down Expand Up @@ -349,7 +349,7 @@ void CUtlMemory<T,I>::ConvertToGrowableMemory( int nGrowSize )
MEM_ALLOC_CREDIT_CLASS();

int nNumBytes = m_nAllocationCount * sizeof(T);
T *pMemory = (T*)malloc( nNumBytes );
T *pMemory = (T*)g_pMemAlloc->Alloc( nNumBytes );
memcpy( pMemory, m_pMemory, nNumBytes );
m_pMemory = pMemory;
}
Expand Down Expand Up @@ -589,13 +589,13 @@ void CUtlMemory<T,I>::Grow( int num )
if (m_pMemory)
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
Assert( m_pMemory );
}
else
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
Assert( m_pMemory );
}
}
Expand Down Expand Up @@ -626,12 +626,12 @@ inline void CUtlMemory<T,I>::EnsureCapacity( int num )
if (m_pMemory)
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
}
else
{
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)malloc( m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Alloc( m_nAllocationCount * sizeof(T) );
}
}

Expand All @@ -647,7 +647,7 @@ void CUtlMemory<T,I>::Purge()
if (m_pMemory)
{
UTLMEMORY_TRACK_FREE();
free( (void*)m_pMemory );
g_pMemAlloc->Free( (void*)m_pMemory );
m_pMemory = 0;
}
m_nAllocationCount = 0;
Expand Down Expand Up @@ -701,7 +701,7 @@ void CUtlMemory<T,I>::Purge( int numElements )

// Allocation count > 0, shrink it down.
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = (T*)realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
m_pMemory = (T*)g_pMemAlloc->Realloc( m_pMemory, m_nAllocationCount * sizeof(T) );
}

//-----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions SDK/orangebox_new/src/all_orangebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define NOMINMAX
#include <direct.h>
#include <windows.h>
#include "memdbgon.h"

#include "checksum_md5.cpp"
#include "cpu.cpp"
Expand Down
Loading

0 comments on commit 109a3c7

Please sign in to comment.