diff options
author | João Abecasis <joao.abecasis@nokia.com> | 2012-03-12 17:14:48 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-23 19:43:00 +0100 |
commit | accfdc85e5cb1816b3eda02ec8d37474259c247e (patch) | |
tree | e2c0369b07eaac3982f155adc8890532e6799e0a /src | |
parent | 98c3b8a44220096a4e2a3967a4e9742c3605a5cd (diff) |
Fallback implementation of Q_ALIGNOF
For all practical purposes, the fallback introduced here returns the
desired value. Having a fallback enables unconditional use of Q_ALIGNOF.
For compilers that provide native support for it, Q_ALIGNOF is otherwise
#defined in qcompilerdetection.h.
Change-Id: Ie148ca8936cbbf8b80fe87771a14797c39a9d30c
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/global/qglobal.h | 50 | ||||
-rw-r--r-- | src/corelib/kernel/qmetaobjectbuilder.cpp | 4 | ||||
-rw-r--r-- | src/corelib/tools/qcontiguouscache.h | 4 | ||||
-rw-r--r-- | src/corelib/tools/qhash.h | 5 | ||||
-rw-r--r-- | src/corelib/tools/qvector.h | 4 |
5 files changed, 50 insertions, 17 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index d0d6e851ad..ee577a7563 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -248,6 +248,56 @@ typedef quint64 qulonglong; #if defined(__cplusplus) +namespace QtPrivate { + template <class T> + struct AlignOfHelper + { + char c; + T type; + + AlignOfHelper(); + ~AlignOfHelper(); + }; + + template <class T> + struct AlignOf_Default + { + enum { Value = sizeof(AlignOfHelper<T>) - sizeof(T) }; + }; + + template <class T> struct AlignOf : AlignOf_Default<T> { }; + template <class T> struct AlignOf<T &> : AlignOf<T> {}; + template <size_t N, class T> struct AlignOf<T[N]> : AlignOf<T> {}; + +#ifdef Q_COMPILER_RVALUE_REFS + template <class T> struct AlignOf<T &&> : AlignOf<T> {}; +#endif + +#if defined(Q_PROCESSOR_X86_32) && !defined(Q_OS_WIN) + template <class T> struct AlignOf_WorkaroundForI386Abi { enum { Value = sizeof(T) }; }; + + // x86 ABI weirdness + // Alignment of naked type is 8, but inside struct has alignment 4. + template <> struct AlignOf<double> : AlignOf_WorkaroundForI386Abi<double> {}; + template <> struct AlignOf<qint64> : AlignOf_WorkaroundForI386Abi<qint64> {}; + template <> struct AlignOf<quint64> : AlignOf_WorkaroundForI386Abi<quint64> {}; +#ifdef Q_CC_CLANG + // GCC and Clang seem to disagree wrt to alignment of arrays + template <size_t N> struct AlignOf<double[N]> : AlignOf_Default<double> {}; + template <size_t N> struct AlignOf<qint64[N]> : AlignOf_Default<qint64> {}; + template <size_t N> struct AlignOf<quint64[N]> : AlignOf_Default<quint64> {}; +#endif +#endif +} // namespace QtPrivate + +#define QT_EMULATED_ALIGNOF(T) \ + (size_t(QT_PREPEND_NAMESPACE(QtPrivate)::AlignOf<T>::Value)) + +#ifndef Q_ALIGNOF +#define Q_ALIGNOF(T) QT_EMULATED_ALIGNOF(T) +#endif + + /* quintptr and qptrdiff is guaranteed to be the same size as a pointer, i.e. diff --git a/src/corelib/kernel/qmetaobjectbuilder.cpp b/src/corelib/kernel/qmetaobjectbuilder.cpp index 59740960c9..8d6d7cbe91 100644 --- a/src/corelib/kernel/qmetaobjectbuilder.cpp +++ b/src/corelib/kernel/qmetaobjectbuilder.cpp @@ -1095,11 +1095,7 @@ int QMetaStringTable::enter(const QByteArray &value) int QMetaStringTable::preferredAlignment() { -#ifdef Q_ALIGNOF return Q_ALIGNOF(QByteArrayData); -#else - return sizeof(void *); -#endif } // Returns the size (in bytes) required for serializing this string table. diff --git a/src/corelib/tools/qcontiguouscache.h b/src/corelib/tools/qcontiguouscache.h index 4dc763f35d..4e01d7b586 100644 --- a/src/corelib/tools/qcontiguouscache.h +++ b/src/corelib/tools/qcontiguouscache.h @@ -170,11 +170,7 @@ private: } int alignOfTypedData() const { -#ifdef Q_ALIGNOF return qMax<int>(sizeof(void*), Q_ALIGNOF(Data)); -#else - return 0; -#endif } }; diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 73162b6cf1..ef003c8a71 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -257,13 +257,8 @@ class QHash return reinterpret_cast<Node *>(node); } -#ifdef Q_ALIGNOF static inline int alignOfNode() { return qMax<int>(sizeof(void*), Q_ALIGNOF(Node)); } static inline int alignOfDummyNode() { return qMax<int>(sizeof(void*), Q_ALIGNOF(DummyNode)); } -#else - static inline int alignOfNode() { return 0; } - static inline int alignOfDummyNode() { return 0; } -#endif public: inline QHash() : d(const_cast<QHashData *>(&QHashData::shared_null)) { } diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 226d0bb258..04ab9e6e80 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -331,11 +331,7 @@ private: } static Q_DECL_CONSTEXPR int alignOfTypedData() { -#ifdef Q_ALIGNOF return Q_ALIGNOF(AlignmentDummy); -#else - return sizeof(void *); -#endif } }; |