Make "struct vconn" opaque.
[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 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
63 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
64
65 #define NOT_REACHED() abort()
66 #define NOT_IMPLEMENTED() abort()
67 #define NOT_TESTED() ((void) 0) /* XXX should print a message. */
68
69 /* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
70    the STRUCT object. */
71 #define CONTAINER_OF(POINTER, STRUCT, MEMBER)                           \
72         ((STRUCT *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))
73
74 #ifdef  __cplusplus
75 extern "C" {
76 #endif
77
78 void set_program_name(const char *);
79
80 void *xmalloc(size_t);
81 void *xcalloc(size_t, size_t);
82 void *xrealloc(void *, size_t);
83 void *xmemdup(const void *, size_t);
84 char *xmemdup0(const char *, size_t);
85 char *xstrdup(const char *);
86 char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2);
87
88 void fatal(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3) NO_RETURN;
89 void error(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
90 void debug(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
91 void debug_msg(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
92 void hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii);
93
94 #ifdef  __cplusplus
95 }
96 #endif
97
98 #endif /* util.h */