887d9be1d3f1e8ee492d225412ba884a1c79428b
1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: LGPL-2.1-or-later
5 #ifndef _URCU_COMPILER_H
6 #define _URCU_COMPILER_H
9 * Compiler definitions.
12 #include <stddef.h> /* for offsetof */
14 #if defined __cplusplus
15 # include <type_traits> /* for std::remove_cv */
18 #include <urcu/config.h>
20 #define caa_likely(x) __builtin_expect(!!(x), 1)
21 #define caa_unlikely(x) __builtin_expect(!!(x), 0)
23 #ifdef CONFIG_RCU_USE_ATOMIC_BUILTINS
24 # define cmm_barrier() __atomic_signal_fence(__ATOMIC_SEQ_CST)
26 # define cmm_barrier() __asm__ __volatile__ ("" : : : "memory")
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
32 * successive instances of CMM_ACCESS_ONCE(), but only when the compiler is aware of
33 * particular ordering. Compiler ordering can be ensured, for example, by
34 * putting two CMM_ACCESS_ONCE() in separate C statements.
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.
41 #define CMM_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
44 #define caa_max(a,b) ((a)>(b)?(a):(b))
48 #define caa_min(a,b) ((a)<(b)?(a):(b))
51 #if defined(__SIZEOF_LONG__)
52 #define CAA_BITS_PER_LONG (__SIZEOF_LONG__ * 8)
54 #define CAA_BITS_PER_LONG 64
56 #define CAA_BITS_PER_LONG 32
60 * caa_container_of - Get the address of an object containing a field.
62 * @ptr: pointer to the field.
63 * @type: type of the object.
64 * @member: name of the field within the object.
66 #define caa_container_of(ptr, type, member) \
69 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
70 (type *)((char *)__ptr - offsetof(type, member)); \
74 * caa_container_of_check_null - Get the address of an object containing a field.
76 * @ptr: pointer to the field.
77 * @type: type of the object.
78 * @member: name of the field within the object.
80 * Return the address of the object containing the field. Return NULL if
83 #define caa_container_of_check_null(ptr, type, member) \
86 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
87 (__ptr) ? (type *)((char *)__ptr - offsetof(type, member)) : NULL; \
90 #define CAA_BUILD_BUG_ON_ZERO(cond) (sizeof(struct { int:-!!(cond); }))
91 #define CAA_BUILD_BUG_ON(cond) ((void)CAA_BUILD_BUG_ON_ZERO(cond))
94 * __rcu is an annotation that documents RCU pointer accesses that need
95 * to be protected by a read-side critical section. Eventually, a static
96 * checker will be able to use this annotation to detect incorrect RCU
102 #define URCU_FORCE_CAST(_type, arg) (reinterpret_cast<std::remove_cv<_type>::type>(arg))
104 #define URCU_FORCE_CAST(type, arg) ((type) (arg))
107 #define caa_is_signed_type(type) ((type) -1 < (type) 0)
110 * Cast to unsigned long, sign-extending if @v is signed.
111 * Note: casting to a larger type or to same type size keeps the sign of
112 * the expression being cast (see C99 6.3.1.3).
114 #define caa_cast_long_keep_sign(v) ((unsigned long) (v))
116 #if defined (__GNUC__) \
117 && ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5) \
119 #define CDS_DEPRECATED(msg) \
120 __attribute__((deprecated(msg)))
122 #define CDS_DEPRECATED(msg) \
123 __attribute__((deprecated))
126 #define CAA_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
129 * URCU_GCC_VERSION is used to blacklist specific GCC versions with known
130 * bugs, clang also defines these macros to an equivalent GCC version it
131 * claims to support, so exclude it.
133 #if defined(__GNUC__) && !defined(__clang__)
134 # define URCU_GCC_VERSION (__GNUC__ * 10000 \
135 + __GNUC_MINOR__ * 100 \
136 + __GNUC_PATCHLEVEL__)
140 #define caa_unqual_scalar_typeof(x) \
141 std::remove_cv<std::remove_reference<decltype(x)>::type>::type
143 #define caa_scalar_type_to_expr(type) \
144 unsigned type: (unsigned type)0, \
145 signed type: (signed type)0
148 * Use C11 _Generic to express unqualified type from expression. This removes
149 * volatile qualifier from expression type.
151 #define caa_unqual_scalar_typeof(x) \
155 caa_scalar_type_to_expr(char), \
156 caa_scalar_type_to_expr(short), \
157 caa_scalar_type_to_expr(int), \
158 caa_scalar_type_to_expr(long), \
159 caa_scalar_type_to_expr(long long), \
166 * Allow user to manually define CMM_SANITIZE_THREAD if their toolchain is not
167 * supported by this check.
169 #ifndef CMM_SANITIZE_THREAD
170 # if defined(__GNUC__) && defined(__SANITIZE_THREAD__)
171 # define CMM_SANITIZE_THREAD
172 # elif defined(__clang__) && defined(__has_feature)
173 # if __has_feature(thread_sanitizer)
174 # define CMM_SANITIZE_THREAD
177 #endif /* !CMM_SANITIZE_THREAD */
180 * Helper to add the volatile qualifier to a pointer.
182 #if defined __cplusplus
183 template <typename T
>
184 volatile T
cmm_cast_volatile(T t
)
186 return static_cast<volatile T
>(t
);
189 # define cmm_cast_volatile(ptr) \
192 (volatile __typeof__(ptr))(ptr); \
196 #endif /* _URCU_COMPILER_H */
This page took 0.036029 seconds and 3 git commands to generate.