From f4c1e2c40fcc1285ea24d55ed4eac036d8bbdf1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Wed, 11 Jan 2012 17:16:04 +0100 Subject: 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 Reviewed-by: Thiago Macieira --- src/corelib/tools/qarraydata.cpp | 9 ++-- src/corelib/tools/qarraydata.h | 23 ++++++++++ src/corelib/tools/qarraydatapointer.h | 4 +- tests/auto/corelib/tools/qarraydata/simplevector.h | 6 +++ .../corelib/tools/qarraydata/tst_qarraydata.cpp | 51 ++++++++++++++++++++++ 5 files changed, 89 insertions(+), 4 deletions(-) diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index efed984aef..8f0a95c82c 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -56,16 +56,19 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, && !(alignment & (alignment - 1))); // Don't allocate empty headers - if (!capacity) + if (!(options & RawData) && !capacity) return !(options & Unsharable) ? const_cast(&qt_array_empty) : const_cast(&qt_array_unsharable_empty); + size_t allocSize = sizeof(QArrayData) + objectSize * capacity; + // Allocate extra (alignment - Q_ALIGNOF(QArrayData)) padding bytes so we // can properly align the data array. This assumes malloc is able to // provide appropriate alignment for the header -- as it should! - size_t allocSize = sizeof(QArrayData) + objectSize * capacity - + (alignment - Q_ALIGNOF(QArrayData)); + // Padding is skipped when allocating a header for RawData. + if (!(options & RawData)) + allocSize += (alignment - Q_ALIGNOF(QArrayData)); QArrayData *header = static_cast(qMalloc(allocSize)); Q_CHECK_PTR(header); 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(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(data) + - reinterpret_cast(result); + result->size = n; + } + return result; + } + static QTypedArrayData *sharedNull() { return static_cast( diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index 1dc02daa63..8b1d2a805c 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -114,6 +114,8 @@ public: void setSharable(bool sharable) { + // Can't call setSharable on static read-only data, like shared_null + // and the internal shared-empties. if (d->alloc == 0 && d->size == 0) { d = Data::allocate(0, sharable ? QArrayData::Default @@ -138,7 +140,7 @@ public: bool detach() { - if (d->ref.isShared()) { + if (!d->isMutable() || d->ref.isShared()) { Data *copy = clone(d->detachFlags()); QArrayDataPointer old(d); d = copy; diff --git a/tests/auto/corelib/tools/qarraydata/simplevector.h b/tests/auto/corelib/tools/qarraydata/simplevector.h index a1eb2dac48..3fa7905a39 100644 --- a/tests/auto/corelib/tools/qarraydata/simplevector.h +++ b/tests/auto/corelib/tools/qarraydata/simplevector.h @@ -274,6 +274,12 @@ public: d.detach(); } + static SimpleVector fromRawData(const T *data, size_t size, + QArrayData::AllocateOptions options = Data::Default) + { + return SimpleVector(Data::fromRawData(data, size, options)); + } + private: QArrayDataPointer d; }; diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 90c865c9e7..4dba02cc70 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -81,6 +81,7 @@ private slots: void arrayOps(); void setSharable_data(); void setSharable(); + void fromRawData(); }; template const T &const_(const T &t) { return t; } @@ -1131,5 +1132,55 @@ void tst_QArrayData::setSharable() QVERIFY(array->ref.isSharable()); } +void tst_QArrayData::fromRawData() +{ + static const int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; + + { + // Default: Immutable, sharable + SimpleVector raw = SimpleVector::fromRawData(array, + sizeof(array)/sizeof(array[0]), QArrayData::Default); + + QCOMPARE(raw.size(), size_t(11)); + QCOMPARE(raw.constBegin(), array); + QCOMPARE(raw.constEnd(), array + sizeof(array)/sizeof(array[0])); + + QVERIFY(!raw.isShared()); + QVERIFY(SimpleVector(raw).isSharedWith(raw)); + QVERIFY(!raw.isShared()); + + // Detach + QCOMPARE(raw.back(), 11); + QVERIFY(raw.constBegin() != array); + } + + { + // Immutable, unsharable + SimpleVector raw = SimpleVector::fromRawData(array, + sizeof(array)/sizeof(array[0]), QArrayData::Unsharable); + + QCOMPARE(raw.size(), size_t(11)); + QCOMPARE(raw.constBegin(), array); + QCOMPARE(raw.constEnd(), array + sizeof(array)/sizeof(array[0])); + + SimpleVector copy(raw); + QVERIFY(!copy.isSharedWith(raw)); + QVERIFY(!raw.isShared()); + + QCOMPARE(copy.size(), size_t(11)); + + for (size_t i = 0; i < 11; ++i) + QCOMPARE(const_(copy)[i], const_(raw)[i]); + + QCOMPARE(raw.size(), size_t(11)); + QCOMPARE(raw.constBegin(), array); + QCOMPARE(raw.constEnd(), array + sizeof(array)/sizeof(array[0])); + + // Detach + QCOMPARE(raw.back(), 11); + QVERIFY(raw.constBegin() != array); + } +} + QTEST_APPLESS_MAIN(tst_QArrayData) #include "tst_qarraydata.moc" -- cgit v1.2.3