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