1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2021 - 2022, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */
#ifndef KMI_UNALIGNED_H
#define KMI_UNALIGNED_H
/**
* @file unaligned.h
* Helpers for unaligned memory accesses. Largely lifted from Linux.
*/
#include <kmi/types.h>
#include <kmi/attrs.h>
/**
* Get unaligned value. Type of value is deduced from pointer type.
*
* @param ptr Pointer to possibly unaligned value to read.
* @return Value pointed to by \c ptr.
*/
#define get_unaligned(ptr) \
_Generic(*(ptr), \
uint8_t: get_unaligned_uint8_t, \
uint16_t: get_unaligned_uint16_t, \
uint32_t: get_unaligned_uint32_t, \
uint64_t: get_unaligned_uint64_t, \
\
int8_t: get_unaligned_int8_t, \
int16_t: get_unaligned_int16_t, \
int32_t: get_unaligned_int32_t, \
int64_t: get_unaligned_int64_t)((void *)ptr)
/**
* Put unaligned value. Type of value is deduced from pointer type.
*
* @param val Value to write to memory.
* @param ptr Pointer to possibly unaligned address.
*/
#define put_unaligned(val, ptr) \
_Generic(*(ptr), \
uint8_t: put_unaligned_uint8_t, \
uint16_t: put_unaligned_uint16_t, \
uint32_t: put_unaligned_uint32_t, \
uint64_t: put_unaligned_uint64_t, \
\
int8_t: put_unaligned_int8_t, \
int16_t: put_unaligned_int16_t, \
int32_t: put_unaligned_int32_t, \
int64_t: put_unaligned_int64_t)(val, (void *)ptr)
/**
* Helper macro for defining an unaligned value reader.
*
* @param type Type of value reader to define.
*/
#define DEFINE_GET(type) \
static inline type get_unaligned_##type(void *ptr) \
{ \
const struct __packed { \
type x; \
} *__pptr = ptr; \
return __pptr->x; \
}
/**
* Read possibly unaligned \ref uint8_t.
*
* Technically speaking a byte can't be unaligned, but this is just here for
* cohesion.
*
* @warning Prefer using \ref get_unaligned().
*
* @param ptr Pointer to \ref uint8_t to read.
* @return Value pointed to by \c ptr.
*/
DEFINE_GET(uint8_t);
/**
* Read possibly unaligned \ref uint16_t.
*
* @warning Prefer using \ref get_unaligned().
*
* @param ptr Pointer to \ref uint16_t to read.
* @return Value pointed to by \c ptr.
*/
DEFINE_GET(uint16_t);
/**
* Read possibly unaligned \ref uint32_t.
*
* @warning Prefer using \ref get_unaligned().
*
* @param ptr Pointer to \ref uint32_t to read.
* @return Value pointed to by \c ptr.
*/
DEFINE_GET(uint32_t);
/**
* Read possibly unaligned \ref uint64_t.
*
* @warning Prefer using \ref get_unaligned().
*
* @param ptr Pointer to \ref uint64_t to read.
* @return Value pointed to by \c ptr.
*/
DEFINE_GET(uint64_t);
/**
* Read possibly unaligned \ref int8_t.
*
* Technically speaking a byte can't be unaligned, but this is just here for
* cohesion.
*
* @warning Prefer using \ref get_unaligned().
*
* @param ptr Pointer to \ref int8_t to read.
* @return Value pointed to by \c ptr.
*/
DEFINE_GET(int8_t);
/**
* Read possibly unaligned \ref int16_t.
*
* @warning Prefer using \ref get_unaligned().
*
* @param ptr Pointer to \ref int16_t to read.
* @return Value pointed to by \c ptr.
*/
DEFINE_GET(int16_t);
/**
* Read possibly unaligned \ref int32_t.
*
* @warning Prefer using \ref get_unaligned().
*
* @param ptr Pointer to \ref int32_t to read.
* @return Value pointed to by \c ptr.
*/
DEFINE_GET(int32_t);
/**
* Read possibly unaligned \ref int64_t.
*
* @warning Prefer using \ref get_unaligned().
*
* @param ptr Pointer to \ref int64_t to read.
* @return Value pointed to by \c ptr.
*/
DEFINE_GET(int64_t);
#undef DEFINE_GET
/**
* Helper macro for defining an unaligned writer.
*
* @param type Type of value to write.
*/
#define DEFINE_PUT(type) \
static inline void put_unaligned_##type(type val, void *ptr) \
{ \
struct __packed { \
type x; \
} *__pptr = ptr; \
__pptr->x = val; \
}
/**
* Write possibly unaligned \ref uint8_t.
*
* Technically speaking a byte can't be unaligned, but this is just here for
* cohesion.
*
* @warning Prefer using \ref put_unaligned().
*
* @param val Value to write.
* @param ptr Address to write to.
*/
DEFINE_PUT(uint8_t);
/**
* Write possibly unaligned \ref uint16_t.
*
* @warning Prefer using \ref put_unaligned().
*
* @param val Value to write.
* @param ptr Address to write to.
*/
DEFINE_PUT(uint16_t);
/**
* Write possibly unaligned \ref uint32_t.
*
* @warning Prefer using \ref put_unaligned().
*
* @param val Value to write.
* @param ptr Address to write to.
*/
DEFINE_PUT(uint32_t);
/**
* Write possibly unaligned \ref uint64_t.
*
* @warning Prefer using \ref put_unaligned().
*
* @param val Value to write.
* @param ptr Address to write to.
*/
DEFINE_PUT(uint64_t);
/**
* Write possibly unaligned \ref int8_t.
*
* Technically speaking a byte can't be unaligned, but this is just here for
* cohesion.
*
* @warning Prefer using \ref put_unaligned().
*
* @param val Value to write.
* @param ptr Address to write to.
*/
DEFINE_PUT(int8_t);
/**
* Write possibly unaligned \ref int16_t.
*
* @warning Prefer using \ref put_unaligned().
*
* @param val Value to write.
* @param ptr Address to write to.
*/
DEFINE_PUT(int16_t);
/**
* Write possibly unaligned \ref int32_t.
*
* @warning Prefer using \ref put_unaligned().
*
* @param val Value to write.
* @param ptr Address to write to.
*/
DEFINE_PUT(int32_t);
/**
* Write possibly unaligned \ref int64_t.
*
* @warning Prefer using \ref put_unaligned().
*
* @param val Value to write.
* @param ptr Address to write to.
*/
DEFINE_PUT(int64_t);
#undef DEFINE_PUT
#endif /* KMI_UNALIGNED_H */
|