[raknet] Implement BitStream::Reset()

This commit is contained in:
RD42 2024-02-12 20:34:08 +08:00
parent 3397c93362
commit b73cd8a5c2
2 changed files with 25 additions and 0 deletions

View File

@ -125,6 +125,28 @@ BitStream::~BitStream()
free( data ); // Use realloc and free so we are more efficient than delete and new for resizing
}
void BitStream::Reset( void )
{
// Note: Do NOT reallocate memory because BitStream is used
// in places to serialize/deserialize a buffer. Reallocation
// is a dangerous operation (may result in leaks).
if ( numberOfBitsUsed > 0 )
{
// memset(data, 0, BITS_TO_BYTES(numberOfBitsUsed));
}
// Don't free memory here for speed efficiency
//free(data); // Use realloc and free so we are more efficient than delete and new for resizing
numberOfBitsUsed = 0;
//numberOfBitsAllocated=8;
readOffset = 0;
//data=(unsigned char*)malloc(1);
// if (numberOfBitsAllocated>0)
// memset(data, 0, BITS_TO_BYTES(numberOfBitsAllocated));
}

View File

@ -47,6 +47,9 @@ namespace RakNet
/// Destructor
~BitStream();
/// Resets the bitstream for reuse.
void Reset( void );
/// Use this if you pass a pointer copy to the constructor
/// *(_copyData==false) and want to overallocate to prevent
/// *reallocation