From bf0b4f332a2f1ec9860c610d98cd27e483869bec Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 4 Jun 2012 21:06:49 +0200 Subject: [PATCH] Rename QArrayData::AllocateOptions enum and update some flags Rename to QArrayData::ArrayOptions in preparation for these flags being in the array itself, instead of used just for allocating new ones. For that reason, rename QArrayData::Default to DefaultAllocationFlags. And introduce QArray::DefaultRawFlags to mean the flags needed for creating a raw (static) QArrayData. Also rename QArrayData::Grow to GrowsForward, so we may add GrowsBackward in the future. Change-Id: I536d9b34124f775d53cf810f62d6b0eaada8daef Reviewed-by: Simon Hausmann --- src/corelib/text/qbytearray.cpp | 16 +++++----- src/corelib/text/qbytearray.h | 2 +- src/corelib/text/qstring.cpp | 2 +- src/corelib/tools/qarraydata.cpp | 6 ++-- src/corelib/tools/qarraydata.h | 32 ++++++++++--------- src/corelib/tools/qarraydatapointer.h | 2 +- src/corelib/tools/qvector.h | 20 ++++++------ .../corelib/tools/qarraydata/simplevector.h | 8 ++--- .../tools/qarraydata/tst_qarraydata.cpp | 30 ++++++++--------- 9 files changed, 60 insertions(+), 58 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 4a00c664c0..380730775d 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1776,7 +1776,7 @@ void QByteArray::resize(int size) d = x; } else { if (d->ref.isShared() || uint(size) + 1u > d->alloc) - reallocData(uint(size) + 1u, d->detachFlags() | Data::Grow); + reallocData(uint(size) + 1u, d->detachFlags() | Data::GrowsForward); if (d->alloc) { d->size = size; d->data()[size] = '\0'; @@ -1803,7 +1803,7 @@ QByteArray &QByteArray::fill(char ch, int size) return *this; } -void QByteArray::reallocData(uint alloc, Data::AllocationOptions options) +void QByteArray::reallocData(uint alloc, Data::ArrayOptions options) { if (d->ref.isShared() || IS_RAW_DATA(d)) { Data *x = Data::allocate(alloc, options); @@ -1901,7 +1901,7 @@ QByteArray &QByteArray::prepend(const char *str, int len) { if (str) { if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc) - reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow); + reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::GrowsForward); memmove(d->data()+len, d->data(), d->size); memcpy(d->data(), str, len); d->size += len; @@ -1927,7 +1927,7 @@ QByteArray &QByteArray::prepend(const char *str, int len) QByteArray &QByteArray::prepend(char ch) { if (d->ref.isShared() || uint(d->size) + 2u > d->alloc) - reallocData(uint(d->size) + 2u, d->detachFlags() | Data::Grow); + reallocData(uint(d->size) + 2u, d->detachFlags() | Data::GrowsForward); memmove(d->data()+1, d->data(), d->size); d->data()[0] = ch; ++d->size; @@ -1965,7 +1965,7 @@ QByteArray &QByteArray::append(const QByteArray &ba) *this = ba; } else if (ba.d->size != 0) { if (d->ref.isShared() || uint(d->size + ba.d->size) + 1u > d->alloc) - reallocData(uint(d->size + ba.d->size) + 1u, d->detachFlags() | Data::Grow); + reallocData(uint(d->size + ba.d->size) + 1u, d->detachFlags() | Data::GrowsForward); memcpy(d->data() + d->size, ba.d->data(), ba.d->size); d->size += ba.d->size; d->data()[d->size] = '\0'; @@ -1997,7 +1997,7 @@ QByteArray& QByteArray::append(const char *str) if (str) { const int len = int(strlen(str)); if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc) - reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow); + reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::GrowsForward); memcpy(d->data() + d->size, str, len + 1); // include null terminator d->size += len; } @@ -2022,7 +2022,7 @@ QByteArray &QByteArray::append(const char *str, int len) len = qstrlen(str); if (str && len) { if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc) - reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow); + reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::GrowsForward); memcpy(d->data() + d->size, str, len); // include null terminator d->size += len; d->data()[d->size] = '\0'; @@ -2050,7 +2050,7 @@ QByteArray &QByteArray::append(const char *str, int len) QByteArray& QByteArray::append(char ch) { if (d->ref.isShared() || uint(d->size) + 2u > d->alloc) - reallocData(uint(d->size) + 2u, d->detachFlags() | Data::Grow); + reallocData(uint(d->size) + 2u, d->detachFlags() | Data::GrowsForward); d->data()[d->size++] = ch; d->data()[d->size] = '\0'; return *this; diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 1376e26260..4d56cd93fd 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -445,7 +445,7 @@ public: private: operator QNoImplicitBoolCast() const; Data *d; - void reallocData(uint alloc, Data::AllocationOptions options); + void reallocData(uint alloc, Data::ArrayOptions options); void expand(int i); QByteArray nulTerminated() const; diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index dcdc87181e..5f5b0acd15 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2351,7 +2351,7 @@ void QString::reallocData(uint alloc, bool grow) { auto allocOptions = d->detachFlags(); if (grow) - allocOptions |= QArrayData::Grow; + allocOptions |= QArrayData::GrowsForward; if (d->ref.isShared() || IS_RAW_DATA(d)) { Data *x = Data::allocate(alloc, allocOptions); diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index ed7dfe2e41..592faf39c3 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -170,7 +170,7 @@ static inline size_t calculateBlockSize(size_t &capacity, size_t objectSize, siz // Calculate the byte size // allocSize = objectSize * capacity + headerSize, but checked for overflow // plus padded to grow in size - if (options & QArrayData::Grow) { + if (options & QArrayData::GrowsForward) { auto r = qCalculateGrowingBlockSize(capacity, objectSize, headerSize); capacity = r.elementCount; return r.size; @@ -188,7 +188,7 @@ static QArrayData *reallocateData(QArrayData *header, size_t allocSize, uint opt } QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, - size_t capacity, AllocationOptions options) noexcept + size_t capacity, ArrayOptions options) noexcept { // Alignment is a power of two Q_ASSERT(alignment >= alignof(QArrayData) @@ -227,7 +227,7 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, } QArrayData *QArrayData::reallocateUnaligned(QArrayData *data, size_t objectSize, size_t capacity, - AllocationOptions options) noexcept + ArrayOptions options) noexcept { Q_ASSERT(data); Q_ASSERT(data->isMutable()); diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index a91dd54262..c2da2767c3 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -76,15 +77,16 @@ struct Q_CORE_EXPORT QArrayData return alloc != 0; } - enum AllocationOption { + enum ArrayOption { CapacityReserved = 0x1, RawData = 0x4, - Grow = 0x8, + GrowsForward = 0x8, - Default = 0 + DefaultAllocationFlags = 0, + DefaultRawFlags = 0 }; - Q_DECLARE_FLAGS(AllocationOptions, AllocationOption) + Q_DECLARE_FLAGS(ArrayOptions, ArrayOption) size_t detachCapacity(size_t newSize) const { @@ -93,17 +95,17 @@ struct Q_CORE_EXPORT QArrayData return newSize; } - AllocationOptions detachFlags() const + ArrayOptions detachFlags() const { - AllocationOptions result; + ArrayOptions result; if (capacityReserved) result |= CapacityReserved; return result; } - AllocationOptions cloneFlags() const + ArrayOptions cloneFlags() const { - AllocationOptions result; + ArrayOptions result; if (capacityReserved) result |= CapacityReserved; return result; @@ -114,9 +116,9 @@ struct Q_CORE_EXPORT QArrayData __attribute__((__malloc__)) #endif static QArrayData *allocate(size_t objectSize, size_t alignment, - size_t capacity, AllocationOptions options = Default) noexcept; + size_t capacity, ArrayOptions options = DefaultAllocationFlags) noexcept; Q_REQUIRED_RESULT static QArrayData *reallocateUnaligned(QArrayData *data, size_t objectSize, - size_t newCapacity, AllocationOptions newOptions = Default) noexcept; + size_t newCapacity, ArrayOptions newOptions = DefaultAllocationFlags) noexcept; static void deallocate(QArrayData *data, size_t objectSize, size_t alignment) noexcept; @@ -130,7 +132,7 @@ struct Q_CORE_EXPORT QArrayData } }; -Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::AllocationOptions) +Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::ArrayOptions) template struct QTypedArrayData @@ -225,7 +227,7 @@ struct QTypedArrayData class AlignmentDummy { QArrayData header; T data; }; Q_REQUIRED_RESULT static QTypedArrayData *allocate(size_t capacity, - AllocationOptions options = Default) + ArrayOptions options = DefaultAllocationFlags) { Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); void *result = QArrayData::allocate(sizeof(T), @@ -237,10 +239,10 @@ struct QTypedArrayData } static QTypedArrayData *reallocateUnaligned(QTypedArrayData *data, size_t capacity, - AllocationOptions options = Default) + ArrayOptions options = DefaultAllocationFlags) { Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); - void *result = QArrayData::reallocateUnaligned(data, sizeof(T), capacity, options)); + void *result = QArrayData::reallocateUnaligned(data, sizeof(T), capacity, options); #if (defined(Q_CC_GNU) && Q_CC_GNU >= 407) || QT_HAS_BUILTIN(__builtin_assume_aligned) result =__builtin_assume_aligned(result, Q_ALIGNOF(AlignmentDummy)); #endif @@ -254,7 +256,7 @@ struct QTypedArrayData } static QTypedArrayData *fromRawData(const T *data, size_t n, - AllocationOptions options = Default) + ArrayOptions options = DefaultRawFlags) { Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); QTypedArrayData *result = allocate(0, options | RawData); diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index b8a1ef5d68..ffeaff5862 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -155,7 +155,7 @@ public: } private: - Q_REQUIRED_RESULT Data *clone(QArrayData::AllocationOptions options) const + Q_REQUIRED_RESULT Data *clone(QArrayData::ArrayOptions options) const { Data *x = Data::allocate(d->detachCapacity(d->size), options); Q_CHECK_PTR(x); diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 5def2eceb2..3220ba1463 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -307,9 +307,9 @@ public: private: // ### Qt6: remove methods, they are unused - void reallocData(const int size, const int alloc, QArrayData::AllocationOptions options = QArrayData::Default); + void reallocData(const int size, const int alloc, QArrayData::ArrayOptions options = QArrayData::DefaultAllocationFlags); void reallocData(const int sz) { reallocData(sz, d->alloc); } - void realloc(int alloc, QArrayData::AllocationOptions options = QArrayData::Default); + void realloc(int alloc, QArrayData::ArrayOptions options = QArrayData::DefaultAllocationFlags); void freeData(Data *d); void defaultConstruct(T *from, T *to); void copyConstruct(const T *srcFrom, const T *srcTo, T *dstFrom); @@ -422,7 +422,7 @@ void QVector::resize(int asize) if (asize == d->size) return detach(); if (asize > int(d->alloc) || !isDetached()) { // there is not enough space - QArrayData::AllocationOptions opt = asize > int(d->alloc) ? QArrayData::Grow : QArrayData::Default; + QArrayData::ArrayOptions opt = asize > int(d->alloc) ? QArrayData::GrowsForward : QArrayData::DefaultAllocationFlags; realloc(qMax(int(d->alloc), asize), opt); } if (asize < d->size) @@ -580,7 +580,7 @@ QT_WARNING_DISABLE_MSVC(4127) // conditional expression is constant #endif template -void QVector::reallocData(const int asize, const int aalloc, QArrayData::AllocationOptions options) +void QVector::reallocData(const int asize, const int aalloc, QArrayData::ArrayOptions options) { Q_ASSERT(asize >= 0 && asize <= aalloc); Data *x = d; @@ -680,7 +680,7 @@ void QVector::reallocData(const int asize, const int aalloc, QArrayData::Allo } template -void QVector::realloc(int aalloc, QArrayData::AllocationOptions options) +void QVector::realloc(int aalloc, QArrayData::ArrayOptions options) { Q_ASSERT(aalloc >= d->size); Data *x = d; @@ -767,7 +767,7 @@ void QVector::append(const T &t) const bool isTooSmall = uint(d->size + 1) > d->alloc; if (!isDetached() || isTooSmall) { T copy(t); - QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default); + QArrayData::ArrayOptions opt(isTooSmall ? QArrayData::GrowsForward : QArrayData::DefaultAllocationFlags); realloc(isTooSmall ? d->size + 1 : d->alloc, opt); if (QTypeInfo::isComplex) @@ -789,7 +789,7 @@ void QVector::append(T &&t) { const bool isTooSmall = uint(d->size + 1) > d->alloc; if (!isDetached() || isTooSmall) { - QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default); + QArrayData::ArrayOptions opt(isTooSmall ? QArrayData::GrowsForward : QArrayData::DefaultAllocationFlags); realloc(isTooSmall ? d->size + 1 : d->alloc, opt); } @@ -820,7 +820,7 @@ typename QVector::iterator QVector::insert(iterator before, size_type n, c if (n != 0) { const T copy(t); if (!isDetached() || d->size + n > int(d->alloc)) - realloc(d->size + n, QArrayData::Grow); + realloc(d->size + n, QArrayData::GrowsForward); if (!QTypeInfoQuery::isRelocatable) { T *b = d->end(); T *i = d->end() + n; @@ -853,7 +853,7 @@ typename QVector::iterator QVector::insert(iterator before, T &&t) const auto offset = std::distance(d->begin(), before); if (!isDetached() || d->size + 1 > int(d->alloc)) - realloc(d->size + 1, QArrayData::Grow); + realloc(d->size + 1, QArrayData::GrowsForward); if (!QTypeInfoQuery::isRelocatable) { T *i = d->end(); T *j = i + 1; @@ -961,7 +961,7 @@ QVector &QVector::operator+=(const QVector &l) uint newSize = d->size + l.d->size; const bool isTooSmall = newSize > d->alloc; if (!isDetached() || isTooSmall) { - QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default); + QArrayData::ArrayOptions opt(isTooSmall ? QArrayData::GrowsForward : QArrayData::DefaultAllocationFlags); realloc(isTooSmall ? newSize : d->alloc, opt); } diff --git a/tests/auto/corelib/tools/qarraydata/simplevector.h b/tests/auto/corelib/tools/qarraydata/simplevector.h index e587ad27b5..ffc8ba770c 100644 --- a/tests/auto/corelib/tools/qarraydata/simplevector.h +++ b/tests/auto/corelib/tools/qarraydata/simplevector.h @@ -199,7 +199,7 @@ public: || capacity() - size() < size_t(last - first)) { SimpleVector detached(Data::allocate( d->detachCapacity(size() + (last - first)), - d->detachFlags() | Data::Grow)); + d->detachFlags() | Data::GrowsForward)); detached.d->copyAppend(first, last); detached.d->copyAppend(begin, begin + d->size); @@ -220,7 +220,7 @@ public: || capacity() - size() < size_t(last - first)) { SimpleVector detached(Data::allocate( d->detachCapacity(size() + (last - first)), - d->detachFlags() | Data::Grow)); + d->detachFlags() | Data::GrowsForward)); if (d->size) { const T *const begin = constBegin(); @@ -260,7 +260,7 @@ public: || capacity() - size() < size_t(last - first)) { SimpleVector detached(Data::allocate( d->detachCapacity(size() + (last - first)), - d->detachFlags() | Data::Grow)); + d->detachFlags() | Data::GrowsForward)); if (position) detached.d->copyAppend(begin, where); @@ -328,7 +328,7 @@ public: } static SimpleVector fromRawData(const T *data, size_t size, - QArrayData::AllocationOptions options = Data::Default) + QArrayData::ArrayOptions options = Data::DefaultRawFlags) { return SimpleVector(Data::fromRawData(data, size, options)); } diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 47bdc2fd38..d3e071bb1b 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -528,13 +528,13 @@ struct Deallocator }; Q_DECLARE_METATYPE(const QArrayData *) -Q_DECLARE_METATYPE(QArrayData::AllocationOptions) +Q_DECLARE_METATYPE(QArrayData::ArrayOptions) void tst_QArrayData::allocate_data() { QTest::addColumn("objectSize"); QTest::addColumn("alignment"); - QTest::addColumn("allocateOptions"); + QTest::addColumn("allocateOptions"); QTest::addColumn("isCapacityReserved"); QTest::addColumn("commonEmpty"); @@ -553,13 +553,13 @@ void tst_QArrayData::allocate_data() struct { char const *description; - QArrayData::AllocationOptions allocateOptions; + QArrayData::ArrayOptions allocateOptions; bool isCapacityReserved; const QArrayData *commonEmpty; } options[] = { - { "Default", QArrayData::Default, false, shared_empty }, + { "Default", QArrayData::DefaultAllocationFlags, false, shared_empty }, { "Reserved", QArrayData::CapacityReserved, true, shared_empty }, - { "Grow", QArrayData::Grow, false, shared_empty } + { "Grow", QArrayData::GrowsForward, false, shared_empty } }; for (size_t i = 0; i < sizeof(types)/sizeof(types[0]); ++i) @@ -577,7 +577,7 @@ void tst_QArrayData::allocate() { QFETCH(size_t, objectSize); QFETCH(size_t, alignment); - QFETCH(QArrayData::AllocationOptions, allocateOptions); + QFETCH(QArrayData::ArrayOptions, allocateOptions); QFETCH(bool, isCapacityReserved); QFETCH(const QArrayData *, commonEmpty); @@ -587,18 +587,18 @@ void tst_QArrayData::allocate() // Shared Empty QCOMPARE(QArrayData::allocate(objectSize, minAlignment, 0, - QArrayData::AllocationOptions(allocateOptions)), commonEmpty); + QArrayData::ArrayOptions(allocateOptions)), commonEmpty); Deallocator keeper(objectSize, minAlignment); keeper.headers.reserve(1024); for (int capacity = 1; capacity <= 1024; capacity <<= 1) { QArrayData *data = QArrayData::allocate(objectSize, minAlignment, - capacity, QArrayData::AllocationOptions(allocateOptions)); + capacity, QArrayData::ArrayOptions(allocateOptions)); keeper.headers.append(data); QCOMPARE(data->size, 0); - if (allocateOptions & QArrayData::Grow) + if (allocateOptions & QArrayData::GrowsForward) QVERIFY(data->alloc > uint(capacity)); else QCOMPARE(data->alloc, uint(capacity)); @@ -614,7 +614,7 @@ void tst_QArrayData::reallocate() { QFETCH(size_t, objectSize); QFETCH(size_t, alignment); - QFETCH(QArrayData::AllocationOptions, allocateOptions); + QFETCH(QArrayData::ArrayOptions, allocateOptions); QFETCH(bool, isCapacityReserved); // Maximum alignment that can be requested is that of QArrayData, @@ -628,7 +628,7 @@ void tst_QArrayData::reallocate() int capacity = 10; Deallocator keeper(objectSize, minAlignment); QArrayData *data = QArrayData::allocate(objectSize, minAlignment, capacity, - QArrayData::AllocationOptions(allocateOptions) & ~QArrayData::Grow); + QArrayData::ArrayOptions(allocateOptions) & ~QArrayData::GrowsForward); keeper.headers.append(data); memset(data->data(), 'A', objectSize * capacity); @@ -637,13 +637,13 @@ void tst_QArrayData::reallocate() // now try to reallocate int newCapacity = 40; data = QArrayData::reallocateUnaligned(data, objectSize, newCapacity, - QArrayData::AllocationOptions(allocateOptions)); + QArrayData::ArrayOptions(allocateOptions)); QVERIFY(data); keeper.headers.clear(); keeper.headers.append(data); QCOMPARE(data->size, capacity); - if (allocateOptions & QArrayData::Grow) + if (allocateOptions & QArrayData::GrowsForward) QVERIFY(data->alloc > uint(newCapacity)); else QCOMPARE(data->alloc, uint(newCapacity)); @@ -681,7 +681,7 @@ void tst_QArrayData::alignment() for (int i = 0; i < 100; ++i) { QArrayData *data = QArrayData::allocate(sizeof(Unaligned), - minAlignment, 8, QArrayData::Default); + minAlignment, 8, QArrayData::DefaultAllocationFlags); keeper.headers.append(data); QVERIFY(data); @@ -1253,7 +1253,7 @@ void fromRawData_impl() { // Default: Immutable, sharable SimpleVector raw = SimpleVector::fromRawData(array, - sizeof(array)/sizeof(array[0]), QArrayData::Default); + sizeof(array)/sizeof(array[0]), QArrayData::DefaultAllocationFlags); QCOMPARE(raw.size(), size_t(11)); QCOMPARE((const T *)raw.constBegin(), array);