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