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 <simon.hausmann@qt.io>
bb10
Thiago Macieira 2012-06-04 21:06:49 +02:00 committed by Lars Knoll
parent 41287d355b
commit bf0b4f332a
9 changed files with 60 additions and 58 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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());

View File

@ -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 <class T>
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);

View File

@ -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);

View File

@ -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<T>::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 <typename T>
void QVector<T>::reallocData(const int asize, const int aalloc, QArrayData::AllocationOptions options)
void QVector<T>::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<T>::reallocData(const int asize, const int aalloc, QArrayData::Allo
}
template<typename T>
void QVector<T>::realloc(int aalloc, QArrayData::AllocationOptions options)
void QVector<T>::realloc(int aalloc, QArrayData::ArrayOptions options)
{
Q_ASSERT(aalloc >= d->size);
Data *x = d;
@ -767,7 +767,7 @@ void QVector<T>::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<T>::isComplex)
@ -789,7 +789,7 @@ void QVector<T>::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<T>::iterator QVector<T>::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<T>::isRelocatable) {
T *b = d->end();
T *i = d->end() + n;
@ -853,7 +853,7 @@ typename QVector<T>::iterator QVector<T>::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<T>::isRelocatable) {
T *i = d->end();
T *j = i + 1;
@ -961,7 +961,7 @@ QVector<T> &QVector<T>::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);
}

View File

@ -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));
}

View File

@ -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<size_t>("objectSize");
QTest::addColumn<size_t>("alignment");
QTest::addColumn<QArrayData::AllocationOptions>("allocateOptions");
QTest::addColumn<QArrayData::ArrayOptions>("allocateOptions");
QTest::addColumn<bool>("isCapacityReserved");
QTest::addColumn<const QArrayData *>("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<T> raw = SimpleVector<T>::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);