UCX  1.14
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 /* Maximal allocation size for on-stack buffers */
31 #define UCS_ALLOCA_MAX_SIZE 1200
32 
33 /* Aliasing structure */
34 #define UCS_S_MAY_ALIAS __attribute__((may_alias))
35 
36 /* A function without side effects */
37 #define UCS_F_PURE __attribute__((pure))
38 
39 /* A function which does not return */
40 #define UCS_F_NORETURN __attribute__((noreturn))
41 
42 /* Packed structure */
43 #define UCS_S_PACKED __attribute__((packed))
44 
45 /* Avoid inlining the function */
46 #define UCS_F_NOINLINE __attribute__ ((noinline))
47 
48 /* Shared library constructor and destructor */
49 #define UCS_F_CTOR __attribute__((constructor))
50 #define UCS_F_DTOR __attribute__((destructor))
51 
52 /* Silence "defined but not used" error for static function */
53 #define UCS_F_MAYBE_UNUSED __attribute__((used))
54 
55 /* Non-null return */
56 #define UCS_F_NON_NULL __attribute__((nonnull))
57 
58 /* Always inline the function */
59 #ifdef __GNUC__
60 #define UCS_F_ALWAYS_INLINE inline __attribute__ ((always_inline))
61 #else
62 #define UCS_F_ALWAYS_INLINE inline
63 #endif
64 
65 /* Silence "uninitialized variable" for stupid compilers (gcc 4.1)
66  * which can't optimize properly.
67  */
68 #if (((__GNUC__ == 4) && (__GNUC_MINOR__ == 1)) || !defined(__OPTIMIZE__))
69 # define UCS_V_INITIALIZED(_v) (_v = (ucs_typeof(_v))0)
70 #else
71 # define UCS_V_INITIALIZED(_v) ((void)0)
72 #endif
73 
74 /* The i-th bit */
75 #define UCS_BIT(i) (1ul << (i))
76 
77 /* Mask of bits 0..i-1 */
78 #define UCS_MASK(i) (UCS_BIT(i) - 1)
79 
80 /*
81  * Enable compiler checks for printf-like formatting.
82  *
83  * @param fmtargN number of formatting argument
84  * @param vargN number of variadic argument
85  */
86 #define UCS_F_PRINTF(fmtargN, vargN) __attribute__((format(printf, fmtargN, vargN)))
87 
88 /* Unused variable */
89 #define UCS_V_UNUSED __attribute__((unused))
90 
91 /* Aligned variable */
92 #define UCS_V_ALIGNED(_align) __attribute__((aligned(_align)))
93 
94 /* Used for labels */
95 #define UCS_EMPTY_STATEMENT {}
96 
97 /* Helper macro for address arithmetic in bytes */
98 #define UCS_PTR_BYTE_OFFSET(_ptr, _offset) \
99  ((void *)((intptr_t)(_ptr) + (intptr_t)(_offset)))
100 
101 /* Helper macro to calculate an address with offset equal to size of _type */
102 #define UCS_PTR_TYPE_OFFSET(_ptr, _type) \
103  ((void *)((ucs_typeof(_type) *)(_ptr) + 1))
104 
105 /* Helper macro to calculate ptr difference (_end - _start) */
106 #define UCS_PTR_BYTE_DIFF(_start, _end) \
107  ((ptrdiff_t)((uintptr_t)(_end) - (uintptr_t)(_start)))
108 
109 
113 #define ucs_static_array_size(_array) \
114  (sizeof(_array) / sizeof((_array)[0]))
115 
116 
120 #define ucs_offsetof(_type, _member) \
121  ((unsigned long)&( ((_type*)0)->_member ))
122 
123 
133 #define ucs_container_of(_ptr, _type, _member) \
134  ( (_type*)( (char*)(void*)(_ptr) - ucs_offsetof(_type, _member) ) )
135 
136 
144 #define ucs_typeof(_type) \
145  __typeof__(_type)
146 
147 
153 #define ucs_derived_of(_ptr, _type) \
154  ({\
155  UCS_STATIC_ASSERT(offsetof(_type, super) == 0) \
156  ucs_container_of(_ptr, _type, super); \
157  })
158 
165 #define ucs_field_sizeof(_type, _field) \
166  sizeof(((_type*)0)->_field)
167 
174 #define ucs_field_type(_type, _field) \
175  ucs_typeof(((_type*)0)->_field)
176 
180 #define ucs_compiler_fence() asm volatile(""::: "memory")
181 
185 #define ucs_prefetch(p) __builtin_prefetch(p)
186 
187 /* Branch prediction */
188 #define ucs_likely(x) __builtin_expect(x, 1)
189 #define ucs_unlikely(x) __builtin_expect(x, 0)
190 
191 /* Check if an expression is a compile-time constant */
192 #define ucs_is_constant(expr) __builtin_constant_p(expr)
193 
194 /*
195  * Define code which runs at global constructor phase
196  */
197 #define UCS_STATIC_INIT \
198  static void UCS_F_CTOR UCS_PP_APPEND_UNIQUE_ID(ucs_initializer_ctor)()
199 
200 /*
201  * Define code which runs at global destructor phase
202  */
203 #define UCS_STATIC_CLEANUP \
204  static void UCS_F_DTOR UCS_PP_APPEND_UNIQUE_ID(ucs_initializer_dtor)()
205 
206 /*
207  * Check if the two types are the same
208  */
209 #define ucs_same_type(_type1, _type2) \
210  __builtin_types_compatible_p(_type1, _type2)
211 
212 /*
213  * Iterate over all elements of a C-array
214  */
215 #define ucs_carray_for_each(_elem, _array, _length) \
216  for ((_elem) = (_array); (_elem) < ((_array) + (_length)); ++(_elem))
217 
218 /*
219  * Swap two variables values
220  */
221 #define ucs_swap(_a, _b) \
222  { \
223  ucs_typeof(*(_a)) __tmp; \
224  \
225  UCS_STATIC_ASSERT(ucs_same_type(ucs_typeof(*(_a)), ucs_typeof(*(_b)))); \
226  __tmp = *(_a); \
227  *(_a) = *(_b); \
228  *(_b) = __tmp; \
229  }
230 
231 #endif /* UCS_COMPILER_DEF_H */