Update required Autoconf version in INSTALL.
[sliver-openvswitch.git] / lib / 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 <stdarg.h>
38 #include <stdbool.h>
39 #include <stddef.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include "compiler.h"
44
45 #ifndef va_copy
46 #ifdef __va_copy
47 #define va_copy __va_copy
48 #else
49 #define va_copy(dst, src) ((dst) = (src))
50 #endif
51 #endif
52
53 #ifndef __cplusplus
54 /* Build-time assertion for use in a statement context. */
55 #define BUILD_ASSERT(EXPR) \
56         sizeof(struct { unsigned int build_assert_failed : (EXPR) ? 1 : -1; })
57
58 /* Build-time assertion for use in a declaration context. */
59 #define BUILD_ASSERT_DECL(EXPR) \
60         extern int (*build_assert(void))[BUILD_ASSERT(EXPR)]
61 #else /* __cplusplus */
62 #include <boost/static_assert.hpp>
63 #define BUILD_ASSERT BOOST_STATIC_ASSERT
64 #define BUILD_ASSERT_DECL BOOST_STATIC_ASSERT
65 #endif /* __cplusplus */
66
67 extern const char *program_name;
68
69 #define ARRAY_SIZE(ARRAY) (sizeof ARRAY / sizeof *ARRAY)
70 #define ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y) * (Y))
71 #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))
72
73 #ifndef MIN
74 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
75 #endif
76
77 #ifndef MAX
78 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
79 #endif
80
81 #define NOT_REACHED() abort()
82 #define NOT_IMPLEMENTED() abort()
83 #define NOT_TESTED() ((void) 0) /* XXX should print a message. */
84
85 /* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
86    the STRUCT object. */
87 #define CONTAINER_OF(POINTER, STRUCT, MEMBER)                           \
88         ((STRUCT *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))
89
90 #ifdef  __cplusplus
91 extern "C" {
92 #endif
93
94 void set_program_name(const char *);
95
96 void out_of_memory(void);
97 void *xmalloc(size_t);
98 void *xcalloc(size_t, size_t);
99 void *xrealloc(void *, size_t);
100 void *xmemdup(const void *, size_t);
101 char *xmemdup0(const char *, size_t);
102 char *xstrdup(const char *);
103 char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2);
104 char *xvasprintf(const char *format, va_list) PRINTF_FORMAT(1, 0);
105
106 #ifndef HAVE_STRLCPY
107 void strlcpy(char *dst, const char *src, size_t size);
108 #endif
109
110 void ofp_fatal(int err_no, const char *format, ...)
111     PRINTF_FORMAT(2, 3) NO_RETURN;
112 void ofp_error(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
113 void ofp_hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii);
114
115 #ifdef  __cplusplus
116 }
117 #endif
118
119 #endif /* util.h */