UCX  1.8
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 *)((intptr_t)(_ptr) + (intptr_t)(_offset)))
94 
95 /* Helper macro to calculate an address with offset equal to size of _type */
96 #define UCS_PTR_TYPE_OFFSET(_ptr, _type) \
97  ((void *)((typeof(_type) *)(_ptr) + 1))
98 
99 /* Helper macro to calculate ptr difference (_end - _start) */
100 #define UCS_PTR_BYTE_DIFF(_start, _end) \
101  ((ptrdiff_t)((uintptr_t)(_end) - (uintptr_t)(_start)))
102 
103 
107 #define ucs_static_array_size(_array) \
108  ({ \
109  UCS_STATIC_ASSERT((void*)&(_array) == (void*)&((_array)[0])); \
110  ( sizeof(_array) / sizeof((_array)[0]) ); \
111  })
112 
116 #define ucs_array_size(_array) \
117  (sizeof(_array) / sizeof((_array)[0]))
118 
122 #define ucs_offsetof(_type, _member) \
123  ((unsigned long)&( ((_type*)0)->_member ))
124 
134 #define ucs_container_of(_ptr, _type, _member) \
135  ( (_type*)( (char*)(void*)(_ptr) - ucs_offsetof(_type, _member) ) )
136 
137 
143 #define ucs_derived_of(_ptr, _type) \
144  ({\
145  UCS_STATIC_ASSERT(offsetof(_type, super) == 0) \
146  ucs_container_of(_ptr, _type, super); \
147  })
148 
155 #define ucs_field_sizeof(_type, _field) \
156  sizeof(((_type*)0)->_field)
157 
164 #define ucs_field_type(_type, _field) \
165  typeof(((_type*)0)->_field)
166 
170 #define ucs_compiler_fence() asm volatile(""::: "memory")
171 
175 #define ucs_prefetch(p) __builtin_prefetch(p)
176 
177 /* Branch prediction */
178 #define ucs_likely(x) __builtin_expect(x, 1)
179 #define ucs_unlikely(x) __builtin_expect(x, 0)
180 
181 /* Check if an expression is a compile-time constant */
182 #define ucs_is_constant(expr) __builtin_constant_p(expr)
183 
184 #endif /* UCS_COMPILER_DEF_H */