Asterisk - The Open Source Telephony Project  18.5.0
unaligned.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2009, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  * \brief Handle unaligned data access
21  */
22 
23 #ifndef _ASTERISK_UNALIGNED_H
24 #define _ASTERISK_UNALIGNED_H
25 
26 #if defined(__cplusplus) || defined(c_plusplus)
27 extern "C" {
28 #endif
29 
30 #ifdef __GNUC__
31 /* If we just tell GCC what's going on, we can trust it to behave optimally */
32 static inline uint64_t get_unaligned_uint64(const void *p)
33 {
34  const struct { uint64_t d; } __attribute__((packed)) *pp = p;
35  return pp->d;
36 }
37 
38 static inline unsigned int get_unaligned_uint32(const void *p)
39 {
40  const struct { unsigned int d; } __attribute__((packed)) *pp = p;
41 
42  return pp->d;
43 }
44 static inline unsigned short get_unaligned_uint16(const void *p)
45 {
46  const struct { unsigned short d; } __attribute__((packed)) *pp = p;
47 
48  return pp->d;
49 }
50 
51 static inline void put_unaligned_uint64(void *p, uint64_t datum)
52 {
53  struct { uint64_t d; } __attribute__((packed,may_alias)) *pp = p;
54 
55  pp->d = datum;
56 }
57 
58 static inline void put_unaligned_uint32(void *p, unsigned int datum)
59 {
60  struct { unsigned int d; } __attribute__((packed)) *pp = p;
61 
62  pp->d = datum;
63 }
64 
65 static inline void put_unaligned_uint16(void *p, unsigned short datum)
66 {
67  struct { unsigned short d; } __attribute__((packed)) *pp = p;
68 
69  pp->d = datum;
70 }
71 #elif defined(SOLARIS) && defined(__sparc__)
72 static inline uint64_t get_unaligned_uint64(const void *p)
73 {
74  const unsigned char *cp = p;
75 
76  return
77  (((uint64_t) cp[0]) << 56) |
78  (((uint64_t) cp[1]) << 48) |
79  (((uint64_t) cp[2]) << 40) |
80  (((uint64_t) cp[3]) << 32) |
81  (((uint64_t) cp[4]) << 24) |
82  (((uint64_t) cp[5]) << 16) |
83  (((uint64_t) cp[6]) << 8) |
84  (((uint64_t) cp[7]) << 0);
85 }
86 
87 static inline unsigned int get_unaligned_uint32(const void *p)
88 {
89  const unsigned char *cp = p;
90 
91  return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | cp[3];
92 }
93 
94 static inline unsigned short get_unaligned_uint16(const void *p)
95 {
96  const unsigned char *cp = p;
97 
98  return (cp[0] << 8) | cp[1] ;
99 }
100 
101 static inline void put_unaligned_uint64(void *p, uint64_t datum)
102 {
103  unsigned char *cp = p;
104 
105  cp[0] = (datum >> 56) & 0xff;
106  cp[1] = (datum >> 48) & 0xff;
107  cp[2] = (datum >> 40) & 0xff;
108  cp[3] = (datum >> 32) & 0xff;
109  cp[4] = (datum >> 24) & 0xff;
110  cp[5] = (datum >> 16) & 0xff;
111  cp[6] = (datum >> 8) & 0xff;
112  cp[7] = (datum >> 0) & 0xff;
113 }
114 
115 static inline void put_unaligned_uint32(void *p, unsigned int datum)
116 {
117  unsigned char *cp = p;
118 
119  cp[0] = datum >> 24;
120  cp[1] = datum >> 16;
121  cp[2] = datum >> 8;
122  cp[3] = datum;
123 }
124 
125 static inline void put_unaligned_uint16(void *p, unsigned int datum)
126 {
127  unsigned char *cp = p;
128 
129  cp[0] = datum >> 8;
130  cp[1] = datum;
131 }
132 #else /* Not GCC, not Solaris/SPARC. Assume we can handle direct load/store. */
133 #define get_unaligned_uint64(p) (*((uint64_t *)(p)))
134 #define get_unaligned_uint32(p) (*((unsigned int *)(p)))
135 #define get_unaligned_uint16(p) (*((unsigned short *)(p)))
136 #define put_unaligned_uint64(p,d) do { uint64_t *__P = (p); *__P = d; } while(0)
137 #define put_unaligned_uint32(p,d) do { unsigned int *__P = (p); *__P = d; } while(0)
138 #define put_unaligned_uint16(p,d) do { unsigned short *__P = (p); *__P = d; } while(0)
139 #endif
140 
141 #if defined(__cplusplus) || defined(c_plusplus)
142 }
143 #endif
144 
145 
146 #endif /* _ASTERISK_UNALIGNED_H */
static struct test_val d
static void put_unaligned_uint16(void *p, unsigned short datum)
Definition: unaligned.h:65
static void put_unaligned_uint32(void *p, unsigned int datum)
Definition: unaligned.h:58
Definition: ndbm.h:57
static unsigned int get_unaligned_uint32(const void *p)
Definition: unaligned.h:38
static unsigned short get_unaligned_uint16(const void *p)
Definition: unaligned.h:44
static void put_unaligned_uint64(void *p, uint64_t datum)
Definition: unaligned.h:51
static uint64_t get_unaligned_uint64(const void *p)
Definition: unaligned.h:32