1 /*******************************************************************
2 uLan Utilities Library - C library of basic reusable constructions
4 ul_utdefs.h - common defines used in uLan utilities library
6 *******************************************************************/
16 #if defined(_WIN32)&&defined(_MSC_VER)&&!defined(inline)
17 #define inline _inline
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) \
24 if (!!(condition)) { \
25 void compile_time_bug_on_line_ ## line (void) __attribute__((error(msg))); \
26 compile_time_bug_on_line_ ## line (); \
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*/
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*/
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*/
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*/
48 #if !defined(UL_OFFSETOF) && defined(__GNUC__) && __GNUC__ >= 4
49 #define UL_OFFSETOF(_type, _member) __builtin_offsetof(_type, _member)
50 #endif /*UL_OFFSETOF*/
53 /* offset of structure field */
54 #define UL_OFFSETOF(_type,_member) \
55 ((size_t)&(((_type*)0)->_member))
58 #ifndef UL_CONTAINEROF
60 #define UL_CONTAINEROF(_ptr, _type, _member) ({ \
61 const typeof( ((_type *)0)->_member ) *__mptr = (_ptr); \
62 (_type *)( (char *)__mptr - UL_OFFSETOF(_type,_member) );})
64 #define UL_CONTAINEROF(_ptr, _type, _member) \
65 ((_type *)( (char *)_ptr - UL_OFFSETOF(_type,_member)))
67 #endif /*UL_CONTAINEROF*/
69 #ifndef UL_ALIGNOF_FIELD
70 #define UL_ALIGNOF_FIELD(_type) UL_OFFSETOF(struct {char _fld0; _type _fld1;}, _fld1)
73 #ifndef UL_ALIGNOF_TYPE
75 /* The compiler provided alignment for performance can differ from ABI struct one */
76 #define UL_ALIGNOF_TYPE(_type) __alignof__(_type)
78 #define UL_ALIGNOF_TYPE UL_ALIGNOF_FIELD
83 #define UL_ALIGNOF UL_ALIGNOF_FIELD
87 #ifndef UL_NOPSTATEMENT
88 #define UL_NOPSTATEMENT do { } while(0)
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 \
103 #endif /*ul_cyclic_gt*/
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 \
117 #endif /*ul_cyclic_ge*/
119 /* GNUC neat features */
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
156 #endif /*UL_ATTR_UNUSED*/
157 #endif /* !__GNUC__ */
159 #ifndef UL_ATTR_REENTRANT
160 #if (!defined(SDCC) && !defined(__SDCC)) || defined(SDCC_z80) || defined(__SDCC_z80)
161 #define UL_ATTR_REENTRANT
163 #define UL_ATTR_REENTRANT __reentrant
165 #endif /*UL_ATTR_REENTRANT*/
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)
171 /* Const and volatile qualifiers removal cast */
173 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) \
174 (const_cast<new_type>(expr))
175 #else /* Standard C code */
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")));
181 extern void UL_CAST_UNQX_types_not_compatible(void);
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)), \
188 UL_CAST_UNQX_types_not_compatible() \
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), \
198 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ((new_type)(expr))
200 #endif /*__cplusplus*/
202 #define UL_CAST_UNQ1(new_type, expr) \
203 UL_CAST_UNQX(*, new_type, expr)
205 #define UL_CAST_UNQ2(new_type, expr) \
206 UL_CAST_UNQX(**, new_type, expr)
208 #define UL_CAST_UNQ3(new_type, expr) \
209 UL_CAST_UNQX(**, new_type, expr)
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*/
216 #define UL_CAST_UNQ1_NULL_ALLOWED(new_type, expr) \
217 UL_CAST_UNQX_NULL_ALLOWED(*, new_type, expr)
223 #endif /* _UL_UTDEFS_H */