Skip to content

Commit

Permalink
QVarLengthArray: Extract Method growBy()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
marcmutz committed Jan 12, 2023
1 parent 7c5ff43 commit 26b227e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/corelib/tools/qvarlengtharray.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ class QVLABase : public QVLABaseBase
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 <typename...Args>
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>(args)...);
++s;
return r;
Expand Down Expand Up @@ -701,7 +703,7 @@ Q_OUTOFLINE_TEMPLATE void QVLABase<T>::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<T>::isComplex)
std::uninitialized_copy_n(abuf, increment, end());
Expand Down Expand Up @@ -842,7 +844,7 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase<T>::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<T>::isRelocatable) {
T *b = begin() + offset;
T *i = end();
Expand Down

0 comments on commit 26b227e

Please sign in to comment.