Skip to content

Commit

Permalink
server: entities private data allocation moved to server library
Browse files Browse the repository at this point in the history
This is required for compatibility with Clang. Thanks to SamVanheer for the idea.
  • Loading branch information
SNMetamorph committed Dec 22, 2024
1 parent ce04b69 commit 5f00739
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
22 changes: 12 additions & 10 deletions server/cbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,19 +484,19 @@ class CBaseEntity

virtual void MoveDone( void ) { if( m_pfnMoveDone )(this->*m_pfnMoveDone)(); };

// allow engine to allocate instance data
void *operator new( size_t stAllocateBlock, entvars_t *pev )
void *operator new(size_t stAllocateBlock)
{
return (void *)ALLOC_PRIVATE(ENT(pev), static_cast<int>(stAllocateBlock));
void *block = ::operator new(stAllocateBlock);
std::memset(block, 0, stAllocateBlock);
return block;
};

// don't use this.
#if _MSC_VER >= 1200 // only build this code if MSVC++ 6.0 or higher
void operator delete(void *pMem, entvars_t *pev)
// don't call delete on entities directly, tell the engine to delete it instead
void operator delete(void *pMem)
{
pev->flags |= FL_KILLME;
::operator delete(pMem);
};
#endif

void UpdateOnRemove( void );

// common member functions
Expand Down Expand Up @@ -1081,7 +1081,8 @@ template <class T> T * GetClassPtr( T *a )
if (a == NULL)
{
// allocate private data
a = new(pev) T;
a = new T;
pev->pContainingEntity->pvPrivateData = a; // replicate the ALLOC_PRIVATE engine function's behavior
a->pev = pev;
}
return a;
Expand All @@ -1101,7 +1102,8 @@ template <class T> T * GetClassPtr( T *newEnt, const char *className )
if (newEnt == NULL)
{
// allocate private data
newEnt = new(pev) T;
newEnt = new T;
pev->pContainingEntity->pvPrivateData = newEnt; // replicate the ALLOC_PRIVATE engine function's behavior
newEnt->pev = pev;
}
newEnt->SetClassname( className );
Expand Down
3 changes: 2 additions & 1 deletion server/dll_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@ void OnFreeEntPrivateData( edict_s *pEdict )
{
CBaseEntity *pEntity = CBaseEntity::Instance(pEdict);
pEntity->UpdateOnRemove();
pEntity->~CBaseEntity();
delete pEntity;
pEdict->pvPrivateData = nullptr; // zero this out so the engine doesn't try to free it again
}
}

Expand Down

0 comments on commit 5f00739

Please sign in to comment.