From 7fadc3ce322706ed6c36ad907e83fed1992b490e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Fri, 16 Dec 2011 17:22:45 +0100 Subject: [PATCH] Get rid of assignment operators in RefCount , 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 Reviewed-by: Thiago Macieira --- src/corelib/tools/qarraydata.cpp | 2 +- src/corelib/tools/qbytearray.cpp | 16 ++++++++-------- src/corelib/tools/qhash.cpp | 2 +- src/corelib/tools/qlinkedlist.h | 2 +- src/corelib/tools/qlist.cpp | 4 ++-- src/corelib/tools/qmap.cpp | 2 +- src/corelib/tools/qrefcount.h | 6 ++---- src/corelib/tools/qstring.cpp | 16 ++++++++-------- src/corelib/tools/qvector.h | 8 ++++---- .../corelib/tools/qvector/qrawvector.h | 2 +- 10 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index 168249c074..e3a6bad7c5 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -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; diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index ed3f31fc43..f959effb43 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -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(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(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(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(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(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(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(qMalloc(sizeof(Data) + 1)); Q_CHECK_PTR(x); - x->ref = 1; + x->ref.initializeOwned(); x->size = size; x->alloc = 0; x->capacityReserved = false; diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index afb5ebe951..48a446cad6 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -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; diff --git a/src/corelib/tools/qlinkedlist.h b/src/corelib/tools/qlinkedlist.h index 36cbc68eb8..55d229d351 100644 --- a/src/corelib/tools/qlinkedlist.h +++ b/src/corelib/tools/qlinkedlist.h @@ -253,7 +253,7 @@ void QLinkedList::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; diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index 94be78ea21..8ef5fadb73 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -85,7 +85,7 @@ QListData::Data *QListData::detach_grow(int *idx, int num) Data* t = static_cast(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(qMalloc(DataHeaderSize + alloc * sizeof(void *))); Q_CHECK_PTR(t); - t->ref = 1; + t->ref.initializeOwned(); t->sharable = true; t->alloc = alloc; if (!alloc) { diff --git a/src/corelib/tools/qmap.cpp b/src/corelib/tools/qmap.cpp index f605a82dfe..6bac127a82 100644 --- a/src/corelib/tools/qmap.cpp +++ b/src/corelib/tools/qmap.cpp @@ -63,7 +63,7 @@ QMapData *QMapData::createData(int alignment) Node *e = reinterpret_cast(d); e->backward = e; e->forward[0] = e; - d->ref = 1; + d->ref.initializeOwned(); d->topLevel = 0; d->size = 0; d->randomBits = 0; diff --git a/src/corelib/tools/qrefcount.h b/src/corelib/tools/qrefcount.h index 619f61e072..6d030cc0b3 100644 --- a/src/corelib/tools/qrefcount.h +++ b/src/corelib/tools/qrefcount.h @@ -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; }; diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index fb818e37b8..02d6788701 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -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(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(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(qMalloc(sizeof(Data) + sizeof(ushort))); Q_CHECK_PTR(x); - x->ref = 1; + x->ref.initializeOwned(); x->size = size; x->alloc = 0; x->capacityReserved = false; diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index eab9311eb3..f408f6571f 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -411,7 +411,7 @@ template QVector::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 QVector::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 QVector::QVector(std::initializer_list 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::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; diff --git a/tests/benchmarks/corelib/tools/qvector/qrawvector.h b/tests/benchmarks/corelib/tools/qvector/qrawvector.h index eb12a25403..f786d83a53 100644 --- a/tests/benchmarks/corelib/tools/qvector/qrawvector.h +++ b/tests/benchmarks/corelib/tools/qvector/qrawvector.h @@ -289,7 +289,7 @@ public: QVector mutateToVector() { Data *d = toBase(m_begin); - d->ref = 1; + d->ref.initializeOwned(); d->alloc = m_alloc; d->size = m_size; d->sharable = 0;