From 37e26d2b3011acc86bbda1e1f46114d7c8441915 Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Fri, 22 Jan 2021 09:26:09 +0100 Subject: Extend alignment of QArrayData to std::max_align_t in allocation Make stricter alignment requirements for the allocated header This strict alignment allows reallocateUnaligned() to property account for the padding occurring in cases when alignof(QArrayData) < alignof(T) <= alignof(std::max_align_t), which happens to be the case on e.g. 32-bit platforms with specific alignment requirements. This adds 4 bytes (the difference between alignof(std::max_align_t) and sizeof(QArrayData)) of overhead for QString, QByteArray and certain QLists on 32-bit systems. Task-number: QTBUG-90359 Pick-to: 6.0 Change-Id: I8176a4cc79f100ee772b09425e88fe8ff3ae226a Reviewed-by: Thiago Macieira Reviewed-by: Qt CI Bot --- tests/auto/corelib/tools/qlist/tst_qlist.cpp | 30 +++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'tests/auto/corelib/tools/qlist') diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index 3975bec678..64123c61a7 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. @@ -347,6 +347,8 @@ private slots: void fromReadOnlyData() const; + void qtbug_90359() const; + private: template void copyConstructor() const; template void add() const; @@ -3181,5 +3183,31 @@ void tst_QList::fromReadOnlyData() const } } +struct alignas(8) CustomAligned +{ + qint64 v = 0; + CustomAligned() = default; + CustomAligned(qint64 i) : v(i) { } + friend bool operator==(const CustomAligned &x, const CustomAligned &y) { return x.v == y.v; } +}; + +void tst_QList::qtbug_90359() const +{ + // Note: a very special test that could only fail for specific alignments + constexpr bool canFail = (alignof(QArrayData) == 4) && (sizeof(QArrayData) == 12); + if constexpr (!canFail) + qWarning() << "This test will always succeed on this system."; + if constexpr (alignof(CustomAligned) > alignof(std::max_align_t)) + QSKIP("The codepaths tested here wouldn't be executed."); + + const QList expected({ 0, 1, 2, 3, 4, 5, 6 }); + QList actual; + for (int i = 0; i < 7; ++i) { + actual.append(i); + QCOMPARE(actual.at(i), i); + } + QCOMPARE(actual, expected); +} + QTEST_MAIN(tst_QList) #include "tst_qlist.moc" -- cgit v1.2.3