]> wimlib.net Git - wimlib/blob - include/wimlib/unaligned.h
d30c9f200704735df1c9d368cb4db5a9214b4c8a
[wimlib] / include / wimlib / unaligned.h
1 /*
2  * unaligned.h
3  *
4  * Inline functions for unaligned memory accesses.
5  *
6  * The author dedicates this file to the public domain.
7  * You can do whatever you want with this file.
8  */
9
10 #ifndef _WIMLIB_UNALIGNED_H
11 #define _WIMLIB_UNALIGNED_H
12
13 #include "compiler.h"
14 #include "endianness.h"
15 #include "types.h"
16
17 #define DEFINE_UNALIGNED_TYPE(type)                             \
18 struct type##_unaligned {                                       \
19         type v;                                                 \
20 } _packed_attribute;                                            \
21                                                                 \
22 static inline type                                              \
23 load_##type##_unaligned(const void *p)                          \
24 {                                                               \
25         return ((const struct type##_unaligned *)p)->v;         \
26 }                                                               \
27                                                                 \
28 static inline void                                              \
29 store_##type##_unaligned(type val, void *p)                     \
30 {                                                               \
31         ((struct type##_unaligned *)p)->v = val;                \
32 }
33
34 DEFINE_UNALIGNED_TYPE(le16);
35 DEFINE_UNALIGNED_TYPE(le32);
36 DEFINE_UNALIGNED_TYPE(le64);
37
38 #endif /* _WIMLIB_UNALIGNED_H */