From 419eaa0679c3867d8d9a3da8845a3015e29800d7 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 30 Oct 2020 14:10:31 +0100 Subject: Cleanup QArrayDataOps::reallocate() and related MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don't use QArrayData::GrowsForward/Backward anymore and replace it with a simple 'bool grow'. Change-Id: Ifddfef3ae860b11dda4c40854c71ef2aeb29df34 Reviewed-by: Andrei Golubev Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 6 +++--- src/corelib/text/qstring.cpp | 4 ++-- src/corelib/tools/qarraydata.cpp | 18 +++++++++--------- src/corelib/tools/qarraydata.h | 12 ++++++++---- src/corelib/tools/qarraydataops.h | 4 ++-- 5 files changed, 24 insertions(+), 20 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 945a2a26fd..1a1b2d86b0 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -671,7 +671,7 @@ QByteArray qUncompress(const uchar* data, qsizetype nbytes) return invalidCompressedData(); } else { // grow the block - d->reallocate(d->allocatedCapacity()*2); + d->reallocate(d->allocatedCapacity()*2, QArrayData::Grow); if (Q_UNLIKELY(d.data() == nullptr)) return invalidCompressedData(); } @@ -1719,7 +1719,7 @@ void QByteArray::reallocData(qsizetype alloc, Data::ArrayOptions options) dd.data()[dd.size] = 0; d = dd; } else { - d->reallocate(alloc); + d->reallocate(alloc, options & (QArrayData::GrowsBackwards|QArrayData::GrowsForward) ? QArrayData::Grow : QArrayData::KeepSize); } } @@ -1734,7 +1734,7 @@ void QByteArray::reallocGrowData(qsizetype n) dd.data()[dd.size] = 0; d = dd; } else { - d->reallocate(d.constAllocatedCapacity() + n); + d->reallocate(d.constAllocatedCapacity() + n, QArrayData::Grow); } } diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 3e6188a38d..ebdb571041 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2516,7 +2516,7 @@ void QString::reallocData(qsizetype alloc, Data::ArrayOptions allocOptions) dd.data()[dd.size] = 0; d = dd; } else { - d->reallocate(alloc); + d->reallocate(alloc, allocOptions & (QArrayData::GrowsBackwards|QArrayData::GrowsForward) ? QArrayData::Grow : QArrayData::KeepSize); } } @@ -2531,7 +2531,7 @@ void QString::reallocGrowData(qsizetype n) dd.data()[dd.size] = 0; d = dd; } else { - d->reallocate(d.constAllocatedCapacity() + n); + d->reallocate(d.constAllocatedCapacity() + n, QArrayData::Grow); } } diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index ab4a0f8af6..4feb52a710 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -159,12 +159,12 @@ static inline qsizetype reserveExtraBytes(qsizetype allocSize) return allocSize; } -static inline qsizetype calculateBlockSize(qsizetype &capacity, qsizetype objectSize, qsizetype headerSize, uint options) +static inline qsizetype calculateBlockSize(qsizetype &capacity, qsizetype objectSize, qsizetype headerSize, QArrayData::AllocationOption option) { // Calculate the byte size // allocSize = objectSize * capacity + headerSize, but checked for overflow // plus padded to grow in size - if (options & (QArrayData::GrowsForward | QArrayData::GrowsBackwards)) { + if (option == QArrayData::Grow) { auto r = qCalculateGrowingBlockSize(capacity, objectSize, headerSize); capacity = r.elementCount; return r.size; @@ -173,12 +173,12 @@ static inline qsizetype calculateBlockSize(qsizetype &capacity, qsizetype object } } -static QArrayData *allocateData(qsizetype allocSize, uint options) +static QArrayData *allocateData(qsizetype allocSize) { QArrayData *header = static_cast(::malloc(size_t(allocSize))); if (header) { header->ref_.storeRelaxed(1); - header->flags = options; + header->flags = 0; header->alloc = 0; } return header; @@ -208,18 +208,19 @@ void *QArrayData::allocate(QArrayData **dptr, qsizetype objectSize, qsizetype al } Q_ASSERT(headerSize > 0); - qsizetype allocSize = calculateBlockSize(capacity, objectSize, headerSize, options); + qsizetype allocSize = calculateBlockSize(capacity, objectSize, headerSize, (options & (GrowsForward|GrowsBackwards)) ? QArrayData::Grow : QArrayData::KeepSize); allocSize = reserveExtraBytes(allocSize); if (Q_UNLIKELY(allocSize < 0)) { // handle overflow. cannot allocate reliably *dptr = nullptr; return nullptr; } - QArrayData *header = allocateData(allocSize, options); + QArrayData *header = allocateData(allocSize); void *data = nullptr; if (header) { // find where offset should point to so that data() is aligned to alignment bytes data = QTypedArrayData::dataStart(header, alignment); + header->flags = options & CapacityReserved; header->alloc = qsizetype(capacity); } @@ -229,12 +230,12 @@ void *QArrayData::allocate(QArrayData **dptr, qsizetype objectSize, qsizetype al QPair QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer, - qsizetype objectSize, qsizetype capacity, ArrayOptions options) noexcept + qsizetype objectSize, qsizetype capacity, AllocationOption option) noexcept { Q_ASSERT(!data || !data->isShared()); qsizetype headerSize = sizeof(QArrayData); - qsizetype allocSize = calculateBlockSize(capacity, objectSize, headerSize, options); + qsizetype allocSize = calculateBlockSize(capacity, objectSize, headerSize, option); qptrdiff offset = dataPointer ? reinterpret_cast(dataPointer) - reinterpret_cast(data) : headerSize; allocSize = reserveExtraBytes(allocSize); @@ -243,7 +244,6 @@ QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer, QArrayData *header = static_cast(::realloc(data, size_t(allocSize))); if (header) { - header->flags = options; header->alloc = uint(capacity); dataPointer = reinterpret_cast(header) + offset; } diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index 4cb0f7a45c..76f2c33043 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -51,6 +51,11 @@ template struct QTypedArrayData; struct Q_CORE_EXPORT QArrayData { + enum AllocationOption { + Grow, + KeepSize + }; + enum ArrayOption { /// this option is used by the allocate() function DefaultAllocationFlags = 0, @@ -122,7 +127,7 @@ struct Q_CORE_EXPORT QArrayData static void *allocate(QArrayData **pdata, qsizetype objectSize, qsizetype alignment, qsizetype capacity, ArrayOptions options = DefaultAllocationFlags) noexcept; [[nodiscard]] static QPair reallocateUnaligned(QArrayData *data, void *dataPointer, - qsizetype objectSize, qsizetype newCapacity, ArrayOptions newOptions = DefaultAllocationFlags) noexcept; + qsizetype objectSize, qsizetype newCapacity, AllocationOption option) noexcept; static void deallocate(QArrayData *data, qsizetype objectSize, qsizetype alignment) noexcept; }; @@ -221,12 +226,11 @@ struct QTypedArrayData } static QPair - reallocateUnaligned(QTypedArrayData *data, T *dataPointer, qsizetype capacity, - ArrayOptions options = DefaultAllocationFlags) + reallocateUnaligned(QTypedArrayData *data, T *dataPointer, qsizetype capacity, AllocationOption option) { static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData)); QPair pair = - QArrayData::reallocateUnaligned(data, dataPointer, sizeof(T), capacity, options); + QArrayData::reallocateUnaligned(data, dataPointer, sizeof(T), capacity, option); return qMakePair(static_cast(pair.first), static_cast(pair.second)); } diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index 140d79b2e3..a904341eff 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -457,9 +457,9 @@ public: } } - void reallocate(qsizetype alloc) + void reallocate(qsizetype alloc, QArrayData::AllocationOption option) { - auto pair = Data::reallocateUnaligned(this->d, this->ptr, alloc, QArrayData::GrowsBackwards); + auto pair = Data::reallocateUnaligned(this->d, this->ptr, alloc, option); this->d = pair.first; this->ptr = pair.second; } -- cgit v1.2.3