diff options
author | Olivier Guiter <olivier.guiter@linux.intel.com> | 2013-01-09 13:42:37 +0100 |
---|---|---|
committer | Samuel Ortiz <sameo@linux.intel.com> | 2013-01-10 13:08:59 +0100 |
commit | c528844e8128fe8eb7da3269a0b5f5142a5690ff (patch) | |
tree | f3a2e0174e2a7ab54560953d308bfeacca968186 /include | |
parent | 8fdb45b93475b8e5d37b8f520b206db0fdf8d999 (diff) | |
download | neard-c528844e8128fe8eb7da3269a0b5f5142a5690ff.tar.gz neard-c528844e8128fe8eb7da3269a0b5f5142a5690ff.tar.bz2 neard-c528844e8128fe8eb7da3269a0b5f5142a5690ff.zip |
types: Fix build error due to unaligned memory access
This fix number of build error on architectures that can't handle
unaligned memory access (eg. ARM).
Most of the code was originally written in BlueZ's bluetooth.h.
Diffstat (limited to 'include')
-rw-r--r-- | include/types.h | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/include/types.h b/include/types.h index bd5b53d..894b7db 100644 --- a/include/types.h +++ b/include/types.h @@ -22,6 +22,8 @@ #ifndef __NEAR_TYPES_H #define __NEAR_TYPES_H +#include <byteswap.h> + #ifndef FALSE #define FALSE (0) #endif @@ -32,4 +34,145 @@ typedef int near_bool_t; +#define near_get_unaligned(ptr) \ +({ \ + struct __attribute__((packed)) { \ + typeof(*(ptr)) __v; \ + } *__p = (typeof(__p)) (ptr); \ + __p->__v; \ +}) + +#define near_put_unaligned(val, ptr) \ +do { \ + struct __attribute__((packed)) { \ + typeof(*(ptr)) __v; \ + } *__p = (typeof(__p)) (ptr); \ + __p->__v = (val); \ +} while (0) + +#if __BYTE_ORDER == __LITTLE_ENDIAN +static inline uint64_t near_get_le64(const void *ptr) +{ + return near_get_unaligned((const uint64_t *) ptr); +} + +static inline uint64_t near_get_be64(const void *ptr) +{ + return bswap_64(near_get_unaligned((const uint64_t *) ptr)); +} + +static inline uint32_t near_get_le32(const void *ptr) +{ + return near_get_unaligned((const uint32_t *) ptr); +} + +static inline uint32_t near_get_be32(const void *ptr) +{ + return bswap_32(near_get_unaligned((const uint32_t *) ptr)); +} + +static inline uint16_t near_get_le16(const void *ptr) +{ + return near_get_unaligned((const uint16_t *) ptr); +} + +static inline uint16_t near_get_be16(const void *ptr) +{ + return bswap_16(near_get_unaligned((const uint16_t *) ptr)); +} + +static inline void near_put_le64(uint64_t val, const void *ptr) +{ + near_put_unaligned(val, (uint64_t *) ptr); +} + +static inline void near_put_be64(uint64_t val, const void *ptr) +{ + near_put_unaligned(bswap_64(val), (uint64_t *) ptr); +} + +static inline void near_put_le32(uint32_t val, const void *ptr) +{ + near_put_unaligned(val, (uint32_t *) ptr); +} + +static inline void near_put_be32(uint32_t val, const void *ptr) +{ + near_put_unaligned(bswap_32(val), (uint32_t *) ptr); +} + +static inline void near_put_le16(uint16_t val, const void *ptr) +{ + near_put_unaligned(val, (uint16_t *) ptr); +} + +static inline void near_put_be16(uint16_t val, const void *ptr) +{ + near_put_unaligned(bswap_16(val), (uint16_t *) ptr); +} + +#elif __BYTE_ORDER == __BIG_ENDIAN +static inline uint64_t near_get_le64(const void *ptr) +{ + return bswap_64(near_get_unaligned((const uint64_t *) ptr)); +} + +static inline uint64_t near_get_be64(const void *ptr) +{ + return near_get_unaligned((const uint64_t *) ptr); +} + +static inline uint32_t near_get_le32(const void *ptr) +{ + return bswap_32(near_get_unaligned((const uint32_t *) ptr)); +} + +static inline uint32_t near_get_be32(const void *ptr) +{ + return near_get_unaligned((const uint32_t *) ptr); +} + +static inline uint16_t near_get_le16(const void *ptr) +{ + return bswap_16(near_get_unaligned((const uint16_t *) ptr)); +} + +static inline uint16_t near_get_be16(const void *ptr) +{ + return near_get_unaligned((const uint16_t *) ptr); +} + +static inline void near_put_le64(uint64_t val, const void *ptr) +{ + near_put_unaligned(bswap_64(val), (uint64_t *) ptr); +} + +static inline void near_put_be64(uint64_t val, const void *ptr) +{ + near_put_unaligned(val, (uint64_t *) ptr); +} + +static inline void near_put_le32(uint32_t val, const void *ptr) +{ + near_put_unaligned(bswap_32(val), (uint32_t *) ptr); +} + +static inline void near_put_be32(uint32_t val, const void *ptr) +{ + near_put_unaligned(val, (uint32_t *) ptr); +} + +static inline void near_put_le16(uint16_t val, const void *ptr) +{ + near_put_unaligned(bswap_16(val), (uint16_t *) ptr); +} + +static inline void near_put_be16(uint16_t val, const void *ptr) +{ + near_put_unaligned(val, (uint16_t *) ptr); +} +#else +#error "Unknown byte order" +#endif + #endif |