2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef OPENVSWITCH_UTIL_H
18 #define OPENVSWITCH_UTIL_H 1
20 #include <openvswitch/compiler.h>
21 #include <openvswitch/version.h>
22 #include <openvswitch/types.h>
28 void ovs_set_program_name__(const char *name, const char *version,
29 const char *date, const char *time);
31 #define ovs_set_program_name(name, version) \
32 ovs_set_program_name__(name, version, __DATE__, __TIME__)
34 const char *ovs_get_program_name(void);
35 const char *ovs_get_program_version(void);
37 /* Expands to a string that looks like "<file>:<line>", e.g. "tmp.c:10".
39 * See http://c-faq.com/ansi/stringize.html for an explanation of OVS_STRINGIZE
40 * and OVS_STRINGIZE2. */
41 #define OVS_SOURCE_LOCATOR __FILE__ ":" OVS_STRINGIZE(__LINE__)
42 #define OVS_STRINGIZE(ARG) OVS_STRINGIZE2(ARG)
43 #define OVS_STRINGIZE2(ARG) #ARG
45 /* Saturating multiplication of "unsigned int"s: overflow yields UINT_MAX. */
46 #define OVS_SAT_MUL(X, Y) \
48 : (X) <= UINT_MAX / (Y) ? (unsigned int) (X) * (unsigned int) (Y) \
51 /* Like the standard assert macro, except writes the failure message to the
54 #define ovs_assert(CONDITION) \
55 if (!OVS_LIKELY(CONDITION)) { \
56 ovs_assert_failure(OVS_SOURCE_LOCATOR, __func__, #CONDITION); \
59 #define ovs_assert(CONDITION) ((void) (CONDITION))
61 OVS_NO_RETURN void ovs_assert_failure(const char *, const char *, const char *);
63 /* This is a void expression that issues a compiler error if POINTER cannot be
64 * compared for equality with the given pointer TYPE. This generally means
65 * that POINTER is a qualified or unqualified TYPE. However,
66 * BUILD_ASSERT_TYPE(POINTER, void *) will accept any pointer to object type,
67 * because any pointer to object can be compared for equality with "void *".
69 * POINTER can be any expression. The use of "sizeof" ensures that the
70 * expression is not actually evaluated, so that any side effects of the
71 * expression do not occur.
73 * The cast to int is present only to suppress an "expression using sizeof
74 * bool" warning from "sparse" (see
75 * http://permalink.gmane.org/gmane.comp.parsers.sparse/2967). */
76 #define BUILD_ASSERT_TYPE(POINTER, TYPE) \
77 ((void) sizeof ((int) ((POINTER) == (TYPE) (POINTER))))
79 /* Casts 'pointer' to 'type' and issues a compiler warning if the cast changes
80 * anything other than an outermost "const" or "volatile" qualifier.
82 * The cast to int is present only to suppress an "expression using sizeof
83 * bool" warning from "sparse" (see
84 * http://permalink.gmane.org/gmane.comp.parsers.sparse/2967). */
85 #define CONST_CAST(TYPE, POINTER) \
86 (BUILD_ASSERT_TYPE(POINTER, TYPE), \
89 /* Given a pointer-typed lvalue OBJECT, expands to a pointer type that may be
90 * assigned to OBJECT. */
92 #define OVS_TYPEOF(OBJECT) typeof(OBJECT)
94 #define OVS_TYPEOF(OBJECT) void *
97 /* Given OBJECT of type pointer-to-structure, expands to the offset of MEMBER
98 * within an instance of the structure.
100 * The GCC-specific version avoids the technicality of undefined behavior if
101 * OBJECT is null, invalid, or not yet initialized. This makes some static
102 * checkers (like Coverity) happier. But the non-GCC version does not actually
103 * dereference any pointer, so it would be surprising for it to cause any
104 * problems in practice.
107 #define OBJECT_OFFSETOF(OBJECT, MEMBER) offsetof(typeof(*(OBJECT)), MEMBER)
109 #define OBJECT_OFFSETOF(OBJECT, MEMBER) \
110 ((char *) &(OBJECT)->MEMBER - (char *) (OBJECT))
113 /* Yields the size of MEMBER within STRUCT. */
114 #define MEMBER_SIZEOF(STRUCT, MEMBER) (sizeof(((STRUCT *) NULL)->MEMBER))
116 /* Yields the offset of the end of MEMBER within STRUCT. */
117 #define OFFSETOFEND(STRUCT, MEMBER) \
118 (offsetof(STRUCT, MEMBER) + MEMBER_SIZEOF(STRUCT, MEMBER))
120 /* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
121 the STRUCT object. */
122 #define CONTAINER_OF(POINTER, STRUCT, MEMBER) \
123 ((STRUCT *) (void *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))
125 /* Given POINTER, the address of the given MEMBER within an object of the type
126 * that that OBJECT points to, returns OBJECT as an assignment-compatible
127 * pointer type (either the correct pointer type or "void *"). OBJECT must be
130 * This is the same as CONTAINER_OF except that it infers the structure type
131 * from the type of '*OBJECT'. */
132 #define OBJECT_CONTAINING(POINTER, OBJECT, MEMBER) \
133 ((OVS_TYPEOF(OBJECT)) (void *) \
134 ((char *) (POINTER) - OBJECT_OFFSETOF(OBJECT, MEMBER)))
136 /* Given POINTER, the address of the given MEMBER within an object of the type
137 * that that OBJECT points to, assigns the address of the outer object to
138 * OBJECT, which must be an lvalue.
140 * Evaluates to (void) 0 as the result is not to be used. */
141 #define ASSIGN_CONTAINER(OBJECT, POINTER, MEMBER) \
142 ((OBJECT) = OBJECT_CONTAINING(POINTER, OBJECT, MEMBER), (void) 0)
144 /* As explained in the comment above OBJECT_OFFSETOF(), non-GNUC compilers
145 * like MSVC will complain about un-initialized variables if OBJECT
146 * hasn't already been initialized. To prevent such warnings, INIT_CONTAINER()
147 * can be used as a wrapper around ASSIGN_CONTAINER. */
148 #define INIT_CONTAINER(OBJECT, POINTER, MEMBER) \
149 ((OBJECT) = NULL, ASSIGN_CONTAINER(OBJECT, POINTER, MEMBER))
151 /* Returns the number of elements in ARRAY. */
152 #define ARRAY_SIZE(ARRAY) __ARRAY_SIZE(ARRAY)
154 /* Returns X / Y, rounding up. X must be nonnegative to round correctly. */
155 #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
157 /* Returns X rounded up to the nearest multiple of Y. */
158 #define ROUND_UP(X, Y) (DIV_ROUND_UP(X, Y) * (Y))
160 /* Returns the least number that, when added to X, yields a multiple of Y. */
161 #define PAD_SIZE(X, Y) (ROUND_UP(X, Y) - (X))
163 /* Returns X rounded down to the nearest multiple of Y. */
164 #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))
166 /* Returns true if X is a power of 2, otherwise false. */
167 #define IS_POW2(X) ((X) && !((X) & ((X) - 1)))
175 /* Returns X rounded up to a power of 2. X must be a constant expression. */
176 #define ROUND_UP_POW2(X) RUP2__(X)
177 #define RUP2__(X) (RUP2_1(X) + 1)
178 #define RUP2_1(X) (RUP2_2(X) | (RUP2_2(X) >> 16))
179 #define RUP2_2(X) (RUP2_3(X) | (RUP2_3(X) >> 8))
180 #define RUP2_3(X) (RUP2_4(X) | (RUP2_4(X) >> 4))
181 #define RUP2_4(X) (RUP2_5(X) | (RUP2_5(X) >> 2))
182 #define RUP2_5(X) (RUP2_6(X) | (RUP2_6(X) >> 1))
183 #define RUP2_6(X) ((X) - 1)
185 /* Returns X rounded down to a power of 2. X must be a constant expression. */
186 #define ROUND_DOWN_POW2(X) RDP2__(X)
187 #define RDP2__(X) (RDP2_1(X) - (RDP2_1(X) >> 1))
188 #define RDP2_1(X) (RDP2_2(X) | (RDP2_2(X) >> 16))
189 #define RDP2_2(X) (RDP2_3(X) | (RDP2_3(X) >> 8))
190 #define RDP2_3(X) (RDP2_4(X) | (RDP2_4(X) >> 4))
191 #define RDP2_4(X) (RDP2_5(X) | (RDP2_5(X) >> 2))
192 #define RDP2_5(X) ( (X) | ( (X) >> 1))
194 /* Given ATTR, and TYPE, cast the ATTR to TYPE by first casting ATTR to
195 * (void *). This is to suppress the alignment warning issued by clang. */
196 #define ALIGNED_CAST(TYPE, ATTR) ((TYPE) (void *) (ATTR))