Add support for TAP virtual network devices in netdev.
[sliver-openvswitch.git] / include / util.h
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #ifndef UTIL_H
35 #define UTIL_H 1
36
37 #include <stdbool.h>
38 #include <stddef.h>
39 #include <stdint.h>
40 #include <stdio.h>
41 #include "compiler.h"
42
43 #ifndef __cplusplus
44 /* Build-time assertion for use in a statement context. */
45 #define BUILD_ASSERT(EXPR) \
46         sizeof(struct { unsigned int build_assert_failed : (EXPR) ? 1 : -1; })
47
48 /* Build-time assertion for use in a declaration context. */
49 #define BUILD_ASSERT_DECL(EXPR) \
50         extern int (*build_assert(void))[BUILD_ASSERT(EXPR)]
51 #else /* __cplusplus */
52 #include <boost/static_assert.hpp>
53 #define BUILD_ASSERT BOOST_STATIC_ASSERT
54 #define BUILD_ASSERT_DECL BOOST_STATIC_ASSERT
55 #endif /* __cplusplus */
56
57 extern const char *program_name;
58
59 #define ARRAY_SIZE(ARRAY) (sizeof ARRAY / sizeof *ARRAY)
60 #define ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y) * (Y))
61 #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))
62
63 #ifndef MIN
64 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
65 #endif
66
67 #ifndef MAX
68 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
69 #endif
70
71 #define NOT_REACHED() abort()
72 #define NOT_IMPLEMENTED() abort()
73 #define NOT_TESTED() ((void) 0) /* XXX should print a message. */
74
75 /* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
76    the STRUCT object. */
77 #define CONTAINER_OF(POINTER, STRUCT, MEMBER)                           \
78         ((STRUCT *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))
79
80 #ifdef  __cplusplus
81 extern "C" {
82 #endif
83
84 void set_program_name(const char *);
85
86 void out_of_memory(void);
87 void *xmalloc(size_t);
88 void *xcalloc(size_t, size_t);
89 void *xrealloc(void *, size_t);
90 void *xmemdup(const void *, size_t);
91 char *xmemdup0(const char *, size_t);
92 char *xstrdup(const char *);
93 char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2);
94
95 void ofp_fatal(int err_no, const char *format, ...)
96     PRINTF_FORMAT(2, 3) NO_RETURN;
97 void ofp_error(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
98 void ofp_hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii);
99
100 #ifdef  __cplusplus
101 }
102 #endif
103
104 #endif /* util.h */