UCX  1.6
Unified Communication X
compiler_def.h
1 
9 #ifndef UCS_COMPILER_DEF_H
10 #define UCS_COMPILER_DEF_H
11 
12 /* Note: Place "@file <file name>.h" after BEGIN_C_DECS
13  * to avoid bugs in a documentation */
14 #ifdef __cplusplus
15 # define BEGIN_C_DECLS extern "C" {
16 # define END_C_DECLS }
17 #else
18 # define BEGIN_C_DECLS
19 # define END_C_DECLS
20 #endif
21 
22 /*
23  * Assertions which are checked in compile-time
24  *
25  * Usage: UCS_STATIC_ASSERT(condition)
26  */
27 #define UCS_STATIC_ASSERT(_cond) \
28  switch(0) {case 0:case (_cond):;}
29 
30 /* Aliasing structure */
31 #define UCS_S_MAY_ALIAS __attribute__((may_alias))
32 
33 /* A function without side effects */
34 #define UCS_F_PURE __attribute__((pure))
35 
36 /* A function which does not return */
37 #define UCS_F_NORETURN __attribute__((noreturn))
38 
39 /* Packed structure */
40 #define UCS_S_PACKED __attribute__((packed))
41 
42 /* Avoid inlining the function */
43 #define UCS_F_NOINLINE __attribute__ ((noinline))
44 
45 /* Shared library constructor and destructor */
46 #define UCS_F_CTOR __attribute__((constructor))
47 #define UCS_F_DTOR __attribute__((destructor))
48 
49 /* Silence "defined but not used" error for static function */
50 #define UCS_F_MAYBE_UNUSED __attribute__((used))
51 
52 /* Always inline the function */
53 #ifdef __GNUC__
54 #define UCS_F_ALWAYS_INLINE inline __attribute__ ((always_inline))
55 #else
56 #define UCS_F_ALWAYS_INLINE inline
57 #endif
58 
59 /* Silence "uninitialized variable" for stupid compilers (gcc 4.1)
60  * which can't optimize properly.
61  */
62 #if (((__GNUC__ == 4) && (__GNUC_MINOR__ == 1)) || !defined(__OPTIMIZE__))
63 # define UCS_V_INITIALIZED(_v) (_v = (typeof(_v))0)
64 #else
65 # define UCS_V_INITIALIZED(_v) ((void)0)
66 #endif
67 
68 /* The i-th bit */
69 #define UCS_BIT(i) (1ul << (i))
70 
71 /* Mask of bits 0..i-1 */
72 #define UCS_MASK(i) (UCS_BIT(i) - 1)
73 
74 /*
75  * Enable compiler checks for printf-like formatting.
76  *
77  * @param fmtargN number of formatting argument
78  * @param vargN number of variadic argument
79  */
80 #define UCS_F_PRINTF(fmtargN, vargN) __attribute__((format(printf, fmtargN, vargN)))
81 
82 /* Unused variable */
83 #define UCS_V_UNUSED __attribute__((unused))
84 
85 /* Aligned variable */
86 #define UCS_V_ALIGNED(_align) __attribute__((aligned(_align)))
87 
88 /* Used for labels */
89 #define UCS_EMPTY_STATEMENT {}
90 
91 /* Helper macro for address arithmetic in bytes */
92 #define UCS_PTR_BYTE_OFFSET(_ptr, _offset) \
93  ((void *)((uintptr_t)(_ptr) + (_offset)))
94 
98 #define ucs_static_array_size(_array) \
99  ({ \
100  UCS_STATIC_ASSERT((void*)&(_array) == (void*)&((_array)[0])); \
101  ( sizeof(_array) / sizeof((_array)[0]) ); \
102  })
103 
107 #define ucs_offsetof(_type, _member) \
108  ((unsigned long)&( ((_type*)0)->_member ))
109 
119 #define ucs_container_of(_ptr, _type, _member) \
120  ( (_type*)( (char*)(void*)(_ptr) - ucs_offsetof(_type, _member) ) )
121 
122 
128 #define ucs_derived_of(_ptr, _type) \
129  ({\
130  UCS_STATIC_ASSERT(offsetof(_type, super) == 0) \
131  ucs_container_of(_ptr, _type, super); \
132  })
133 
137 #define ucs_field_sizeof(_type, _field) \
138  sizeof(((_type*)0)->_field)
139 
143 #define ucs_compiler_fence() asm volatile(""::: "memory")
144 
148 #define ucs_prefetch(p) __builtin_prefetch(p)
149 
150 /* Branch prediction */
151 #define ucs_likely(x) __builtin_expect(x, 1)
152 #define ucs_unlikely(x) __builtin_expect(x, 0)
153 
154 /* Check if an expression is a compile-time constant */
155 #define ucs_is_constant(expr) __builtin_constant_p(expr)
156 
157 #endif /* UCS_COMPILER_DEF_H */