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