Statistics
| Branch: | Revision:

iof-bird-daemon / lib / unaligned.h @ 7152e5ef

History | View | Annotate | Download (1.24 KB)

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 "sysdep/unix/endian.h"
21
#include "lib/string.h"
22

    
23
static inline u16
24
get_u16(const void *p)
25
{
26
  u16 x;
27
  memcpy(&x, p, 2);
28
  return ntohs(x);
29
}
30

    
31
static inline u32
32
get_u32(const void *p)
33
{
34
  u32 x;
35
  memcpy(&x, p, 4);
36
  return ntohl(x);
37
}
38

    
39
static inline u64
40
get_u64(const void *p)
41
{
42
  u32 xh, xl;
43
  memcpy(&xh, p, 4);
44
  memcpy(&xl, p+4, 4);
45
  return (((u64) ntohl(xh)) << 32) | ntohl(xl);
46
}
47

    
48
static inline void
49
put_u16(void *p, u16 x)
50
{
51
  x = htons(x);
52
  memcpy(p, &x, 2);
53
}
54

    
55
static inline void
56
put_u32(void *p, u32 x)
57
{
58
  x = htonl(x);
59
  memcpy(p, &x, 4);
60
}
61

    
62
static inline void
63
put_u64(void *p, u64 x)
64
{
65
  u32 xh, xl;
66
  xh = htonl(x >> 32);
67
  xl = htonl((u32) x);
68
  memcpy(p, &xh, 4);
69
  memcpy(p+4, &xl, 4);
70
}
71

    
72
#endif