Commit | Line | Data |
---|---|---|
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 | * |
329045f0 MD |
11 | * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
12 | * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. | |
af02d47e | 13 | * |
329045f0 MD |
14 | * Permission is hereby granted to use or copy this program |
15 | * for any purpose, provided the above notices are retained on all copies. | |
16 | * Permission to modify the code and to distribute modified code is granted, | |
17 | * provided the above notices are retained, and a notice that the code was | |
18 | * modified is included with the above copyright notice. | |
af02d47e MD |
19 | */ |
20 | ||
49d7d158 MD |
21 | #include <stddef.h> /* for offsetof */ |
22 | ||
329045f0 MD |
23 | #define likely(x) __builtin_expect(!!(x), 1) |
24 | #define unlikely(x) __builtin_expect(!!(x), 0) | |
2dc5fa0f | 25 | |
5481ddb3 | 26 | #define cmm_barrier() asm volatile("" : : : "memory") |
2dc5fa0f | 27 | |
af02d47e MD |
28 | /* |
29 | * Instruct the compiler to perform only a single access to a variable | |
30 | * (prohibits merging and refetching). The compiler is also forbidden to reorder | |
6cf3827c | 31 | * successive instances of CMM_ACCESS_ONCE(), but only when the compiler is aware of |
af02d47e | 32 | * particular ordering. Compiler ordering can be ensured, for example, by |
6cf3827c | 33 | * putting two CMM_ACCESS_ONCE() in separate C statements. |
af02d47e MD |
34 | * |
35 | * This macro does absolutely -nothing- to prevent the CPU from reordering, | |
36 | * merging, or refetching absolutely anything at any time. Its main intended | |
37 | * use is to mediate communication between process-level code and irq/NMI | |
38 | * handlers, all running on the same CPU. | |
39 | */ | |
6cf3827c | 40 | #define CMM_ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) |
af02d47e | 41 | |
2f8a5ae7 MD |
42 | #ifndef caa_max |
43 | #define caa_max(a,b) ((a)>(b)?(a):(b)) | |
fdee2e6d MD |
44 | #endif |
45 | ||
2f8a5ae7 MD |
46 | #ifndef caa_min |
47 | #define caa_min(a,b) ((a)<(b)?(a):(b)) | |
fdee2e6d MD |
48 | #endif |
49 | ||
1b27a772 | 50 | #if defined(__SIZEOF_LONG__) |
06f22bdb | 51 | #define CAA_BITS_PER_LONG (__SIZEOF_LONG__ * 8) |
1b27a772 | 52 | #elif defined(_LP64) |
06f22bdb | 53 | #define CAA_BITS_PER_LONG 64 |
1b27a772 | 54 | #else |
06f22bdb | 55 | #define CAA_BITS_PER_LONG 32 |
1b27a772 PB |
56 | #endif |
57 | ||
b194c06e MD |
58 | /* |
59 | * caa_container_of - Get the address of an object containing a field. | |
60 | * | |
61 | * @ptr: pointer to the field. | |
62 | * @type: type of the object. | |
63 | * @member: name of the field within the object. | |
64 | */ | |
65 | #define caa_container_of(ptr, type, member) \ | |
453629a9 | 66 | ({ \ |
b194c06e | 67 | const typeof(((type *) NULL)->member) * __ptr = (ptr); \ |
453629a9 MD |
68 | (type *)((char *)__ptr - offsetof(type, member)); \ |
69 | }) | |
70 | ||
78bec10c PB |
71 | #define CAA_BUILD_BUG_ON_ZERO(cond) (sizeof(struct { int:-!!(cond); })) |
72 | #define CAA_BUILD_BUG_ON(cond) ((void)BUILD_BUG_ON_ZERO(cond)) | |
73 | ||
dc53e23e MD |
74 | /* |
75 | * __rcu is an annotation that documents RCU pointer accesses that need | |
76 | * to be protected by a read-side critical section. Eventually, a static | |
77 | * checker will be able to use this annotation to detect incorrect RCU | |
78 | * usage. | |
79 | */ | |
80 | #define __rcu | |
81 | ||
ec4e58a3 | 82 | #endif /* _URCU_COMPILER_H */ |