diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index 8498d0e4d5..f1b88d5051 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -46,10 +46,17 @@ QT_BEGIN_NAMESPACE -const QArrayData QArrayData::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, 0 }; +const QArrayData QArrayData::shared_null[2] = { + { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, sizeof(QArrayData) }, // shared null + /* zero initialized terminator */}; -static const QArrayData qt_array_empty = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, 0 }; -static const QArrayData qt_array_unsharable_empty = { { Q_BASIC_ATOMIC_INITIALIZER(0) }, 0, 0, 0, 0 }; +static const QArrayData qt_array[3] = { + { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, sizeof(QArrayData) }, // shared empty + { { Q_BASIC_ATOMIC_INITIALIZER(0) }, 0, 0, 0, sizeof(QArrayData) }, // unsharable empty + /* zero initialized terminator */}; + +static const QArrayData &qt_array_empty = qt_array[0]; +static const QArrayData &qt_array_unsharable_empty = qt_array[1]; QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, size_t capacity, AllocationOptions options) diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index b4cefe6729..ae4cbc3081 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -115,7 +115,8 @@ struct Q_CORE_EXPORT QArrayData static void deallocate(QArrayData *data, size_t objectSize, size_t alignment); - static const QArrayData shared_null; + static const QArrayData shared_null[2]; + static QArrayData *sharedNull() { return const_cast(shared_null); } }; Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::AllocationOptions) @@ -169,8 +170,7 @@ struct QTypedArrayData static QTypedArrayData *sharedNull() { Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); - return static_cast( - const_cast(&QArrayData::shared_null)); + return static_cast(QArrayData::sharedNull()); } }; diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 9bfbac0017..4bd04f9bc3 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -50,9 +50,9 @@ struct SharedNullVerifier { SharedNullVerifier() { - Q_ASSERT(QArrayData::shared_null.ref.isStatic()); - Q_ASSERT(QArrayData::shared_null.ref.isShared()); - Q_ASSERT(QArrayData::shared_null.ref.isSharable()); + Q_ASSERT(QArrayData::shared_null[0].ref.isStatic()); + Q_ASSERT(QArrayData::shared_null[0].ref.isShared()); + Q_ASSERT(QArrayData::shared_null[0].ref.isSharable()); } }; @@ -159,7 +159,7 @@ void tst_QArrayData::referenceCounting() void tst_QArrayData::sharedNullEmpty() { - QArrayData *null = const_cast(&QArrayData::shared_null); + QArrayData *null = const_cast(QArrayData::shared_null); QArrayData *empty = QArrayData::allocate(1, Q_ALIGNOF(QArrayData), 0); QVERIFY(null->ref.isStatic());