Skip to content

Commit

Permalink
Implement conditional memory allocation overrides for new and delete …
Browse files Browse the repository at this point in the history
…operators
  • Loading branch information
andrewmcwatters committed Dec 15, 2024
1 parent bdae57c commit ee1943f
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 3 deletions.
22 changes: 21 additions & 1 deletion mp/src/game/client/c_baseentity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3803,7 +3803,11 @@ void *C_BaseEntity::operator new( size_t stAllocateBlock )
{
Assert( stAllocateBlock != 0 );
MEM_ALLOC_CREDIT();
#if !defined(NO_MALLOC_OVERRIDE)
void *pMem = MemAlloc_Alloc( stAllocateBlock );
#else
void* pMem = malloc(stAllocateBlock);
#endif
memset( pMem, 0, stAllocateBlock );
return pMem;
}
Expand All @@ -3812,23 +3816,35 @@ void *C_BaseEntity::operator new[]( size_t stAllocateBlock )
{
Assert( stAllocateBlock != 0 );
MEM_ALLOC_CREDIT();
#if !defined(NO_MALLOC_OVERRIDE)
void *pMem = MemAlloc_Alloc( stAllocateBlock );
#else
void* pMem = malloc(stAllocateBlock);
#endif
memset( pMem, 0, stAllocateBlock );
return pMem;
}

void *C_BaseEntity::operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine )
{
Assert( stAllocateBlock != 0 );
#if !defined(NO_MALLOC_OVERRIDE)
void *pMem = MemAlloc_Alloc( stAllocateBlock, pFileName, nLine );
#else
void* pMem = malloc(stAllocateBlock);
#endif
memset( pMem, 0, stAllocateBlock );
return pMem;
}

void *C_BaseEntity::operator new[]( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine )
{
Assert( stAllocateBlock != 0 );
#if !defined(NO_MALLOC_OVERRIDE)
void *pMem = MemAlloc_Alloc( stAllocateBlock, pFileName, nLine );
#else
void* pMem = malloc(stAllocateBlock);
#endif
memset( pMem, 0, stAllocateBlock );
return pMem;
}
Expand All @@ -3841,7 +3857,11 @@ void *C_BaseEntity::operator new[]( size_t stAllocateBlock, int nBlockUse, const
void C_BaseEntity::operator delete( void *pMem )
{
// get the engine to free the memory
#if !defined(NO_MALLOC_OVERRIDE)
MemAlloc_Free( pMem );
#else
free(pMem);
#endif
}

#include "tier0/memdbgon.h"
Expand Down Expand Up @@ -4751,7 +4771,7 @@ C_BaseEntity *C_BaseEntity::Instance( int iEnt )

#ifdef WIN32
#pragma warning( push )
#include <typeinfo.h>
#include <typeinfo>
#pragma warning( pop )
#endif

Expand Down
2 changes: 1 addition & 1 deletion mp/src/game/client/client_base.vpc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ $Configuration
$Compiler
{
$AdditionalIncludeDirectories ".\;$BASE;$SRCDIR\vgui2\include;$SRCDIR\vgui2\controls;$SRCDIR\game\shared;.\game_controls;$SRCDIR\thirdparty\sixensesdk\include"
$PreprocessorDefinitions "$BASE;NO_STRING_T;CLIENT_DLL;VECTOR;VERSION_SAFE_STEAM_API_INTERFACES;PROTECTED_THINGS_ENABLE;strncpy=use_Q_strncpy_instead;_snprintf=use_Q_snprintf_instead"
$PreprocessorDefinitions "$BASE;NO_STRING_T;CLIENT_DLL;VECTOR;VERSION_SAFE_STEAM_API_INTERFACES;PROTECTED_THINGS_ENABLE;strncpy=use_Q_strncpy_instead;_snprintf=use_Q_snprintf_instead;NO_MALLOC_OVERRIDE;REMEMBER_ALLOC_SIZE_FOR_VALGRIND"
$PreprocessorDefinitions "$BASE;fopen=dont_use_fopen" [$WIN32]
$PreprocessorDefinitions "$BASE;USE_WEBM_FOR_REPLAY;" [$LINUXALL]
$PreprocessorDefinitions "$BASE;CURL_STATICLIB" [$WIN32 && $BUILD_REPLAY]
Expand Down
40 changes: 40 additions & 0 deletions mp/src/game/client/detailobjectsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,17 +1408,29 @@ void CDetailObjectSystem::FreeSortBuffers( void )
{
if ( m_pSortInfo )
{
#if !defined(NO_MALLOC_OVERRIDE)
MemAlloc_FreeAligned( m_pSortInfo );
#else
delete[] m_pSortInfo;
#endif
m_pSortInfo = NULL;
}
if ( m_pFastSortInfo )
{
#if !defined(NO_MALLOC_OVERRIDE)
MemAlloc_FreeAligned( m_pFastSortInfo );
#else
delete[] m_pFastSortInfo;
#endif
m_pFastSortInfo = NULL;
}
if ( m_pBuildoutBuffer )
{
#if !defined(NO_MALLOC_OVERRIDE)
MemAlloc_FreeAligned( m_pBuildoutBuffer );
#else
delete[] m_pBuildoutBuffer;
#endif
m_pBuildoutBuffer = NULL;
}
}
Expand All @@ -1427,7 +1439,11 @@ CDetailObjectSystem::~CDetailObjectSystem()
{
if ( m_pFastSpriteData )
{
#if !defined(NO_MALLOC_OVERRIDE)
MemAlloc_FreeAligned( m_pFastSpriteData );
#else
delete[] m_pFastSpriteData;
#endif
m_pFastSpriteData = NULL;
}
FreeSortBuffers();
Expand Down Expand Up @@ -1543,7 +1559,11 @@ void CDetailObjectSystem::LevelShutdownPreEntity()
m_DetailSpriteMaterial.Shutdown();
if ( m_pFastSpriteData )
{
#if !defined(NO_MALLOC_OVERRIDE)
MemAlloc_FreeAligned( m_pFastSpriteData );
#else
delete[] m_pFastSpriteData;
#endif
m_pFastSpriteData = NULL;
}
FreeSortBuffers();
Expand Down Expand Up @@ -1735,28 +1755,48 @@ void CDetailObjectSystem::UnserializeModels( CUtlBuffer& buf )

if ( nMaxOldInLeaf )
{
#if !defined(NO_MALLOC_OVERRIDE)
m_pSortInfo = reinterpret_cast<SortInfo_t *> (
MemAlloc_AllocAligned( (3 + nMaxOldInLeaf ) * sizeof( SortInfo_t ), sizeof( fltx4 ) ) );
#else
m_pSortInfo = reinterpret_cast<SortInfo_t*> (
::operator new((3 + nMaxOldInLeaf) * sizeof(SortInfo_t), std::nothrow));
#endif
}
if ( nMaxFastInLeaf )
{
#if !defined(NO_MALLOC_OVERRIDE)
m_pFastSortInfo = reinterpret_cast<SortInfo_t *> (
MemAlloc_AllocAligned( (3 + nMaxFastInLeaf ) * sizeof( SortInfo_t ), sizeof( fltx4 ) ) );
#else
m_pFastSortInfo = reinterpret_cast<SortInfo_t*> (
::operator new((3 + nMaxFastInLeaf) * sizeof(SortInfo_t), std::nothrow));
#endif

#if !defined(NO_MALLOC_OVERRIDE)
m_pBuildoutBuffer = reinterpret_cast<FastSpriteQuadBuildoutBufferX4_t *> (
MemAlloc_AllocAligned(
( 1 + nMaxFastInLeaf / 4 ) * sizeof( FastSpriteQuadBuildoutBufferX4_t ),
sizeof( fltx4 ) ) );
#else
m_pBuildoutBuffer = reinterpret_cast<FastSpriteQuadBuildoutBufferX4_t*> (
::operator new((1 + nMaxFastInLeaf / 4) * sizeof(FastSpriteQuadBuildoutBufferX4_t), std::nothrow));
#endif
}

if ( nNumFastSpritesToAllocate )
{
Assert( ( nNumFastSpritesToAllocate & 3 ) == 0 );
Assert( ! m_pFastSpriteData ); // wtf? didn't free?
#if !defined(NO_MALLOC_OVERRIDE)
m_pFastSpriteData = reinterpret_cast<FastSpriteX4_t *> (
MemAlloc_AllocAligned(
( nNumFastSpritesToAllocate >> 2 ) * sizeof( FastSpriteX4_t ),
sizeof( fltx4 ) ) );
#else
m_pFastSpriteData = reinterpret_cast<FastSpriteX4_t*> (
::operator new((nNumFastSpritesToAllocate >> 2) * sizeof(FastSpriteX4_t), std::nothrow));
#endif
}

m_DetailObjects.EnsureCapacity( nNumOldStyleObjects );
Expand Down
4 changes: 4 additions & 0 deletions mp/src/game/client/hudelement.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ class CHudElement : public CGameEventListener
void* operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine )
{
Assert( stAllocateBlock != 0 );
#if !defined(NO_MALLOC_OVERRIDE)
void *pMem = MemAlloc_Alloc( stAllocateBlock, pFileName, nLine );
#else
void* pMem = malloc(stAllocateBlock);
#endif
memset( pMem, 0, stAllocateBlock );
return pMem;
}
Expand Down
8 changes: 8 additions & 0 deletions mp/src/game/server/ai_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,23 @@ class CAI_Component
void *operator new( size_t nBytes )
{
MEM_ALLOC_CREDIT();
#if !defined(NO_MALLOC_OVERRIDE)
void *pResult = MemAlloc_Alloc( nBytes );
#else
void* pResult = malloc(nBytes);
#endif
memset( pResult, 0, nBytes );
return pResult;
};

void *operator new( size_t nBytes, int nBlockUse, const char *pFileName, int nLine )
{
MEM_ALLOC_CREDIT();
#if !defined(NO_MALLOC_OVERRIDE)
void *pResult = MemAlloc_Alloc( nBytes, pFileName, nLine );
#else
void* pResult = malloc(nBytes);
#endif
memset( pResult, 0, nBytes );
return pResult;
}
Expand Down
2 changes: 1 addition & 1 deletion mp/src/game/server/server_base.vpc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ $Configuration
$Compiler
{
$AdditionalIncludeDirectories "$BASE;.\;$SRCDIR\game\shared;$SRCDIR\utils\common;$SRCDIR\game\shared\econ;$SRCDIR\game\server\NextBot"
$PreprocessorDefinitions "$BASE;GAME_DLL;VECTOR;VERSION_SAFE_STEAM_API_INTERFACES;PROTECTED_THINGS_ENABLE;sprintf=use_Q_snprintf_instead_of_sprintf;strncpy=use_Q_strncpy_instead;_snprintf=use_Q_snprintf_instead"
$PreprocessorDefinitions "$BASE;GAME_DLL;VECTOR;VERSION_SAFE_STEAM_API_INTERFACES;PROTECTED_THINGS_ENABLE;sprintf=use_Q_snprintf_instead_of_sprintf;strncpy=use_Q_strncpy_instead;_snprintf=use_Q_snprintf_instead;NO_MALLOC_OVERRIDE;REMEMBER_ALLOC_SIZE_FOR_VALGRIND"
$PreprocessorDefinitions "$BASE;SWDS" [$POSIX]
$PreprocessorDefinitions "$BASE;fopen=dont_use_fopen" [$WINDOWS||$X360]
$Create/UsePrecompiledHeader "Use Precompiled Header (/Yu)"
Expand Down
36 changes: 36 additions & 0 deletions mp/src/public/tier0/tslist.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,40 @@ class CTSListBase
// override new/delete so we can guarantee 8-byte aligned allocs
static void * operator new( size_t size )
{
#if !defined(NO_MALLOC_OVERRIDE)
CTSListBase *pNode = (CTSListBase *)MemAlloc_AllocAligned( size, TSLIST_HEAD_ALIGNMENT, __FILE__, __LINE__ );
#else
CTSListBase* pNode = (CTSListBase*)malloc(size);
#endif
return pNode;
}

static void * operator new( size_t size, int nBlockUse, const char *pFileName, int nLine )
{
#if !defined(NO_MALLOC_OVERRIDE)
CTSListBase *pNode = (CTSListBase *)MemAlloc_AllocAligned( size, TSLIST_HEAD_ALIGNMENT, pFileName, nLine );
#else
CTSListBase* pNode = (CTSListBase*)malloc(size);
#endif
return pNode;
}

static void operator delete( void *p)
{
#if !defined(NO_MALLOC_OVERRIDE)
MemAlloc_FreeAligned( p );
#else
free(p);
#endif
}

static void operator delete( void *p, int nBlockUse, const char *pFileName, int nLine )
{
#if !defined(NO_MALLOC_OVERRIDE)
MemAlloc_FreeAligned( p );
#else
free(p);
#endif
}

private:
Expand Down Expand Up @@ -417,7 +433,11 @@ class TSLIST_HEAD_ALIGN CTSList : public CTSListBase
// override new/delete so we can guarantee 8-byte aligned allocs
static void * operator new( size_t size )
{
#if !defined(NO_MALLOC_OVERRIDE)
Node_t *pNode = (Node_t *)MemAlloc_AllocAligned( size, TSLIST_NODE_ALIGNMENT, __FILE__, __LINE__ );
#else
Node_t* pNode = (Node_t*)malloc(size);
#endif
return pNode;
}

Expand All @@ -430,7 +450,11 @@ class TSLIST_HEAD_ALIGN CTSList : public CTSListBase

static void operator delete( void *p)
{
#if !defined(NO_MALLOC_OVERRIDE)
MemAlloc_FreeAligned( p );
#else
free(p);
#endif
}
static void operator delete( void *p, int nBlockUse, const char *pFileName, int nLine )
{
Expand Down Expand Up @@ -649,19 +673,31 @@ class TSLIST_HEAD_ALIGN CTSQueue
// override new/delete so we can guarantee 8-byte aligned allocs
static void * operator new( size_t size )
{
#if !defined(NO_MALLOC_OVERRIDE)
Node_t *pNode = (Node_t *)MemAlloc_AllocAligned( size, TSLIST_HEAD_ALIGNMENT, __FILE__, __LINE__ );
#else
Node_t* pNode = (Node_t*)malloc(size);
#endif
return pNode;
}

static void * operator new( size_t size, int nBlockUse, const char *pFileName, int nLine )
{
#if !defined(NO_MALLOC_OVERRIDE)
Node_t *pNode = (Node_t *)MemAlloc_AllocAligned( size, TSLIST_HEAD_ALIGNMENT, pFileName, nLine );
#else
Node_t* pNode = (Node_t*)malloc(size);
#endif
return pNode;
}

static void operator delete( void *p)
{
#if !defined(NO_MALLOC_OVERRIDE)
MemAlloc_FreeAligned( p );
#else
free(p);
#endif
}

static void operator delete( void *p, int nBlockUse, const char *pFileName, int nLine )
Expand Down

0 comments on commit ee1943f

Please sign in to comment.