2
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
2
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
66
67
#define BUILD_ASSERT_DECL_GCCONLY(EXPR) ((void) 0)
69
/* Like the standard assert macro, except:
71
* - Writes the failure message to the log.
73
* - Not affected by NDEBUG. */
70
/* Like the standard assert macro, except writes the failure message to the
74
73
#define ovs_assert(CONDITION) \
75
74
if (!OVS_LIKELY(CONDITION)) { \
76
ovs_assert_failure(SOURCE_LOCATOR, __func__, #CONDITION); \
75
ovs_assert_failure(OVS_SOURCE_LOCATOR, __func__, #CONDITION); \
78
void ovs_assert_failure(const char *, const char *, const char *) NO_RETURN;
78
#define ovs_assert(CONDITION) ((void) (CONDITION))
80
OVS_NO_RETURN void ovs_assert_failure(const char *, const char *, const char *);
80
82
/* Casts 'pointer' to 'type' and issues a compiler warning if the cast changes
81
83
* anything other than an outermost "const" or "volatile" qualifier.
174
176
#define OVS_NOT_REACHED() abort()
176
/* Expands to a string that looks like "<file>:<line>", e.g. "tmp.c:10".
178
* See http://c-faq.com/ansi/stringize.html for an explanation of STRINGIZE and
180
#define SOURCE_LOCATOR __FILE__ ":" STRINGIZE(__LINE__)
181
#define STRINGIZE(ARG) STRINGIZE2(ARG)
182
#define STRINGIZE2(ARG) #ARG
184
178
/* Given a pointer-typed lvalue OBJECT, expands to a pointer type that may be
185
179
* assigned to OBJECT. */
205
199
((char *) &(OBJECT)->MEMBER - (char *) (OBJECT))
202
/* Yields the size of MEMBER within STRUCT. */
203
#define MEMBER_SIZEOF(STRUCT, MEMBER) (sizeof(((STRUCT *) NULL)->MEMBER))
208
205
/* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
209
206
the STRUCT object. */
210
207
#define CONTAINER_OF(POINTER, STRUCT, MEMBER) \
229
226
#define ASSIGN_CONTAINER(OBJECT, POINTER, MEMBER) \
230
227
((OBJECT) = OBJECT_CONTAINING(POINTER, OBJECT, MEMBER), (void) 0)
229
/* As explained in the comment above OBJECT_OFFSETOF(), non-GNUC compilers
230
* like MSVC will complain about un-initialized variables if OBJECT
231
* hasn't already been initialized. To prevent such warnings, INIT_CONTAINER()
232
* can be used as a wrapper around ASSIGN_CONTAINER. */
233
#define INIT_CONTAINER(OBJECT, POINTER, MEMBER) \
234
((OBJECT) = NULL, ASSIGN_CONTAINER(OBJECT, POINTER, MEMBER))
232
236
/* Given ATTR, and TYPE, cast the ATTR to TYPE by first casting ATTR to
233
237
* (void *). This is to suppress the alignment warning issued by clang. */
234
238
#define ALIGNED_CAST(TYPE, ATTR) ((TYPE) (void *) (ATTR))
250
254
#define PRIXSIZE "zX"
258
typedef uint32_t HANDLE;
253
261
#ifdef __cplusplus
257
void set_program_name__(const char *name, const char *version,
258
const char *date, const char *time);
259
265
#define set_program_name(name) \
260
set_program_name__(name, VERSION, __DATE__, __TIME__)
266
ovs_set_program_name(name, OVS_PACKAGE_VERSION)
262
268
const char *get_subprogram_name(void);
263
void set_subprogram_name(const char *format, ...) PRINTF_FORMAT(1, 2);
269
void set_subprogram_name(const char *);
265
const char *get_program_version(void);
266
271
void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp);
268
void out_of_memory(void) NO_RETURN;
273
OVS_NO_RETURN void out_of_memory(void);
269
274
void *xmalloc(size_t) MALLOC_LIKE;
270
275
void *xcalloc(size_t, size_t) MALLOC_LIKE;
271
276
void *xzalloc(size_t) MALLOC_LIKE;
273
278
void *xmemdup(const void *, size_t) MALLOC_LIKE;
274
279
char *xmemdup0(const char *, size_t) MALLOC_LIKE;
275
280
char *xstrdup(const char *) MALLOC_LIKE;
276
char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2) MALLOC_LIKE;
277
char *xvasprintf(const char *format, va_list) PRINTF_FORMAT(1, 0) MALLOC_LIKE;
281
char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE;
282
char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE;
278
283
void *x2nrealloc(void *p, size_t *n, size_t s);
280
285
void *xmalloc_cacheline(size_t) MALLOC_LIKE;
284
289
void ovs_strlcpy(char *dst, const char *src, size_t size);
285
290
void ovs_strzcpy(char *dst, const char *src, size_t size);
287
void ovs_abort(int err_no, const char *format, ...)
288
PRINTF_FORMAT(2, 3) NO_RETURN;
289
void ovs_abort_valist(int err_no, const char *format, va_list)
290
PRINTF_FORMAT(2, 0) NO_RETURN;
291
void ovs_fatal(int err_no, const char *format, ...)
292
PRINTF_FORMAT(2, 3) NO_RETURN;
293
void ovs_fatal_valist(int err_no, const char *format, va_list)
294
PRINTF_FORMAT(2, 0) NO_RETURN;
295
void ovs_error(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
292
OVS_NO_RETURN void ovs_abort(int err_no, const char *format, ...)
293
OVS_PRINTF_FORMAT(2, 3);
294
OVS_NO_RETURN void ovs_abort_valist(int err_no, const char *format, va_list)
295
OVS_PRINTF_FORMAT(2, 0);
296
OVS_NO_RETURN void ovs_fatal(int err_no, const char *format, ...)
297
OVS_PRINTF_FORMAT(2, 3);
298
OVS_NO_RETURN void ovs_fatal_valist(int err_no, const char *format, va_list)
299
OVS_PRINTF_FORMAT(2, 0);
300
void ovs_error(int err_no, const char *format, ...) OVS_PRINTF_FORMAT(2, 3);
296
301
void ovs_error_valist(int err_no, const char *format, va_list)
302
OVS_PRINTF_FORMAT(2, 0);
298
303
const char *ovs_retval_to_string(int);
299
304
const char *ovs_strerror(int);
300
305
void ovs_hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii);
304
309
bool str_to_llong(const char *, int base, long long *);
305
310
bool str_to_uint(const char *, int base, unsigned int *);
307
bool ovs_scan(const char *s, const char *format, ...) SCANF_FORMAT(2, 3);
312
bool ovs_scan(const char *s, const char *format, ...) OVS_SCANF_FORMAT(2, 3);
313
bool ovs_scan_len(const char *s, int *n, const char *format, ...);
309
315
bool str_to_double(const char *, double *);
311
317
int hexit_value(int c);
312
unsigned int hexits_value(const char *s, size_t n, bool *ok);
318
uintmax_t hexits_value(const char *s, size_t n, bool *ok);
320
int parse_int_string(const char *s, uint8_t *valuep, int field_width,
314
323
const char *english_list_delimiter(size_t index, size_t total);
316
325
char *get_cwd(void);
317
327
char *dir_name(const char *file_name);
318
328
char *base_name(const char *file_name);
319
330
char *abs_file_name(const char *dir, const char *file_name);
321
332
char *follow_symlinks(const char *filename);
343
354
return __builtin_clzll(n);
362
_BitScanForward64(&r, n);
365
unsigned long low = n, high, r = 0;
366
if (_BitScanForward(&r, low)) {
370
_BitScanForward(&r, high);
376
raw_clz64(uint64_t n)
380
_BitScanReverse64(&r, n);
383
unsigned long low, high = n >> 32, r = 0;
384
if (_BitScanReverse(&r, high)) {
388
_BitScanReverse(&r, low);
346
393
/* Defined in util.c. */
347
394
int raw_ctz(uint64_t n);
488
535
return htonl((uint64_t)UINT32_MAX << (32 - plen));
491
bool is_all_zeros(const uint8_t *, size_t);
492
bool is_all_ones(const uint8_t *, size_t);
538
bool is_all_zeros(const void *, size_t);
539
bool is_all_ones(const void *, size_t);
493
540
void bitwise_copy(const void *src, unsigned int src_len, unsigned int src_ofs,
494
541
void *dst, unsigned int dst_len, unsigned int dst_ofs,
495
542
unsigned int n_bits);
499
546
unsigned int n_bits);
500
547
bool bitwise_is_all_zeros(const void *, unsigned int len, unsigned int ofs,
501
548
unsigned int n_bits);
549
unsigned int bitwise_scan(const void *, unsigned int len,
550
bool target, unsigned int start, unsigned int end);
502
551
void bitwise_put(uint64_t value,
503
552
void *dst, unsigned int dst_len, unsigned int dst_ofs,
504
553
unsigned int n_bits);
505
554
uint64_t bitwise_get(const void *src, unsigned int src_len,
506
555
unsigned int src_ofs, unsigned int n_bits);
557
/* Returns non-zero if the parameters have equal value. */
559
ovs_u128_equals(const ovs_u128 *a, const ovs_u128 *b)
561
return (a->u64.hi == b->u64.hi) && (a->u64.lo == b->u64.lo);
508
564
void xsleep(unsigned int seconds);