unaligned.h 920 Bytes
Newer Older
1
/*
2
 *	Unaligned Data Accesses -- Generic Version, Network Order
3
 *
4
 *	(c) 2000 Martin Mares <mj@ucw.cz>
5 6 7 8 9 10 11
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#ifndef _BIRD_UNALIGNED_H_
#define _BIRD_UNALIGNED_H_

12 13 14 15 16 17 18 19
/*
 *  We don't do any clever tricks with unaligned accesses since it's
 *  virtually impossible to figure out what alignment does the CPU want
 *  (unaligned accesses can be emulated by the OS which makes them work,
 *  but unusably slow). We use memcpy and hope GCC will optimize it out
 *  if possible.
 */

20 21 22
#include <string.h>

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

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

38 39 40 41 42 43 44 45 46 47 48 49
static inline void
put_u16(void *p, u16 x)
{
  x = htons(x);
  memcpy(p, &x, 2);
}

static inline void
put_u32(void *p, u32 x)
{
  x = htonl(x);
  memcpy(p, &x, 4);
50 51
}

52
#endif