From 61be5c94ed84ed4395eec74257102d73324df8a2 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 30 Nov 2021 16:35:06 +0100 Subject: [PATCH] QVLA: separate control from inline storage [5/N]: Move reallocate() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is the core of QVarLengthArray. To move it down into QVLABase, we need to pass Prealloc and this->array as additional function arguments. Task-number: QTBUG-84785 Change-Id: I082fa4ef957fcde7b8fcc3ca421621aa01ba5d59 Reviewed-by: Fabian Kosmale Reviewed-by: MÃ¥rten Nordheim --- src/corelib/tools/qvarlengtharray.h | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 365be9d241..4c8fbc3e0e 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -205,6 +205,8 @@ protected: return std::lexicographical_compare(begin(), end(), other.begin(), other.end()); } + void reallocate_impl(qsizetype prealloc, void *array, qsizetype size, qsizetype alloc); + bool isValidIterator(const const_iterator &i) const { const std::less less = {}; @@ -579,7 +581,8 @@ private: bool less_than(const QVarLengthArray &other) const { return Base::less_than(other); } - void reallocate(qsizetype size, qsizetype alloc); + void reallocate(qsizetype sz, qsizetype alloc) + { Base::reallocate_impl(Prealloc, this->array, sz, alloc); } using Base::isValidIterator; }; @@ -678,8 +681,8 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::append(const T *abuf, qs this->s = asize; } -template -Q_OUTOFLINE_TEMPLATE void QVarLengthArray::reallocate(qsizetype asize, qsizetype aalloc) +template +Q_OUTOFLINE_TEMPLATE void QVLABase::reallocate_impl(qsizetype prealloc, void *array, qsizetype asize, qsizetype aalloc) { Q_ASSERT(aalloc >= asize); Q_ASSERT(data()); @@ -696,24 +699,24 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::reallocate(qsizetype asi std::unique_ptr guard; void *newPtr; qsizetype newA; - if (aalloc > Prealloc) { + if (aalloc > prealloc) { newPtr = malloc(aalloc * sizeof(T)); guard.reset(newPtr); Q_CHECK_PTR(newPtr); // could throw // by design: in case of QT_NO_EXCEPTIONS malloc must not fail or it crashes here newA = aalloc; } else { - newPtr = this->array; - newA = Prealloc; + newPtr = array; + newA = prealloc; } QtPrivate::q_uninitialized_relocate_n(oldPtr, copySize, reinterpret_cast(newPtr)); // commit: - this->ptr = newPtr; + ptr = newPtr; guard.release(); - this->a = newA; + a = newA; } - this->s = copySize; + s = copySize; // destroy remaining old objects if constexpr (QTypeInfo::isComplex) { @@ -721,17 +724,17 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::reallocate(qsizetype asi std::destroy(oldPtr + asize, oldPtr + osize); } - if (oldPtr != reinterpret_cast(this->array) && oldPtr != data()) + if (oldPtr != reinterpret_cast(array) && oldPtr != data()) free(oldPtr); if constexpr (QTypeInfo::isComplex) { // call default constructor for new objects (which can throw) while (size() < asize) { new (data() + size()) T; - ++this->s; + ++s; } } else { - this->s = asize; + s = asize; } }