[*] Unfuck/simplify bytebuffers resize
This commit is contained in:
parent
fb1920ccba
commit
6d49de430a
@ -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;
|
||||
}
|
||||
|
||||
auto temp = Memory::ZRealloc(this->base, this->length);
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
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)
|
||||
ByteBuffer replacement(length, (bool)this->flagCircular, (bool)this->flagExpandable);
|
||||
if (!replacement)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
replacement.flagAlwaysExpandable = this->flagAlwaysExpandable;
|
||||
|
||||
oldWriteIdx = this->writePtr - this->base;
|
||||
oldReadIdx = this->readPtr - this->base;
|
||||
auto pOldHead = this->readPtr;
|
||||
auto pOldWrite = this->writePtr;
|
||||
AuUInt uBytesRem {};
|
||||
|
||||
if (!flagCircular)
|
||||
if (!this->flagCircular && length < (uBytesRem = this->RemainingBytes()))
|
||||
{
|
||||
oldWriteIdx = AuMin<AuUInt>(length, oldWriteIdx);
|
||||
oldReadIdx = AuMin<AuUInt>(length, oldReadIdx);
|
||||
this->writePtr = pOldHead + uBytesRem;
|
||||
}
|
||||
|
||||
nextRead = nextPtr + oldReadIdx;
|
||||
nextWrite = nextPtr + oldWriteIdx;
|
||||
|
||||
this->allocSize = newLength;
|
||||
this->length = length;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user