Commit | Line | Data |
---|---|---|
ec4e58a3 MD |
1 | #ifndef _URCU_COMPILER_H |
2 | #define _URCU_COMPILER_H | |
af02d47e MD |
3 | |
4 | /* | |
5 | * compiler.h | |
6 | * | |
7 | * Compiler definitions. | |
8 | * | |
6982d6d7 | 9 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
af02d47e | 10 | * |
3282a76b MD |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
af02d47e | 17 | * |
3282a76b MD |
18 | * The above copyright notice and this permission notice shall be included in |
19 | * all copies or substantial portions of the Software. | |
af02d47e MD |
20 | */ |
21 | ||
49d7d158 MD |
22 | #include <stddef.h> /* for offsetof */ |
23 | ||
a0b7f7ea MD |
24 | #define caa_likely(x) __builtin_expect(!!(x), 1) |
25 | #define caa_unlikely(x) __builtin_expect(!!(x), 0) | |
2dc5fa0f | 26 | |
e51500ed | 27 | #define cmm_barrier() __asm__ __volatile__ ("" : : : "memory") |
2dc5fa0f | 28 | |
af02d47e MD |
29 | /* |
30 | * Instruct the compiler to perform only a single access to a variable | |
31 | * (prohibits merging and refetching). The compiler is also forbidden to reorder | |
6cf3827c | 32 | * successive instances of CMM_ACCESS_ONCE(), but only when the compiler is aware of |
af02d47e | 33 | * particular ordering. Compiler ordering can be ensured, for example, by |
6cf3827c | 34 | * putting two CMM_ACCESS_ONCE() in separate C statements. |
af02d47e MD |
35 | * |
36 | * This macro does absolutely -nothing- to prevent the CPU from reordering, | |
37 | * merging, or refetching absolutely anything at any time. Its main intended | |
38 | * use is to mediate communication between process-level code and irq/NMI | |
39 | * handlers, all running on the same CPU. | |
40 | */ | |
e51500ed | 41 | #define CMM_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x)) |
af02d47e | 42 | |
2f8a5ae7 MD |
43 | #ifndef caa_max |
44 | #define caa_max(a,b) ((a)>(b)?(a):(b)) | |
fdee2e6d MD |
45 | #endif |
46 | ||
2f8a5ae7 MD |
47 | #ifndef caa_min |
48 | #define caa_min(a,b) ((a)<(b)?(a):(b)) | |
fdee2e6d MD |
49 | #endif |
50 | ||
1b27a772 | 51 | #if defined(__SIZEOF_LONG__) |
06f22bdb | 52 | #define CAA_BITS_PER_LONG (__SIZEOF_LONG__ * 8) |
1b27a772 | 53 | #elif defined(_LP64) |
06f22bdb | 54 | #define CAA_BITS_PER_LONG 64 |
1b27a772 | 55 | #else |
06f22bdb | 56 | #define CAA_BITS_PER_LONG 32 |
1b27a772 PB |
57 | #endif |
58 | ||
b194c06e MD |
59 | /* |
60 | * caa_container_of - Get the address of an object containing a field. | |
61 | * | |
62 | * @ptr: pointer to the field. | |
63 | * @type: type of the object. | |
64 | * @member: name of the field within the object. | |
65 | */ | |
66 | #define caa_container_of(ptr, type, member) \ | |
453629a9 | 67 | ({ \ |
bdffa73a | 68 | const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \ |
453629a9 MD |
69 | (type *)((char *)__ptr - offsetof(type, member)); \ |
70 | }) | |
71 | ||
78bec10c | 72 | #define CAA_BUILD_BUG_ON_ZERO(cond) (sizeof(struct { int:-!!(cond); })) |
9f59b220 | 73 | #define CAA_BUILD_BUG_ON(cond) ((void)CAA_BUILD_BUG_ON_ZERO(cond)) |
78bec10c | 74 | |
dc53e23e MD |
75 | /* |
76 | * __rcu is an annotation that documents RCU pointer accesses that need | |
77 | * to be protected by a read-side critical section. Eventually, a static | |
78 | * checker will be able to use this annotation to detect incorrect RCU | |
79 | * usage. | |
80 | */ | |
81 | #define __rcu | |
82 | ||
4501f284 MD |
83 | #ifdef __cplusplus |
84 | #define URCU_FORCE_CAST(type, arg) (reinterpret_cast<type>(arg)) | |
85 | #else | |
86 | #define URCU_FORCE_CAST(type, arg) ((type) (arg)) | |
87 | #endif | |
88 | ||
550a46b4 | 89 | #define caa_is_signed_type(type) ((type) -1 < (type) 0) |
e56d99bf | 90 | |
550a46b4 MD |
91 | /* |
92 | * Sign-extend to long if needed, and output type is unsigned long. | |
93 | */ | |
94 | #define caa_cast_long_keep_sign(v) \ | |
95 | (caa_is_signed_type(__typeof__(v)) ? \ | |
96 | (unsigned long) (long) (v) : \ | |
97 | (unsigned long) (v)) | |
e56d99bf | 98 | |
706d1165 MD |
99 | #if defined (__GNUC__) \ |
100 | && ((__GNUC_MAJOR__ == 4) && (__GNUC_MINOR__ >= 5) \ | |
101 | || __GNUC_MAJOR__ >= 5) | |
102 | #define CDS_DEPRECATED(msg) \ | |
103 | __attribute__((deprecated(msg))) | |
104 | #else | |
105 | #define CDS_DEPRECATED(msg) \ | |
106 | __attribute__((deprecated)) | |
107 | #endif | |
108 | ||
e323ceac MD |
109 | #define CAA_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
110 | ||
26cb9d81 MD |
111 | /* |
112 | * Don't allow compiling with buggy compiler. | |
113 | */ | |
114 | ||
115 | #ifdef __GNUC__ | |
116 | # define URCU_GCC_VERSION (__GNUC__ * 10000 \ | |
117 | + __GNUC_MINOR__ * 100 \ | |
118 | + __GNUC_PATCHLEVEL__) | |
119 | ||
120 | /* | |
121 | * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58854 | |
122 | */ | |
123 | # ifdef __ARMEL__ | |
124 | # if URCU_GCC_VERSION >= 40800 && URCU_GCC_VERSION <= 40802 | |
125 | # error Your gcc version produces clobbered frame accesses | |
126 | # endif | |
127 | # endif | |
128 | #endif | |
129 | ||
ec4e58a3 | 130 | #endif /* _URCU_COMPILER_H */ |