]> git.proxmox.com Git - mirror_ovs.git/blob - lib/util.h
util: Move build assertion macros to compiler.h.
[mirror_ovs.git] / lib / util.h
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #ifndef UTIL_H
18 #define UTIL_H 1
19
20 #include <arpa/inet.h>
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <stdarg.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "compiler.h"
31 #include "openvswitch/types.h"
32 #include "openvswitch/util.h"
33
34 #ifndef va_copy
35 #ifdef __va_copy
36 #define va_copy __va_copy
37 #else
38 #define va_copy(dst, src) ((dst) = (src))
39 #endif
40 #endif
41
42 extern char *program_name;
43
44 #define __ARRAY_SIZE_NOCHECK(ARRAY) (sizeof(ARRAY) / sizeof((ARRAY)[0]))
45 #ifdef __GNUC__
46 /* return 0 for array types, 1 otherwise */
47 #define __ARRAY_CHECK(ARRAY) \
48 !__builtin_types_compatible_p(typeof(ARRAY), typeof(&ARRAY[0]))
49
50 /* compile-time fail if not array */
51 #define __ARRAY_FAIL(ARRAY) (sizeof(char[-2*!__ARRAY_CHECK(ARRAY)]))
52 #define __ARRAY_SIZE(ARRAY) \
53 __builtin_choose_expr(__ARRAY_CHECK(ARRAY), \
54 __ARRAY_SIZE_NOCHECK(ARRAY), __ARRAY_FAIL(ARRAY))
55 #else
56 #define __ARRAY_SIZE(ARRAY) __ARRAY_SIZE_NOCHECK(ARRAY)
57 #endif
58
59 /* Returns the number of elements in ARRAY. */
60 #define ARRAY_SIZE(ARRAY) __ARRAY_SIZE(ARRAY)
61
62 /* Returns X / Y, rounding up. X must be nonnegative to round correctly. */
63 #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
64
65 /* Returns X rounded up to the nearest multiple of Y. */
66 #define ROUND_UP(X, Y) (DIV_ROUND_UP(X, Y) * (Y))
67
68 /* Returns the least number that, when added to X, yields a multiple of Y. */
69 #define PAD_SIZE(X, Y) (ROUND_UP(X, Y) - (X))
70
71 /* Returns X rounded down to the nearest multiple of Y. */
72 #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))
73
74 /* Returns true if X is a power of 2, otherwise false. */
75 #define IS_POW2(X) ((X) && !((X) & ((X) - 1)))
76
77 static inline bool
78 is_pow2(uintmax_t x)
79 {
80 return IS_POW2(x);
81 }
82
83 /* Returns X rounded up to a power of 2. X must be a constant expression. */
84 #define ROUND_UP_POW2(X) RUP2__(X)
85 #define RUP2__(X) (RUP2_1(X) + 1)
86 #define RUP2_1(X) (RUP2_2(X) | (RUP2_2(X) >> 16))
87 #define RUP2_2(X) (RUP2_3(X) | (RUP2_3(X) >> 8))
88 #define RUP2_3(X) (RUP2_4(X) | (RUP2_4(X) >> 4))
89 #define RUP2_4(X) (RUP2_5(X) | (RUP2_5(X) >> 2))
90 #define RUP2_5(X) (RUP2_6(X) | (RUP2_6(X) >> 1))
91 #define RUP2_6(X) ((X) - 1)
92
93 /* Returns X rounded down to a power of 2. X must be a constant expression. */
94 #define ROUND_DOWN_POW2(X) RDP2__(X)
95 #define RDP2__(X) (RDP2_1(X) - (RDP2_1(X) >> 1))
96 #define RDP2_1(X) (RDP2_2(X) | (RDP2_2(X) >> 16))
97 #define RDP2_2(X) (RDP2_3(X) | (RDP2_3(X) >> 8))
98 #define RDP2_3(X) (RDP2_4(X) | (RDP2_4(X) >> 4))
99 #define RDP2_4(X) (RDP2_5(X) | (RDP2_5(X) >> 2))
100 #define RDP2_5(X) ( (X) | ( (X) >> 1))
101
102 /* This system's cache line size, in bytes.
103 * Being wrong hurts performance but not correctness. */
104 #define CACHE_LINE_SIZE 64
105 BUILD_ASSERT_DECL(IS_POW2(CACHE_LINE_SIZE));
106
107 static inline void
108 ovs_prefetch_range(const void *start, size_t size)
109 {
110 const char *addr = (const char *)start;
111 size_t ofs;
112
113 for (ofs = 0; ofs < size; ofs += CACHE_LINE_SIZE) {
114 OVS_PREFETCH(addr + ofs);
115 }
116 }
117
118 #ifndef MIN
119 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
120 #endif
121
122 #ifndef MAX
123 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
124 #endif
125
126 #define OVS_NOT_REACHED() abort()
127
128 /* Given ATTR, and TYPE, cast the ATTR to TYPE by first casting ATTR to
129 * (void *). This is to suppress the alignment warning issued by clang. */
130 #define ALIGNED_CAST(TYPE, ATTR) ((TYPE) (void *) (ATTR))
131
132 /* Use "%"PRIuSIZE to format size_t with printf(). */
133 #ifdef _WIN32
134 #define PRIdSIZE "Id"
135 #define PRIiSIZE "Ii"
136 #define PRIoSIZE "Io"
137 #define PRIuSIZE "Iu"
138 #define PRIxSIZE "Ix"
139 #define PRIXSIZE "IX"
140 #else
141 #define PRIdSIZE "zd"
142 #define PRIiSIZE "zi"
143 #define PRIoSIZE "zo"
144 #define PRIuSIZE "zu"
145 #define PRIxSIZE "zx"
146 #define PRIXSIZE "zX"
147 #endif
148
149 #ifndef _WIN32
150 typedef uint32_t HANDLE;
151 #endif
152
153 #ifdef __cplusplus
154 extern "C" {
155 #endif
156
157 #define set_program_name(name) \
158 ovs_set_program_name(name, OVS_PACKAGE_VERSION)
159
160 const char *get_subprogram_name(void);
161 void set_subprogram_name(const char *);
162
163 void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp);
164
165 OVS_NO_RETURN void out_of_memory(void);
166 void *xmalloc(size_t) MALLOC_LIKE;
167 void *xcalloc(size_t, size_t) MALLOC_LIKE;
168 void *xzalloc(size_t) MALLOC_LIKE;
169 void *xrealloc(void *, size_t);
170 void *xmemdup(const void *, size_t) MALLOC_LIKE;
171 char *xmemdup0(const char *, size_t) MALLOC_LIKE;
172 char *xstrdup(const char *) MALLOC_LIKE;
173 char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE;
174 char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE;
175 void *x2nrealloc(void *p, size_t *n, size_t s);
176
177 void *xmalloc_cacheline(size_t) MALLOC_LIKE;
178 void *xzalloc_cacheline(size_t) MALLOC_LIKE;
179 void free_cacheline(void *);
180
181 void ovs_strlcpy(char *dst, const char *src, size_t size);
182 void ovs_strzcpy(char *dst, const char *src, size_t size);
183
184 OVS_NO_RETURN void ovs_abort(int err_no, const char *format, ...)
185 OVS_PRINTF_FORMAT(2, 3);
186 OVS_NO_RETURN void ovs_abort_valist(int err_no, const char *format, va_list)
187 OVS_PRINTF_FORMAT(2, 0);
188 OVS_NO_RETURN void ovs_fatal(int err_no, const char *format, ...)
189 OVS_PRINTF_FORMAT(2, 3);
190 OVS_NO_RETURN void ovs_fatal_valist(int err_no, const char *format, va_list)
191 OVS_PRINTF_FORMAT(2, 0);
192 void ovs_error(int err_no, const char *format, ...) OVS_PRINTF_FORMAT(2, 3);
193 void ovs_error_valist(int err_no, const char *format, va_list)
194 OVS_PRINTF_FORMAT(2, 0);
195 const char *ovs_retval_to_string(int);
196 const char *ovs_strerror(int);
197 void ovs_hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii);
198
199 bool str_to_int(const char *, int base, int *);
200 bool str_to_long(const char *, int base, long *);
201 bool str_to_llong(const char *, int base, long long *);
202 bool str_to_uint(const char *, int base, unsigned int *);
203
204 bool ovs_scan(const char *s, const char *format, ...) OVS_SCANF_FORMAT(2, 3);
205 bool ovs_scan_len(const char *s, int *n, const char *format, ...);
206
207 bool str_to_double(const char *, double *);
208
209 int hexit_value(int c);
210 uintmax_t hexits_value(const char *s, size_t n, bool *ok);
211
212 int parse_int_string(const char *s, uint8_t *valuep, int field_width,
213 char **tail);
214
215 const char *english_list_delimiter(size_t index, size_t total);
216
217 char *get_cwd(void);
218 #ifndef _WIN32
219 char *dir_name(const char *file_name);
220 char *base_name(const char *file_name);
221 #endif
222 char *abs_file_name(const char *dir, const char *file_name);
223
224 char *follow_symlinks(const char *filename);
225
226 void ignore(bool x OVS_UNUSED);
227 \f
228 /* Bitwise tests. */
229
230 /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */
231 #if __GNUC__ >= 4
232 static inline int
233 raw_ctz(uint64_t n)
234 {
235 /* With GCC 4.7 on 32-bit x86, if a 32-bit integer is passed as 'n', using
236 * a plain __builtin_ctzll() here always generates an out-of-line function
237 * call. The test below helps it to emit a single 'bsf' instruction. */
238 return (__builtin_constant_p(n <= UINT32_MAX) && n <= UINT32_MAX
239 ? __builtin_ctz(n)
240 : __builtin_ctzll(n));
241 }
242
243 static inline int
244 raw_clz64(uint64_t n)
245 {
246 return __builtin_clzll(n);
247 }
248 #elif _MSC_VER
249 static inline int
250 raw_ctz(uint64_t n)
251 {
252 #ifdef _WIN64
253 unsigned long r = 0;
254 _BitScanForward64(&r, n);
255 return r;
256 #else
257 unsigned long low = n, high, r = 0;
258 if (_BitScanForward(&r, low)) {
259 return r;
260 }
261 high = n >> 32;
262 _BitScanForward(&r, high);
263 return r + 32;
264 #endif
265 }
266
267 static inline int
268 raw_clz64(uint64_t n)
269 {
270 #ifdef _WIN64
271 unsigned long r = 0;
272 _BitScanReverse64(&r, n);
273 return 63 - r;
274 #else
275 unsigned long low, high = n >> 32, r = 0;
276 if (_BitScanReverse(&r, high)) {
277 return 31 - r;
278 }
279 low = n;
280 _BitScanReverse(&r, low);
281 return 63 - r;
282 #endif
283 }
284 #else
285 /* Defined in util.c. */
286 int raw_ctz(uint64_t n);
287 int raw_clz64(uint64_t n);
288 #endif
289
290 /* Returns the number of trailing 0-bits in 'n', or 32 if 'n' is 0. */
291 static inline int
292 ctz32(uint32_t n)
293 {
294 return n ? raw_ctz(n) : 32;
295 }
296
297 /* Returns the number of trailing 0-bits in 'n', or 64 if 'n' is 0. */
298 static inline int
299 ctz64(uint64_t n)
300 {
301 return n ? raw_ctz(n) : 64;
302 }
303
304 /* Returns the number of leading 0-bits in 'n', or 32 if 'n' is 0. */
305 static inline int
306 clz32(uint32_t n)
307 {
308 return n ? raw_clz64(n) - 32 : 32;
309 }
310
311 /* Returns the number of leading 0-bits in 'n', or 64 if 'n' is 0. */
312 static inline int
313 clz64(uint64_t n)
314 {
315 return n ? raw_clz64(n) : 64;
316 }
317
318 /* Given a word 'n', calculates floor(log_2('n')). This is equivalent
319 * to finding the bit position of the most significant one bit in 'n'. It is
320 * an error to call this function with 'n' == 0. */
321 static inline int
322 log_2_floor(uint64_t n)
323 {
324 return 63 - raw_clz64(n);
325 }
326
327 /* Given a word 'n', calculates ceil(log_2('n')). It is an error to
328 * call this function with 'n' == 0. */
329 static inline int
330 log_2_ceil(uint64_t n)
331 {
332 return log_2_floor(n) + !is_pow2(n);
333 }
334
335 /* unsigned int count_1bits(uint64_t x):
336 *
337 * Returns the number of 1-bits in 'x', between 0 and 64 inclusive. */
338 #if UINTPTR_MAX == UINT64_MAX
339 static inline unsigned int
340 count_1bits(uint64_t x)
341 {
342 #if __GNUC__ >= 4 && __POPCNT__
343 return __builtin_popcountll(x);
344 #else
345 /* This portable implementation is the fastest one we know of for 64
346 * bits, and about 3x faster than GCC 4.7 __builtin_popcountll(). */
347 const uint64_t h55 = UINT64_C(0x5555555555555555);
348 const uint64_t h33 = UINT64_C(0x3333333333333333);
349 const uint64_t h0F = UINT64_C(0x0F0F0F0F0F0F0F0F);
350 const uint64_t h01 = UINT64_C(0x0101010101010101);
351 x -= (x >> 1) & h55; /* Count of each 2 bits in-place. */
352 x = (x & h33) + ((x >> 2) & h33); /* Count of each 4 bits in-place. */
353 x = (x + (x >> 4)) & h0F; /* Count of each 8 bits in-place. */
354 return (x * h01) >> 56; /* Sum of all bytes. */
355 #endif
356 }
357 #else /* Not 64-bit. */
358 #if __GNUC__ >= 4 && __POPCNT__
359 static inline unsigned int
360 count_1bits_32__(uint32_t x)
361 {
362 return __builtin_popcount(x);
363 }
364 #else
365 #define NEED_COUNT_1BITS_8 1
366 extern const uint8_t count_1bits_8[256];
367 static inline unsigned int
368 count_1bits_32__(uint32_t x)
369 {
370 /* This portable implementation is the fastest one we know of for 32 bits,
371 * and faster than GCC __builtin_popcount(). */
372 return (count_1bits_8[x & 0xff] +
373 count_1bits_8[(x >> 8) & 0xff] +
374 count_1bits_8[(x >> 16) & 0xff] +
375 count_1bits_8[x >> 24]);
376 }
377 #endif
378 static inline unsigned int
379 count_1bits(uint64_t x)
380 {
381 return count_1bits_32__(x) + count_1bits_32__(x >> 32);
382 }
383 #endif
384
385 /* Returns the rightmost 1-bit in 'x' (e.g. 01011000 => 00001000), or 0 if 'x'
386 * is 0. */
387 static inline uintmax_t
388 rightmost_1bit(uintmax_t x)
389 {
390 return x & -x;
391 }
392
393 /* Returns 'x' with its rightmost 1-bit changed to a zero (e.g. 01011000 =>
394 * 01010000), or 0 if 'x' is 0. */
395 static inline uintmax_t
396 zero_rightmost_1bit(uintmax_t x)
397 {
398 return x & (x - 1);
399 }
400
401 /* Returns the index of the rightmost 1-bit in 'x' (e.g. 01011000 => 3), or an
402 * undefined value if 'x' is 0. */
403 static inline int
404 rightmost_1bit_idx(uint64_t x)
405 {
406 return ctz64(x);
407 }
408
409 /* Returns the index of the leftmost 1-bit in 'x' (e.g. 01011000 => 6), or an
410 * undefined value if 'x' is 0. */
411 static inline uint32_t
412 leftmost_1bit_idx(uint64_t x)
413 {
414 return log_2_floor(x);
415 }
416
417 /* Return a ovs_be32 prefix in network byte order with 'plen' highest bits set.
418 * Shift with 32 is undefined behavior, but we rather use 64-bit shift than
419 * compare. */
420 static inline ovs_be32 be32_prefix_mask(int plen)
421 {
422 return htonl((uint64_t)UINT32_MAX << (32 - plen));
423 }
424 \f
425 bool is_all_zeros(const void *, size_t);
426 bool is_all_ones(const void *, size_t);
427 void bitwise_copy(const void *src, unsigned int src_len, unsigned int src_ofs,
428 void *dst, unsigned int dst_len, unsigned int dst_ofs,
429 unsigned int n_bits);
430 void bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs,
431 unsigned int n_bits);
432 void bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs,
433 unsigned int n_bits);
434 bool bitwise_is_all_zeros(const void *, unsigned int len, unsigned int ofs,
435 unsigned int n_bits);
436 unsigned int bitwise_scan(const void *, unsigned int len,
437 bool target, unsigned int start, unsigned int end);
438 int bitwise_rscan(const void *, unsigned int len, bool target,
439 int start, int end);
440 void bitwise_put(uint64_t value,
441 void *dst, unsigned int dst_len, unsigned int dst_ofs,
442 unsigned int n_bits);
443 uint64_t bitwise_get(const void *src, unsigned int src_len,
444 unsigned int src_ofs, unsigned int n_bits);
445 bool bitwise_get_bit(const void *src, unsigned int len, unsigned int ofs);
446 void bitwise_put0(void *dst, unsigned int len, unsigned int ofs);
447 void bitwise_put1(void *dst, unsigned int len, unsigned int ofs);
448 void bitwise_put_bit(void *dst, unsigned int len, unsigned int ofs, bool);
449 void bitwise_toggle_bit(void *dst, unsigned int len, unsigned int ofs);
450
451 /* Returns non-zero if the parameters have equal value. */
452 static inline int
453 ovs_u128_equals(const ovs_u128 *a, const ovs_u128 *b)
454 {
455 return (a->u64.hi == b->u64.hi) && (a->u64.lo == b->u64.lo);
456 }
457
458 /* Returns true if 'val' is 0. */
459 static inline bool
460 ovs_u128_is_zero(const ovs_u128 *val)
461 {
462 return !(val->u64.hi || val->u64.lo);
463 }
464
465 /* Returns true if 'val' is all ones. */
466 static inline bool
467 ovs_u128_is_ones(const ovs_u128 *val)
468 {
469 return ovs_u128_equals(val, &OVS_U128_MAX);
470 }
471
472 /* Returns non-zero if the parameters have equal value. */
473 static inline int
474 ovs_be128_equals(const ovs_be128 *a, const ovs_be128 *b)
475 {
476 return (a->be64.hi == b->be64.hi) && (a->be64.lo == b->be64.lo);
477 }
478
479 /* Returns true if 'val' is 0. */
480 static inline bool
481 ovs_be128_is_zero(const ovs_be128 *val)
482 {
483 return !(val->be64.hi || val->be64.lo);
484 }
485
486 void xsleep(unsigned int seconds);
487
488 bool is_stdout_a_tty(void);
489
490 #ifdef _WIN32
491 \f
492 char *ovs_format_message(int error);
493 char *ovs_lasterror_to_string(void);
494 int ftruncate(int fd, off_t length);
495 #endif
496
497 #ifdef __cplusplus
498 }
499 #endif
500
501 #endif /* util.h */