]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/unaligned.h
2 * Copyright (c) 2010, 2011, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "byte-order.h"
22 #include "openvswitch/types.h"
23 #include "openvswitch/type-props.h"
27 static inline uint16_t get_unaligned_u16(const uint16_t *);
28 static inline uint32_t get_unaligned_u32(const uint32_t *);
29 static inline void put_unaligned_u16(uint16_t *, uint16_t);
30 static inline void put_unaligned_u32(uint32_t *, uint32_t);
31 static inline void put_unaligned_u64(uint64_t *, uint64_t);
33 static inline ovs_be16
get_unaligned_be16(const ovs_be16
*);
34 static inline ovs_be32
get_unaligned_be32(const ovs_be32
*);
35 static inline ovs_be64
get_unaligned_be64(const ovs_be64
*);
36 static inline void put_unaligned_be16(ovs_be16
*, ovs_be16
);
37 static inline void put_unaligned_be32(ovs_be32
*, ovs_be32
);
38 static inline void put_unaligned_be64(ovs_be64
*, ovs_be64
);
40 /* uint64_t get_unaligned_u64(uint64_t *p);
42 * Returns the value of the possibly misaligned uint64_t at 'p'. 'p' may
43 * actually be any type that points to a 64-bit integer. That is, on Unix-like
44 * 32-bit ABIs, it may point to an "unsigned long long int", and on Unix-like
45 * 64-bit ABIs, it may point to an "unsigned long int" or an "unsigned long
48 * This is special-cased because on some Linux targets, the kernel __u64 is
49 * unsigned long long int and the userspace uint64_t is unsigned long int, so
50 * that any single function prototype would fail to accept one or the other.
52 * Below, "sizeof (*(P) % 1)" verifies that *P has an integer type, since
53 * operands to % must be integers.
55 #define get_unaligned_u64(P) \
56 (BUILD_ASSERT(sizeof *(P) == 8), \
57 BUILD_ASSERT_GCCONLY(!TYPE_IS_SIGNED(typeof(*(P)))), \
58 (void) sizeof (*(P) % 1), \
59 get_unaligned_u64__((const uint64_t *) (P)))
62 /* GCC implementations. */
63 #define GCC_UNALIGNED_ACCESSORS(TYPE, ABBREV) \
64 struct unaligned_##ABBREV { \
65 TYPE x __attribute__((__packed__)); \
67 static inline struct unaligned_##ABBREV * \
68 unaligned_##ABBREV(const TYPE *p) \
70 return (struct unaligned_##ABBREV *) p; \
74 get_unaligned_##ABBREV(const TYPE *p) \
76 return unaligned_##ABBREV(p)->x; \
80 put_unaligned_##ABBREV(TYPE *p, TYPE x) \
82 unaligned_##ABBREV(p)->x = x; \
85 GCC_UNALIGNED_ACCESSORS(uint16_t, u16
);
86 GCC_UNALIGNED_ACCESSORS(uint32_t, u32
);
87 GCC_UNALIGNED_ACCESSORS(uint64_t, u64__
); /* Special case: see below. */
89 GCC_UNALIGNED_ACCESSORS(ovs_be16
, be16
);
90 GCC_UNALIGNED_ACCESSORS(ovs_be32
, be32
);
91 GCC_UNALIGNED_ACCESSORS(ovs_be64
, be64
);
93 /* Generic implementations. */
95 static inline uint16_t get_unaligned_u16(const uint16_t *p_
)
97 const uint8_t *p
= (const uint8_t *) p_
;
98 return ntohs((p
[0] << 8) | p
[1]);
101 static inline void put_unaligned_u16(uint16_t *p_
, uint16_t x_
)
103 uint8_t *p
= (uint8_t *) p_
;
104 uint16_t x
= ntohs(x_
);
110 static inline uint32_t get_unaligned_u32(const uint32_t *p_
)
112 const uint8_t *p
= (const uint8_t *) p_
;
113 return ntohl((p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3]);
116 static inline void put_unaligned_u32(uint32_t *p_
, uint32_t x_
)
118 uint8_t *p
= (uint8_t *) p_
;
119 uint32_t x
= ntohl(x_
);
127 static inline uint64_t get_unaligned_u64__(const uint64_t *p_
)
129 const uint8_t *p
= (const uint8_t *) p_
;
130 return ntohll(((uint64_t) p
[0] << 56)
131 | ((uint64_t) p
[1] << 48)
132 | ((uint64_t) p
[2] << 40)
133 | ((uint64_t) p
[3] << 32)
140 static inline void put_unaligned_u64__(uint64_t *p_
, uint64_t x_
)
142 uint8_t *p
= (uint8_t *) p_
;
143 uint64_t x
= ntohll(x_
);
155 /* Only sparse cares about the difference between uint<N>_t and ovs_be<N>, and
156 * that takes the GCC branch, so there's no point in working too hard on these
158 #define get_unaligned_be16 get_unaligned_u16
159 #define get_unaligned_be32 get_unaligned_u32
160 #define put_unaligned_be16 put_unaligned_u16
161 #define put_unaligned_be32 put_unaligned_u32
162 #define put_unaligned_be64 put_unaligned_u64
164 /* We do not #define get_unaligned_be64 as for the other be<N> functions above,
165 * because such a definition would mean that get_unaligned_be64() would have a
166 * different interface in each branch of the #if: with GCC it would take a
167 * "ovs_be64 *", with other compilers any pointer-to-64-bit-type (but not void
168 * *). The latter means code like "get_unaligned_be64(ofpbuf_data(b))" would
169 * work with GCC but not with other compilers, which is surprising and
170 * undesirable. Hence this wrapper function. */
171 static inline ovs_be64
172 get_unaligned_be64(const ovs_be64
*p
)
174 return get_unaligned_u64(p
);
178 /* Stores 'x' at possibly misaligned address 'p'.
180 * put_unaligned_u64() could be overloaded in the same way as
181 * get_unaligned_u64(), but so far it has not proven necessary.
184 put_unaligned_u64(uint64_t *p
, uint64_t x
)
186 put_unaligned_u64__(p
, x
);
189 /* Returns the value in 'x'. */
190 static inline uint32_t
191 get_16aligned_u32(const ovs_16aligned_u32
*x
)
193 return ((uint32_t) x
->hi
<< 16) | x
->lo
;
196 /* Stores 'value' in 'x'. */
198 put_16aligned_u32(ovs_16aligned_u32
*x
, uint32_t value
)
204 /* Returns the value in 'x'. */
205 static inline uint64_t
206 get_32aligned_u64(const ovs_32aligned_u64
*x
)
208 return ((uint64_t) x
->hi
<< 32) | x
->lo
;
211 /* Stores 'value' in 'x'. */
213 put_32aligned_u64(ovs_32aligned_u64
*x
, uint64_t value
)
220 /* Returns the value of 'x'. */
221 static inline ovs_be32
222 get_16aligned_be32(const ovs_16aligned_be32
*x
)
224 #ifdef WORDS_BIGENDIAN
225 return ((ovs_be32
) x
->hi
<< 16) | x
->lo
;
227 return ((ovs_be32
) x
->lo
<< 16) | x
->hi
;
231 /* Stores network byte order 'value' into 'x'. */
233 put_16aligned_be32(ovs_16aligned_be32
*x
, ovs_be32 value
)
244 /* Returns the value of 'x'. */
245 static inline ovs_be64
246 get_32aligned_be64(const ovs_32aligned_be64
*x
)
248 #ifdef WORDS_BIGENDIAN
249 return ((ovs_be64
) x
->hi
<< 32) | x
->lo
;
251 return ((ovs_be64
) x
->lo
<< 32) | x
->hi
;
255 /* Stores network byte order 'value' into 'x'. */
257 put_32aligned_be64(ovs_32aligned_be64
*x
, ovs_be64 value
)
267 #else /* __CHECKER__ */
268 /* Making sparse happy with these functions also makes them unreadable, so
269 * don't bother to show it their implementations. */
270 ovs_be32
get_16aligned_be32(const ovs_16aligned_be32
*);
271 void put_16aligned_be32(ovs_16aligned_be32
*, ovs_be32
);
272 ovs_be64
get_32aligned_be64(const ovs_32aligned_be64
*);
273 void put_32aligned_be64(ovs_32aligned_be64
*, ovs_be64
);
276 #endif /* unaligned.h */