urcu/uatomic/riscv: Mark RISC-V as broken
[userspace-rcu.git] / include / urcu / compiler.h
1 #ifndef _URCU_COMPILER_H
2 #define _URCU_COMPILER_H
3
4 /*
5 * compiler.h
6 *
7 * Compiler definitions.
8 *
9 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
22 #include <stddef.h> /* for offsetof */
23
24 #define caa_likely(x) __builtin_expect(!!(x), 1)
25 #define caa_unlikely(x) __builtin_expect(!!(x), 0)
26
27 #define cmm_barrier() __asm__ __volatile__ ("" : : : "memory")
28
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
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.
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 */
41 #define CMM_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
42
43 #ifndef caa_max
44 #define caa_max(a,b) ((a)>(b)?(a):(b))
45 #endif
46
47 #ifndef caa_min
48 #define caa_min(a,b) ((a)<(b)?(a):(b))
49 #endif
50
51 #if defined(__SIZEOF_LONG__)
52 #define CAA_BITS_PER_LONG (__SIZEOF_LONG__ * 8)
53 #elif defined(_LP64)
54 #define CAA_BITS_PER_LONG 64
55 #else
56 #define CAA_BITS_PER_LONG 32
57 #endif
58
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) \
67 __extension__ \
68 ({ \
69 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
70 (type *)((char *)__ptr - offsetof(type, member)); \
71 })
72
73 /*
74 * caa_container_of_check_null - Get the address of an object containing a field.
75 *
76 * @ptr: pointer to the field.
77 * @type: type of the object.
78 * @member: name of the field within the object.
79 *
80 * Return the address of the object containing the field. Return NULL if
81 * @ptr is NULL.
82 */
83 #define caa_container_of_check_null(ptr, type, member) \
84 __extension__ \
85 ({ \
86 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
87 (__ptr) ? (type *)((char *)__ptr - offsetof(type, member)) : NULL; \
88 })
89
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))
92
93 /*
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
97 * usage.
98 */
99 #define __rcu
100
101 #ifdef __cplusplus
102 #define URCU_FORCE_CAST(type, arg) (reinterpret_cast<type>(arg))
103 #else
104 #define URCU_FORCE_CAST(type, arg) ((type) (arg))
105 #endif
106
107 #define caa_is_signed_type(type) ((type) -1 < (type) 0)
108
109 /*
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).
113 */
114 #define caa_cast_long_keep_sign(v) ((unsigned long) (v))
115
116 #if defined (__GNUC__) \
117 && ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5) \
118 || __GNUC__ >= 5)
119 #define CDS_DEPRECATED(msg) \
120 __attribute__((deprecated(msg)))
121 #else
122 #define CDS_DEPRECATED(msg) \
123 __attribute__((deprecated))
124 #endif
125
126 #define CAA_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
127
128 /*
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.
132 */
133 #if defined(__GNUC__) && !defined(__clang__)
134 # define URCU_GCC_VERSION (__GNUC__ * 10000 \
135 + __GNUC_MINOR__ * 100 \
136 + __GNUC_PATCHLEVEL__)
137 #endif
138
139 #endif /* _URCU_COMPILER_H */
This page took 0.045019 seconds and 4 git commands to generate.