Fix: futex.h: include headers outside extern C
[urcu.git] / include / urcu / compiler.h
CommitLineData
ec4e58a3
MD
1#ifndef _URCU_COMPILER_H
2#define _URCU_COMPILER_H
af02d47e
MD
3
4/*
5 * compiler.h
6 *
7 * Compiler definitions.
8 *
6982d6d7 9 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
af02d47e 10 *
3282a76b
MD
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:
af02d47e 17 *
3282a76b
MD
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
af02d47e
MD
20 */
21
49d7d158
MD
22#include <stddef.h> /* for offsetof */
23
06326a94
SM
24#if defined __cplusplus
25# include <type_traits> /* for std::remove_cv */
26#endif
27
a0b7f7ea
MD
28#define caa_likely(x) __builtin_expect(!!(x), 1)
29#define caa_unlikely(x) __builtin_expect(!!(x), 0)
2dc5fa0f 30
e51500ed 31#define cmm_barrier() __asm__ __volatile__ ("" : : : "memory")
2dc5fa0f 32
af02d47e
MD
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
6cf3827c 36 * successive instances of CMM_ACCESS_ONCE(), but only when the compiler is aware of
af02d47e 37 * particular ordering. Compiler ordering can be ensured, for example, by
6cf3827c 38 * putting two CMM_ACCESS_ONCE() in separate C statements.
af02d47e
MD
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 */
e51500ed 45#define CMM_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
af02d47e 46
2f8a5ae7
MD
47#ifndef caa_max
48#define caa_max(a,b) ((a)>(b)?(a):(b))
fdee2e6d
MD
49#endif
50
2f8a5ae7
MD
51#ifndef caa_min
52#define caa_min(a,b) ((a)<(b)?(a):(b))
fdee2e6d
MD
53#endif
54
1b27a772 55#if defined(__SIZEOF_LONG__)
06f22bdb 56#define CAA_BITS_PER_LONG (__SIZEOF_LONG__ * 8)
1b27a772 57#elif defined(_LP64)
06f22bdb 58#define CAA_BITS_PER_LONG 64
1b27a772 59#else
06f22bdb 60#define CAA_BITS_PER_LONG 32
1b27a772
PB
61#endif
62
b194c06e
MD
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) \
1b85da85 71 __extension__ \
453629a9 72 ({ \
bdffa73a 73 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
453629a9
MD
74 (type *)((char *)__ptr - offsetof(type, member)); \
75 })
76
78bec10c 77#define CAA_BUILD_BUG_ON_ZERO(cond) (sizeof(struct { int:-!!(cond); }))
9f59b220 78#define CAA_BUILD_BUG_ON(cond) ((void)CAA_BUILD_BUG_ON_ZERO(cond))
78bec10c 79
dc53e23e
MD
80/*
81 * __rcu is an annotation that documents RCU pointer accesses that need
82 * to be protected by a read-side critical section. Eventually, a static
83 * checker will be able to use this annotation to detect incorrect RCU
84 * usage.
85 */
86#define __rcu
87
4501f284 88#ifdef __cplusplus
06326a94 89#define URCU_FORCE_CAST(_type, arg) (reinterpret_cast<std::remove_cv<_type>::type>(arg))
4501f284
MD
90#else
91#define URCU_FORCE_CAST(type, arg) ((type) (arg))
92#endif
93
4b5f005b 94#define caa_is_signed_type(type) ((type) -1 < (type) 0)
e56d99bf 95
4b5f005b 96/*
e4749953
MD
97 * Cast to unsigned long, sign-extending if @v is signed.
98 * Note: casting to a larger type or to same type size keeps the sign of
99 * the expression being cast (see C99 6.3.1.3).
4b5f005b 100 */
e4749953 101#define caa_cast_long_keep_sign(v) ((unsigned long) (v))
e56d99bf 102
706d1165 103#if defined (__GNUC__) \
a0a0c4d5
EC
104 && ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5) \
105 || __GNUC__ >= 5)
706d1165
MD
106#define CDS_DEPRECATED(msg) \
107 __attribute__((deprecated(msg)))
108#else
109#define CDS_DEPRECATED(msg) \
110 __attribute__((deprecated))
111#endif
112
e323ceac
MD
113#define CAA_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
114
1b4fed78
MJ
115/*
116 * URCU_GCC_VERSION is used to blacklist specific GCC versions with known
117 * bugs, clang also defines these macros to an equivalent GCC version it
118 * claims to support, so exclude it.
119 */
120#if defined(__GNUC__) && !defined(__clang__)
4b79310a
MD
121# define URCU_GCC_VERSION (__GNUC__ * 10000 \
122 + __GNUC_MINOR__ * 100 \
123 + __GNUC_PATCHLEVEL__)
4b79310a
MD
124#endif
125
ec4e58a3 126#endif /* _URCU_COMPILER_H */
This page took 0.05241 seconds and 4 git commands to generate.