]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/unaligned.h
Merge branch 'master' into rip-new
[thirdparty/bird.git] / lib / unaligned.h
1 /*
2 * Unaligned Data Accesses -- Generic Version, Network Order
3 *
4 * (c) 2000 Martin Mares <mj@ucw.cz>
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
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
20 #include "lib/string.h"
21
22 static inline u16
23 get_u16(void *p)
24 {
25 u16 x;
26 memcpy(&x, p, 2);
27 return ntohs(x);
28 }
29
30 static inline u32
31 get_u32(void *p)
32 {
33 u32 x;
34 memcpy(&x, p, 4);
35 return ntohl(x);
36 }
37
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);
50 }
51
52 #endif