Add CSnapshot::EmptySnapshot, mark pointer arguments as const

Instead of keeping track of a permanently empty `CSnapshot` object in client and server separately, add `CSnapshot::EmptySnapshot` to access a singleton empty `CSnapshot`.

Mark pointer parameters of snapshot functions as `const` when possible.
This commit is contained in:
Robert Müller 2023-09-26 20:03:45 +02:00
parent 875c51e28f
commit 74192b9051
4 changed files with 21 additions and 27 deletions

View file

@ -1985,8 +1985,6 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket, int Conn, bool Dummy)
if((NumParts < CSnapshot::MAX_PARTS && m_aSnapshotParts[Conn] == (((uint64_t)(1) << NumParts) - 1)) ||
(NumParts == CSnapshot::MAX_PARTS && m_aSnapshotParts[Conn] == std::numeric_limits<uint64_t>::max()))
{
static CSnapshot Emptysnap;
CSnapshot *pDeltaShot = &Emptysnap;
unsigned char aTmpBuffer2[CSnapshot::MAX_SIZE];
unsigned char aTmpBuffer3[CSnapshot::MAX_SIZE];
CSnapshot *pTmpBuffer3 = (CSnapshot *)aTmpBuffer3; // Fix compiler warning for strict-aliasing
@ -1995,9 +1993,7 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket, int Conn, bool Dummy)
m_aSnapshotParts[Conn] = 0;
// find snapshot that we should use as delta
Emptysnap.Clear();
// find delta
const CSnapshot *pDeltaShot = CSnapshot::EmptySnapshot();
if(DeltaTick >= 0)
{
int DeltashotSize = m_aSnapshotStorage[Conn].Get(DeltaTick, 0, &pDeltaShot, 0);

View file

@ -974,11 +974,8 @@ void CServer::DoSnapshot()
m_aClients[i].m_Snapshots.Add(m_CurrentGameTick, time_get(), SnapshotSize, pData, 0, nullptr);
// find snapshot that we can perform delta against
static CSnapshot s_EmptySnap;
s_EmptySnap.Clear();
int DeltaTick = -1;
CSnapshot *pDeltashot = &s_EmptySnap;
const CSnapshot *pDeltashot = CSnapshot::EmptySnapshot();
{
int DeltashotSize = m_aClients[i].m_Snapshots.Get(m_aClients[i].m_LastAckedSnapshot, 0, &pDeltashot, 0);
if(DeltashotSize >= 0)

View file

@ -19,6 +19,8 @@ const CSnapshotItem *CSnapshot::GetItem(int Index) const
return (const CSnapshotItem *)(DataStart() + Offsets()[Index]);
}
const CSnapshot CSnapshot::ms_EmptySnapshot;
int CSnapshot::GetItemSize(int Index) const
{
if(Index == m_NumItems - 1)
@ -168,7 +170,7 @@ inline size_t CalcHashID(int Key)
return Hash % HASHLIST_SIZE;
}
static void GenerateHash(CItemList *pHashlist, CSnapshot *pSnapshot)
static void GenerateHash(CItemList *pHashlist, const CSnapshot *pSnapshot)
{
for(int i = 0; i < HASHLIST_SIZE; i++)
pHashlist[i].m_Num = 0;
@ -215,7 +217,7 @@ int CSnapshotDelta::DiffItem(const int *pPast, const int *pCurrent, int *pOut, i
return Needed;
}
void CSnapshotDelta::UndiffItem(const int *pPast, int *pDiff, int *pOut, int Size, int *pDataRate)
void CSnapshotDelta::UndiffItem(const int *pPast, const int *pDiff, int *pOut, int Size, int *pDataRate)
{
while(Size)
{
@ -267,7 +269,7 @@ const CSnapshotDelta::CData *CSnapshotDelta::EmptyDelta() const
}
// TODO: OPT: this should be made much faster
int CSnapshotDelta::CreateDelta(CSnapshot *pFrom, CSnapshot *pTo, void *pDstData)
int CSnapshotDelta::CreateDelta(const CSnapshot *pFrom, CSnapshot *pTo, void *pDstData)
{
CData *pDelta = (CData *)pDstData;
int *pData = (int *)pDelta->m_aData;
@ -357,7 +359,7 @@ static int RangeCheck(void *pEnd, void *pPtr, int Size)
return 0;
}
int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pSrcData, int DataSize)
int CSnapshotDelta::UnpackDelta(const CSnapshot *pFrom, CSnapshot *pTo, const void *pSrcData, int DataSize)
{
CData *pDelta = (CData *)pSrcData;
int *pData = (int *)pDelta->m_aData;
@ -509,7 +511,7 @@ void CSnapshotStorage::PurgeUntil(int Tick)
m_pLast = 0;
}
void CSnapshotStorage::Add(int Tick, int64_t Tagtime, int DataSize, void *pData, int AltDataSize, void *pAltData)
void CSnapshotStorage::Add(int Tick, int64_t Tagtime, int DataSize, const void *pData, int AltDataSize, const void *pAltData)
{
// allocate memory for holder + snapshot_data
int TotalSize = sizeof(CHolder) + DataSize;
@ -550,7 +552,7 @@ void CSnapshotStorage::Add(int Tick, int64_t Tagtime, int DataSize, void *pData,
m_pLast = pHolder;
}
int CSnapshotStorage::Get(int Tick, int64_t *pTagtime, CSnapshot **ppData, CSnapshot **ppAltData)
int CSnapshotStorage::Get(int Tick, int64_t *pTagtime, const CSnapshot **ppData, const CSnapshot **ppAltData)
{
CHolder *pHolder = m_pFirst;

View file

@ -26,8 +26,8 @@ public:
class CSnapshot
{
friend class CSnapshotBuilder;
int m_DataSize;
int m_NumItems;
int m_DataSize = 0;
int m_NumItems = 0;
int *Offsets() const { return (int *)(this + 1); }
char *DataStart() const { return (char *)(Offsets() + m_NumItems); }
@ -35,6 +35,8 @@ class CSnapshot
size_t OffsetSize() const { return sizeof(int) * m_NumItems; }
size_t TotalSize() const { return sizeof(CSnapshot) + OffsetSize() + m_DataSize; }
static const CSnapshot ms_EmptySnapshot;
public:
enum
{
@ -46,11 +48,6 @@ public:
MAX_SIZE = MAX_PARTS * 1024
};
void Clear()
{
m_DataSize = 0;
m_NumItems = 0;
}
int NumItems() const { return m_NumItems; }
const CSnapshotItem *GetItem(int Index) const;
int GetItemSize(int Index) const;
@ -62,6 +59,8 @@ public:
unsigned Crc();
void DebugDump();
bool IsValid(size_t ActualSize) const;
static const CSnapshot *EmptySnapshot() { return &ms_EmptySnapshot; }
};
// CSnapshotDelta
@ -88,7 +87,7 @@ private:
int m_aSnapshotDataUpdates[CSnapshot::MAX_TYPE + 1];
CData m_Empty;
static void UndiffItem(const int *pPast, int *pDiff, int *pOut, int Size, int *pDataRate);
static void UndiffItem(const int *pPast, const int *pDiff, int *pOut, int Size, int *pDataRate);
public:
static int DiffItem(const int *pPast, const int *pCurrent, int *pOut, int Size);
@ -98,8 +97,8 @@ public:
int GetDataUpdates(int Index) const { return m_aSnapshotDataUpdates[Index]; }
void SetStaticsize(int ItemType, int Size);
const CData *EmptyDelta() const;
int CreateDelta(class CSnapshot *pFrom, class CSnapshot *pTo, void *pDstData);
int UnpackDelta(class CSnapshot *pFrom, class CSnapshot *pTo, const void *pSrcData, int DataSize);
int CreateDelta(const class CSnapshot *pFrom, class CSnapshot *pTo, void *pDstData);
int UnpackDelta(const class CSnapshot *pFrom, class CSnapshot *pTo, const void *pSrcData, int DataSize);
};
// CSnapshotStorage
@ -131,8 +130,8 @@ public:
void Init();
void PurgeAll();
void PurgeUntil(int Tick);
void Add(int Tick, int64_t Tagtime, int DataSize, void *pData, int AltDataSize, void *pAltData);
int Get(int Tick, int64_t *pTagtime, CSnapshot **ppData, CSnapshot **ppAltData);
void Add(int Tick, int64_t Tagtime, int DataSize, const void *pData, int AltDataSize, const void *pAltData);
int Get(int Tick, int64_t *pTagtime, const CSnapshot **ppData, const CSnapshot **ppAltData);
};
class CSnapshotBuilder