a6a74322549fc66f3210dc9b52406d1fd43a5475
[fio.git] / compiler / compiler.h
1 #ifndef FIO_COMPILER_H
2 #define FIO_COMPILER_H
3 #include <assert.h>
4
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
13 #ifndef __must_check
14 #define __must_check
15 #endif
16
17 /*
18  * Mark unused variables passed to ops functions as unused, to silence gcc
19  */
20 #define fio_unused      __attribute__((__unused__))
21 #define fio_init        __attribute__((constructor))
22 #define fio_exit        __attribute__((destructor))
23
24 #define fio_unlikely(x) __builtin_expect(!!(x), 0)
25
26 /*
27  * Check at compile time that something is of a particular type.
28  * Always evaluates to 1 so you may use it easily in comparisons.
29  */
30 #define typecheck(type,x) \
31 ({      type __dummy; \
32         typeof(x) __dummy2; \
33         (void)(&__dummy == &__dummy2); \
34         1; \
35 })
36
37
38 #if defined(CONFIG_STATIC_ASSERT)
39 #define compiletime_assert(condition, msg) _Static_assert(condition, msg)
40
41 #else
42 #ifndef __compiletime_error
43 #define __compiletime_error(message)
44 #endif
45 #ifndef __compiletime_error_fallback
46 #define __compiletime_error_fallback(condition) do { } while (0)
47 #endif
48
49 #define __compiletime_assert(condition, msg, prefix, suffix)            \
50         do {                                                            \
51                 int __cond = !(condition);                              \
52                 extern void prefix ## suffix(void) __compiletime_error(msg); \
53                 if (__cond)                                             \
54                         prefix ## suffix();                             \
55                 __compiletime_error_fallback(__cond);                   \
56         } while (0)
57
58 #define _compiletime_assert(condition, msg, prefix, suffix) \
59         __compiletime_assert(condition, msg, prefix, suffix)
60
61 #define compiletime_assert(condition, msg) \
62         _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
63
64 #endif
65
66 #endif