]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - include/unaligned.h
5 * For the benefit of those who are trying to port Linux to another
6 * architecture, here are some C-language equivalents.
8 * This is based almost entirely upon Richard Henderson's
9 * asm-alpha/unaligned.h implementation. Some comments were
10 * taken from David Mosberger's asm-ia64/unaligned.h header.
14 * The main single-value unaligned transfer routines.
16 #define get_unaligned(ptr) \
17 __get_unaligned((ptr), sizeof(*(ptr)))
18 #define put_unaligned(x,ptr) \
19 __put_unaligned((__u64)(x), (ptr), sizeof(*(ptr)))
22 * This function doesn't actually exist. The idea is that when
23 * someone uses the macros below with an unsupported size (datatype),
24 * the linker will alert us to the problem via an unresolved reference
27 extern void bad_unaligned_access_length(void) __attribute__((noreturn
));
29 struct __una_u64
{ __u64 x
__attribute__((packed
)); };
30 struct __una_u32
{ __u32 x
__attribute__((packed
)); };
31 struct __una_u16
{ __u16 x
__attribute__((packed
)); };
34 * Elemental unaligned loads
37 static inline __u64
__uldq(const __u64
*addr
)
39 const struct __una_u64
*ptr
= (const struct __una_u64
*) addr
;
43 static inline __u32
__uldl(const __u32
*addr
)
45 const struct __una_u32
*ptr
= (const struct __una_u32
*) addr
;
49 static inline __u16
__uldw(const __u16
*addr
)
51 const struct __una_u16
*ptr
= (const struct __una_u16
*) addr
;
56 * Elemental unaligned stores
59 static inline void __ustq(__u64 val
, __u64
*addr
)
61 struct __una_u64
*ptr
= (struct __una_u64
*) addr
;
65 static inline void __ustl(__u32 val
, __u32
*addr
)
67 struct __una_u32
*ptr
= (struct __una_u32
*) addr
;
71 static inline void __ustw(__u16 val
, __u16
*addr
)
73 struct __una_u16
*ptr
= (struct __una_u16
*) addr
;
77 #define __get_unaligned(ptr, size) ({ \
78 const void *__gu_p = ptr; \
82 val = *(const __u8 *)__gu_p; \
85 val = __uldw(__gu_p); \
88 val = __uldl(__gu_p); \
91 val = __uldq(__gu_p); \
94 bad_unaligned_access_length(); \
96 (__typeof__(*(ptr)))val; \
99 #define __put_unaligned(val, ptr, size) \
101 void *__gu_p = ptr; \
104 *(__u8 *)__gu_p = val; \
107 __ustw(val, __gu_p); \
110 __ustl(val, __gu_p); \
113 __ustq(val, __gu_p); \
116 bad_unaligned_access_length(); \
120 #endif /* _UNALIGNED_H */