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_NOPSTATEMENT
70 #define UL_NOPSTATEMENT do { } while(0)
74 #define ul_cyclic_gt(x,y) \
75 ((sizeof(x)>=sizeof(long long))&&(sizeof(y)>=sizeof(long long))? \
76 (long long)((unsigned long long)(x)-(unsigned long long)(y))>0: \
77 (sizeof(x)>=sizeof(long))&&(sizeof(y)>=sizeof(long))? \
78 (long)((unsigned long)(x)-(unsigned long)(y))>0: \
79 (sizeof(x)>=sizeof(int))&&(sizeof(y)>=sizeof(int))? \
80 (int)((unsigned int)(x)-(unsigned int)(y))>0: \
81 (sizeof(x)>=sizeof(short))&&(sizeof(y)>=sizeof(short))? \
82 (short)((unsigned short)(x)-(unsigned short)(y))>0: \
83 (signed char)((unsigned char)(x)-(unsigned char)(y))>0 \
85 #endif /*ul_cyclic_gt*/
88 #define ul_cyclic_ge(x,y) \
89 ((sizeof(x)>=sizeof(long long))&&(sizeof(y)>=sizeof(long long))? \
90 (long long)((unsigned long long)(x)-(unsigned long long)(y))>=0: \
91 (sizeof(x)>=sizeof(long))&&(sizeof(y)>=sizeof(long))? \
92 (long)((unsigned long)(x)-(unsigned long)(y))>=0: \
93 (sizeof(x)>=sizeof(int))&&(sizeof(y)>=sizeof(int))? \
94 (int)((unsigned int)(x)-(unsigned int)(y))>=0: \
95 (sizeof(x)>=sizeof(short))&&(sizeof(y)>=sizeof(short))? \
96 (short)((unsigned short)(x)-(unsigned short)(y))>=0: \
97 (signed char)((unsigned char)(x)-(unsigned char)(y))>=0 \
99 #endif /*ul_cyclic_ge*/
101 /* GNUC neat features */
104 #ifndef UL_ATTR_UNUSED
105 #define UL_ATTR_PRINTF( format_idx, arg_idx ) \
106 __attribute__((format (printf, format_idx, arg_idx)))
107 #define UL_ATTR_SCANF( format_idx, arg_idx ) \
108 __attribute__((format (scanf, format_idx, arg_idx)))
109 #define UL_ATTR_FORMAT( arg_idx ) \
110 __attribute__((format_arg (arg_idx)))
111 #define UL_ATTR_NORETURN \
112 __attribute__((noreturn))
113 #define UL_ATTR_CONST \
114 __attribute__((const))
115 #define UL_ATTR_UNUSED \
116 __attribute__((unused))
117 #define UL_ATTR_CONSTRUCTOR \
118 __attribute__((constructor))
119 #define UL_ATTR_DESCRUCTOR \
120 __attribute__((destructor))
121 #define UL_ATTR_ALWAYS_INLINE \
122 __attribute__((always_inline))
123 #define UL_ATTR_WEAK \
124 __attribute__((weak))
125 #endif /*UL_ATTR_UNUSED*/
126 #else /* !__GNUC__ */
127 #ifndef UL_ATTR_UNUSED
128 #define UL_ATTR_PRINTF( format_idx, arg_idx )
129 #define UL_ATTR_SCANF( format_idx, arg_idx )
130 #define UL_ATTR_FORMAT( arg_idx )
131 #define UL_ATTR_NORETURN
132 #define UL_ATTR_CONST
133 #define UL_ATTR_UNUSED
134 #define UL_ATTR_CONSTRUCTOR
135 #define UL_ATTR_DESCRUCTOR
136 #define UL_ATTR_ALWAYS_INLINE
138 #endif /*UL_ATTR_UNUSED*/
139 #endif /* !__GNUC__ */
141 #ifndef UL_ATTR_REENTRANT
142 #if (!defined(SDCC) && !defined(__SDCC)) || defined(SDCC_z80) || defined(__SDCC_z80)
143 #define UL_ATTR_REENTRANT
145 #define UL_ATTR_REENTRANT __reentrant
147 #endif /*UL_ATTR_REENTRANT*/
149 /* The cast idea based on libHX by Jan Engelhardt */
150 #define UL_TYPEOF_REFX(ref_asterisks, ptr_type) \
151 typeof(ref_asterisks(union { int z; typeof(ptr_type) x; }){0}.x)
153 /* Const and volatile qualifiers removal cast */
155 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) \
156 (const_cast<new_type>(expr))
157 #else /* Standard C code */
159 #if ((__GNUC__ * 1000 + __GNUC_MINOR__) >= 4004)
160 extern void* UL_CAST_UNQX_types_not_compatible(void)
161 __attribute__((error ("UL_CAST_UNQX types differ not only by volatile and const")));
163 extern void UL_CAST_UNQX_types_not_compatible(void);
165 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ( \
166 __builtin_choose_expr(__builtin_types_compatible_p \
167 (UL_TYPEOF_REFX(ref_asterisks, expr), \
168 UL_TYPEOF_REFX(ref_asterisks, new_type)), \
170 UL_CAST_UNQX_types_not_compatible() \
173 #define UL_CAST_UNQX_NULL_ALLOWED(ref_asterisks, new_type, expr) ( \
174 __builtin_choose_expr(!__builtin_types_compatible_p(void *, typeof(expr)), \
175 UL_CAST_UNQX(ref_asterisks, new_type, expr), \
180 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ((new_type)(expr))
182 #endif /*__cplusplus*/
184 #define UL_CAST_UNQ1(new_type, expr) \
185 UL_CAST_UNQX(*, new_type, expr)
187 #define UL_CAST_UNQ2(new_type, expr) \
188 UL_CAST_UNQX(**, new_type, expr)
190 #define UL_CAST_UNQ3(new_type, expr) \
191 UL_CAST_UNQX(**, new_type, expr)
193 #ifndef UL_CAST_UNQX_NULL_ALLOWED
194 #define UL_CAST_UNQX_NULL_ALLOWED(ref_asterisks, new_type, expr) \
195 UL_CAST_UNQX(ref_asterisks, new_type, expr)
196 #endif /*UL_CAST_UNQX_NULL_ALLOWED*/
198 #define UL_CAST_UNQ1_NULL_ALLOWED(new_type, expr) \
199 UL_CAST_UNQX_NULL_ALLOWED(*, new_type, expr)
205 #endif /* _UL_UTDEFS_H */