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