diff options
-rw-r--r-- | src/corelib/tools/qarraydata.cpp | 8 | ||||
-rw-r--r-- | src/corelib/tools/qarraydata.h | 18 |
2 files changed, 19 insertions, 7 deletions
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index ff3bf2d6b8..3f417fc390 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -145,18 +145,13 @@ static QArrayData *allocateData(qsizetype allocSize) return header; } - namespace { -// QArrayData with strictest alignment requirements supported by malloc() -struct alignas(std::max_align_t) AlignedQArrayData : QArrayData -{ -}; - struct AllocationResult { void *data; QArrayData *header; }; } +using QtPrivate::AlignedQArrayData; static inline AllocationResult allocateHelper(qsizetype objectSize, qsizetype alignment, qsizetype capacity, @@ -172,6 +167,7 @@ allocateHelper(qsizetype objectSize, qsizetype alignment, qsizetype capacity, // Allocate extra (alignment - Q_ALIGNOF(AlignedQArrayData)) padding // bytes so we can properly align the data array. This assumes malloc is // able to provide appropriate alignment for the header -- as it should! + // Effectively, we allocate one QTypedArrayData<T>::AlignmentDummy. headerSize += alignment - headerAlignment; } Q_ASSERT(headerSize > 0); diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index 2369a27e84..4b71619038 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -102,11 +102,27 @@ struct QArrayData Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::ArrayOptions) +namespace QtPrivate { +// QArrayData with strictest alignment requirements supported by malloc() +#if defined(Q_PROCESSOR_X86_32) && defined(Q_CC_GNU) +// GCC's definition is incorrect since GCC 8 (commit r240248 in SVN; commit +// 63012d9a57edc950c5f30242d1e19318b5708060 in Git). This is applied to all +// GCC-like compilers in case they decide to follow GCC's lead in being wrong. +constexpr size_t MaxPrimitiveAlignment = 2 * sizeof(void *); +#else +constexpr size_t MaxPrimitiveAlignment = alignof(std::max_align_t); +#endif + +struct alignas(MaxPrimitiveAlignment) AlignedQArrayData : QArrayData +{ +}; +} + template <class T> struct QTypedArrayData : QArrayData { - struct AlignmentDummy { QArrayData header; T data; }; + struct AlignmentDummy { QtPrivate::AlignedQArrayData header; T data; }; [[nodiscard]] static QPair<QTypedArrayData *, T *> allocate(qsizetype capacity, AllocationOption option = QArrayData::KeepSize) { |