Saveload: Move AcquireBytes loop out of CheckBytes

This commit is contained in:
Jonathan G Rennison 2024-08-08 20:57:56 +01:00
parent 0ab976e069
commit d0cbc48f7d
2 changed files with 13 additions and 7 deletions

View File

@ -139,18 +139,24 @@ void ReadBuffer::SkipBytesSlowPath(size_t bytes)
}
}
void ReadBuffer::AcquireBytes()
void ReadBuffer::AcquireBytes(size_t bytes)
{
size_t remainder = this->bufe - this->bufp;
if (remainder) {
memmove(this->buf, this->bufp, remainder);
}
size_t len = this->reader->Read(this->buf + remainder, lengthof(this->buf) - remainder);
if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
size_t total = remainder;
size_t target = remainder + bytes;
do {
size_t len = this->reader->Read(this->buf + total, lengthof(this->buf) - total);
if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
this->read += len;
total += len;
} while (total < target);
this->read += total - remainder;
this->bufp = this->buf;
this->bufe = this->buf + remainder + len;
this->bufe = this->buf + total;
}
void MemoryDumper::FinaliseBlock()

View File

@ -106,7 +106,7 @@ struct ReadBuffer {
static ReadBuffer *GetCurrent();
void SkipBytesSlowPath(size_t bytes);
void AcquireBytes();
void AcquireBytes(size_t bytes = 0);
inline void SkipBytes(size_t bytes)
{
@ -138,7 +138,7 @@ struct ReadBuffer {
inline void CheckBytes(size_t bytes)
{
while (unlikely(this->bufp + bytes > this->bufe)) this->AcquireBytes();
if (unlikely(this->bufp + bytes > this->bufe)) this->AcquireBytes(bytes);
}
inline RawReadBuffer ReadRawBytes(size_t bytes)