2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 #define va_copy __va_copy
32 #define va_copy(dst, src) ((dst) = (src))
37 /* Build-time assertion building block. */
38 #define BUILD_ASSERT__(EXPR) \
39 sizeof(struct { unsigned int build_assert_failed : (EXPR) ? 1 : -1; })
41 /* Build-time assertion for use in a statement context. */
42 #define BUILD_ASSERT(EXPR) (void) BUILD_ASSERT__(EXPR)
44 /* Build-time assertion for use in a declaration context. */
45 #define BUILD_ASSERT_DECL(EXPR) \
46 extern int (*build_assert(void))[BUILD_ASSERT__(EXPR)]
47 #else /* __cplusplus */
48 #include <boost/static_assert.hpp>
49 #define BUILD_ASSERT BOOST_STATIC_ASSERT
50 #define BUILD_ASSERT_DECL BOOST_STATIC_ASSERT
51 #endif /* __cplusplus */
53 extern const char *program_name;
55 #define ARRAY_SIZE(ARRAY) (sizeof ARRAY / sizeof *ARRAY)
56 #define ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y) * (Y))
57 #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))
58 #define IS_POW2(X) ((X) && !((X) & ((X) - 1)))
61 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
65 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
68 #define NOT_REACHED() abort()
69 #define NOT_IMPLEMENTED() abort()
70 #define NOT_TESTED() ((void) 0) /* XXX should print a message. */
72 /* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
74 #define CONTAINER_OF(POINTER, STRUCT, MEMBER) \
75 ((STRUCT *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))
81 void set_program_name(const char *);
83 void ovs_print_version(char *date, char *time,
84 uint8_t min_ofp, uint8_t max_ofp);
85 #define OVS_PRINT_VERSION(min_ofp, max_ofp) \
86 ovs_print_version(__DATE__, __TIME__, (min_ofp), (max_ofp))
88 void out_of_memory(void) NO_RETURN;
89 void *xmalloc(size_t) MALLOC_LIKE;
90 void *xcalloc(size_t, size_t) MALLOC_LIKE;
91 void *xrealloc(void *, size_t);
92 void *xmemdup(const void *, size_t) MALLOC_LIKE;
93 char *xmemdup0(const char *, size_t) MALLOC_LIKE;
94 char *xstrdup(const char *) MALLOC_LIKE;
95 char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2) MALLOC_LIKE;
96 char *xvasprintf(const char *format, va_list) PRINTF_FORMAT(1, 0) MALLOC_LIKE;
97 void *x2nrealloc(void *p, size_t *n, size_t s);
99 void ovs_strlcpy(char *dst, const char *src, size_t size);
101 void ovs_fatal(int err_no, const char *format, ...)
102 PRINTF_FORMAT(2, 3) NO_RETURN;
103 void ovs_error(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
104 void ovs_hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii);
106 bool str_to_int(const char *, int base, int *);
107 bool str_to_long(const char *, int base, long *);
108 bool str_to_llong(const char *, int base, long long *);
109 bool str_to_uint(const char *, int base, unsigned int *);
110 bool str_to_ulong(const char *, int base, unsigned long *);
111 bool str_to_ullong(const char *, int base, unsigned long long *);