From 54255f7c5e7cecc58076c412183ce2d877a34c18 Mon Sep 17 00:00:00 2001 From: "Bradley T. Hughes" Date: Fri, 9 Sep 2011 11:07:48 +0200 Subject: Make QVectorData::shared_null const Similar to QByteArray and QString, keep the shared_null in shareable memory and never modify it. Since QRegion uses the internals of QVector, we need to make sure that QRegion also never modifies the shared_null. Change-Id: I809e5873fe414138f97d501e05458b73c04b18fb Reviewed-on: http://codereview.qt-project.org/4529 Reviewed-by: Qt Sanity Bot Reviewed-by: Lars Knoll --- src/corelib/tools/qvector.cpp | 2 +- src/corelib/tools/qvector.h | 10 +++++----- src/gui/painting/qregion.cpp | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index 98a2412347..e1828c2b0f 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -52,7 +52,7 @@ static inline int alignmentThreshold() return 2 * sizeof(void*); } -QVectorData QVectorData::shared_null = { Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0, true, false, 0 }; +const QVectorData QVectorData::shared_null = { Q_REFCOUNT_INITIALIZER(-1), 0, 0, true, false, 0 }; QVectorData *QVectorData::malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init) { diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 5c25266ede..34d1ed3717 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -42,10 +42,10 @@ #ifndef QVECTOR_H #define QVECTOR_H -#include -#include #include +#include #include +#include #ifndef QT_NO_STL #include @@ -65,7 +65,7 @@ QT_MODULE(Core) struct Q_CORE_EXPORT QVectorData { - QBasicAtomicInt ref; + QtPrivate::RefCount ref; int alloc; int size; #if defined(QT_ARCH_SPARC) && defined(Q_CC_GNU) && defined(__LP64__) && defined(QT_BOOTSTRAPPED) @@ -79,7 +79,7 @@ struct Q_CORE_EXPORT QVectorData uint reserved : 30; #endif - static QVectorData shared_null; + static const QVectorData shared_null; // ### Qt 5: rename to 'allocate()'. The current name causes problems for // some debugges when the QVector is member of a class within an unnamed namespace. // ### Qt 5: can be removed completely. (Ralf) @@ -117,7 +117,7 @@ class QVector public: // ### Qt 5: Consider making QVector non-shared to get at least one // "really fast" container. See tests/benchmarks/corelib/tools/qvector/ - inline QVector() : d(&QVectorData::shared_null) { d->ref.ref(); } + inline QVector() : d(const_cast(&QVectorData::shared_null)) { } explicit QVector(int size); QVector(int size, const T &t); inline QVector(const QVector &v) : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); } diff --git a/src/gui/painting/qregion.cpp b/src/gui/painting/qregion.cpp index d3a43c4193..f343fa9b4c 100644 --- a/src/gui/painting/qregion.cpp +++ b/src/gui/painting/qregion.cpp @@ -4268,7 +4268,8 @@ QVector QRegion::rects() const if (d->qt_rgn) { d->qt_rgn->vectorize(); // hw: modify the vector size directly to avoid reallocation - d->qt_rgn->rects.d->size = d->qt_rgn->numRects; + if (d->qt_rgn->rects.d != &QVectorData::shared_null) + d->qt_rgn->rects.d->size = d->qt_rgn->numRects; return d->qt_rgn->rects; } else { return QVector(); -- cgit v1.2.3