UCX 1.17
Unified Communication X
Loading...
Searching...
No Matches
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 * remove it by linker if it's not used at all.
54 */
55#define UCS_F_MAYBE_UNUSED __attribute__((unused))
56
57/* Non-null return */
58#define UCS_F_NON_NULL __attribute__((nonnull))
59
60/* Always inline the function */
61#ifdef __GNUC__
62#define UCS_F_ALWAYS_INLINE inline __attribute__ ((always_inline))
63#else
64#define UCS_F_ALWAYS_INLINE inline
65#endif
66
67/* Silence "uninitialized variable" for stupid compilers (gcc 4.1)
68 * which can't optimize properly.
69 */
70#if (((__GNUC__ == 4) && (__GNUC_MINOR__ == 1)) || !defined(__OPTIMIZE__))
71# define UCS_V_INITIALIZED(_v) (_v = (ucs_typeof(_v))0)
72#else
73# define UCS_V_INITIALIZED(_v) ((void)0)
74#endif
75
76/* The i-th bit */
77#define UCS_BIT(i) (1ul << (i))
78
79/* Mask of bits 0..i-1 */
80#define UCS_MASK(i) (UCS_BIT(i) - 1)
81
82/*
83 * Enable compiler checks for printf-like formatting.
84 *
85 * @param fmtargN number of formatting argument
86 * @param vargN number of variadic argument
87 */
88#define UCS_F_PRINTF(fmtargN, vargN) __attribute__((format(printf, fmtargN, vargN)))
89
90/* Unused variable */
91#define UCS_V_UNUSED __attribute__((unused))
92
93/* Aligned variable */
94#define UCS_V_ALIGNED(_align) __attribute__((aligned(_align)))
95
96/* Disable address sanitizer */
97#ifdef __SANITIZE_ADDRESS__
98# define UCS_F_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
99#else
100# define UCS_F_NO_SANITIZE_ADDRESS
101#endif
102
103/* Used for labels */
104#define UCS_EMPTY_STATEMENT {}
105
106/* Helper macro for address arithmetic in bytes */
107#define UCS_PTR_BYTE_OFFSET(_ptr, _offset) \
108 ((void *)((intptr_t)(_ptr) + (intptr_t)(_offset)))
109
110/* Helper macro to calculate an address with offset equal to size of _type */
111#define UCS_PTR_TYPE_OFFSET(_ptr, _type) \
112 ((void *)((ucs_typeof(_type) *)(_ptr) + 1))
113
114/* Helper macro to calculate ptr difference (_end - _start) */
115#define UCS_PTR_BYTE_DIFF(_start, _end) \
116 ((ptrdiff_t)((uintptr_t)(_end) - (uintptr_t)(_start)))
117
118
122#define ucs_static_array_size(_array) \
123 (sizeof(_array) / sizeof((_array)[0]))
124
125
129#define ucs_offsetof(_type, _member) \
130 ((unsigned long)&( ((_type*)0)->_member ))
131
132
142#define ucs_container_of(_ptr, _type, _member) \
143 ( (_type*)( (char*)(void*)(_ptr) - ucs_offsetof(_type, _member) ) )
144
145
153#define ucs_typeof(_type) \
154 __typeof__(_type)
155
156
162#define ucs_derived_of(_ptr, _type) \
163 ({\
164 UCS_STATIC_ASSERT(offsetof(_type, super) == 0) \
165 ucs_container_of(_ptr, _type, super); \
166 })
167
174#define ucs_field_sizeof(_type, _field) \
175 sizeof(((_type*)0)->_field)
176
183#define ucs_field_type(_type, _field) \
184 ucs_typeof(((_type*)0)->_field)
185
191#define ucs_is_unsigned_type(_type) \
192 ((_type)(-1) > (_type)(0))
193
197#define ucs_compiler_fence() asm volatile(""::: "memory")
198
202#define ucs_prefetch(p) __builtin_prefetch(p)
203
204/* Branch prediction */
205#define ucs_likely(x) __builtin_expect(x, 1)
206#define ucs_unlikely(x) __builtin_expect(x, 0)
207
208/* Check if an expression is a compile-time constant */
209#define ucs_is_constant(expr) __builtin_constant_p(expr)
210
211/*
212 * Define code which runs at global constructor phase
213 */
214#define UCS_STATIC_INIT \
215 static void UCS_F_CTOR UCS_PP_APPEND_UNIQUE_ID(ucs_initializer_ctor)()
216
217/*
218 * Define code which runs at global destructor phase
219 */
220#define UCS_STATIC_CLEANUP \
221 static void UCS_F_DTOR UCS_PP_APPEND_UNIQUE_ID(ucs_initializer_dtor)()
222
223/*
224 * Check if the two types are the same
225 */
226#define ucs_same_type(_type1, _type2) \
227 __builtin_types_compatible_p(_type1, _type2)
228
229/*
230 * Iterate over all elements of a C-array
231 */
232#define ucs_carray_for_each(_elem, _array, _length) \
233 for ((_elem) = (_array); (_elem) < ((_array) + (_length)); ++(_elem))
234
235/*
236 * Swap two variables values
237 */
238#define ucs_swap(_a, _b) \
239 { \
240 ucs_typeof(*(_a)) __tmp; \
241 \
242 UCS_STATIC_ASSERT(ucs_same_type(ucs_typeof(*(_a)), ucs_typeof(*(_b)))); \
243 __tmp = *(_a); \
244 *(_a) = *(_b); \
245 *(_b) = __tmp; \
246 }
247
248#endif /* UCS_COMPILER_DEF_H */