From add048bc4eee8e4422fe2b434b4b817f56693d33 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 14 Jun 2012 15:05:34 +0200 Subject: Start moving QArrayData's size and data pointer to the main class This requires that the allocation functions return two pointers: the d pointer and the pointer to the actual data. Ported QArrayDataPointer & SimpleVector to the inlined size & data. For now, the size and offset members are not yet removed from QArrayData, to let QVector, QByteArray and QString compile unmodified. Change-Id: I8489300976723d75b8fd5831427b1e2bba486196 Reviewed-by: Simon Hausmann --- src/corelib/tools/qarraydata.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'src/corelib/tools/qarraydata.cpp') diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index aa7fac15efa..8052c2ca69b 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -198,16 +198,19 @@ static QArrayData *reallocateData(QArrayData *header, size_t allocSize, uint opt return header; } -QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, +void *QArrayData::allocate(QArrayData **dptr, size_t objectSize, size_t alignment, size_t capacity, ArrayOptions options) noexcept { + Q_ASSERT(dptr); // Alignment is a power of two Q_ASSERT(alignment >= alignof(QArrayData) && !(alignment & (alignment - 1))); - if (capacity == 0) + if (capacity == 0) { // optimization for empty headers - return const_cast(&qt_array_empty); + *dptr = const_cast(&qt_array_empty); + return sharedNullData(); + } size_t headerSize = sizeof(QArrayData); @@ -225,14 +228,17 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, options |= AllocatedDataType | MutableData; options &= ~ImmutableHeader; QArrayData *header = allocateData(allocSize, options); + quintptr data = 0; if (header) { - quintptr data = (quintptr(header) + sizeof(QArrayData) + alignment - 1) + // find where offset should point to so that data() is aligned to alignment bytes + data = (quintptr(header) + sizeof(QArrayData) + alignment - 1) & ~(alignment - 1); header->offset = data - quintptr(header); header->alloc = capacity; } - return header; + *dptr = header; + return reinterpret_cast(data); } QArrayData *QArrayData::prepareRawData(ArrayOptions options) Q_DECL_NOTHROW @@ -243,8 +249,9 @@ QArrayData *QArrayData::prepareRawData(ArrayOptions options) Q_DECL_NOTHROW return header; } -QArrayData *QArrayData::reallocateUnaligned(QArrayData *data, size_t objectSize, size_t capacity, - ArrayOptions options) noexcept +QPair +QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer, + size_t objectSize, size_t capacity, ArrayOptions options) noexcept { Q_ASSERT(data); Q_ASSERT(data->isMutable()); @@ -252,12 +259,14 @@ QArrayData *QArrayData::reallocateUnaligned(QArrayData *data, size_t objectSize, size_t headerSize = sizeof(QArrayData); size_t allocSize = calculateBlockSize(capacity, objectSize, headerSize, options); + qptrdiff offset = reinterpret_cast(dataPointer) - reinterpret_cast(data); options |= AllocatedDataType | MutableData; - options &= ~ImmutableHeader; QArrayData *header = reallocateData(data, allocSize, options); - if (header) + if (header) { header->alloc = capacity; - return header; + dataPointer = reinterpret_cast(header) + offset; + } + return qMakePair(static_cast(header), dataPointer); } void QArrayData::deallocate(QArrayData *data, size_t objectSize, -- cgit v1.2.3