From 5c2ff22ba117f295718c529198ab42ee4646d90c Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 26 Apr 2016 16:40:37 -0700 Subject: Use void instead of uchar in the endian-swapping function parameters This allows us to pass pointers to storage that is not an array of uchar, which it hardly ever is. Change-Id: Ifea6e497f11a461db432ffff14490d2c2df21906 Reviewed-by: Konstantin Ritt Reviewed-by: Marc Mutz --- src/corelib/global/qendian.h | 57 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'src/corelib/global/qendian.h') diff --git a/src/corelib/global/qendian.h b/src/corelib/global/qendian.h index 34bb015a2f..c2028289a7 100644 --- a/src/corelib/global/qendian.h +++ b/src/corelib/global/qendian.h @@ -58,25 +58,26 @@ QT_BEGIN_NAMESPACE /* * ENDIAN FUNCTIONS */ -inline void qbswap_helper(const uchar *src, uchar *dest, int size) +inline void qbswap_helper(const void *src, void *dest, int size) { - for (int i = 0; i < size ; ++i) dest[i] = src[size - 1 - i]; + for (int i = 0; i < size ; ++i) + static_cast(dest)[i] = static_cast(src)[size - 1 - i]; } /* - * qbswap(const T src, const uchar *dest); + * qbswap(const T src, const void *dest); * Changes the byte order of \a src from big endian to little endian or vice versa * and stores the result in \a dest. * There is no alignment requirements for \a dest. */ -template inline void qbswap(const T src, uchar *dest) +template inline void qbswap(const T src, void *dest) { - qbswap_helper(reinterpret_cast(&src), dest, sizeof(T)); + qbswap_helper(&src, dest, sizeof(T)); } // Used to implement a type-safe and alignment-safe copy operation // If you want to avoid the memcpy, you must write specializations for these functions -template Q_ALWAYS_INLINE void qToUnaligned(const T src, uchar *dest) +template Q_ALWAYS_INLINE void qToUnaligned(const T src, void *dest) { // Using sizeof(T) inside memcpy function produces internal compiler error with // MSVC2008/ARM in tst_endian -> use extra indirection to resolve size of T. @@ -89,7 +90,7 @@ template Q_ALWAYS_INLINE void qToUnaligned(const T src, uchar *dest (dest, &src, size); } -template Q_ALWAYS_INLINE T qFromUnaligned(const uchar *src) +template Q_ALWAYS_INLINE T qFromUnaligned(const void *src) { T dest; const size_t size = sizeof(T); @@ -122,11 +123,11 @@ template <> inline quint32 qbswap(quint32 source) return __builtin_bswap32(source); } -template <> inline void qbswap(quint64 source, uchar *dest) +template <> inline void qbswap(quint64 source, void *dest) { qToUnaligned(__builtin_bswap64(source), dest); } -template <> inline void qbswap(quint32 source, uchar *dest) +template <> inline void qbswap(quint32 source, void *dest) { qToUnaligned(__builtin_bswap32(source), dest); } @@ -158,7 +159,7 @@ template <> inline quint16 qbswap(quint16 source) { return __builtin_bswap16(source); } -template <> inline void qbswap(quint16 source, uchar *dest) +template <> inline void qbswap(quint16 source, void *dest) { qToUnaligned(__builtin_bswap16(source), dest); } @@ -187,17 +188,17 @@ template <> inline qint16 qbswap(qint16 source) return qbswap(quint16(source)); } -template <> inline void qbswap(qint64 source, uchar *dest) +template <> inline void qbswap(qint64 source, void *dest) { qbswap(quint64(source), dest); } -template <> inline void qbswap(qint32 source, uchar *dest) +template <> inline void qbswap(qint32 source, void *dest) { qbswap(quint32(source), dest); } -template <> inline void qbswap(qint16 source, uchar *dest) +template <> inline void qbswap(qint16 source, void *dest) { qbswap(quint16(source), dest); } @@ -212,9 +213,9 @@ template inline T qToLittleEndian(T source) { return qbswap(source); } template inline T qFromLittleEndian(T source) { return qbswap(source); } -template inline void qToBigEndian(T src, uchar *dest) +template inline void qToBigEndian(T src, void *dest) { qToUnaligned(src, dest); } -template inline void qToLittleEndian(T src, uchar *dest) +template inline void qToLittleEndian(T src, void *dest) { qbswap(src, dest); } #else // Q_LITTLE_ENDIAN @@ -226,9 +227,9 @@ template inline T qToLittleEndian(T source) { return source; } template inline T qFromLittleEndian(T source) { return source; } -template inline void qToBigEndian(T src, uchar *dest) +template inline void qToBigEndian(T src, void *dest) { qbswap(src, dest); } -template inline void qToLittleEndian(T src, uchar *dest) +template inline void qToLittleEndian(T src, void *dest) { qToUnaligned(src, dest); } #endif // Q_BYTE_ORDER == Q_BIG_ENDIAN @@ -243,34 +244,34 @@ template <> inline qint8 qbswap(qint8 source) return source; } -/* T qFromLittleEndian(const uchar *src) +/* T qFromLittleEndian(const void *src) * This function will read a little-endian encoded value from \a src * and return the value in host-endian encoding. * There is no requirement that \a src must be aligned. */ -template inline T qFromLittleEndian(const uchar *src) +template inline T qFromLittleEndian(const void *src) { return qFromLittleEndian(qFromUnaligned(src)); } -template <> inline quint8 qFromLittleEndian(const uchar *src) -{ return static_cast(src[0]); } -template <> inline qint8 qFromLittleEndian(const uchar *src) -{ return static_cast(src[0]); } +template <> inline quint8 qFromLittleEndian(const void *src) +{ return static_cast(src)[0]; } +template <> inline qint8 qFromLittleEndian(const void *src) +{ return static_cast(src)[0]; } /* This function will read a big-endian (also known as network order) encoded value from \a src * and return the value in host-endian encoding. * There is no requirement that \a src must be aligned. */ -template inline T qFromBigEndian(const uchar *src) +template inline T qFromBigEndian(const void *src) { return qFromBigEndian(qFromUnaligned(src)); } -template <> inline quint8 qFromBigEndian(const uchar *src) -{ return static_cast(src[0]); } -template <> inline qint8 qFromBigEndian(const uchar *src) -{ return static_cast(src[0]); } +template <> inline quint8 qFromBigEndian(const void *src) +{ return static_cast(src)[0]; } +template <> inline qint8 qFromBigEndian(const void *src) +{ return static_cast(src)[0]; } QT_END_NAMESPACE -- cgit v1.2.3