doom3-bfg/neo/idlib/DataQueue.h

124 lines
2.2 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
#ifndef DATAQUEUE_H
#define DATAQUEUE_H
template< int maxItems, int maxBuffer >
class idDataQueue
{
2012-11-26 18:58:24 +00:00
public:
idDataQueue()
{
2012-11-26 18:58:24 +00:00
dataLength = 0;
}
bool Append( int sequence, const byte* b1, int b1Len, const byte* b2 = NULL, int b2Len = 0 );
2012-11-26 18:58:24 +00:00
void RemoveOlderThan( int sequence );
2019-11-11 19:27:44 +00:00
int GetDataLength() const
{
return dataLength;
}
2019-11-11 19:27:44 +00:00
int Num() const
{
return items.Num();
}
int ItemSequence( int i ) const
{
return items[i].sequence;
}
int ItemLength( int i ) const
{
return items[i].length;
}
const byte* ItemData( int i ) const
{
return &data[items[i].dataOffset];
}
2019-11-11 19:27:44 +00:00
void Clear()
{
dataLength = 0;
items.Clear();
memset( data, 0, sizeof( data ) );
}
2019-11-11 19:27:44 +00:00
2012-11-26 18:58:24 +00:00
private:
struct msgItem_t
{
2012-11-26 18:58:24 +00:00
int sequence;
int length;
int dataOffset;
};
idStaticList<msgItem_t, maxItems > items;
int dataLength;
byte data[ maxBuffer ];
};
/*
========================
idDataQueue::RemoveOlderThan
========================
*/
template< int maxItems, int maxBuffer >
void idDataQueue< maxItems, maxBuffer >::RemoveOlderThan( int sequence )
{
2012-11-26 18:58:24 +00:00
int length = 0;
while( items.Num() > 0 && items[0].sequence < sequence )
{
2012-11-26 18:58:24 +00:00
length += items[0].length;
items.RemoveIndex( 0 );
}
if( length >= dataLength )
{
2012-11-26 18:58:24 +00:00
assert( items.Num() == 0 );
assert( dataLength == length );
dataLength = 0;
}
else if( length > 0 )
{
2012-11-26 18:58:24 +00:00
memmove( data, data + length, dataLength - length );
dataLength -= length;
}
length = 0;
for( int i = 0; i < items.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
items[i].dataOffset = length;
length += items[i].length;
}
assert( length == dataLength );
}
/*
========================
idDataQueue::Append
========================
*/
template< int maxItems, int maxBuffer >
bool idDataQueue< maxItems, maxBuffer >::Append( int sequence, const byte* b1, int b1Len, const byte* b2, int b2Len )
{
if( items.Num() == items.Max() )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( dataLength + b1Len + b2Len >= maxBuffer )
{
2012-11-26 18:58:24 +00:00
return false;
}
msgItem_t& item = *items.Alloc();
2012-11-26 18:58:24 +00:00
item.length = b1Len + b2Len;
item.sequence = sequence;
item.dataOffset = dataLength;
if( b1 != NULL)
{
memcpy( data + dataLength, b1, b1Len );
dataLength += b1Len;
}
Update DataQueue.h to fix a build failure in Append method This is a quick and dirty fix for this gcc-13 build failure on ppc64el with -O3 optimization level: /usr/bin/g++-13 -DCPUSTRING=\"ppc64el\" -DUSE_DOOMCLASSIC -DUSE_EXCEPTIONS -DUSE_FFMPEG -DUSE_NEWER_JPEG -DUSE_OPENAL -D__DOOM__ -I/usr/include/imgui -I/usr/include/stb -I/<<PKGBUILDDIR>>/neo/. -I/<<PKGBUILDDIR>>/neo/idlib -isystem /usr/include/SDL2 -g -O3 -ffile-prefix-map=/<<PKGBUILDDIR>>=. -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fno-stack-clash-protection -fdebug-prefix-map=/<<PKGBUILDDIR>>=/usr/src/rbdoom3bfg-1.4.0+dfsg-3ubuntu2 -Wdate-time -D_FORTIFY_SOURCE=3 -std=c++11 -pipe -Werror=format-security -Werror=format -Wno-pragmas -Wno-unused-variable -Wno-switch -Wno-unused-value -Winvalid-pch -Wno-multichar -fno-strict-aliasing -MD -MT CMakeFiles/rbdoom3bfg.dir/sys/Snapshot_Jobs.cpp.o -MF CMakeFiles/rbdoom3bfg.dir/sys/Snapshot_Jobs.cpp.o.d -o CMakeFiles/rbdoom3bfg.dir/sys/Snapshot_Jobs.cpp.o -c /<<PKGBUILDDIR>>/neo/sys/Snapshot_Jobs.cpp In file included from /usr/include/string.h:548, from /<<PKGBUILDDIR>>/neo/idlib/sys/sys_includes.h:141, from /<<PKGBUILDDIR>>/neo/idlib/precompiled.h:34, from /<<PKGBUILDDIR>>/neo/sys/PacketProcessor.cpp:29: In function ‘memcpy’, inlined from ‘idDataQueue<64, 8000>::Append(int, unsigned char const*, int, unsigned char const*, int)’ at /<<PKGBUILDDIR>>/neo/idlib/../idlib/DataQueue.h:112:8, inlined from ‘idPacketProcessor::VerifyEmptyReliableQueue(unsigned char, unsigned char)’ at /<<PKGBUILDDIR>>/neo/sys/PacketProcessor.cpp:660:16: /usr/include/powerpc64le-linux-gnu/bits/string_fortified.h:29:33: error: argument 2 null where non-null expected [-Werror=nonnull] 29 | return __builtin___memcpy_chk (__dest, __src, __len, | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~ 30 | __glibc_objsize0 (__dest)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/powerpc64le-linux-gnu/bits/string_fortified.h:29:33: note: in a call to built-in function ‘__memcpy_chk’ cc1plus: some warnings being treated as errors
2024-08-01 06:17:34 +00:00
if( b2 != NULL )
{
memcpy( data + dataLength, b2, b2Len );
dataLength += b2Len;
}
2012-11-26 18:58:24 +00:00
return true;
}
#endif // DATAQUEUE_H