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 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/corelib/tools/qarraydata.cpp') 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); -- cgit v1.2.3