[*] Unfuck/simplify bytebuffers resize

This commit is contained in:
Reece Wilson 2023-10-22 06:32:55 +01:00
parent fb1920ccba
commit 6d49de430a

View File

@ -139,6 +139,17 @@ namespace Aurora::Memory
return;
}
if (this->base &&
this->length == 0)
{
Memory::Free(this->base);
this->base = nullptr;
this->allocSize = 0;
this->readPtr = nullptr;
this->writePtr = nullptr;
return;
}
if (!this->base)
{
return;
@ -149,8 +160,15 @@ namespace Aurora::Memory
return;
}
if (this->flagCircular)
{
return;
}
auto uReadOffset = this->readPtr - this->base;
auto uWriteOffset = this->writePtr - this->base;
auto temp = Memory::ZRealloc(this->base, this->length);
if (!temp)
{
return;
@ -158,6 +176,8 @@ namespace Aurora::Memory
this->base = temp;
this->allocSize = this->length;
this->readPtr = temp + uReadOffset;
this->writePtr = temp + uWriteOffset;
}
bool ByteBuffer::Resize(AuUInt length)
@ -167,9 +187,6 @@ namespace Aurora::Memory
return false;
}
AuUInt oldWriteIdx, oldReadIdx, oldLength, newLength;
AuUInt8 *nextRead, *nextWrite, *nextPtr;
if (length == 0)
{
this->length = 0;
@ -183,72 +200,30 @@ namespace Aurora::Memory
return Allocate(length, false);
}
if (this->allocSize > length)
ByteBuffer replacement(length, (bool)this->flagCircular, (bool)this->flagExpandable);
if (!replacement)
{
this->length = length;
oldLength = this->length;
newLength = length;
nextPtr = this->base;
oldWriteIdx = this->writePtr - this->base;
oldReadIdx = this->readPtr - this->base;
nextRead = nextPtr + oldReadIdx;
nextWrite = nextPtr + oldWriteIdx;
return false;
}
else
replacement.flagAlwaysExpandable = this->flagAlwaysExpandable;
auto pOldHead = this->readPtr;
auto pOldWrite = this->writePtr;
AuUInt uBytesRem {};
if (!this->flagCircular && length < (uBytesRem = this->RemainingBytes()))
{
auto scale = GetAllocationPower();
oldLength = this->length;
newLength = AuMax(AuUInt(length), (((this->allocSize * 2) / scale) + 1) * scale);// AuUInt(((this->allocSize / scale) + 2) * scale)); < this spends too much time spinning
nextPtr = ZRealloc(this->base, newLength);
if (!nextPtr)
{
return false;
}
oldWriteIdx = this->writePtr - this->base;
oldReadIdx = this->readPtr - this->base;
if (!flagCircular)
{
oldWriteIdx = AuMin<AuUInt>(length, oldWriteIdx);
oldReadIdx = AuMin<AuUInt>(length, oldReadIdx);
}
nextRead = nextPtr + oldReadIdx;
nextWrite = nextPtr + oldWriteIdx;
this->allocSize = newLength;
this->length = length;
this->writePtr = pOldHead + uBytesRem;
}
this->base = nextPtr;
if (!flagCircular)
if (!replacement.WriteFrom(*this))
{
this->readPtr = nextRead;
this->writePtr = nextWrite;
}
else
{
if (this->writePtr > this->readPtr)
{
this->readPtr = nextRead;
this->writePtr = nextWrite;
}
else
{
auto expansion = newLength - oldLength;
auto movableTail = AuMin(oldWriteIdx, expansion);
AuMemcpy(nextPtr + oldLength, nextPtr, movableTail);
this->readPtr = nextRead;
this->writePtr = nextPtr + oldLength + movableTail;
}
this->readPtr = pOldHead;
this->writePtr = pOldWrite;
return false;
}
this->operator=(AuMove(replacement));
return true;
}
}