]>
Commit | Line | Data |
---|---|---|
58ef912c | 1 | /* |
a8f944cb | 2 | * Unaligned Data Accesses -- Generic Version, Network Order |
58ef912c | 3 | * |
a8f944cb | 4 | * (c) 2000 Martin Mares <mj@ucw.cz> |
58ef912c MM |
5 | * |
6 | * Can be freely distributed and used under the terms of the GNU GPL. | |
7 | */ | |
8 | ||
9 | #ifndef _BIRD_UNALIGNED_H_ | |
10 | #define _BIRD_UNALIGNED_H_ | |
11 | ||
a8f944cb MM |
12 | /* |
13 | * We don't do any clever tricks with unaligned accesses since it's | |
14 | * virtually impossible to figure out what alignment does the CPU want | |
15 | * (unaligned accesses can be emulated by the OS which makes them work, | |
16 | * but unusably slow). We use memcpy and hope GCC will optimize it out | |
17 | * if possible. | |
18 | */ | |
19 | ||
221135d6 | 20 | #include "lib/string.h" |
b1487ee9 | 21 | |
b1487ee9 | 22 | static inline u16 |
a8f944cb | 23 | get_u16(void *p) |
b1487ee9 MM |
24 | { |
25 | u16 x; | |
a8f944cb MM |
26 | memcpy(&x, p, 2); |
27 | return ntohs(x); | |
b1487ee9 | 28 | } |
b1487ee9 | 29 | |
b1487ee9 | 30 | static inline u32 |
a8f944cb | 31 | get_u32(void *p) |
b1487ee9 MM |
32 | { |
33 | u32 x; | |
a8f944cb MM |
34 | memcpy(&x, p, 4); |
35 | return ntohl(x); | |
36 | } | |
b1487ee9 | 37 | |
a8f944cb MM |
38 | static inline void |
39 | put_u16(void *p, u16 x) | |
40 | { | |
41 | x = htons(x); | |
42 | memcpy(p, &x, 2); | |
43 | } | |
44 | ||
45 | static inline void | |
46 | put_u32(void *p, u32 x) | |
47 | { | |
48 | x = htonl(x); | |
49 | memcpy(p, &x, 4); | |
b1487ee9 | 50 | } |
b1487ee9 | 51 | |
58ef912c | 52 | #endif |