Get rid of assignment operators in RefCount
authorJoão Abecasis <joao.abecasis@nokia.com>
Fri, 16 Dec 2011 16:22:45 +0000 (17:22 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 4 Jan 2012 22:37:58 +0000 (23:37 +0100)
, and make it strictly a POD struct.

Since this operator was only being used to set the initial (owned) value
of the reference count, the name of the function introduced here to
replace it makes that use case explicit.

Change-Id: I2feadd2ac35dcb75ca211471baf5044a5f57cd62
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
src/corelib/tools/qarraydata.cpp
src/corelib/tools/qbytearray.cpp
src/corelib/tools/qhash.cpp
src/corelib/tools/qlinkedlist.h
src/corelib/tools/qlist.cpp
src/corelib/tools/qmap.cpp
src/corelib/tools/qrefcount.h
src/corelib/tools/qstring.cpp
src/corelib/tools/qvector.h
tests/benchmarks/corelib/tools/qvector/qrawvector.h

index 168249c..e3a6bad 100644 (file)
@@ -69,7 +69,7 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment,
         quintptr data = (quintptr(header) + sizeof(QArrayData) + alignment - 1)
                 & ~(alignment - 1);
 
-        header->ref = 1;
+        header->ref.initializeOwned();
         header->size = 0;
         header->alloc = capacity;
         header->capacityReserved = reserve;
index ed3f31f..f959eff 100644 (file)
@@ -576,7 +576,7 @@ QByteArray qUncompress(const uchar* data, int nbytes)
                 d.take(); // realloc was successful
                 d.reset(p);
             }
-            d->ref = 1;
+            d->ref.initializeOwned();
             d->size = len;
             d->alloc = len;
             d->capacityReserved = false;
@@ -1304,7 +1304,7 @@ QByteArray::QByteArray(const char *str)
         int len = qstrlen(str);
         d = static_cast<Data *>(qMalloc(sizeof(Data) + len + 1));
         Q_CHECK_PTR(d);
-        d->ref = 1;
+        d->ref.initializeOwned();
         d->size = len;
         d->alloc = len;
         d->capacityReserved = false;
@@ -1333,7 +1333,7 @@ QByteArray::QByteArray(const char *data, int size)
     } else {
         d = static_cast<Data *>(qMalloc(sizeof(Data) + size + 1));
         Q_CHECK_PTR(d);
-        d->ref = 1;
+        d->ref.initializeOwned();
         d->size = size;
         d->alloc = size;
         d->capacityReserved = false;
@@ -1357,7 +1357,7 @@ QByteArray::QByteArray(int size, char ch)
     } else {
         d = static_cast<Data *>(qMalloc(sizeof(Data) + size + 1));
         Q_CHECK_PTR(d);
-        d->ref = 1;
+        d->ref.initializeOwned();
         d->size = size;
         d->alloc = size;
         d->capacityReserved = false;
@@ -1377,7 +1377,7 @@ QByteArray::QByteArray(int size, Qt::Initialization)
 {
     d = static_cast<Data *>(qMalloc(sizeof(Data) + size + 1));
     Q_CHECK_PTR(d);
-    d->ref = 1;
+    d->ref.initializeOwned();
     d->size = size;
     d->alloc = size;
     d->capacityReserved = false;
@@ -1424,7 +1424,7 @@ void QByteArray::resize(int size)
         //
         Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + size + 1));
         Q_CHECK_PTR(x);
-        x->ref = 1;
+        x->ref.initializeOwned();
         x->size = size;
         x->alloc = size;
         x->capacityReserved = false;
@@ -1466,7 +1466,7 @@ void QByteArray::realloc(int alloc)
     if (d->ref != 1 || d->offset) {
         Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc + 1));
         Q_CHECK_PTR(x);
-        x->ref = 1;
+        x->ref.initializeOwned();
         x->size = qMin(alloc, d->size);
         x->alloc = alloc;
         x->capacityReserved = d->capacityReserved;
@@ -3887,7 +3887,7 @@ QByteArray QByteArray::fromRawData(const char *data, int size)
     } else {
         x = static_cast<Data *>(qMalloc(sizeof(Data) + 1));
         Q_CHECK_PTR(x);
-        x->ref = 1;
+        x->ref.initializeOwned();
         x->size = size;
         x->alloc = 0;
         x->capacityReserved = false;
index afb5ebe..48a446c 100644 (file)
@@ -196,7 +196,7 @@ QHashData *QHashData::detach_helper(void (*node_duplicate)(Node *, void *),
     d = new QHashData;
     d->fakeNext = 0;
     d->buckets = 0;
-    d->ref = 1;
+    d->ref.initializeOwned();
     d->size = size;
     d->nodeSize = nodeSize;
     d->userNumBits = userNumBits;
index 36cbc68..55d229d 100644 (file)
@@ -253,7 +253,7 @@ void QLinkedList<T>::detach_helper()
 {
     union { QLinkedListData *d; Node *e; } x;
     x.d = new QLinkedListData;
-    x.d->ref = 1;
+    x.d->ref.initializeOwned();
     x.d->size = d->size;
     x.d->sharable = true;
     Node *original = e->n;
index 94be78e..8ef5fad 100644 (file)
@@ -85,7 +85,7 @@ QListData::Data *QListData::detach_grow(int *idx, int num)
     Data* t = static_cast<Data *>(qMalloc(DataHeaderSize + alloc * sizeof(void *)));
     Q_CHECK_PTR(t);
 
-    t->ref = 1;
+    t->ref.initializeOwned();
     t->sharable = true;
     t->alloc = alloc;
     // The space reservation algorithm's optimization is biased towards appending:
@@ -127,7 +127,7 @@ QListData::Data *QListData::detach(int alloc)
     Data* t = static_cast<Data *>(qMalloc(DataHeaderSize + alloc * sizeof(void *)));
     Q_CHECK_PTR(t);
 
-    t->ref = 1;
+    t->ref.initializeOwned();
     t->sharable = true;
     t->alloc = alloc;
     if (!alloc) {
index f605a82..6bac127 100644 (file)
@@ -63,7 +63,7 @@ QMapData *QMapData::createData(int alignment)
     Node *e = reinterpret_cast<Node *>(d);
     e->backward = e;
     e->forward[0] = e;
-    d->ref = 1;
+    d->ref.initializeOwned();
     d->topLevel = 0;
     d->size = 0;
     d->randomBits = 0;
index 619f61e..6d030cc 100644 (file)
@@ -75,10 +75,8 @@ public:
     { return !atomic.load(); }
     inline operator int() const
     { return atomic.load(); }
-    inline RefCount &operator=(int value)
-    { atomic.store(value); return *this; }
-    inline RefCount &operator=(const RefCount &other)
-    { atomic.store(other.atomic.load()); return *this; }
+
+    void initializeOwned() { atomic.store(1); }
 
     QBasicAtomicInt atomic;
 };
index fb818e3..02d6788 100644 (file)
@@ -1032,7 +1032,7 @@ QString::QString(const QChar *unicode, int size)
     } else {
         d = (Data*) qMalloc(sizeof(Data)+(size+1)*sizeof(QChar));
         Q_CHECK_PTR(d);
-        d->ref = 1;
+        d->ref.initializeOwned();
         d->size = size;
         d->alloc = (uint) size;
         d->capacityReserved = false;
@@ -1064,7 +1064,7 @@ QString::QString(const QChar *unicode)
          } else {
              d = (Data*) qMalloc(sizeof(Data)+(size+1)*sizeof(QChar));
              Q_CHECK_PTR(d);
-             d->ref = 1;
+             d->ref.initializeOwned();
              d->size = size;
              d->alloc = (uint) size;
              d->capacityReserved = false;
@@ -1089,7 +1089,7 @@ QString::QString(int size, QChar ch)
     } else {
         d = (Data*) qMalloc(sizeof(Data)+(size+1)*sizeof(QChar));
         Q_CHECK_PTR(d);
-        d->ref = 1;
+        d->ref.initializeOwned();
         d->size = size;
         d->alloc = (uint) size;
         d->capacityReserved = false;
@@ -1113,7 +1113,7 @@ QString::QString(int size, Qt::Initialization)
 {
     d = (Data*) qMalloc(sizeof(Data)+(size+1)*sizeof(QChar));
     Q_CHECK_PTR(d);
-    d->ref = 1;
+    d->ref.initializeOwned();
     d->size = size;
     d->alloc = (uint) size;
     d->capacityReserved = false;
@@ -1135,7 +1135,7 @@ QString::QString(QChar ch)
 {
     d = (Data *) qMalloc(sizeof(Data) + 2*sizeof(QChar));
     Q_CHECK_PTR(d);
-    d->ref = 1;
+    d->ref.initializeOwned();
     d->size = 1;
     d->alloc = 1;
     d->capacityReserved = false;
@@ -1314,7 +1314,7 @@ void QString::realloc(int alloc)
     if (d->ref != 1 || d->offset) {
         Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + (alloc+1) * sizeof(QChar)));
         Q_CHECK_PTR(x);
-        x->ref = 1;
+        x->ref.initializeOwned();
         x->size = qMin(alloc, d->size);
         x->alloc = (uint) alloc;
         x->capacityReserved = d->capacityReserved;
@@ -3758,7 +3758,7 @@ QString::Data *QString::fromLatin1_helper(const char *str, int size)
             size = qstrlen(str);
         d = static_cast<Data *>(qMalloc(sizeof(Data) + (size+1) * sizeof(QChar)));
         Q_CHECK_PTR(d);
-        d->ref = 1;
+        d->ref.initializeOwned();
         d->size = size;
         d->alloc = (uint) size;
         d->capacityReserved = false;
@@ -7074,7 +7074,7 @@ QString QString::fromRawData(const QChar *unicode, int size)
     } else {
         x = static_cast<Data *>(qMalloc(sizeof(Data) + sizeof(ushort)));
         Q_CHECK_PTR(x);
-        x->ref = 1;
+        x->ref.initializeOwned();
         x->size = size;
         x->alloc = 0;
         x->capacityReserved = false;
index eab9311..f408f65 100644 (file)
@@ -411,7 +411,7 @@ template <typename T>
 QVector<T>::QVector(int asize)
 {
     d = malloc(asize);
-    d->ref = 1;
+    d->ref.initializeOwned();
     d->alloc = d->size = asize;
     d->sharable = true;
     d->capacity = false;
@@ -429,7 +429,7 @@ template <typename T>
 QVector<T>::QVector(int asize, const T &t)
 {
     d = malloc(asize);
-    d->ref = 1;
+    d->ref.initializeOwned();
     d->alloc = d->size = asize;
     d->sharable = true;
     d->capacity = false;
@@ -443,7 +443,7 @@ template <typename T>
 QVector<T>::QVector(std::initializer_list<T> args)
 {
     d = malloc(int(args.size()));
-    d->ref = 1;
+    d->ref.initializeOwned();
     d->alloc = d->size = int(args.size());
     d->sharable = true;
     d->capacity = false;
@@ -515,7 +515,7 @@ void QVector<T>::realloc(int asize, int aalloc)
                     QT_RETHROW;
             }
         }
-        x.d->ref = 1;
+        x.d->ref.initializeOwned();
         x.d->alloc = aalloc;
         x.d->sharable = true;
         x.d->capacity = d->capacity;
index eb12a25..f786d83 100644 (file)
@@ -289,7 +289,7 @@ public:
     QVector<T> mutateToVector()
     {
         Data *d = toBase(m_begin);
-        d->ref = 1;
+        d->ref.initializeOwned();
         d->alloc = m_alloc;
         d->size = m_size;
         d->sharable = 0;