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/corelib/global/qglobal.h | |
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/corelib/global/qglobal.h')
-rw-r--r-- | src/corelib/global/qglobal.h | 50 |
1 files changed, 50 insertions, 0 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. |