[*] Redo ByteBuffer::Pad for write streams

This commit is contained in:
Reece Wilson 2024-06-01 23:53:33 +01:00
parent a1c1e88f51
commit 588322252f
2 changed files with 55 additions and 28 deletions

View File

@ -19,35 +19,48 @@ namespace Aurora::Memory
return Resize(this->length - tail);
}
bool ByteBuffer::Fill(AuUInt length, AuUInt8 magicCharacter)
bool ByteBuffer::Fill(AuUInt uLength, AuUInt8 magicCharacter)
{
ByteBufferPushWriteState a(*this);
auto maxLinear = this->writePtr - AuMin(this->readPtr, this->base + this->length);
maxLinear = AuMin(AuUInt(maxLinear), length);
if (maxLinear)
AuUInt32 uLinear {};
if (auto view = this->GetNextLinearWrite())
{
AuMemset(this->writePtr, magicCharacter, maxLinear);
this->writePtr += maxLinear;
uLinear = view.ToLength();
AuMemset(view.Begin(), magicCharacter, uLinear);
this->writePtr += uLinear;
}
if (maxLinear != length)
if (uLinear != uLength)
{
auto delta = length - maxLinear;
auto delta = uLength - uLinear;
AuArray<AuUInt8, 32> tempA;
if (delta > 32)
{
// HACK:
Memory::ByteBuffer inner(delta);
if (!inner)
{
return {};
Memory::ByteBuffer inner(delta);
if (!inner)
{
return {};
}
AuMemset(inner.base, magicCharacter, inner.length);
if (!Write(inner.base, inner.length))
{
return {};
}
}
AuMemset(inner.base, magicCharacter, inner.length);
if (!Write(inner.base, inner.length))
else
{
return {};
AuMemset(tempA.data(), magicCharacter, 32);
if (!Write(tempA.data(), delta))
{
return {};
}
}
}
@ -58,22 +71,21 @@ namespace Aurora::Memory
{
ByteBufferPushWriteState a(*this);
auto oldLen = this->length;
auto newLen = ((this->length + aPowOf2) & ~(aPowOf2 - 1));
auto oldLen = this->writePtr - this->base;
auto length = this->flagCircular ? this->RemainingBytes() : oldLen;
auto newLen = AuPageRoundUp<AuUInt>(length, aPowOf2);
if (!Resize(newLen))
{
return {};
return false;
}
auto old = this->writePtr - this->base;
this->writePtr = this->base + oldLen;
if (!this->Fill(newLen - oldLen))
if (!this->Fill(newLen - oldLen, magicCharacter))
{
return {};
return false;
}
this->writePtr = this->base + old;
this->writePtr = this->base + length;
return true;
}
}

View File

@ -268,7 +268,22 @@ namespace Aurora::Memory
return this->length;
}
template<typename T>
AuUInt Length() const
{
return this->length;
}
AuUInt ToSize() const
{
return this->length;
}
AuUInt Size() const
{
return this->length;
}
template<typename T = AuUInt8>
AuUInt ToCount() const
{
return this->length / sizeof(T);