Don't use RefCount int operators
authorJoão Abecasis <joao.abecasis@nokia.com>
Thu, 12 Jan 2012 14:39:17 +0000 (15:39 +0100)
committerQt by Nokia <qt-info@nokia.com>
Mon, 23 Jan 2012 16:40:18 +0000 (17:40 +0100)
, 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 <thiago.macieira@intel.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
src/corelib/tools/qbytearray.cpp
src/corelib/tools/qbytearray.h
src/corelib/tools/qhash.h
src/corelib/tools/qlinkedlist.h
src/corelib/tools/qlist.cpp
src/corelib/tools/qlist.h
src/corelib/tools/qmap.h
src/corelib/tools/qstring.cpp
src/corelib/tools/qstring.h
tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp

index 47bf5da..4173cf2 100644 (file)
@@ -904,7 +904,7 @@ QByteArray &QByteArray::operator=(const char *str)
         x = const_cast<Data *>(&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<Data *>(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) {
index 2409ff1..bf75ef6 100644 (file)
@@ -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;
     }
 }
index 0fdb1ad..ecd0200 100644 (file)
@@ -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<Key, T> &other) const { return d == other.d; }
 
index 279eda6..e8696eb 100644 (file)
@@ -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<T> &other) const { return d == other.d; }
 
index 2afe1ab..8eb4be0 100644 (file)
@@ -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<Data *>(::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;
index d192d31..c3db56d 100644 (file)
@@ -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<T> &other) const { return d == other.d; }
 
@@ -479,7 +479,7 @@ template <typename T>
 Q_OUTOFLINE_TEMPLATE void QList<T>::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<T>::reserve(int alloc)
 template <typename T>
 Q_OUTOFLINE_TEMPLATE void QList<T>::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<T>::append(const T &t)
 template <typename T>
 inline void QList<T>::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<T>::prepend(const T &t)
 template <typename T>
 inline void QList<T>::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<T> &QList<T>::operator+=(const QList<T> &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<Node *>(p.append(l.p));
             QT_TRY {
index f975b7e..7e19760 100644 (file)
@@ -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<Key, T> &other) const { return d == other.d; }
     inline void setInsertInOrder(bool ordered) { if (ordered) detach(); if (d != &QMapData::shared_null) d->insertInOrder = ordered; }
index dbe1e91..8e49106 100644 (file)
@@ -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<Data *>(::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) {
index f7898bb..42fe85e 100644 (file)
@@ -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<const QChar*>(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;
     }
 }
index 8767571..5b660ec 100644 (file)
@@ -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();