From 504972f838761f79a170c22225add496e7e5af6a Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Tue, 3 Nov 2020 11:26:20 +0100 Subject: Refine {QString, QBA}::reallocData() logic Fixed misleading naming of "slowReallocatePath". It's no longer "slow", it's downright dangerous now to reallocate under certain conditions While at it, added extra assert to QArrayData::reallocateUnaligned() and cleaned up that function a bit Change-Id: I05921fb5058eb563997e66107566c87fb4ea5599 Reviewed-by: Lars Knoll Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/corelib/text/qstring.cpp') diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 32e5bb7b20..eed98e7c7e 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2504,12 +2504,11 @@ void QString::reallocData(qsizetype alloc, QArrayData::AllocationOption option) return; } - // 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; + // 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(); - if (d->needsDetach() || slowReallocatePath) { + if (d->needsDetach() || cannotUseReallocate) { DataPointer dd(Data::allocate(alloc, option), qMin(alloc, d.size)); if (dd.size > 0) ::memcpy(dd.data(), d.data(), dd.size * sizeof(QChar)); -- cgit v1.2.3