[*] 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; 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) if (!this->base)
{ {
return; return;
@ -149,8 +160,15 @@ namespace Aurora::Memory
return; 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); auto temp = Memory::ZRealloc(this->base, this->length);
if (!temp) if (!temp)
{ {
return; return;
@ -158,6 +176,8 @@ namespace Aurora::Memory
this->base = temp; this->base = temp;
this->allocSize = this->length; this->allocSize = this->length;
this->readPtr = temp + uReadOffset;
this->writePtr = temp + uWriteOffset;
} }
bool ByteBuffer::Resize(AuUInt length) bool ByteBuffer::Resize(AuUInt length)
@ -167,9 +187,6 @@ namespace Aurora::Memory
return false; return false;
} }
AuUInt oldWriteIdx, oldReadIdx, oldLength, newLength;
AuUInt8 *nextRead, *nextWrite, *nextPtr;
if (length == 0) if (length == 0)
{ {
this->length = 0; this->length = 0;
@ -183,72 +200,30 @@ namespace Aurora::Memory
return Allocate(length, false); return Allocate(length, false);
} }
if (this->allocSize > length) ByteBuffer replacement(length, (bool)this->flagCircular, (bool)this->flagExpandable);
if (!replacement)
{ {
this->length = length; return false;
oldLength = this->length;
newLength = length;
nextPtr = this->base;
oldWriteIdx = this->writePtr - this->base;
oldReadIdx = this->readPtr - this->base;
nextRead = nextPtr + oldReadIdx;
nextWrite = nextPtr + oldWriteIdx;
} }
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(); this->writePtr = pOldHead + uBytesRem;
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->base = nextPtr; if (!replacement.WriteFrom(*this))
if (!flagCircular)
{ {
this->readPtr = nextRead; this->readPtr = pOldHead;
this->writePtr = nextWrite; this->writePtr = pOldWrite;
} return false;
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->operator=(AuMove(replacement));
return true; return true;
} }
} }