Public headers: use SPDX identifiers
[urcu.git] / include / urcu / compiler.h
CommitLineData
d3d3857f
MJ
1// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
ec4e58a3
MD
5#ifndef _URCU_COMPILER_H
6#define _URCU_COMPILER_H
af02d47e
MD
7
8/*
af02d47e 9 * Compiler definitions.
af02d47e
MD
10 */
11
49d7d158
MD
12#include <stddef.h> /* for offsetof */
13
06326a94
SM
14#if defined __cplusplus
15# include <type_traits> /* for std::remove_cv */
16#endif
17
a0b7f7ea
MD
18#define caa_likely(x) __builtin_expect(!!(x), 1)
19#define caa_unlikely(x) __builtin_expect(!!(x), 0)
2dc5fa0f 20
e51500ed 21#define cmm_barrier() __asm__ __volatile__ ("" : : : "memory")
2dc5fa0f 22
af02d47e
MD
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
6cf3827c 26 * successive instances of CMM_ACCESS_ONCE(), but only when the compiler is aware of
af02d47e 27 * particular ordering. Compiler ordering can be ensured, for example, by
6cf3827c 28 * putting two CMM_ACCESS_ONCE() in separate C statements.
af02d47e
MD
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 */
e51500ed 35#define CMM_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
af02d47e 36
2f8a5ae7
MD
37#ifndef caa_max
38#define caa_max(a,b) ((a)>(b)?(a):(b))
fdee2e6d
MD
39#endif
40
2f8a5ae7
MD
41#ifndef caa_min
42#define caa_min(a,b) ((a)<(b)?(a):(b))
fdee2e6d
MD
43#endif
44
1b27a772 45#if defined(__SIZEOF_LONG__)
06f22bdb 46#define CAA_BITS_PER_LONG (__SIZEOF_LONG__ * 8)
1b27a772 47#elif defined(_LP64)
06f22bdb 48#define CAA_BITS_PER_LONG 64
1b27a772 49#else
06f22bdb 50#define CAA_BITS_PER_LONG 32
1b27a772
PB
51#endif
52
b194c06e
MD
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) \
1b85da85 61 __extension__ \
453629a9 62 ({ \
bdffa73a 63 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
453629a9
MD
64 (type *)((char *)__ptr - offsetof(type, member)); \
65 })
66
4b3a7ebd
MD
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
78bec10c 84#define CAA_BUILD_BUG_ON_ZERO(cond) (sizeof(struct { int:-!!(cond); }))
9f59b220 85#define CAA_BUILD_BUG_ON(cond) ((void)CAA_BUILD_BUG_ON_ZERO(cond))
78bec10c 86
dc53e23e
MD
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
4501f284 95#ifdef __cplusplus
06326a94 96#define URCU_FORCE_CAST(_type, arg) (reinterpret_cast<std::remove_cv<_type>::type>(arg))
4501f284
MD
97#else
98#define URCU_FORCE_CAST(type, arg) ((type) (arg))
99#endif
100
4b5f005b 101#define caa_is_signed_type(type) ((type) -1 < (type) 0)
e56d99bf 102
4b5f005b 103/*
e4749953
MD
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).
4b5f005b 107 */
e4749953 108#define caa_cast_long_keep_sign(v) ((unsigned long) (v))
e56d99bf 109
706d1165 110#if defined (__GNUC__) \
a0a0c4d5
EC
111 && ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5) \
112 || __GNUC__ >= 5)
706d1165
MD
113#define CDS_DEPRECATED(msg) \
114 __attribute__((deprecated(msg)))
115#else
116#define CDS_DEPRECATED(msg) \
117 __attribute__((deprecated))
118#endif
119
e323ceac
MD
120#define CAA_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
121
1b4fed78
MJ
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__)
4b79310a
MD
128# define URCU_GCC_VERSION (__GNUC__ * 10000 \
129 + __GNUC_MINOR__ * 100 \
130 + __GNUC_PATCHLEVEL__)
4b79310a
MD
131#endif
132
ec4e58a3 133#endif /* _URCU_COMPILER_H */
This page took 0.055938 seconds and 4 git commands to generate.