]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_utdefs.h
a85614e57ad7cb9e0c3bd4ef8df2d44d540b3b9d
[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
70 #ifdef  __GNUC__
71 #define UL_ALIGNOF(_type) __alignof__(_type)
72 #else /*__GNUC__*/
73 #define UL_ALIGNOF(_type) UL_OFFSETOF(struct {char _fld0; _type _fld1;}, _fld1)
74 #endif /*__GNUC__*/
75 #endif /*UL_ALIGNOF*/
76
77 #ifndef UL_NOPSTATEMENT
78 #define UL_NOPSTATEMENT do { } while(0)
79 #endif
80
81 #ifndef ul_cyclic_gt
82 #define ul_cyclic_gt(x,y) \
83         ((sizeof(x)>=sizeof(long long))&&(sizeof(y)>=sizeof(long long))? \
84                 (long long)((unsigned long long)(x)-(unsigned long long)(y))>0: \
85          (sizeof(x)>=sizeof(long))&&(sizeof(y)>=sizeof(long))? \
86                 (long)((unsigned long)(x)-(unsigned long)(y))>0: \
87          (sizeof(x)>=sizeof(int))&&(sizeof(y)>=sizeof(int))? \
88                 (int)((unsigned int)(x)-(unsigned int)(y))>0: \
89          (sizeof(x)>=sizeof(short))&&(sizeof(y)>=sizeof(short))? \
90                 (short)((unsigned short)(x)-(unsigned short)(y))>0: \
91          (signed char)((unsigned char)(x)-(unsigned char)(y))>0 \
92         )
93 #endif /*ul_cyclic_gt*/
94
95 #ifndef ul_cyclic_ge
96 #define ul_cyclic_ge(x,y) \
97         ((sizeof(x)>=sizeof(long long))&&(sizeof(y)>=sizeof(long long))? \
98                 (long long)((unsigned long long)(x)-(unsigned long long)(y))>=0: \
99          (sizeof(x)>=sizeof(long))&&(sizeof(y)>=sizeof(long))? \
100                 (long)((unsigned long)(x)-(unsigned long)(y))>=0: \
101          (sizeof(x)>=sizeof(int))&&(sizeof(y)>=sizeof(int))? \
102                 (int)((unsigned int)(x)-(unsigned int)(y))>=0: \
103          (sizeof(x)>=sizeof(short))&&(sizeof(y)>=sizeof(short))? \
104                 (short)((unsigned short)(x)-(unsigned short)(y))>=0: \
105          (signed char)((unsigned char)(x)-(unsigned char)(y))>=0 \
106         )
107 #endif /*ul_cyclic_ge*/
108
109 /* GNUC neat features */
110
111 #ifdef  __GNUC__
112 #ifndef UL_ATTR_UNUSED
113 #define UL_ATTR_PRINTF( format_idx, arg_idx )   \
114   __attribute__((format (printf, format_idx, arg_idx)))
115 #define UL_ATTR_SCANF( format_idx, arg_idx )    \
116   __attribute__((format (scanf, format_idx, arg_idx)))
117 #define UL_ATTR_FORMAT( arg_idx )               \
118   __attribute__((format_arg (arg_idx)))
119 #define UL_ATTR_NORETURN                        \
120   __attribute__((noreturn))
121 #define UL_ATTR_CONST                           \
122   __attribute__((const))
123 #define UL_ATTR_UNUSED                          \
124   __attribute__((unused))
125 #define UL_ATTR_CONSTRUCTOR                     \
126   __attribute__((constructor))
127 #define UL_ATTR_DESCRUCTOR                      \
128   __attribute__((destructor))
129 #define UL_ATTR_ALWAYS_INLINE                   \
130   __attribute__((always_inline))
131 #define UL_ATTR_WEAK                            \
132   __attribute__((weak))
133 #endif  /*UL_ATTR_UNUSED*/
134 #else   /* !__GNUC__ */
135 #ifndef UL_ATTR_UNUSED
136 #define UL_ATTR_PRINTF( format_idx, arg_idx )
137 #define UL_ATTR_SCANF( format_idx, arg_idx )
138 #define UL_ATTR_FORMAT( arg_idx )
139 #define UL_ATTR_NORETURN
140 #define UL_ATTR_CONST
141 #define UL_ATTR_UNUSED
142 #define UL_ATTR_CONSTRUCTOR
143 #define UL_ATTR_DESCRUCTOR
144 #define UL_ATTR_ALWAYS_INLINE
145 #define UL_ATTR_WEAK
146 #endif  /*UL_ATTR_UNUSED*/
147 #endif  /* !__GNUC__ */
148
149 #ifndef UL_ATTR_REENTRANT
150 #if (!defined(SDCC) && !defined(__SDCC)) || defined(SDCC_z80) || defined(__SDCC_z80)
151   #define UL_ATTR_REENTRANT
152 #else
153   #define UL_ATTR_REENTRANT __reentrant
154 #endif
155 #endif /*UL_ATTR_REENTRANT*/
156
157 /* The cast idea based on libHX by Jan Engelhardt */
158 #define UL_TYPEOF_REFX(ref_asterisks, ptr_type) \
159   typeof(ref_asterisks(union { int z; typeof(ptr_type) x; }){0}.x)
160
161 /* Const and volatile qualifiers removal cast */
162 #ifdef __cplusplus
163 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) \
164             (const_cast<new_type>(expr))
165 #else /* Standard C code */
166 #ifdef __GNUC__
167 #if  ((__GNUC__ * 1000 + __GNUC_MINOR__) >= 4004)
168 extern void* UL_CAST_UNQX_types_not_compatible(void)
169   __attribute__((error ("UL_CAST_UNQX types differ not only by volatile and const")));
170 #else
171 extern void UL_CAST_UNQX_types_not_compatible(void);
172 #endif
173 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ( \
174   __builtin_choose_expr(__builtin_types_compatible_p \
175     (UL_TYPEOF_REFX(ref_asterisks, expr), \
176      UL_TYPEOF_REFX(ref_asterisks, new_type)), \
177     (new_type)(expr), \
178     UL_CAST_UNQX_types_not_compatible() \
179   ) \
180 )
181 #define UL_CAST_UNQX_NULL_ALLOWED(ref_asterisks, new_type, expr) ( \
182   __builtin_choose_expr(!__builtin_types_compatible_p(void *, typeof(expr)), \
183     UL_CAST_UNQX(ref_asterisks, new_type, expr), \
184     expr \
185   ) \
186 )
187 #else /*__GNUC__*/
188 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ((new_type)(expr))
189 #endif /*__GNUC__*/
190 #endif /*__cplusplus*/
191
192 #define UL_CAST_UNQ1(new_type, expr) \
193   UL_CAST_UNQX(*, new_type, expr)
194
195 #define UL_CAST_UNQ2(new_type, expr) \
196   UL_CAST_UNQX(**, new_type, expr)
197
198 #define UL_CAST_UNQ3(new_type, expr) \
199   UL_CAST_UNQX(**, new_type, expr)
200
201 #ifndef UL_CAST_UNQX_NULL_ALLOWED
202 #define UL_CAST_UNQX_NULL_ALLOWED(ref_asterisks, new_type, expr) \
203   UL_CAST_UNQX(ref_asterisks, new_type, expr)
204 #endif /*UL_CAST_UNQX_NULL_ALLOWED*/
205
206 #define UL_CAST_UNQ1_NULL_ALLOWED(new_type, expr) \
207   UL_CAST_UNQX_NULL_ALLOWED(*, new_type, expr)
208
209 #ifdef __cplusplus
210 } /* extern "C"*/
211 #endif
212
213 #endif /* _UL_UTDEFS_H */