~ubuntu-branches/ubuntu/wily/openvswitch/wily

« back to all changes in this revision

Viewing changes to lib/util.h

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-08-10 11:35:15 UTC
  • mfrom: (1.1.30)
  • Revision ID: package-import@ubuntu.com-20150810113515-575vj06oq29emxsn
Tags: 2.4.0~git20150810.97bab95-0ubuntu1
* New upstream snapshot from 2.4 branch:
  - d/*: Align any relevant packaging changes with upstream.
* d/*: wrap-and-sort.
* d/openvswitch-{common,vswitch}.install: Correct install location for
  bash completion files.
* d/tests/openflow.py: Explicitly use ovs-testcontroller as provided
  by 2.4.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
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.
3
3
 *
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.
29
29
#include <string.h>
30
30
#include "compiler.h"
31
31
#include "openvswitch/types.h"
 
32
#include "openvswitch/util.h"
32
33
 
33
34
#ifndef va_copy
34
35
#ifdef __va_copy
66
67
#define BUILD_ASSERT_DECL_GCCONLY(EXPR) ((void) 0)
67
68
#endif
68
69
 
69
 
/* Like the standard assert macro, except:
70
 
 *
71
 
 *   - Writes the failure message to the log.
72
 
 *
73
 
 *   - Not affected by NDEBUG. */
 
70
/* Like the standard assert macro, except writes the failure message to the
 
71
 * log. */
 
72
#ifndef NDEBUG
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);       \
77
76
    }
78
 
void ovs_assert_failure(const char *, const char *, const char *) NO_RETURN;
 
77
#else
 
78
#define ovs_assert(CONDITION) ((void) (CONDITION))
 
79
#endif
 
80
OVS_NO_RETURN void ovs_assert_failure(const char *, const char *, const char *);
79
81
 
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.
173
175
 
174
176
#define OVS_NOT_REACHED() abort()
175
177
 
176
 
/* Expands to a string that looks like "<file>:<line>", e.g. "tmp.c:10".
177
 
 *
178
 
 * See http://c-faq.com/ansi/stringize.html for an explanation of STRINGIZE and
179
 
 * STRINGIZE2. */
180
 
#define SOURCE_LOCATOR __FILE__ ":" STRINGIZE(__LINE__)
181
 
#define STRINGIZE(ARG) STRINGIZE2(ARG)
182
 
#define STRINGIZE2(ARG) #ARG
183
 
 
184
178
/* Given a pointer-typed lvalue OBJECT, expands to a pointer type that may be
185
179
 * assigned to OBJECT. */
186
180
#ifdef __GNUC__
205
199
    ((char *) &(OBJECT)->MEMBER - (char *) (OBJECT))
206
200
#endif
207
201
 
 
202
/* Yields the size of MEMBER within STRUCT. */
 
203
#define MEMBER_SIZEOF(STRUCT, MEMBER) (sizeof(((STRUCT *) NULL)->MEMBER))
 
204
 
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)
231
228
 
 
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))
 
235
 
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"
251
255
#endif
252
256
 
 
257
#ifndef _WIN32
 
258
typedef uint32_t HANDLE;
 
259
#endif
 
260
 
253
261
#ifdef  __cplusplus
254
262
extern "C" {
255
263
#endif
256
264
 
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)
261
267
 
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 *);
264
270
 
265
 
const char *get_program_version(void);
266
271
void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp);
267
272
 
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);
279
284
 
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);
286
291
 
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)
297
 
    PRINTF_FORMAT(2, 0);
 
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 *);
306
311
 
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, ...);
308
314
 
309
315
bool str_to_double(const char *, double *);
310
316
 
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);
 
319
 
 
320
int parse_int_string(const char *s, uint8_t *valuep, int field_width,
 
321
                     char **tail);
313
322
 
314
323
const char *english_list_delimiter(size_t index, size_t total);
315
324
 
316
325
char *get_cwd(void);
 
326
#ifndef _WIN32
317
327
char *dir_name(const char *file_name);
318
328
char *base_name(const char *file_name);
 
329
#endif
319
330
char *abs_file_name(const char *dir, const char *file_name);
320
331
 
321
332
char *follow_symlinks(const char *filename);
342
353
{
343
354
    return __builtin_clzll(n);
344
355
}
 
356
#elif _MSC_VER
 
357
static inline int
 
358
raw_ctz(uint64_t n)
 
359
{
 
360
#ifdef _WIN64
 
361
    unsigned long r = 0;
 
362
    _BitScanForward64(&r, n);
 
363
    return r;
 
364
#else
 
365
    unsigned long low = n, high, r = 0;
 
366
    if (_BitScanForward(&r, low)) {
 
367
        return r;
 
368
    }
 
369
    high = n >> 32;
 
370
    _BitScanForward(&r, high);
 
371
    return r + 32;
 
372
#endif
 
373
}
 
374
 
 
375
static inline int
 
376
raw_clz64(uint64_t n)
 
377
{
 
378
#ifdef _WIN64
 
379
    unsigned long r = 0;
 
380
    _BitScanReverse64(&r, n);
 
381
    return 63 - r;
 
382
#else
 
383
    unsigned long low, high = n >> 32, r = 0;
 
384
    if (_BitScanReverse(&r, high)) {
 
385
        return 31 - r;
 
386
    }
 
387
    low = n;
 
388
    _BitScanReverse(&r, low);
 
389
    return 63 - r;
 
390
#endif
 
391
}
345
392
#else
346
393
/* Defined in util.c. */
347
394
int raw_ctz(uint64_t n);
464
511
 *
465
512
 * Unlike the other functions for rightmost 1-bits, this function only works
466
513
 * with 32-bit integers. */
467
 
static inline uint32_t
 
514
static inline int
468
515
rightmost_1bit_idx(uint32_t x)
469
516
{
470
517
    return ctz32(x);
488
535
    return htonl((uint64_t)UINT32_MAX << (32 - plen));
489
536
}
490
537
 
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);
507
556
 
 
557
/* Returns non-zero if the parameters have equal value. */
 
558
static inline int
 
559
ovs_u128_equals(const ovs_u128 *a, const ovs_u128 *b)
 
560
{
 
561
    return (a->u64.hi == b->u64.hi) && (a->u64.lo == b->u64.lo);
 
562
}
 
563
 
508
564
void xsleep(unsigned int seconds);
509
565
 
510
566
#ifdef _WIN32