summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-06-11 19:42:05 +0200
committerLars Knoll <lars.knoll@qt.io>2019-12-08 10:29:09 +0100
commit62c673ccc6f81cee09a25f5acceec2768cea4672 (patch)
tree883720fc4a6d333dbf3afb7b8276bbd33d202ffc /src/corelib/tools
parenta3aa2fcfa72ab69bdbded26dcd43e37b35796a17 (diff)
Add reference-count manipulation functions to QArrayData and hide ref
The next change will stop using some values in the reference counter as settings from the data. Change-Id: I94df1fe643896373fac2f000fff55bc7708fc807 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r--src/corelib/tools/qarraydata.cpp6
-rw-r--r--src/corelib/tools/qarraydata.h26
-rw-r--r--src/corelib/tools/qarraydataops.h26
-rw-r--r--src/corelib/tools/qarraydatapointer.h4
-rw-r--r--src/corelib/tools/qvector.h20
5 files changed, 51 insertions, 31 deletions
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp
index 3123d7467f..fdd441c7de 100644
--- a/src/corelib/tools/qarraydata.cpp
+++ b/src/corelib/tools/qarraydata.cpp
@@ -183,7 +183,7 @@ static QArrayData *allocateData(size_t allocSize, uint options)
{
QArrayData *header = static_cast<QArrayData *>(::malloc(allocSize));
if (header) {
- header->ref.atomic.storeRelaxed(1);
+ header->ref_.atomic.storeRelaxed(1);
header->flags = options;
header->size = 0;
}
@@ -247,7 +247,7 @@ QArrayData *QArrayData::reallocateUnaligned(QArrayData *data, size_t objectSize,
{
Q_ASSERT(data);
Q_ASSERT(data->isMutable());
- Q_ASSERT(!data->ref.isShared());
+ Q_ASSERT(!data->isShared());
options |= ArrayOption(AllocatedDataType);
size_t headerSize = sizeof(QArrayData);
@@ -267,7 +267,7 @@ void QArrayData::deallocate(QArrayData *data, size_t objectSize,
&& !(alignment & (alignment - 1)));
Q_UNUSED(objectSize) Q_UNUSED(alignment)
- Q_ASSERT_X(data == 0 || !data->ref.isStatic(), "QArrayData::deallocate",
+ Q_ASSERT_X(data == 0 || !data->isStatic(), "QArrayData::deallocate",
"Static data cannot be deleted");
::free(data);
}
diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h
index 8c21fd55c8..183cf9f002 100644
--- a/src/corelib/tools/qarraydata.h
+++ b/src/corelib/tools/qarraydata.h
@@ -67,7 +67,7 @@ struct Q_CORE_EXPORT QArrayData
};
Q_DECLARE_FLAGS(ArrayOptions, ArrayOption)
- QtPrivate::RefCount ref;
+ QtPrivate::RefCount ref_;
uint flags;
int size;
uint alloc;
@@ -84,6 +84,16 @@ struct Q_CORE_EXPORT QArrayData
return alloc;
}
+ bool ref()
+ {
+ return ref_.ref();
+ }
+
+ bool deref()
+ {
+ return ref_.deref();
+ }
+
void *data()
{
Q_ASSERT(size == 0
@@ -106,13 +116,23 @@ struct Q_CORE_EXPORT QArrayData
return flags & Mutable;
}
+ bool isStatic() const
+ {
+ return ref_.isStatic();
+ }
+
+ bool isShared() const
+ {
+ return ref_.isShared();
+ }
+
// Returns true if a detach is necessary before modifying the data
// This method is intentionally not const: if you want to know whether
// detaching is necessary, you should be in a non-const function already
bool needsDetach()
{
// ### optimize me -- this currently requires 3 conditionals!
- return !isMutable() || ref.isShared();
+ return !isMutable() || isShared();
}
size_t detachCapacity(size_t newSize) const
@@ -290,7 +310,7 @@ struct QTypedArrayData
Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData));
QTypedArrayData *result = static_cast<QTypedArrayData *>(prepareRawData(options));
if (result) {
- Q_ASSERT(!result->ref.isShared()); // No shared empty, please!
+ Q_ASSERT(!result->isShared()); // No shared empty, please!
result->offset = reinterpret_cast<const char *>(data)
- reinterpret_cast<const char *>(result);
diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h
index 7724049be8..777f0d839f 100644
--- a/src/corelib/tools/qarraydataops.h
+++ b/src/corelib/tools/qarraydataops.h
@@ -61,7 +61,7 @@ struct QPodArrayOps
void appendInitialize(size_t newSize)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(newSize > uint(this->size));
Q_ASSERT(newSize <= this->allocatedCapacity());
@@ -72,7 +72,7 @@ struct QPodArrayOps
void copyAppend(const T *b, const T *e)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(b < e);
Q_ASSERT(e - b <= this->allocatedCapacity() - this->size);
@@ -84,7 +84,7 @@ struct QPodArrayOps
void copyAppend(size_t n, const T &t)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(n <= uint(this->allocatedCapacity() - this->size));
T *iter = this->end();
@@ -97,7 +97,7 @@ struct QPodArrayOps
void truncate(size_t newSize)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(newSize < size_t(this->size));
this->size = int(newSize);
@@ -106,7 +106,7 @@ struct QPodArrayOps
void destroyAll() // Call from destructors, ONLY!
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(this->ref.atomic.loadRelaxed() == 0);
+ Q_ASSERT(this->ref_.atomic.loadRelaxed() == 0);
// As this is to be called only from destructor, it doesn't need to be
// exception safe; size not updated.
@@ -115,7 +115,7 @@ struct QPodArrayOps
void insert(T *where, const T *b, const T *e)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
Q_ASSERT(b < e);
Q_ASSERT(e <= where || b > this->end()); // No overlap
@@ -148,7 +148,7 @@ struct QGenericArrayOps
void appendInitialize(size_t newSize)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(newSize > uint(this->size));
Q_ASSERT(newSize <= this->allocatedCapacity());
@@ -161,7 +161,7 @@ struct QGenericArrayOps
void copyAppend(const T *b, const T *e)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(b < e);
Q_ASSERT(e - b <= this->allocatedCapacity() - this->size);
@@ -175,7 +175,7 @@ struct QGenericArrayOps
void copyAppend(size_t n, const T &t)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(n <= size_t(this->allocatedCapacity() - this->size));
T *iter = this->end();
@@ -189,7 +189,7 @@ struct QGenericArrayOps
void truncate(size_t newSize)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(newSize < size_t(this->size));
const T *const b = this->begin();
@@ -204,7 +204,7 @@ struct QGenericArrayOps
// As this is to be called only from destructor, it doesn't need to be
// exception safe; size not updated.
- Q_ASSERT(this->ref.atomic.loadRelaxed() == 0);
+ Q_ASSERT(this->ref_.atomic.loadRelaxed() == 0);
const T *const b = this->begin();
const T *i = this->end();
@@ -216,7 +216,7 @@ struct QGenericArrayOps
void insert(T *where, const T *b, const T *e)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
Q_ASSERT(b < e);
Q_ASSERT(e <= where || b > this->end()); // No overlap
@@ -312,7 +312,7 @@ struct QMovableArrayOps
void insert(T *where, const T *b, const T *e)
{
Q_ASSERT(this->isMutable());
- Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(!this->isShared());
Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
Q_ASSERT(b < e);
Q_ASSERT(e <= where || b > this->end()); // No overlap
diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h
index b7236d485a..86997985d1 100644
--- a/src/corelib/tools/qarraydatapointer.h
+++ b/src/corelib/tools/qarraydatapointer.h
@@ -58,7 +58,7 @@ public:
}
QArrayDataPointer(const QArrayDataPointer &other)
- : d(other.d->ref.ref()
+ : d(other.d->ref()
? other.d
: other.clone(other.d->cloneFlags()))
{
@@ -109,7 +109,7 @@ public:
~QArrayDataPointer()
{
- if (!d->ref.deref()) {
+ if (!d->deref()) {
if (d->isMutable())
(*this)->destroyAll();
Data::deallocate(d);
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h
index a596beedcf..d9512ee90e 100644
--- a/src/corelib/tools/qvector.h
+++ b/src/corelib/tools/qvector.h
@@ -89,7 +89,7 @@ public:
explicit QVector(int size);
QVector(int size, const T &t);
inline QVector(const QVector<T> &v);
- inline ~QVector() { if (!d->ref.deref()) freeData(d); }
+ inline ~QVector() { if (!d->deref()) freeData(d); }
QVector<T> &operator=(const QVector<T> &v);
QVector(QVector<T> &&other) noexcept : d(other.d) { other.d = Data::sharedNull(); }
QVector<T> &operator=(QVector<T> &&other) noexcept
@@ -114,17 +114,17 @@ public:
void reserve(int size);
inline void squeeze()
{
- if (d->size < int(d->alloc)) {
+ if (d->size < int(d->allocatedCapacity())) {
if (!d->size) {
*this = QVector<T>();
return;
}
- realloc(d->size, QArrayData::ArrayOptions(d->flags));
+ realloc(d->size, d->detachFlags());
}
}
inline void detach();
- inline bool isDetached() const { return !d->ref.isShared(); }
+ inline bool isDetached() const { return !d->isShared(); }
inline bool isSharedWith(const QVector<T> &other) const { return d == other.d; }
@@ -365,7 +365,7 @@ void QVector<T>::destruct(T *from, T *to)
template <typename T>
inline QVector<T>::QVector(const QVector<T> &v)
{
- if (v.d->ref.ref()) {
+ if (v.d->ref()) {
d = v.d;
} else {
if (v.d->flags & Data::CapacityReserved) {
@@ -391,7 +391,7 @@ template <typename T>
void QVector<T>::detach()
{
// ### check whether this is still required
- if (d->ref.isStatic())
+ if (d->isStatic())
return;
if (d->needsDetach())
@@ -581,14 +581,14 @@ void QVector<T>::realloc(int aalloc, QArrayData::ArrayOptions options)
Q_ASSERT(aalloc >= d->size);
Data *x = d;
- const bool isShared = d->ref.isShared();
+ const bool isShared = d->isShared();
QT_TRY {
// allocate memory
x = Data::allocate(aalloc, options);
Q_CHECK_PTR(x);
// aalloc is bigger then 0 so it is not [un]sharedEmpty
- Q_ASSERT(!x->ref.isStatic());
+ Q_ASSERT(!x->isStatic());
x->size = d->size;
T *srcBegin = d->begin();
@@ -621,7 +621,7 @@ void QVector<T>::realloc(int aalloc, QArrayData::ArrayOptions options)
}
Q_ASSERT(d != x);
- if (!d->ref.deref()) {
+ if (!d->deref()) {
if (!QTypeInfoQuery<T>::isRelocatable || !aalloc || (isShared && QTypeInfo<T>::isComplex)) {
// data was copy constructed, we need to call destructors
// or if !alloc we did nothing to the old 'd'.
@@ -701,7 +701,7 @@ void QVector<T>::removeLast()
Q_ASSERT(!isEmpty());
Q_ASSERT(d->allocatedCapacity());
- if (d->ref.isShared())
+ if (d->isShared())
detach();
--d->size;
if (QTypeInfo<T>::isComplex)