]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_utdefs.h
c49630b9a6094b8f8343f2786fd633c13ee50173
[ulut.git] / ulut / ul_utdefs.h
1 /*******************************************************************
2   uLan Utilities Library - C library of basic reusable constructions
3
4   ul_utdefs.h   - common defines used in uLan utilities library
5
6  *******************************************************************/
7
8
9 #ifndef _UL_UTDEFS_H
10 #define _UL_UTDEFS_H
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 #if defined(_WIN32)&&defined(_MSC_VER)&&!defined(inline)
17 #define inline _inline
18 #endif
19
20 #if !defined(UL_BUILD_BUG_ON_MSG_LINE) && defined(__OPTIMIZE__) && \
21   ((__GNUC__ * 1000 + __GNUC_MINOR__) >= 4004)
22 #define UL_BUILD_BUG_ON_MSG_LINE_EXP1(condition, msg, line) \
23 ({ \
24   if (!!(condition)) { \
25     void compile_time_bug_on_line_ ## line (void) __attribute__((error(msg))); \
26     compile_time_bug_on_line_ ## line (); \
27   } \
28 })
29 #define UL_BUILD_BUG_ON_MSG_LINE(condition, msg, line) \
30   UL_BUILD_BUG_ON_MSG_LINE_EXP1(condition, msg, line)
31 #endif /*UL_BUILD_BUG_ON_MSG for GCC*/
32
33 #ifndef UL_BUILD_BUG_ON_MSG_LINE
34 #define UL_BUILD_BUG_ON_MSG_LINE(condition, msg, line) \
35   ((void)sizeof(char[1 - 2*!!(condition)]))
36 #endif /*UL_BUILD_BUG_ON_MSG*/
37
38 #ifndef UL_BUILD_BUG_ON_MSG
39 #define UL_BUILD_BUG_ON_MSG(condition, msg) \
40   UL_BUILD_BUG_ON_MSG_LINE(condition, msg, __LINE__)
41 #endif /*UL_BUILD_BUG_ON_MSG*/
42
43 #ifndef UL_BUILD_BUG_ON
44 #define UL_BUILD_BUG_ON(condition) \
45   UL_BUILD_BUG_ON_MSG(condition, "Build time check " #condition " failed")
46 #endif /*UL_BUILD_BUG_ON*/
47
48 #if !defined(UL_OFFSETOF) && defined(__GNUC__) && __GNUC__ >= 4
49 #define UL_OFFSETOF(_type, _member) __builtin_offsetof(_type, _member)
50 #endif /*UL_OFFSETOF*/
51
52 #ifndef UL_OFFSETOF
53 /* offset of structure field */
54 #define UL_OFFSETOF(_type,_member) \
55                 ((size_t)&(((_type*)0)->_member))
56 #endif /*UL_OFFSET*/
57
58 #ifndef UL_CONTAINEROF
59 #ifdef  __GNUC__
60 #define UL_CONTAINEROF(_ptr, _type, _member) ({ \
61         const typeof( ((_type *)0)->_member ) *__mptr = (_ptr); \
62         (_type *)( (char *)__mptr - UL_OFFSETOF(_type,_member) );})
63 #else /*!__GNUC__*/
64 #define UL_CONTAINEROF(_ptr, _type, _member) \
65         ((_type *)( (char *)_ptr - UL_OFFSETOF(_type,_member)))
66 #endif /*__GNUC__*/
67 #endif /*UL_CONTAINEROF*/
68
69 #ifndef UL_ALIGNOF_FIELD
70 #define UL_ALIGNOF_FIELD(_type) UL_OFFSETOF(struct {char _fld0; _type _fld1;}, _fld1)
71 #endif /*UL_ALIGNOF*/
72
73 #ifndef UL_ALIGNOF_TYPE
74 #ifdef  __GNUC__
75 /* The compiler provided alignment for performance can differ from ABI struct one */
76 #define UL_ALIGNOF_TYPE(_type) __alignof__(_type)
77 #else /*__GNUC__*/
78 #define UL_ALIGNOF_TYPE UL_ALIGNOF_FIELD
79 #endif /*__GNUC__*/
80 #endif /*UL_ALIGNOF*/
81
82 #ifndef UL_ALIGNOF
83 #define UL_ALIGNOF UL_ALIGNOF_FIELD
84 #endif /*UL_ALIGNOF*/
85
86
87 #ifndef UL_NOPSTATEMENT
88 #define UL_NOPSTATEMENT do { } while(0)
89 #endif
90
91 #ifndef ul_cyclic_gt
92 #define ul_cyclic_gt(x,y) \
93         ((sizeof(x)>=sizeof(long long))&&(sizeof(y)>=sizeof(long long))? \
94                 (long long)((unsigned long long)(x)-(unsigned long long)(y))>0: \
95          (sizeof(x)>=sizeof(long))&&(sizeof(y)>=sizeof(long))? \
96                 (long)((unsigned long)(x)-(unsigned long)(y))>0: \
97          (sizeof(x)>=sizeof(int))&&(sizeof(y)>=sizeof(int))? \
98                 (int)((unsigned int)(x)-(unsigned int)(y))>0: \
99          (sizeof(x)>=sizeof(short))&&(sizeof(y)>=sizeof(short))? \
100                 (short)((unsigned short)(x)-(unsigned short)(y))>0: \
101          (signed char)((unsigned char)(x)-(unsigned char)(y))>0 \
102         )
103 #endif /*ul_cyclic_gt*/
104
105 #ifndef ul_cyclic_ge
106 #define ul_cyclic_ge(x,y) \
107         ((sizeof(x)>=sizeof(long long))&&(sizeof(y)>=sizeof(long long))? \
108                 (long long)((unsigned long long)(x)-(unsigned long long)(y))>=0: \
109          (sizeof(x)>=sizeof(long))&&(sizeof(y)>=sizeof(long))? \
110                 (long)((unsigned long)(x)-(unsigned long)(y))>=0: \
111          (sizeof(x)>=sizeof(int))&&(sizeof(y)>=sizeof(int))? \
112                 (int)((unsigned int)(x)-(unsigned int)(y))>=0: \
113          (sizeof(x)>=sizeof(short))&&(sizeof(y)>=sizeof(short))? \
114                 (short)((unsigned short)(x)-(unsigned short)(y))>=0: \
115          (signed char)((unsigned char)(x)-(unsigned char)(y))>=0 \
116         )
117 #endif /*ul_cyclic_ge*/
118
119 /* GNUC neat features */
120
121 #ifdef  __GNUC__
122 #ifndef UL_ATTR_UNUSED
123 #define UL_ATTR_PRINTF( format_idx, arg_idx )   \
124   __attribute__((format (printf, format_idx, arg_idx)))
125 #define UL_ATTR_SCANF( format_idx, arg_idx )    \
126   __attribute__((format (scanf, format_idx, arg_idx)))
127 #define UL_ATTR_FORMAT( arg_idx )               \
128   __attribute__((format_arg (arg_idx)))
129 #define UL_ATTR_NORETURN                        \
130   __attribute__((noreturn))
131 #define UL_ATTR_CONST                           \
132   __attribute__((const))
133 #define UL_ATTR_UNUSED                          \
134   __attribute__((unused))
135 #define UL_ATTR_CONSTRUCTOR                     \
136   __attribute__((constructor))
137 #define UL_ATTR_DESCRUCTOR                      \
138   __attribute__((destructor))
139 #define UL_ATTR_ALWAYS_INLINE                   \
140   __attribute__((always_inline))
141 #define UL_ATTR_WEAK                            \
142   __attribute__((weak))
143 #endif  /*UL_ATTR_UNUSED*/
144 #else   /* !__GNUC__ */
145 #ifndef UL_ATTR_UNUSED
146 #define UL_ATTR_PRINTF( format_idx, arg_idx )
147 #define UL_ATTR_SCANF( format_idx, arg_idx )
148 #define UL_ATTR_FORMAT( arg_idx )
149 #define UL_ATTR_NORETURN
150 #define UL_ATTR_CONST
151 #define UL_ATTR_UNUSED
152 #define UL_ATTR_CONSTRUCTOR
153 #define UL_ATTR_DESCRUCTOR
154 #define UL_ATTR_ALWAYS_INLINE
155 #define UL_ATTR_WEAK
156 #endif  /*UL_ATTR_UNUSED*/
157 #endif  /* !__GNUC__ */
158
159 #ifndef UL_ATTR_REENTRANT
160 #if (!defined(SDCC) && !defined(__SDCC)) || defined(SDCC_z80) || defined(__SDCC_z80)
161   #define UL_ATTR_REENTRANT
162 #else
163   #define UL_ATTR_REENTRANT __reentrant
164 #endif
165 #endif /*UL_ATTR_REENTRANT*/
166
167 /* The cast idea based on libHX by Jan Engelhardt */
168 #define UL_TYPEOF_REFX(ref_asterisks, ptr_type) \
169   typeof(ref_asterisks(union { int z; typeof(ptr_type) x; }){0}.x)
170
171 /* Const and volatile qualifiers removal cast */
172 #ifdef __cplusplus
173 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) \
174             (const_cast<new_type>(expr))
175 #else /* Standard C code */
176 #ifdef __GNUC__
177 #if  ((__GNUC__ * 1000 + __GNUC_MINOR__) >= 4004)
178 extern void* UL_CAST_UNQX_types_not_compatible(void)
179   __attribute__((error ("UL_CAST_UNQX types differ not only by volatile and const")));
180 #else
181 extern void UL_CAST_UNQX_types_not_compatible(void);
182 #endif
183 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ( \
184   __builtin_choose_expr(__builtin_types_compatible_p \
185     (UL_TYPEOF_REFX(ref_asterisks, expr), \
186      UL_TYPEOF_REFX(ref_asterisks, new_type)), \
187     (new_type)(expr), \
188     UL_CAST_UNQX_types_not_compatible() \
189   ) \
190 )
191 #define UL_CAST_UNQX_NULL_ALLOWED(ref_asterisks, new_type, expr) ( \
192   __builtin_choose_expr(!__builtin_types_compatible_p(void *, typeof(expr)), \
193     UL_CAST_UNQX(ref_asterisks, new_type, expr), \
194     expr \
195   ) \
196 )
197 #else /*__GNUC__*/
198 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ((new_type)(expr))
199 #endif /*__GNUC__*/
200 #endif /*__cplusplus*/
201
202 #define UL_CAST_UNQ1(new_type, expr) \
203   UL_CAST_UNQX(*, new_type, expr)
204
205 #define UL_CAST_UNQ2(new_type, expr) \
206   UL_CAST_UNQX(**, new_type, expr)
207
208 #define UL_CAST_UNQ3(new_type, expr) \
209   UL_CAST_UNQX(**, new_type, expr)
210
211 #ifndef UL_CAST_UNQX_NULL_ALLOWED
212 #define UL_CAST_UNQX_NULL_ALLOWED(ref_asterisks, new_type, expr) \
213   UL_CAST_UNQX(ref_asterisks, new_type, expr)
214 #endif /*UL_CAST_UNQX_NULL_ALLOWED*/
215
216 #define UL_CAST_UNQ1_NULL_ALLOWED(new_type, expr) \
217   UL_CAST_UNQX_NULL_ALLOWED(*, new_type, expr)
218
219 #ifdef __cplusplus
220 } /* extern "C"*/
221 #endif
222
223 #endif /* _UL_UTDEFS_H */