From 8f91cace8ffa2ccb4a3fa1649e4a3240af0a0ed6 Mon Sep 17 00:00:00 2001 From: Noah Watkins Date: Thu, 2 Jan 2014 10:45:41 -0800 Subject: [PATCH] endian: check byte order on OSX Defines CEPH_[BIG|LITTLE]_ENDIAN to use, as BYTE_ORDER etc macros may be different names on different platforms. Signed-off-by: Noah Watkins --- src/common/sctp_crc32.c | 12 ++++------- src/include/byteorder.h | 47 +++++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/common/sctp_crc32.c b/src/common/sctp_crc32.c index c02ed856dbd78..4acf5298510ab 100644 --- a/src/common/sctp_crc32.c +++ b/src/common/sctp_crc32.c @@ -42,11 +42,7 @@ __FBSDID("$FreeBSD: src/sys/netinet/sctp_crc32.c,v 1.8 2007/05/08 17:01:10 rrs E #include -#if defined(__FreeBSD__) -#include -#else -#include -#endif +#include "include/byteorder.h" #ifndef SCTP_USE_ADLER32 @@ -541,7 +537,7 @@ sctp_crc32c_sb8_64_bit(uint32_t crc, crc = sctp_crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^ (crc >> 8); for (li = 0; li < running_length / 8; li++) { -#if BYTE_ORDER == BIG_ENDIAN +#ifdef CEPH_BIG_ENDIAN crc ^= *p_buf++; crc ^= (*p_buf++) << 8; crc ^= (*p_buf++) << 16; @@ -557,7 +553,7 @@ sctp_crc32c_sb8_64_bit(uint32_t crc, sctp_crc_tableil8_o72[term2 & 0x000000FF] ^ sctp_crc_tableil8_o64[(term2 >> 8) & 0x000000FF]; -#if BYTE_ORDER == BIG_ENDIAN +#ifdef CEPH_BIG_ENDIAN crc ^= sctp_crc_tableil8_o56[*p_buf++]; crc ^= sctp_crc_tableil8_o48[*p_buf++]; crc ^= sctp_crc_tableil8_o40[*p_buf++]; @@ -610,7 +606,7 @@ sctp_crc32c_sb8_64_bit_zero(uint32_t crc, sctp_crc_tableil8_o72[term2 & 0x000000FF] ^ sctp_crc_tableil8_o64[(term2 >> 8) & 0x000000FF]; -#if BYTE_ORDER == BIG_ENDIAN +#ifdef CEPH_BIG_ENDIAN crc ^= sctp_crc_tableil8_o56[0]; crc ^= sctp_crc_tableil8_o48[0]; crc ^= sctp_crc_tableil8_o40[0]; diff --git a/src/include/byteorder.h b/src/include/byteorder.h index f8c74991e7ab3..191d24256db99 100644 --- a/src/include/byteorder.h +++ b/src/include/byteorder.h @@ -7,18 +7,31 @@ #ifndef CEPH_BYTEORDER_H #define CEPH_BYTEORDER_H -#if defined(__linux__) -#include -#elif defined(__FreeBSD__) -#include -#else -#error "Your platform is not yet supported." +#include +#include "int_types.h" + +#if defined(__APPLE__) +# if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN +# define CEPH_LITTLE_ENDIAN +# elif __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN +# define CEPH_BIG_ENDIAN +# endif #endif #if defined(__FreeBSD__) -#define __BYTE_ORDER _BYTE_ORDER -#define __BIG_ENDIAN _BIG_ENDIAN -#define __LITTLE_ENDIAN _LITTLE_ENDIAN +# if _BYTE_ORDER == _LITTLE_ENDIAN +# define CEPH_LITTLE_ENDIAN +# elif _BYTE_ORDER == _BIG_ENDIAN +# define CEPH_BIG_ENDIAN +# endif +#endif + +#if defined(__linux__) +# if BYTE_ORDER == LITTLE_ENDIAN +# define CEPH_LITTLE_ENDIAN +# elif BYTE_ORDER == BIG_ENDIAN +# define CEPH_BIG_ENDIAN +# endif #endif static __inline__ __u16 swab16(__u16 val) @@ -44,24 +57,20 @@ static __inline__ uint64_t swab64(uint64_t val) ((val << 56))); } -#if !defined(__BYTE_ORDER) || !defined(__BIG_ENDIAN) || !defined(__LITTLE_ENDIAN) -#error "Endianess is unknown!" -#endif - // mswab == maybe swab (if not LE) -#if __BYTE_ORDER == __BIG_ENDIAN +#ifdef CEPH_BIG_ENDIAN # define mswab64(a) swab64(a) # define mswab32(a) swab32(a) # define mswab16(a) swab16(a) -#else -# if __BYTE_ORDER != __LITTLE_ENDIAN -# warning __BYTE_ORDER is not defined, assuming little endian -# endif +#elif defined(CEPH_LITTLE_ENDIAN) # define mswab64(a) (a) # define mswab32(a) (a) # define mswab16(a) (a) +#else +# error "Could not determine endianess" #endif +#ifdef __cplusplus #define MAKE_LE_CLASS(bits) \ struct ceph_le##bits { \ @@ -81,6 +90,8 @@ MAKE_LE_CLASS(32) MAKE_LE_CLASS(16) #undef MAKE_LE_CLASS +#endif /* __cplusplus */ + #define init_le64(x) { (__u64)mswab64(x) } #define init_le32(x) { (__u32)mswab32(x) } #define init_le16(x) { (__u16)mswab16(x) } -- 2.39.5