summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qarraydata.h
diff options
context:
space:
mode:
authorJoão Abecasis <joao.abecasis@nokia.com>2012-01-11 17:16:04 +0100
committerQt by Nokia <qt-info@nokia.com>2012-01-16 18:10:58 +0100
commitf4c1e2c40fcc1285ea24d55ed4eac036d8bbdf1b (patch)
tree048a192cf1694738bf80349f39eb978f971d6dcd /src/corelib/tools/qarraydata.h
parent5b250d497fd798c476765b22a2906e1c0ff1e432 (diff)
Enable QArrayData to reference external array data
By default, QTypedArrayData::fromRawData provides the same semantics as already exist in QByteArray and QString (immutable, sharable data), but more combinations are possible. In particular, immutable-unsharable leaves the data owner in control of its lifetime by forcing deep copies. As part of this, a new isMutable property is introduced in QArrayData. This could be taken to be implicit in statics that are initialized with a proper size but with alloc set to 0. QStringLiteral and QByteLiteral already did this, forcing re-allocations on resize even before the (static, thus shared) ref-count is considered. The isMutable property detaches data mutability and shared status, which are orthogonal concepts (at least in the unshared state). For the time being, there is no API to explicitly (re)set mutability, but statics and RawData mark data immutable. Change-Id: I33a995a35e1c3d7a12391b1d7c36095aa28e221a Reviewed-by: Robin Burchell <robin+qt@viroteck.net> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qarraydata.h')
-rw-r--r--src/corelib/tools/qarraydata.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h
index 8eb543ee51..5a17d718c9 100644
--- a/src/corelib/tools/qarraydata.h
+++ b/src/corelib/tools/qarraydata.h
@@ -73,9 +73,18 @@ struct Q_CORE_EXPORT QArrayData
return reinterpret_cast<const char *>(this) + offset;
}
+ // This refers to array data mutability, not "header data" represented by
+ // data members in QArrayData. Shared data (array and header) must still
+ // follow COW principles.
+ bool isMutable() const
+ {
+ return alloc != 0;
+ }
+
enum AllocateOption {
CapacityReserved = 0x1,
Unsharable = 0x2,
+ RawData = 0x4,
Default = 0
};
@@ -139,6 +148,20 @@ struct QTypedArrayData
QArrayData::deallocate(data, sizeof(T), Q_ALIGNOF(AlignmentDummy));
}
+ static QTypedArrayData *fromRawData(const T *data, size_t n,
+ AllocateOptions options = Default)
+ {
+ QTypedArrayData *result = allocate(0, options | RawData);
+ if (result) {
+ Q_ASSERT(!result->ref.isShared()); // No shared empty, please!
+
+ result->offset = reinterpret_cast<const char *>(data)
+ - reinterpret_cast<const char *>(result);
+ result->size = n;
+ }
+ return result;
+ }
+
static QTypedArrayData *sharedNull()
{
return static_cast<QTypedArrayData *>(