Debian Codegraph @

lib/usual/base.h

raw symbols SCIP
1 /** @file
2 * Basic C environment.
3 */
4
5 /*
6 * Copyright (c) 2007-2009 Marko Kreen
7 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 #ifndef _USUAL_BASE_H_
22 #define _USUAL_BASE_H_
23
24 #ifdef USUAL_TEST_CONFIG
25 #include "test_config.h"
26 #elif defined(_MSC_VER)
27 #include <usual/config_msvc.h>
28 #else
29 #include <usual/config.h>
30 #endif
31
32 /* solaris is broken otherwise */
33 #if defined(__sun)
34 #define _XPG4_2
35 #define __EXTENSIONS__
36 #endif
37
38 /* C11 */
39 #ifndef __STDC_WANT_LIB_EXT1__
40 #define __STDC_WANT_LIB_EXT1__ 1
41 #endif
42
43 #include <sys/types.h>
44 #ifdef HAVE_SYS_PARAM_H
45 #include <sys/param.h>
46 #endif
47 #include <stddef.h>
48 #include <stdint.h>
49 #ifdef HAVE_INTTYPES_H
50 #include <inttypes.h>
51 #endif
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <stdarg.h>
55 #include <errno.h>
56 #include <assert.h>
57 #ifdef HAVE_UNISTD_H
58 #include <unistd.h>
59 #endif
60
61 #ifdef HAVE_STDBOOL_H
62 #include <stdbool.h>
63 #else
64 /* we really want bool type */
65 typedef enum { true=1, false=0 } bool;
66 #endif
67
68 #ifdef WIN32
69 #include <usual/base_win32.h>
70 #define DLLEXPORT __declspec(dllexport)
71 #define DLLIMPORT __declspec(dllimport)
72 #else
73 #define DLLEXPORT
74 #define DLLIMPORT
75 #endif
76
77 /** give offset of a field inside struct */
78 #ifndef offsetof
79 #define offsetof(type, field) ((unsigned long)&(((type *)0)->field))
80 #endif
81
82 /** given pointer to field inside struct, return pointer to struct */
83 #ifndef container_of
84 #define container_of(ptr, type, field) ((type *)((char *)(ptr) - offsetof(type, field)))
85 #endif
86
87 /** get alignment requirement for a type */
88 #ifndef alignof
89 #define alignof(type) offsetof(struct { char c; type t; }, t)
90 #endif
91
92 /** power-of-2 alignment */
93 #ifndef CUSTOM_ALIGN
94 #define CUSTOM_ALIGN(x, a) (((uintptr_t)(x) + (uintptr_t)(a) - 1) & ~((uintptr_t)(a) - 1))
95 #endif
96
97 /** preferred alignment */
98 #ifndef ALIGN
99 #define ALIGN(x) CUSTOM_ALIGN(x, sizeof(long))
100 #endif
101
102 /** number of elements in array */
103 #define ARRAY_NELEM(a) (sizeof(a) / sizeof((a)[0]))
104
105 /**
106 * Compat helper to specify array with unknown length.
107 *
108 * Usage:
109 *
110 * @code
111 * char flex_string[FLEX_ARRAY];
112 * @endcode
113 */
114 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
115 #define FLEX_ARRAY
116 #elif defined(__GNUC__) && (__GNUC__ >= 3)
117 #define FLEX_ARRAY
118 #else
119 #define FLEX_ARRAY 1
120 #endif
121
122 /** Make string token from C expression */
123 #define STR(x) _STR_(x)
124 #define _STR_(x) #x
125
126 /** Make single C token from 2 separate tokens */
127 #define CONCAT(a, b) _CONCAT_(a, b)
128 #define _CONCAT_(a, b) a ## b
129
130 /** Make single C token from 3 separate tokens */
131 #define CONCAT3(a, b, c) _CONCAT3_(a, b, c)
132 #define _CONCAT3_(a, b, c) a ## b ## c
133
134 /** Make single C token from 4 separate tokens */
135 #define CONCAT4(a, b, c, d) _CONCAT4_(a, b, c, d)
136 #define _CONCAT4_(a, b, c, d) a ## b ## c ## d
137
138 /** Pre-processor macro for current function name. */
139 #ifndef HAVE_FUNCNAME__FUNC
140 #define __func__ __FUNCTION__
141 #endif
142
143 /**
144 * @name Compiler checks, mainly for internal usage.
145 *
146 * @{
147 */
148
149 /** Pre-processor macro to check if compiler is GCC with high enough version */
150 #if defined(__GNUC__)
151 #define _COMPILER_GNUC(maj,min) ((__GNUC__ > (maj)) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
152 #else
153 #define _COMPILER_GNUC(maj,min) (0)
154 #endif
155 /** Pre-processor macro to check if compiler is CLANG with high enough version */
156 #if defined(__clang__)
157 #define _COMPILER_CLANG(maj,min) ((__clang_major__ > (maj)) || (__clang_major__ == (maj) && __clang_minor__ >= (min)))
158 #else
159 #define _COMPILER_CLANG(maj,min) (0)
160 #endif
161 /** Pre-processor macro to check if compiler is Visual C with high enough version */
162 #if defined(_MSC_VER)
163 #define _COMPILER_MSC(ver) (_MSC_VER >= (ver))
164 #else
165 #define _COMPILER_MSC(ver) (0)
166 #endif
167 /** Pre-processor macro to check if compiler is Intel CC with high enough version */
168 #if defined(__INTEL_COMPILER)
169 #define _COMPILER_ICC(ver) (__INTEL_COMPILER >= (ver))
170 #else
171 #define _COMPILER_ICC(ver) (0)
172 #endif
173
174 /*
175 * clang compat
176 *
177 * They work only if the compiler is clang,
178 * return 0 otherwise.
179 */
180
181 #ifndef __has_builtin
182 #define __has_builtin(x) (0)
183 #endif
184 #ifndef __has_feature
185 #define __has_feature(x) (0)
186 #endif
187 #ifndef __has_extension
188 #define __has_extension(x) __has_feature(x)
189 #endif
190 #ifndef __has_attribute
191 #define __has_attribute(x) (0)
192 #endif
193
194 /*
195 * clang macros that cannot be defined here:
196 * __is_identifier
197 * __has_include
198 * __has_include_next
199 * __has_warning
200 */
201
202 /**
203 * @}
204 *
205 * @name Function/struct attributes.
206 *
207 * @{
208 */
209
210 /** Disable padding for structure */
211 #ifndef _MSC_VER
212 #define _PACKED __attribute__((packed))
213 #endif
214
215 /*
216 * make compiler do something useful
217 */
218
219 /** Show warning if function result is not used */
220 #if _COMPILER_GNUC(4,0) || __has_attribute(warn_unused_result)
221 #define _MUSTCHECK __attribute__((warn_unused_result))
222 #else
223 #define _MUSTCHECK
224 #endif
225
226 /** Show warning if used */
227 #if _COMPILER_GNUC(4,0) || __has_attribute(deprecated)
228 #define _DEPRECATED __attribute__((deprecated))
229 #else
230 #define _DEPRECATED
231 #endif
232
233 /** Check printf-style format and arg sanity */
234 #if _COMPILER_GNUC(4,0) || __has_attribute(printf)
235 #ifdef __MINGW32__
236 #define _PRINTF(fmtpos, argpos) __attribute__((format(__MINGW_PRINTF_FORMAT, fmtpos, argpos)))
237 #else
238 #define _PRINTF(fmtpos, argpos) __attribute__((format(printf, fmtpos, argpos)))
239 #endif
240 #else
241 #define _PRINTF(fmtpos, argpos)
242 #endif
243
244 /** Function returns new pointer */
245 #if _COMPILER_GNUC(4,0) || __has_attribute(malloc)
246 #define _MALLOC __attribute__((malloc))
247 #else
248 #define _MALLOC
249 #endif
250
251 /** Disable 'unused' warning for function/argument. */
252 #if _COMPILER_GNUC(4,0) || __has_attribute(unused)
253 #define _UNUSED __attribute__((unused))
254 #else
255 #define _UNUSED
256 #endif
257
258 /** Do not inline function. */
259 #if _COMPILER_GNUC(4,0) || __has_attribute(noinline)
260 #define _NOINLINE __attribute__((noinline))
261 #else
262 #define _NOINLINE
263 #endif
264
265 /** Indicates that function never returns */
266 #if _COMPILER_GNUC(4,0) || __has_attribute(noreturn)
267 #define _NORETURN __attribute__((noreturn))
268 #else
269 #define _NORETURN
270 #endif
271
272 /** Hint for compiler that expression (x) is likely to be true */
273 #if _COMPILER_GNUC(4,0) || __has_builtin(__builtin_expect)
274 #define likely(x) __builtin_expect(!!(x), 1)
275 #else
276 #define likely(x) (x)
277 #endif
278
279 /** Hint for compiler that expression (x) is likely to be false */
280 #if _COMPILER_GNUC(4,0) || __has_builtin(__builtin_expect)
281 #define unlikely(x) __builtin_expect(!!(x), 0)
282 #else
283 #define unlikely(x) (x)
284 #endif
285
286 /* @} */
287
288
289 /**
290 * Compile-time assert.
291 *
292 * Expression must be evaluatable at compile time.
293 * If false, stop compilation with message.
294 *
295 * It can be used in either global or function scope.
296 */
297 #ifndef static_assert
298 #if _COMPILER_GNUC(4,6) || _COMPILER_MSC(1600) || __has_feature(c_static_assert)
299 /* Version for new compilers */
300 #define static_assert(expr, msg) _Static_assert(expr, msg)
301 #else
302 /* Version for old compilers */
303 #define static_assert(expr, msg) enum { CONCAT4(static_assert_failure_, __LINE__, _, __COUNTER__) = 1/(1 != (1 + (expr))) }
304 #endif
305 #endif /* !static_assert */
306
307
308 /** assert() that uses <usual/logging> module */
309 #ifndef Assert
310 #ifdef CASSERT
311 void log_fatal(const char *file, int line, const char *func, bool show_perror, void *ctx, const char *s, ...) _PRINTF(6, 7);
312 #define Assert(e) \
313 do { \
314 if (unlikely(!(e))) { \
315 log_fatal(__FILE__, __LINE__, __func__, false, NULL, \
316 "Assert(%s) failed", #e); \
317 abort(); \
318 } \
319 } while (0)
320 #else
321 #define Assert(e)
322 #endif
323 #endif
324
325 /** Zeroing malloc */
326 _MUSTCHECK
327 static inline void *zmalloc(size_t len)
328 {
329 return calloc(1, len);
330 }
331
332 #ifndef HAVE_POSIX_MEMALIGN
333 #define posix_memalign(a,b,c) usual_memalign(a,b,c)
334 /** Compat: posix_memalign() */
335 int posix_memalign(void **ptr_p, size_t align, size_t len);
336 #endif
337
338 #ifndef HAVE_REALLOCARRAY
339 #define reallocarray(a,b,c) usual_reallocarray(a,b,c)
340
341 /**
342 * Same as realloc(), but safely calculates total size.
343 */
344 void *reallocarray(void *p, size_t count, size_t size);
345
346 #endif
347
348 #endif