4d92ac4949c4a5111df6ae5ba0fd36325281be49
[fio.git] / compiler / compiler.h
1 #ifndef FIO_COMPILER_H
2 #define FIO_COMPILER_H
3
4 /* IWYU pragma: begin_exports */
5 #if __GNUC__ >= 4
6 #include "compiler-gcc4.h"
7 #elif __GNUC__ == 3
8 #include "compiler-gcc3.h"
9 #else
10 #error Compiler too old, need gcc at least gcc 3.x
11 #endif
12 /* IWYU pragma: end_exports */
13
14 #ifndef __must_check
15 #define __must_check
16 #endif
17
18 /*
19  * Mark unused variables passed to ops functions as unused, to silence gcc
20  */
21 #define fio_unused      __attribute__((__unused__))
22 #define fio_init        __attribute__((constructor))
23 #define fio_exit        __attribute__((destructor))
24
25 #define fio_unlikely(x) __builtin_expect(!!(x), 0)
26
27 /*
28  * Check at compile time that something is of a particular type.
29  * Always evaluates to 1 so you may use it easily in comparisons.
30  */
31 #define typecheck(type,x) \
32 ({      type __dummy; \
33         typeof(x) __dummy2; \
34         (void)(&__dummy == &__dummy2); \
35         1; \
36 })
37
38
39 #if defined(CONFIG_STATIC_ASSERT)
40 #define compiletime_assert(condition, msg) _Static_assert(condition, msg)
41
42 #elif !defined(CONFIG_DISABLE_OPTIMIZATIONS)
43
44 #ifndef __compiletime_error
45 #define __compiletime_error(message)
46 #endif
47
48 #ifndef __compiletime_error_fallback
49 #define __compiletime_error_fallback(condition) do { } while (0)
50 #endif
51
52 #define __compiletime_assert(condition, msg, prefix, suffix)            \
53         do {                                                            \
54                 int __cond = !(condition);                              \
55                 extern void prefix ## suffix(void) __compiletime_error(msg); \
56                 if (__cond)                                             \
57                         prefix ## suffix();                             \
58                 __compiletime_error_fallback(__cond);                   \
59         } while (0)
60
61 #define _compiletime_assert(condition, msg, prefix, suffix) \
62         __compiletime_assert(condition, msg, prefix, suffix)
63
64 #define compiletime_assert(condition, msg) \
65         _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
66
67 #else
68
69 #define compiletime_assert(condition, msg)      do { } while (0)
70
71 #endif
72
73 #ifdef FIO_INTERNAL
74 #define ARRAY_SIZE(x)    (sizeof((x)) / (sizeof((x)[0])))
75 #define FIELD_SIZE(s, f) (sizeof(((typeof(s))0)->f))
76 #endif
77
78 #endif