UCX  1.10
Unified Communication X
compiler_def.h
1 
8 #ifndef UCS_COMPILER_DEF_H
9 #define UCS_COMPILER_DEF_H
10 
11 /* Note: Place "@file <file name>.h" after BEGIN_C_DECS
12  * to avoid bugs in a documentation */
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 /* Maximal allocation size for on-stack buffers */
30 #define UCS_ALLOCA_MAX_SIZE 1200
31 
32 /* Aliasing structure */
33 #define UCS_S_MAY_ALIAS __attribute__((may_alias))
34 
35 /* A function without side effects */
36 #define UCS_F_PURE __attribute__((pure))
37 
38 /* A function which does not return */
39 #define UCS_F_NORETURN __attribute__((noreturn))
40 
41 /* Packed structure */
42 #define UCS_S_PACKED __attribute__((packed))
43 
44 /* Avoid inlining the function */
45 #define UCS_F_NOINLINE __attribute__ ((noinline))
46 
47 /* Shared library constructor and destructor */
48 #define UCS_F_CTOR __attribute__((constructor))
49 #define UCS_F_DTOR __attribute__((destructor))
50 
51 /* Silence "defined but not used" error for static function */
52 #define UCS_F_MAYBE_UNUSED __attribute__((used))
53 
54 /* Non-null return */
55 #define UCS_F_NON_NULL __attribute__((nonnull))
56 
57 /* Always inline the function */
58 #ifdef __GNUC__
59 #define UCS_F_ALWAYS_INLINE inline __attribute__ ((always_inline))
60 #else
61 #define UCS_F_ALWAYS_INLINE inline
62 #endif
63 
64 /* Silence "uninitialized variable" for stupid compilers (gcc 4.1)
65  * which can't optimize properly.
66  */
67 #if (((__GNUC__ == 4) && (__GNUC_MINOR__ == 1)) || !defined(__OPTIMIZE__))
68 # define UCS_V_INITIALIZED(_v) (_v = (typeof(_v))0)
69 #else
70 # define UCS_V_INITIALIZED(_v) ((void)0)
71 #endif
72 
73 /* The i-th bit */
74 #define UCS_BIT(i) (1ul << (i))
75 
76 /* Mask of bits 0..i-1 */
77 #define UCS_MASK(i) (UCS_BIT(i) - 1)
78 
79 /*
80  * Enable compiler checks for printf-like formatting.
81  *
82  * @param fmtargN number of formatting argument
83  * @param vargN number of variadic argument
84  */
85 #define UCS_F_PRINTF(fmtargN, vargN) __attribute__((format(printf, fmtargN, vargN)))
86 
87 /* Unused variable */
88 #define UCS_V_UNUSED __attribute__((unused))
89 
90 /* Aligned variable */
91 #define UCS_V_ALIGNED(_align) __attribute__((aligned(_align)))
92 
93 /* Used for labels */
94 #define UCS_EMPTY_STATEMENT {}
95 
96 /* Helper macro for address arithmetic in bytes */
97 #define UCS_PTR_BYTE_OFFSET(_ptr, _offset) \
98  ((void *)((intptr_t)(_ptr) + (intptr_t)(_offset)))
99 
100 /* Helper macro to calculate an address with offset equal to size of _type */
101 #define UCS_PTR_TYPE_OFFSET(_ptr, _type) \
102  ((void *)((typeof(_type) *)(_ptr) + 1))
103 
104 /* Helper macro to calculate ptr difference (_end - _start) */
105 #define UCS_PTR_BYTE_DIFF(_start, _end) \
106  ((ptrdiff_t)((uintptr_t)(_end) - (uintptr_t)(_start)))
107 
108 
112 #define ucs_static_array_size(_array) \
113  (sizeof(_array) / sizeof((_array)[0]))
114 
115 
119 #define ucs_offsetof(_type, _member) \
120  ((unsigned long)&( ((_type*)0)->_member ))
121 
122 
132 #define ucs_container_of(_ptr, _type, _member) \
133  ( (_type*)( (char*)(void*)(_ptr) - ucs_offsetof(_type, _member) ) )
134 
135 
141 #define ucs_derived_of(_ptr, _type) \
142  ({\
143  UCS_STATIC_ASSERT(offsetof(_type, super) == 0) \
144  ucs_container_of(_ptr, _type, super); \
145  })
146 
153 #define ucs_field_sizeof(_type, _field) \
154  sizeof(((_type*)0)->_field)
155 
162 #define ucs_field_type(_type, _field) \
163  typeof(((_type*)0)->_field)
164 
168 #define ucs_compiler_fence() asm volatile(""::: "memory")
169 
173 #define ucs_prefetch(p) __builtin_prefetch(p)
174 
175 /* Branch prediction */
176 #define ucs_likely(x) __builtin_expect(x, 1)
177 #define ucs_unlikely(x) __builtin_expect(x, 0)
178 
179 /* Check if an expression is a compile-time constant */
180 #define ucs_is_constant(expr) __builtin_constant_p(expr)
181 
182 /*
183  * Define code which runs at global constructor phase
184  */
185 #define UCS_STATIC_INIT \
186  static void UCS_F_CTOR UCS_PP_APPEND_UNIQUE_ID(ucs_initializer_ctor)()
187 
188 /*
189  * Define code which runs at global destructor phase
190  */
191 #define UCS_STATIC_CLEANUP \
192  static void UCS_F_DTOR UCS_PP_APPEND_UNIQUE_ID(ucs_initializer_dtor)()
193 
194 #endif /* UCS_COMPILER_DEF_H */