Final removal of the size and offset members from QArrayData

Those members are not required anymore and now part of the
object itself.

Change-Id: If9eb5355ca8f2cf9528f6f63ca4e172acc9f9aed
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
Thiago Macieira 2012-06-25 21:22:19 +02:00 committed by Lars Knoll
parent 00fbc087dd
commit eab6eb64d2
5 changed files with 13 additions and 60 deletions

View File

@ -125,7 +125,7 @@ struct QByteArrayData
([]() -> QByteArray { \
enum { Size = sizeof(str) - 1 }; \
static const QArrayData qbytearray_literal = { \
Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, Size, 0, sizeof(QArrayData) }; \
Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }; \
QByteArrayData holder = { \
const_cast<QArrayData *>(&qbytearray_literal), \
const_cast<char *>(str), \

View File

@ -64,7 +64,7 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2,
([]() noexcept -> QString { \
enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
static const QArrayData qstring_literal = { \
Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, Size, 0, sizeof(QArrayData) \
Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 \
}; \
QStringPrivate holder = { \
const_cast<QArrayData *>(&qstring_literal), \

View File

@ -153,11 +153,11 @@ QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Wmissing-field-initializers")
const QArrayData QArrayData::shared_null[2] = {
{ Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0, 0, sizeof(QArrayData) }, // shared null
{ Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }, // shared null
/* zero initialized terminator */};
static const QArrayData emptyNotNullShared[2] = {
{ Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0, 0, sizeof(QArrayData) }, // shared empty
{ Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }, // shared empty
/* zero initialized terminator */};
QT_WARNING_POP
@ -185,7 +185,6 @@ static QArrayData *allocateData(size_t allocSize, uint options)
if (header) {
header->ref_.storeRelaxed(1);
header->flags = options;
header->size = 0;
}
return header;
}
@ -233,7 +232,6 @@ void *QArrayData::allocate(QArrayData **dptr, size_t objectSize, size_t alignmen
// 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;
}

View File

@ -73,11 +73,8 @@ struct Q_CORE_EXPORT QArrayData
QBasicAtomicInt ref_;
uint flags;
int size;
uint alloc;
qptrdiff offset; // in bytes from beginning of header
inline size_t allocatedCapacity()
{
return alloc;
@ -104,20 +101,6 @@ struct Q_CORE_EXPORT QArrayData
return ref_.deref();
}
void *data()
{
Q_ASSERT(size == 0
|| offset < 0 || size_t(offset) >= sizeof(QArrayData));
return reinterpret_cast<char *>(this) + offset;
}
const void *data() const
{
Q_ASSERT(size == 0
|| offset < 0 || size_t(offset) >= sizeof(QArrayData));
return reinterpret_cast<const char *>(this) + offset;
}
// This refers to array data mutability, not "header data" represented by
// data members in QArrayData. Shared data (array and header) must still
// follow COW principles.
@ -188,7 +171,6 @@ struct Q_CORE_EXPORT QArrayData
static void *sharedNullData()
{
QArrayData *const null = const_cast<QArrayData *>(&shared_null[1]);
Q_ASSERT(sharedNull()->data() == null);
return null;
}
};
@ -292,16 +274,6 @@ struct QTypedArrayData
typedef const T* const_iterator;
#endif
T *data() { return static_cast<T *>(QArrayData::data()); }
const T *data() const { return static_cast<const T *>(QArrayData::data()); }
iterator begin(iterator = iterator()) { return data(); }
iterator end(iterator = iterator()) { return data() + size; }
const_iterator begin(const_iterator = const_iterator()) const { return data(); }
const_iterator end(const_iterator = const_iterator()) const { return data() + size; }
const_iterator constBegin(const_iterator = const_iterator()) const { return data(); }
const_iterator constEnd(const_iterator = const_iterator()) const { return data() + size; }
class AlignmentDummy { QArrayData header; T data; };
Q_REQUIRED_RESULT static QPair<QTypedArrayData *, T *> allocate(size_t capacity,
@ -341,9 +313,6 @@ struct QTypedArrayData
};
if (result.ptr) {
Q_ASSERT(!result.ptr->isShared()); // No shared empty, please!
result.ptr->offset = reinterpret_cast<const char *>(data)
- reinterpret_cast<const char *>(result.ptr);
result.ptr->size = int(n);
}
return result;
}
@ -367,15 +336,6 @@ struct QTypedArrayData
}
};
#define Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \
{ Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, size, 0, offset } \
/**/
#define Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(type, size) \
Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size,\
((sizeof(QArrayData) + (alignof(type) - 1)) & ~(alignof(type) - 1) )) \
/**/
////////////////////////////////////////////////////////////////////////////////
// Q_ARRAY_LITERAL
@ -411,16 +371,15 @@ struct QTypedArrayData
Q_ARRAY_LITERAL_CHECK_LITERAL_TYPE(Type); \
\
/* Portable compile-time array size computation */ \
Q_CONSTEXPR Type data[] = { __VA_ARGS__ }; Q_UNUSED(data); \
static Type const data[] = { __VA_ARGS__ }; \
enum { Size = sizeof(data) / sizeof(data[0]) }; \
\
static const QStaticArrayData<Type, Size> literal = { \
Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(Type, Size), { __VA_ARGS__ } }; \
static const QArrayData literal = { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 }; \
\
QArrayDataPointerRef<Type> ref = \
{ static_cast<QTypedArrayData<Type> *>( \
const_cast<QArrayData *>(&literal.header)), \
const_cast<Type *>(literal.data), \
const_cast<QArrayData *>(&literal)), \
const_cast<Type *>(data), \
Size }; \
/**/

View File

@ -83,7 +83,7 @@ void tst_QArrayData::referenceCounting()
{
{
// Reference counting initialized to 1 (owned)
QArrayData array = { Q_BASIC_ATOMIC_INITIALIZER(1), QArrayData::DefaultRawFlags, 0, 0, 0 };
QArrayData array = { Q_BASIC_ATOMIC_INITIALIZER(1), QArrayData::DefaultRawFlags, 0 };
QCOMPARE(array.ref_.loadRelaxed(), 1);
@ -108,7 +108,7 @@ void tst_QArrayData::referenceCounting()
}
{
// Reference counting initialized to -1 (static read-only data)
QArrayData array = { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0, 0, 0 };
QArrayData array = { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 };
QCOMPARE(array.ref_.loadRelaxed(), -1);
@ -156,9 +156,9 @@ void tst_QArrayData::sharedNullEmpty()
void tst_QArrayData::simpleVector()
{
QArrayData data0 = { Q_REFCOUNT_INITIALIZE_STATIC, QArrayData::StaticDataFlags, 0, 0, 0 };
QArrayData data0 = { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 };
QStaticArrayData<int, 7> data1 = {
Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(int, 7),
{ Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 },
{ 0, 1, 2, 3, 4, 5, 6 }
};
@ -429,7 +429,7 @@ void tst_QArrayData::simpleVectorReserve_data()
QTest::newRow("non-empty") << SimpleVector<int>(5, 42) << size_t(5) << size_t(5);
static const QStaticArrayData<int, 15> array = {
Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(int, 15),
{ Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } };
const QArrayDataPointerRef<int> p = {
static_cast<QTypedArrayData<int> *>(
@ -590,10 +590,6 @@ void tst_QArrayData::reallocate()
QFETCH(QArrayData::ArrayOptions, allocateOptions);
QFETCH(bool, isCapacityReserved);
// Maximum alignment that can be requested is that of QArrayData,
// otherwise, we can't use reallocate().
Q_ASSERT(alignment <= alignof(QArrayData));
// Minimum alignment that can be requested is that of QArrayData.
// Typically, this alignment is sizeof(void *) and ensured by malloc.
size_t minAlignment = qMax(alignment, alignof(QArrayData));