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*/
71 #define UL_ALIGNOF(_type) __alignof__(_type)
73 #define UL_ALIGNOF(_type) UL_OFFSETOF(struct {char _fld0; _type _fld1;}, _fld1)
77 #ifndef UL_NOPSTATEMENT
78 #define UL_NOPSTATEMENT do { } while(0)
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 \
93 #endif /*ul_cyclic_gt*/
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 \
107 #endif /*ul_cyclic_ge*/
109 /* GNUC neat features */
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
146 #endif /*UL_ATTR_UNUSED*/
147 #endif /* !__GNUC__ */
149 #ifndef UL_ATTR_REENTRANT
150 #if (!defined(SDCC) && !defined(__SDCC)) || defined(SDCC_z80) || defined(__SDCC_z80)
151 #define UL_ATTR_REENTRANT
153 #define UL_ATTR_REENTRANT __reentrant
155 #endif /*UL_ATTR_REENTRANT*/
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)
161 /* Const and volatile qualifiers removal cast */
163 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) \
164 (const_cast<new_type>(expr))
165 #else /* Standard C code */
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")));
171 extern void UL_CAST_UNQX_types_not_compatible(void);
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)), \
178 UL_CAST_UNQX_types_not_compatible() \
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), \
188 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ((new_type)(expr))
190 #endif /*__cplusplus*/
192 #define UL_CAST_UNQ1(new_type, expr) \
193 UL_CAST_UNQX(*, new_type, expr)
195 #define UL_CAST_UNQ2(new_type, expr) \
196 UL_CAST_UNQX(**, new_type, expr)
198 #define UL_CAST_UNQ3(new_type, expr) \
199 UL_CAST_UNQX(**, new_type, expr)
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*/
206 #define UL_CAST_UNQ1_NULL_ALLOWED(new_type, expr) \
207 UL_CAST_UNQX_NULL_ALLOWED(*, new_type, expr)
213 #endif /* _UL_UTDEFS_H */