summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qarraydata.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-06-14 15:05:34 +0200
committerLars Knoll <lars.knoll@qt.io>2019-12-08 10:29:42 +0100
commitadd048bc4eee8e4422fe2b434b4b817f56693d33 (patch)
treec83275a8046a64b38ce474a1f9a6f2297526aab5 /src/corelib/tools/qarraydata.cpp
parentf2569c0ff75eb9a8418bb065c33c318f0a44c8ed (diff)
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 <simon.hausmann@qt.io>
Diffstat (limited to 'src/corelib/tools/qarraydata.cpp')
-rw-r--r--src/corelib/tools/qarraydata.cpp29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp
index aa7fac15ef..8052c2ca69 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<QArrayData *>(&qt_array_empty);
+ *dptr = const_cast<QArrayData *>(&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<void *>(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 *, void *>
+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<char *>(dataPointer) - reinterpret_cast<char *>(data);
options |= AllocatedDataType | MutableData;
- options &= ~ImmutableHeader;
QArrayData *header = reallocateData(data, allocSize, options);
- if (header)
+ if (header) {
header->alloc = capacity;
- return header;
+ dataPointer = reinterpret_cast<char *>(header) + offset;
+ }
+ return qMakePair(static_cast<QArrayData *>(header), dataPointer);
}
void QArrayData::deallocate(QArrayData *data, size_t objectSize,