From 26b227e128475da3f88a6b34921a08994bf71cf4 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 9 Jan 2023 09:36:04 +0100 Subject: [PATCH] QVarLengthArray: Extract Method growBy() Separates the actual reallocation use-cases from the resizing ones when calling reallocate_impl, in preparation for fixing QTBUG-109745. Task-number: QTBUG-109745 Pick-to: 6.5 6.4 Change-Id: Iee0c3e56569ec7877beda8db3a645e2bbb6b4d4a Reviewed-by: Thiago Macieira --- src/corelib/tools/qvarlengtharray.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 469fbda2aac..204364b52fe 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -180,11 +180,13 @@ public: return qHashRange(begin(), end(), seed); } protected: + void growBy(qsizetype prealloc, void *array, qsizetype increment) + { reallocate_impl(prealloc, array, size(), (std::max)(size() * 2, increment)); } template reference emplace_back_impl(qsizetype prealloc, void *array, Args&&...args) { if (size() == capacity()) // ie. size() != 0 - reallocate_impl(prealloc, array, size(), size() << 1); + growBy(prealloc, array, 1); reference r = *new (end()) T(std::forward(args)...); ++s; return r; @@ -701,7 +703,7 @@ Q_OUTOFLINE_TEMPLATE void QVLABase::append_impl(qsizetype prealloc, void *arr const qsizetype asize = size() + increment; if (asize >= capacity()) - reallocate_impl(prealloc, array, size(), qMax(size() * 2, asize)); + growBy(prealloc, array, increment); if constexpr (QTypeInfo::isComplex) std::uninitialized_copy_n(abuf, increment, end()); @@ -842,7 +844,7 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase::emplace_impl(qsizetype prealloc, void *ar qsizetype offset = qsizetype(before - cbegin()); if (size() == capacity()) - reallocate_impl(prealloc, array, size(), size() * 2); + growBy(prealloc, array, 1); if constexpr (!QTypeInfo::isRelocatable) { T *b = begin() + offset; T *i = end();