From 156eff71fb481ea7e8ec6d648b098adc8d94e4b9 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Wed, 14 Jul 2021 02:03:24 +0300 Subject: [PATCH] simplify QList::append(), QList::prepend() and QList::insert() [ci reset] Signed-off-by: Ivailo Monev --- src/core/tools/qlist.cpp | 49 ------------ src/core/tools/qlist.h | 131 +++++++++------------------------ src/gui/image/qiconloader.cpp | 2 + src/gui/painting/qrasterizer.cpp | 8 +- src/gui/util/qcompleter.cpp | 2 + tests/auto/qsqlfield/tst_qsqlfield.cpp | 1 + 6 files changed, 44 insertions(+), 149 deletions(-) diff --git a/src/core/tools/qlist.cpp b/src/core/tools/qlist.cpp index e953a71bd..35bf051c2 100644 --- a/src/core/tools/qlist.cpp +++ b/src/core/tools/qlist.cpp @@ -19,10 +19,7 @@ ** ****************************************************************************/ -#include #include "qlist.h" -#include "qtools_p.h" -#include QT_BEGIN_NAMESPACE @@ -40,52 +37,6 @@ QT_BEGIN_NAMESPACE QListData::Data QListData::shared_null = { QAtomicInt(1), 0, 0, 0, { 0 } }; /*! - * Detaches the QListData by allocating new memory for a list which will be bigger - * than the copied one and is expected to grow further. - * *idx is the desired insertion point and is clamped to the actual size of the list. - * num is the number of new elements to insert at the insertion point. - * Returns the old (shared) data, it is up to the caller to deref() and free(). - * For the new data node_copy needs to be called. - * - * \internal - */ -QListData::Data *QListData::detach_grow(int *idx, int num) -{ - Data *x = d; - int l = x->end - x->begin; - int nl = l + num; - int alloc = qAllocMore(nl * QT_POINTER_SIZE, QListData::DataHeaderSize) / QT_POINTER_SIZE; - Data* t = static_cast(::malloc(DataHeaderSize + alloc * QT_POINTER_SIZE)); - Q_CHECK_PTR(t); - - t->ref = 1; - t->alloc = alloc; - // The space reservation algorithm's optimization is biased towards appending: - // Something which looks like an append will put the data at the beginning, - // while something which looks like a prepend will put it in the middle - // instead of at the end. That's based on the assumption that prepending - // is uncommon and even an initial prepend will eventually be followed by - // at least some appends. - int bg; - if (*idx < 0) { - *idx = 0; - bg = (alloc - nl) >> 1; - } else if (*idx > l) { - *idx = l; - bg = 0; - } else if (*idx < (l >> 1)) { - bg = (alloc - nl) >> 1; - } else { - bg = 0; - } - t->begin = bg; - t->end = bg + nl; - d = t; - - return x; -} - -/*! * Detaches the QListData by allocating new memory for a list which possibly * has a different size than the copied one. * Returns the old (shared) data, it is up to the caller to deref() and free() diff --git a/src/core/tools/qlist.h b/src/core/tools/qlist.h index 6769d1c46..a01402f1e 100644 --- a/src/core/tools/qlist.h +++ b/src/core/tools/qlist.h @@ -33,7 +33,6 @@ #endif #include -#include #include @@ -52,7 +51,6 @@ struct Q_CORE_EXPORT QListData { enum { DataHeaderSize = sizeof(Data) - QT_POINTER_SIZE }; Data *detach(int alloc); - Data *detach_grow(int *i, int n); static void freeData(Data *); void reallocData(int alloc); static Data shared_null; @@ -291,7 +289,6 @@ public: { std::list tmp; qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; } private: - Node *detach_helper_grow(int i, int n); void detach_helper(int alloc); void detach_helper(); @@ -432,8 +429,9 @@ Q_OUTOFLINE_TEMPLATE void QList::reserve(int alloc) template Q_OUTOFLINE_TEMPLATE void QList::append(const T &t) { - if (d->ref != 1) { - Node *n = detach_helper_grow(INT_MAX, 1); + detach(); + if (QTypeInfo::isLarge || QTypeInfo::isStatic) { + Node *n = reinterpret_cast(p.append()); QT_TRY { node_construct(n, t); } QT_CATCH(...) { @@ -441,33 +439,24 @@ Q_OUTOFLINE_TEMPLATE void QList::append(const T &t) QT_RETHROW; } } else { - if (QTypeInfo::isLarge || QTypeInfo::isStatic) { - Node *n = reinterpret_cast(p.append()); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - --d->end; - QT_RETHROW; - } - } else { - Node *n, copy; - node_construct(©, t); // t might be a reference to an object in the array - QT_TRY { - n = reinterpret_cast(p.append());; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; + Node *n, copy; + node_construct(©, t); // t might be a reference to an object in the array + QT_TRY { + n = reinterpret_cast(p.append());; + } QT_CATCH(...) { + node_destruct(©); + QT_RETHROW; } + *n = copy; } } template inline void QList::prepend(const T &t) { - if (d->ref != 1) { - Node *n = detach_helper_grow(0, 1); + detach(); + if (QTypeInfo::isLarge || QTypeInfo::isStatic) { + Node *n = reinterpret_cast(p.prepend()); QT_TRY { node_construct(n, t); } QT_CATCH(...) { @@ -475,33 +464,24 @@ inline void QList::prepend(const T &t) QT_RETHROW; } } else { - if (QTypeInfo::isLarge || QTypeInfo::isStatic) { - Node *n = reinterpret_cast(p.prepend()); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - ++d->begin; - QT_RETHROW; - } - } else { - Node *n, copy; - node_construct(©, t); // t might be a reference to an object in the array - QT_TRY { - n = reinterpret_cast(p.prepend());; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; + Node *n, copy; + node_construct(©, t); // t might be a reference to an object in the array + QT_TRY { + n = reinterpret_cast(p.prepend());; + } QT_CATCH(...) { + node_destruct(©); + QT_RETHROW; } + *n = copy; } } template inline void QList::insert(int i, const T &t) { - if (d->ref != 1) { - Node *n = detach_helper_grow(i, 1); + detach(); + if (QTypeInfo::isLarge || QTypeInfo::isStatic) { + Node *n = reinterpret_cast(p.insert(i)); QT_TRY { node_construct(n, t); } QT_CATCH(...) { @@ -509,25 +489,15 @@ inline void QList::insert(int i, const T &t) QT_RETHROW; } } else { - if (QTypeInfo::isLarge || QTypeInfo::isStatic) { - Node *n = reinterpret_cast(p.insert(i)); - QT_TRY { - node_construct(n, t); - } QT_CATCH(...) { - p.remove(i); - QT_RETHROW; - } - } else { - Node *n, copy; - node_construct(©, t); // t might be a reference to an object in the array - QT_TRY { - n = reinterpret_cast(p.insert(i));; - } QT_CATCH(...) { - node_destruct(©); - QT_RETHROW; - } - *n = copy; + Node *n, copy; + node_construct(©, t); // t might be a reference to an object in the array + QT_TRY { + n = reinterpret_cast(p.insert(i));; + } QT_CATCH(...) { + node_destruct(©); + QT_RETHROW; } + *n = copy; } } @@ -599,36 +569,6 @@ Q_OUTOFLINE_TEMPLATE T QList::value(int i, const T& defaultValue) const } template -Q_OUTOFLINE_TEMPLATE typename QList::Node *QList::detach_helper_grow(int i, int c) -{ - Node *n = reinterpret_cast(p.begin()); - QListData::Data *x = p.detach_grow(&i, c); - QT_TRY { - node_copy(reinterpret_cast(p.begin()), - reinterpret_cast(p.begin() + i), n); - } QT_CATCH(...) { - QListData::freeData(d); - d = x; - QT_RETHROW; - } - QT_TRY { - node_copy(reinterpret_cast(p.begin() + i + c), - reinterpret_cast(p.end()), n + i); - } QT_CATCH(...) { - node_destruct(reinterpret_cast(p.begin()), - reinterpret_cast(p.begin() + i)); - QListData::freeData(d); - d = x; - QT_RETHROW; - } - - if (!x->ref.deref()) - QListData::freeData(x); - - return reinterpret_cast(p.begin() + i); -} - -template Q_OUTOFLINE_TEMPLATE void QList::detach_helper(int alloc) { Node *n = reinterpret_cast(p.begin()); @@ -740,9 +680,8 @@ Q_OUTOFLINE_TEMPLATE QList &QList::operator+=(const QList &l) if (isEmpty()) { *this = l; } else { - Node *n = (d->ref != 1) - ? detach_helper_grow(INT_MAX, l.size()) - : reinterpret_cast(p.append(l.p)); + detach(); + Node *n = reinterpret_cast(p.append(l.p)); QT_TRY { node_copy(n, reinterpret_cast(p.end()), reinterpret_cast(l.p.begin())); diff --git a/src/gui/image/qiconloader.cpp b/src/gui/image/qiconloader.cpp index 156356a04..5f790b5f4 100644 --- a/src/gui/image/qiconloader.cpp +++ b/src/gui/image/qiconloader.cpp @@ -39,6 +39,8 @@ #include "qstylehelper_p.h" +#include + QT_BEGIN_NAMESPACE Q_GLOBAL_STATIC(QIconLoader, iconLoaderInstance) diff --git a/src/gui/painting/qrasterizer.cpp b/src/gui/painting/qrasterizer.cpp index f1cc9caed..cd019ab1b 100644 --- a/src/gui/painting/qrasterizer.cpp +++ b/src/gui/painting/qrasterizer.cpp @@ -20,13 +20,13 @@ ****************************************************************************/ #include "qrasterizer_p.h" - -#include -#include - +#include "qpoint.h" +#include "qrect.h" #include "qdatabuffer_p.h" #include "qdrawhelper_p.h" +#include + QT_BEGIN_NAMESPACE typedef int Q16Dot16; diff --git a/src/gui/util/qcompleter.cpp b/src/gui/util/qcompleter.cpp index c17725d5f..8e2b839d2 100644 --- a/src/gui/util/qcompleter.cpp +++ b/src/gui/util/qcompleter.cpp @@ -135,6 +135,8 @@ #include "QtGui/qdesktopwidget.h" #include "QtGui/qlineedit.h" +#include + QT_BEGIN_NAMESPACE QCompletionModel::QCompletionModel(QCompleterPrivate *c, QObject *parent) diff --git a/tests/auto/qsqlfield/tst_qsqlfield.cpp b/tests/auto/qsqlfield/tst_qsqlfield.cpp index 041adcbf7..006608181 100644 --- a/tests/auto/qsqlfield/tst_qsqlfield.cpp +++ b/tests/auto/qsqlfield/tst_qsqlfield.cpp @@ -26,6 +26,7 @@ #include #include +#include //TESTED_CLASS= //TESTED_FILES= -- 2.11.0