Public headers: use SPDX identifiers
[urcu.git] / include / urcu / compiler.h
1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4
5 #ifndef _URCU_COMPILER_H
6 #define _URCU_COMPILER_H
7
8 /*
9 * Compiler definitions.
10 */
11
12 #include <stddef.h> /* for offsetof */
13
14 #if defined __cplusplus
15 # include <type_traits> /* for std::remove_cv */
16 #endif
17
18 #define caa_likely(x) __builtin_expect(!!(x), 1)
19 #define caa_unlikely(x) __builtin_expect(!!(x), 0)
20
21 #define cmm_barrier() __asm__ __volatile__ ("" : : : "memory")
22
23 /*
24 * Instruct the compiler to perform only a single access to a variable
25 * (prohibits merging and refetching). The compiler is also forbidden to reorder
26 * successive instances of CMM_ACCESS_ONCE(), but only when the compiler is aware of
27 * particular ordering. Compiler ordering can be ensured, for example, by
28 * putting two CMM_ACCESS_ONCE() in separate C statements.
29 *
30 * This macro does absolutely -nothing- to prevent the CPU from reordering,
31 * merging, or refetching absolutely anything at any time. Its main intended
32 * use is to mediate communication between process-level code and irq/NMI
33 * handlers, all running on the same CPU.
34 */
35 #define CMM_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
36
37 #ifndef caa_max
38 #define caa_max(a,b) ((a)>(b)?(a):(b))
39 #endif
40
41 #ifndef caa_min
42 #define caa_min(a,b) ((a)<(b)?(a):(b))
43 #endif
44
45 #if defined(__SIZEOF_LONG__)
46 #define CAA_BITS_PER_LONG (__SIZEOF_LONG__ * 8)
47 #elif defined(_LP64)
48 #define CAA_BITS_PER_LONG 64
49 #else
50 #define CAA_BITS_PER_LONG 32
51 #endif
52
53 /*
54 * caa_container_of - Get the address of an object containing a field.
55 *
56 * @ptr: pointer to the field.
57 * @type: type of the object.
58 * @member: name of the field within the object.
59 */
60 #define caa_container_of(ptr, type, member) \
61 __extension__ \
62 ({ \
63 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
64 (type *)((char *)__ptr - offsetof(type, member)); \
65 })
66
67 /*
68 * caa_container_of_check_null - Get the address of an object containing a field.
69 *
70 * @ptr: pointer to the field.
71 * @type: type of the object.
72 * @member: name of the field within the object.
73 *
74 * Return the address of the object containing the field. Return NULL if
75 * @ptr is NULL.
76 */
77 #define caa_container_of_check_null(ptr, type, member) \
78 __extension__ \
79 ({ \
80 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
81 (__ptr) ? (type *)((char *)__ptr - offsetof(type, member)) : NULL; \
82 })
83
84 #define CAA_BUILD_BUG_ON_ZERO(cond) (sizeof(struct { int:-!!(cond); }))
85 #define CAA_BUILD_BUG_ON(cond) ((void)CAA_BUILD_BUG_ON_ZERO(cond))
86
87 /*
88 * __rcu is an annotation that documents RCU pointer accesses that need
89 * to be protected by a read-side critical section. Eventually, a static
90 * checker will be able to use this annotation to detect incorrect RCU
91 * usage.
92 */
93 #define __rcu
94
95 #ifdef __cplusplus
96 #define URCU_FORCE_CAST(_type, arg) (reinterpret_cast<std::remove_cv<_type>::type>(arg))
97 #else
98 #define URCU_FORCE_CAST(type, arg) ((type) (arg))
99 #endif
100
101 #define caa_is_signed_type(type) ((type) -1 < (type) 0)
102
103 /*
104 * Cast to unsigned long, sign-extending if @v is signed.
105 * Note: casting to a larger type or to same type size keeps the sign of
106 * the expression being cast (see C99 6.3.1.3).
107 */
108 #define caa_cast_long_keep_sign(v) ((unsigned long) (v))
109
110 #if defined (__GNUC__) \
111 && ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5) \
112 || __GNUC__ >= 5)
113 #define CDS_DEPRECATED(msg) \
114 __attribute__((deprecated(msg)))
115 #else
116 #define CDS_DEPRECATED(msg) \
117 __attribute__((deprecated))
118 #endif
119
120 #define CAA_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
121
122 /*
123 * URCU_GCC_VERSION is used to blacklist specific GCC versions with known
124 * bugs, clang also defines these macros to an equivalent GCC version it
125 * claims to support, so exclude it.
126 */
127 #if defined(__GNUC__) && !defined(__clang__)
128 # define URCU_GCC_VERSION (__GNUC__ * 10000 \
129 + __GNUC_MINOR__ * 100 \
130 + __GNUC_PATCHLEVEL__)
131 #endif
132
133 #endif /* _URCU_COMPILER_H */
This page took 0.033277 seconds and 5 git commands to generate.