Revert "Refine {QString, QBA}::reallocData() logic"

This reverts commit 504972f838761f79a170c22225add496e7e5af6a.

Introduced realloc failures in qdoc.

Task-number: QTBUG-88258
Change-Id: I953e8d3933085022c75068af357ec9a44ab7e984
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Volker Hilsheimer 2020-11-07 17:06:18 +01:00
parent 0b0118ef28
commit c5a51926d6
3 changed files with 13 additions and 14 deletions

View File

@ -1708,11 +1708,12 @@ void QByteArray::reallocData(qsizetype alloc, QArrayData::AllocationOption optio
return;
}
// don't use reallocate path when reducing capacity and there's free space
// at the beginning: might shift data pointer outside of allocated space
const bool cannotUseReallocate = d.freeSpaceAtBegin() > 0 && alloc < d.constAllocatedCapacity();
// there's a case of slow reallocate path where we need to memmove the data
// before a call to ::realloc(), meaning that there's an extra "heavy"
// operation. just prefer ::malloc() branch in this case
const bool slowReallocatePath = d.freeSpaceAtBegin() > 0;
if (d->needsDetach() || cannotUseReallocate) {
if (d->needsDetach() || slowReallocatePath) {
DataPointer dd(Data::allocate(alloc, option), qMin(alloc, d.size));
if (dd.size > 0)
::memcpy(dd.data(), d.data(), dd.size);

View File

@ -2504,11 +2504,12 @@ void QString::reallocData(qsizetype alloc, QArrayData::AllocationOption option)
return;
}
// don't use reallocate path when reducing capacity and there's free space
// at the beginning: might shift data pointer outside of allocated space
const bool cannotUseReallocate = d.freeSpaceAtBegin() > 0 && alloc < d.constAllocatedCapacity();
// there's a case of slow reallocate path where we need to memmove the data
// before a call to ::realloc(), meaning that there's an extra "heavy"
// operation. just prefer ::malloc() branch in this case
const bool slowReallocatePath = d.freeSpaceAtBegin() > 0;
if (d->needsDetach() || cannotUseReallocate) {
if (d->needsDetach() || slowReallocatePath) {
DataPointer dd(Data::allocate(alloc, option), qMin(alloc, d.size));
if (dd.size > 0)
::memcpy(dd.data(), d.data(), dd.size * sizeof(QChar));

View File

@ -233,13 +233,10 @@ QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer,
{
Q_ASSERT(!data || !data->isShared());
const qsizetype headerSize = sizeof(QArrayData);
qsizetype headerSize = sizeof(QArrayData);
qsizetype allocSize = calculateBlockSize(capacity, objectSize, headerSize, option);
const qptrdiff offset = dataPointer
? reinterpret_cast<char *>(dataPointer) - reinterpret_cast<char *>(data)
: headerSize;
qptrdiff offset = dataPointer ? reinterpret_cast<char *>(dataPointer) - reinterpret_cast<char *>(data) : headerSize;
Q_ASSERT(offset > 0);
Q_ASSERT(offset <= allocSize); // == when all free space is at the beginning
allocSize = reserveExtraBytes(allocSize);
if (Q_UNLIKELY(allocSize < 0)) // handle overflow. cannot reallocate reliably
@ -247,7 +244,7 @@ QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer,
QArrayData *header = static_cast<QArrayData *>(::realloc(data, size_t(allocSize)));
if (header) {
header->alloc = capacity;
header->alloc = uint(capacity);
dataPointer = reinterpret_cast<char *>(header) + offset;
} else {
dataPointer = nullptr;