From 3249aab539b37a98d75329598bd6838082a8158e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20Abecasis?= Date: Thu, 12 Jan 2012 15:39:17 +0100 Subject: [PATCH] Don't use RefCount int operators , as those are going away. The comment in QString/QByteArray::squeeze about shared_null was updated as it also affects other static data, such as that generated by QStringLiteral and QByteArrayLiteral. Change-Id: I26a757d29db62b1e3566a1f7c8d4030918ed8a89 Reviewed-by: Thiago Macieira Reviewed-by: Bradley T. Hughes Reviewed-by: Olivier Goffart --- src/corelib/tools/qbytearray.cpp | 22 +++++++++++----------- src/corelib/tools/qbytearray.h | 11 ++++++----- src/corelib/tools/qhash.h | 4 ++-- src/corelib/tools/qlinkedlist.h | 4 ++-- src/corelib/tools/qlist.cpp | 16 ++++++++-------- src/corelib/tools/qlist.h | 16 ++++++++-------- src/corelib/tools/qmap.h | 4 ++-- src/corelib/tools/qstring.cpp | 14 +++++++------- src/corelib/tools/qstring.h | 13 +++++++------ .../corelib/tools/qbytearray/tst_qbytearray.cpp | 2 +- 10 files changed, 54 insertions(+), 52 deletions(-) diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 47bf5da..4173cf2 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -904,7 +904,7 @@ QByteArray &QByteArray::operator=(const char *str) x = const_cast(&shared_empty.ba); } else { int len = qstrlen(str); - if (d->ref != 1 || len > int(d->alloc) || (len < d->size && len < int(d->alloc) >> 1)) + if (d->ref.isShared() || len > int(d->alloc) || (len < d->size && len < int(d->alloc) >> 1)) realloc(len); x = d; memcpy(x->data(), str, len + 1); // include null terminator @@ -1403,7 +1403,7 @@ void QByteArray::resize(int size) if (size < 0) size = 0; - if (d->offset && d->ref == 1 && size < d->size) { + if (d->offset && !d->ref.isShared() && size < d->size) { d->size = size; return; } @@ -1432,7 +1432,7 @@ void QByteArray::resize(int size) x->data()[size] = '\0'; d = x; } else { - if (d->ref != 1 || size > int(d->alloc) + if (d->ref.isShared() || size > int(d->alloc) || (!d->capacityReserved && size < d->size && size < int(d->alloc) >> 1)) realloc(qAllocMore(size, sizeof(Data))); if (int(d->alloc) >= size) { @@ -1463,7 +1463,7 @@ QByteArray &QByteArray::fill(char ch, int size) void QByteArray::realloc(int alloc) { - if (d->ref != 1 || d->offset) { + if (d->ref.isShared() || d->offset) { Data *x = static_cast(malloc(sizeof(Data) + alloc + 1)); Q_CHECK_PTR(x); x->ref.initializeOwned(); @@ -1564,7 +1564,7 @@ QByteArray &QByteArray::prepend(const char *str) QByteArray &QByteArray::prepend(const char *str, int len) { if (str) { - if (d->ref != 1 || d->size + len > int(d->alloc)) + if (d->ref.isShared() || d->size + len > int(d->alloc)) realloc(qAllocMore(d->size + len, sizeof(Data))); memmove(d->data()+len, d->data(), d->size); memcpy(d->data(), str, len); @@ -1582,7 +1582,7 @@ QByteArray &QByteArray::prepend(const char *str, int len) QByteArray &QByteArray::prepend(char ch) { - if (d->ref != 1 || d->size + 1 > int(d->alloc)) + if (d->ref.isShared() || d->size + 1 > int(d->alloc)) realloc(qAllocMore(d->size + 1, sizeof(Data))); memmove(d->data()+1, d->data(), d->size); d->data()[0] = ch; @@ -1620,7 +1620,7 @@ QByteArray &QByteArray::append(const QByteArray &ba) if ((d == &shared_null.ba || d == &shared_empty.ba) && !IS_RAW_DATA(ba.d)) { *this = ba; } else if (ba.d != &shared_null.ba) { - if (d->ref != 1 || d->size + ba.d->size > int(d->alloc)) + if (d->ref.isShared() || d->size + ba.d->size > int(d->alloc)) realloc(qAllocMore(d->size + ba.d->size, sizeof(Data))); memcpy(d->data() + d->size, ba.d->data(), ba.d->size); d->size += ba.d->size; @@ -1654,7 +1654,7 @@ QByteArray& QByteArray::append(const char *str) { if (str) { int len = qstrlen(str); - if (d->ref != 1 || d->size + len > int(d->alloc)) + if (d->ref.isShared() || d->size + len > int(d->alloc)) realloc(qAllocMore(d->size + len, sizeof(Data))); memcpy(d->data() + d->size, str, len + 1); // include null terminator d->size += len; @@ -1679,7 +1679,7 @@ QByteArray &QByteArray::append(const char *str, int len) if (len < 0) len = qstrlen(str); if (str && len) { - if (d->ref != 1 || d->size + len > int(d->alloc)) + if (d->ref.isShared() || d->size + len > int(d->alloc)) realloc(qAllocMore(d->size + len, sizeof(Data))); memcpy(d->data() + d->size, str, len); // include null terminator d->size += len; @@ -1696,7 +1696,7 @@ QByteArray &QByteArray::append(const char *str, int len) QByteArray& QByteArray::append(char ch) { - if (d->ref != 1 || d->size + 1 > int(d->alloc)) + if (d->ref.isShared() || d->size + 1 > int(d->alloc)) realloc(qAllocMore(d->size + 1, sizeof(Data))); d->data()[d->size++] = ch; d->data()[d->size] = '\0'; @@ -3912,7 +3912,7 @@ QByteArray QByteArray::fromRawData(const char *data, int size) */ QByteArray &QByteArray::setRawData(const char *data, uint size) { - if (d->ref != 1 || d->alloc) { + if (d->ref.isShared() || d->alloc) { *this = fromRawData(data, size); } else { if (data) { diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 2409ff1..bf75ef6 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -429,9 +429,9 @@ inline const char *QByteArray::data() const inline const char *QByteArray::constData() const { return d->data(); } inline void QByteArray::detach() -{ if (d->ref != 1 || d->offset) realloc(d->size); } +{ if (d->ref.isShared() || d->offset) realloc(d->size); } inline bool QByteArray::isDetached() const -{ return d->ref == 1; } +{ return !d->ref.isShared(); } inline QByteArray::QByteArray(const QByteArray &a) : d(a.d) { d->ref.ref(); } @@ -440,7 +440,7 @@ inline int QByteArray::capacity() const inline void QByteArray::reserve(int asize) { - if (d->ref != 1 || asize > int(d->alloc)) + if (d->ref.isShared() || asize > int(d->alloc)) realloc(asize); if (!d->capacityReserved) { @@ -451,11 +451,12 @@ inline void QByteArray::reserve(int asize) inline void QByteArray::squeeze() { - if (d->ref > 1 || d->size < int(d->alloc)) + if (d->ref.isShared() || d->size < int(d->alloc)) realloc(d->size); if (d->capacityReserved) { - // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const) + // cannot set unconditionally, since d could be shared_null or + // otherwise static. d->capacityReserved = false; } } diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 0fdb1ad..ecd0200 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -289,8 +289,8 @@ public: void reserve(int size); inline void squeeze() { reserve(1); } - inline void detach() { if (d->ref != 1) detach_helper(); } - inline bool isDetached() const { return d->ref == 1; } + inline void detach() { if (d->ref.isShared()) detach_helper(); } + inline bool isDetached() const { return !d->ref.isShared(); } inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QHashData::shared_null) d->sharable = sharable; } inline bool isSharedWith(const QHash &other) const { return d == other.d; } diff --git a/src/corelib/tools/qlinkedlist.h b/src/corelib/tools/qlinkedlist.h index 279eda6..e8696eb 100644 --- a/src/corelib/tools/qlinkedlist.h +++ b/src/corelib/tools/qlinkedlist.h @@ -95,8 +95,8 @@ public: inline int size() const { return d->size; } inline void detach() - { if (d->ref != 1) detach_helper(); } - inline bool isDetached() const { return d->ref == 1; } + { if (d->ref.isShared()) detach_helper(); } + inline bool isDetached() const { return !d->ref.isShared(); } inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QLinkedListData::shared_null) d->sharable = sharable; } inline bool isSharedWith(const QLinkedList &other) const { return d == other.d; } diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index 2afe1ab..8eb4be0 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -146,7 +146,7 @@ QListData::Data *QListData::detach(int alloc) void QListData::realloc(int alloc) { - Q_ASSERT(d->ref == 1); + Q_ASSERT(!d->ref.isShared()); Data *x = static_cast(::realloc(d, DataHeaderSize + alloc * sizeof(void *))); Q_CHECK_PTR(x); @@ -159,7 +159,7 @@ void QListData::realloc(int alloc) // ensures that enough space is available to append n elements void **QListData::append(int n) { - Q_ASSERT(d->ref == 1); + Q_ASSERT(!d->ref.isShared()); int e = d->end; if (e + n > d->alloc) { int b = d->begin; @@ -190,7 +190,7 @@ void **QListData::append(const QListData& l) void **QListData::prepend() { - Q_ASSERT(d->ref == 1); + Q_ASSERT(!d->ref.isShared()); if (d->begin == 0) { if (d->end >= d->alloc / 3) realloc(grow(d->alloc + 1)); @@ -208,7 +208,7 @@ void **QListData::prepend() void **QListData::insert(int i) { - Q_ASSERT(d->ref == 1); + Q_ASSERT(!d->ref.isShared()); if (i <= 0) return prepend(); int size = d->end - d->begin; @@ -247,7 +247,7 @@ void **QListData::insert(int i) void QListData::remove(int i) { - Q_ASSERT(d->ref == 1); + Q_ASSERT(!d->ref.isShared()); i += d->begin; if (i - d->begin < d->end - i) { if (int offset = i - d->begin) @@ -262,7 +262,7 @@ void QListData::remove(int i) void QListData::remove(int i, int n) { - Q_ASSERT(d->ref == 1); + Q_ASSERT(!d->ref.isShared()); i += d->begin; int middle = i + n/2; if (middle - d->begin < d->end - middle) { @@ -278,7 +278,7 @@ void QListData::remove(int i, int n) void QListData::move(int from, int to) { - Q_ASSERT(d->ref == 1); + Q_ASSERT(!d->ref.isShared()); if (from == to) return; @@ -318,7 +318,7 @@ void QListData::move(int from, int to) void **QListData::erase(void **xi) { - Q_ASSERT(d->ref == 1); + Q_ASSERT(!d->ref.isShared()); int i = xi - (d->array + d->begin); remove(i); return d->array + d->begin + i; diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index d192d31..c3db56d 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -133,16 +133,16 @@ public: inline int size() const { return p.size(); } - inline void detach() { if (d->ref != 1) detach_helper(); } + inline void detach() { if (d->ref.isShared()) detach_helper(); } inline void detachShared() { // The "this->" qualification is needed for GCCE. - if (d->ref != 1 && this->d != &QListData::shared_null) + if (d->ref.isShared() && this->d != &QListData::shared_null) detach_helper(); } - inline bool isDetached() const { return d->ref == 1; } + inline bool isDetached() const { return !d->ref.isShared(); } inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QListData::shared_null) d->sharable = sharable; } inline bool isSharedWith(const QList &other) const { return d == other.d; } @@ -479,7 +479,7 @@ template Q_OUTOFLINE_TEMPLATE void QList::reserve(int alloc) { if (d->alloc < alloc) { - if (d->ref != 1) + if (d->ref.isShared()) detach_helper(alloc); else p.realloc(alloc); @@ -489,7 +489,7 @@ Q_OUTOFLINE_TEMPLATE void QList::reserve(int alloc) template Q_OUTOFLINE_TEMPLATE void QList::append(const T &t) { - if (d->ref != 1) { + if (d->ref.isShared()) { Node *n = detach_helper_grow(INT_MAX, 1); QT_TRY { node_construct(n, t); @@ -523,7 +523,7 @@ Q_OUTOFLINE_TEMPLATE void QList::append(const T &t) template inline void QList::prepend(const T &t) { - if (d->ref != 1) { + if (d->ref.isShared()) { Node *n = detach_helper_grow(0, 1); QT_TRY { node_construct(n, t); @@ -557,7 +557,7 @@ inline void QList::prepend(const T &t) template inline void QList::insert(int i, const T &t) { - if (d->ref != 1) { + if (d->ref.isShared()) { Node *n = detach_helper_grow(i, 1); QT_TRY { node_construct(n, t); @@ -803,7 +803,7 @@ Q_OUTOFLINE_TEMPLATE QList &QList::operator+=(const QList &l) if (isEmpty()) { *this = l; } else { - Node *n = (d->ref != 1) + Node *n = (d->ref.isShared()) ? detach_helper_grow(INT_MAX, l.size()) : reinterpret_cast(p.append(l.p)); QT_TRY { diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index f975b7e..7e19760 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -200,8 +200,8 @@ public: inline bool isEmpty() const { return d->size == 0; } - inline void detach() { if (d->ref != 1) detach_helper(); } - inline bool isDetached() const { return d->ref == 1; } + inline void detach() { if (d->ref.isShared()) detach_helper(); } + inline bool isDetached() const { return !d->ref.isShared(); } inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QMapData::shared_null) d->sharable = sharable; } inline bool isSharedWith(const QMap &other) const { return d == other.d; } inline void setInsertInOrder(bool ordered) { if (ordered) detach(); if (d != &QMapData::shared_null) d->insertInOrder = ordered; } diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index dbe1e91..8e49106 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -1237,7 +1237,7 @@ void QString::resize(int size) if (size < 0) size = 0; - if (d->offset && d->ref == 1 && size < d->size) { + if (d->offset && !d->ref.isShared() && size < d->size) { d->size = size; return; } @@ -1248,7 +1248,7 @@ void QString::resize(int size) QString::free(d); d = x; } else { - if (d->ref != 1 || size > int(d->alloc) || + if (d->ref.isShared() || size > int(d->alloc) || (!d->capacityReserved && size < d->size && size < int(d->alloc) >> 1)) realloc(grow(size)); if (int(d->alloc) >= size) { @@ -1311,7 +1311,7 @@ void QString::resize(int size) // ### Qt 5: rename reallocData() to avoid confusion. 197625 void QString::realloc(int alloc) { - if (d->ref != 1 || d->offset) { + if (d->ref.isShared() || d->offset) { Data *x = static_cast(::malloc(sizeof(Data) + (alloc+1) * sizeof(QChar))); Q_CHECK_PTR(x); x->ref.initializeOwned(); @@ -1541,7 +1541,7 @@ QString &QString::append(const QString &str) if (d == &shared_null.str) { operator=(str); } else { - if (d->ref != 1 || d->size + str.d->size > int(d->alloc)) + if (d->ref.isShared() || d->size + str.d->size > int(d->alloc)) realloc(grow(d->size + str.d->size)); memcpy(d->data() + d->size, str.d->data(), str.d->size * sizeof(QChar)); d->size += str.d->size; @@ -1561,7 +1561,7 @@ QString &QString::append(const QLatin1String &str) const uchar *s = (const uchar *)str.latin1(); if (s) { int len = qstrlen((char *)s); - if (d->ref != 1 || d->size + len > int(d->alloc)) + if (d->ref.isShared() || d->size + len > int(d->alloc)) realloc(grow(d->size + len)); ushort *i = d->data() + d->size; while ((*i++ = *s++)) @@ -1604,7 +1604,7 @@ QString &QString::append(const QLatin1String &str) */ QString &QString::append(QChar ch) { - if (d->ref != 1 || d->size + 1 > int(d->alloc)) + if (d->ref.isShared() || d->size + 1 > int(d->alloc)) realloc(grow(d->size + 1)); d->data()[d->size++] = ch.unicode(); d->data()[d->size] = '\0'; @@ -7090,7 +7090,7 @@ QString QString::fromRawData(const QChar *unicode, int size) */ QString &QString::setRawData(const QChar *unicode, int size) { - if (d->ref != 1 || d->alloc) { + if (d->ref.isShared() || d->alloc) { *this = fromRawData(unicode, size); } else { if (unicode) { diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index f7898bb..42fe85e 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -339,7 +339,7 @@ public: inline QString &prepend(const QLatin1String &s) { return insert(0, s); } inline QString &operator+=(QChar c) { - if (d->ref != 1 || d->size + 1 > int(d->alloc)) + if (d->ref.isShared() || d->size + 1 > int(d->alloc)) realloc(grow(d->size + 1)); d->data()[d->size++] = c.unicode(); d->data()[d->size] = '\0'; @@ -706,9 +706,9 @@ inline QChar *QString::data() inline const QChar *QString::constData() const { return reinterpret_cast(d->data()); } inline void QString::detach() -{ if (d->ref != 1 || d->offset) realloc(); } +{ if (d->ref.isShared() || d->offset) realloc(); } inline bool QString::isDetached() const -{ return d->ref == 1; } +{ return !d->ref.isShared(); } inline QString &QString::operator=(const QLatin1String &s) { *this = fromLatin1(s.latin1()); @@ -871,7 +871,7 @@ inline QString::~QString() { if (!d->ref.deref()) free(d); } inline void QString::reserve(int asize) { - if (d->ref != 1 || asize > int(d->alloc)) + if (d->ref.isShared() || asize > int(d->alloc)) realloc(asize); if (!d->capacityReserved) { @@ -882,11 +882,12 @@ inline void QString::reserve(int asize) inline void QString::squeeze() { - if (d->ref > 1 || d->size < int(d->alloc)) + if (d->ref.isShared() || d->size < int(d->alloc)) realloc(); if (d->capacityReserved) { - // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const) + // cannot set unconditionally, since d could be shared_null or + // otherwise static. d->capacityReserved = false; } } diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index 8767571..5b660ec 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -1504,7 +1504,7 @@ void tst_QByteArray::literals() QVERIFY(str.length() == 4); QVERIFY(str == "abcd"); - QVERIFY(str.data_ptr()->ref == -1); + QVERIFY(str.data_ptr()->ref.isStatic()); QVERIFY(str.data_ptr()->offset == 0); const char *s = str.constData(); -- 2.7.4