Initial import
[sliver-openvswitch.git] / include / util.h
1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #ifndef UTIL_H
23 #define UTIL_H 1
24
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include "compiler.h"
30
31 #ifndef __cplusplus
32 /* Build-time assertion for use in a statement context. */
33 #define BUILD_ASSERT(EXPR) \
34         sizeof(struct { unsigned int build_assert_failed : (EXPR) ? 1 : -1; })
35
36 /* Build-time assertion for use in a declaration context. */
37 #define BUILD_ASSERT_DECL(EXPR) \
38         extern int (*build_assert(void))[BUILD_ASSERT(EXPR)]
39 #else /* __cplusplus */
40 #include <boost/static_assert.hpp>
41 #define BUILD_ASSERT BOOST_STATIC_ASSERT
42 #define BUILD_ASSERT_DECL BOOST_STATIC_ASSERT
43 #endif /* __cplusplus */
44
45 extern const char *program_name;
46
47 #define ARRAY_SIZE(ARRAY) (sizeof ARRAY / sizeof *ARRAY)
48 #define ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y) * (Y))
49 #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))
50 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
51 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
52
53 #define NOT_REACHED() abort()
54 #define NOT_IMPLEMENTED() abort()
55 #define NOT_TESTED() ((void) 0) /* XXX should print a message. */
56
57 /* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
58    the STRUCT object. */
59 #define CONTAINER_OF(POINTER, STRUCT, MEMBER)                           \
60         ((STRUCT *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))
61
62 #ifdef  __cplusplus
63 extern "C" {
64 #endif
65
66 void set_program_name(const char *);
67
68 void *xmalloc(size_t);
69 void *xcalloc(size_t, size_t);
70 void *xrealloc(void *, size_t);
71 char *xstrdup(const char *);
72 char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2);
73
74 void fatal(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3) NO_RETURN;
75 void error(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
76 void debug(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
77 void debug_msg(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
78 void hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii);
79
80 #ifdef  __cplusplus
81 }
82 #endif
83
84 #endif /* util.h */