summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qarraydata.cpp8
-rw-r--r--src/corelib/tools/qarraydata.h37
-rw-r--r--src/corelib/tools/qarraydatapointer.h12
3 files changed, 44 insertions, 13 deletions
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp
index 150f23cc12..efed984aef 100644
--- a/src/corelib/tools/qarraydata.cpp
+++ b/src/corelib/tools/qarraydata.cpp
@@ -49,7 +49,7 @@ static const QArrayData qt_array_empty = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0
static const QArrayData qt_array_unsharable_empty = { { Q_BASIC_ATOMIC_INITIALIZER(0) }, 0, 0, 0, 0 };
QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment,
- size_t capacity, bool reserve, bool sharable)
+ size_t capacity, AllocateOptions options)
{
// Alignment is a power of two
Q_ASSERT(alignment >= Q_ALIGNOF(QArrayData)
@@ -57,7 +57,7 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment,
// Don't allocate empty headers
if (!capacity)
- return sharable
+ return !(options & Unsharable)
? const_cast<QArrayData *>(&qt_array_empty)
: const_cast<QArrayData *>(&qt_array_unsharable_empty);
@@ -73,10 +73,10 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment,
quintptr data = (quintptr(header) + sizeof(QArrayData) + alignment - 1)
& ~(alignment - 1);
- header->ref.atomic.store(sharable ? 1 : 0);
+ header->ref.atomic.store(bool(!(options & Unsharable)));
header->size = 0;
header->alloc = capacity;
- header->capacityReserved = reserve;
+ header->capacityReserved = bool(options & CapacityReserved);
header->offset = data - quintptr(header);
}
diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h
index 2486bebafa..8eb543ee51 100644
--- a/src/corelib/tools/qarraydata.h
+++ b/src/corelib/tools/qarraydata.h
@@ -73,14 +73,43 @@ struct Q_CORE_EXPORT QArrayData
return reinterpret_cast<const char *>(this) + offset;
}
+ enum AllocateOption {
+ CapacityReserved = 0x1,
+ Unsharable = 0x2,
+
+ Default = 0
+ };
+
+ Q_DECLARE_FLAGS(AllocateOptions, AllocateOption)
+
+ AllocateOptions detachFlags() const
+ {
+ AllocateOptions result;
+ if (!ref.isSharable())
+ result |= Unsharable;
+ if (capacityReserved)
+ result |= CapacityReserved;
+ return result;
+ }
+
+ AllocateOptions cloneFlags() const
+ {
+ AllocateOptions result;
+ if (capacityReserved)
+ result |= CapacityReserved;
+ return result;
+ }
+
static QArrayData *allocate(size_t objectSize, size_t alignment,
- size_t capacity, bool reserve, bool sharable) Q_REQUIRED_RESULT;
+ size_t capacity, AllocateOptions options = Default) Q_REQUIRED_RESULT;
static void deallocate(QArrayData *data, size_t objectSize,
size_t alignment);
static const QArrayData shared_null;
};
+Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::AllocateOptions)
+
template <class T>
struct QTypedArrayData
: QArrayData
@@ -98,11 +127,11 @@ struct QTypedArrayData
class AlignmentDummy { QArrayData header; T data; };
- static QTypedArrayData *allocate(size_t capacity, bool reserve = false,
- bool sharable = true) Q_REQUIRED_RESULT
+ static QTypedArrayData *allocate(size_t capacity,
+ AllocateOptions options = Default) Q_REQUIRED_RESULT
{
return static_cast<QTypedArrayData *>(QArrayData::allocate(sizeof(T),
- Q_ALIGNOF(AlignmentDummy), capacity, reserve, sharable));
+ Q_ALIGNOF(AlignmentDummy), capacity, options));
}
static void deallocate(QArrayData *data)
diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h
index c03e2ef849..1dc02daa63 100644
--- a/src/corelib/tools/qarraydatapointer.h
+++ b/src/corelib/tools/qarraydatapointer.h
@@ -66,7 +66,7 @@ public:
QArrayDataPointer(const QArrayDataPointer &other)
: d(other.d->ref.ref()
? other.d
- : other.clone())
+ : other.clone(other.d->cloneFlags()))
{
}
@@ -115,7 +115,9 @@ public:
void setSharable(bool sharable)
{
if (d->alloc == 0 && d->size == 0) {
- d = Data::allocate(0, false, sharable);
+ d = Data::allocate(0, sharable
+ ? QArrayData::Default
+ : QArrayData::Unsharable);
return;
}
@@ -137,7 +139,7 @@ public:
bool detach()
{
if (d->ref.isShared()) {
- Data *copy = clone();
+ Data *copy = clone(d->detachFlags());
QArrayDataPointer old(d);
d = copy;
return true;
@@ -147,10 +149,10 @@ public:
}
private:
- Data *clone() const Q_REQUIRED_RESULT
+ Data *clone(QArrayData::AllocateOptions options) const Q_REQUIRED_RESULT
{
QArrayDataPointer copy(Data::allocate(d->alloc ? d->alloc : d->size,
- d->capacityReserved));
+ options));
if (d->size)
copy->copyAppend(d->begin(), d->end());