]> rtime.felk.cvut.cz Git - ulut.git/blobdiff - ulut/ul_utdefs.h
Add missing #include
[ulut.git] / ulut / ul_utdefs.h
index 0f45a03612d1a434c408580bd951c5d4a0a4ab4a..aa96537695b45bd8545657e1bdb0e6a54d5acd96 100644 (file)
@@ -9,6 +9,8 @@
 #ifndef _UL_UTDEFS_H
 #define _UL_UTDEFS_H
 
+#include <stddef.h> /* For size_t */
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -17,6 +19,38 @@ extern "C" {
 #define inline _inline
 #endif
 
+#if !defined(UL_BUILD_BUG_ON_MSG_LINE) && defined(__OPTIMIZE__) && \
+  ((__GNUC__ * 1000 + __GNUC_MINOR__) >= 4004)
+#define UL_BUILD_BUG_ON_MSG_LINE_EXP1(condition, msg, line) \
+({ \
+  if (!!(condition)) { \
+    void compile_time_bug_on_line_ ## line (void) __attribute__((error(msg))); \
+    compile_time_bug_on_line_ ## line (); \
+  } \
+})
+#define UL_BUILD_BUG_ON_MSG_LINE(condition, msg, line) \
+  UL_BUILD_BUG_ON_MSG_LINE_EXP1(condition, msg, line)
+#endif /*UL_BUILD_BUG_ON_MSG for GCC*/
+
+#ifndef UL_BUILD_BUG_ON_MSG_LINE
+#define UL_BUILD_BUG_ON_MSG_LINE(condition, msg, line) \
+  ((void)sizeof(char[1 - 2*!!(condition)]))
+#endif /*UL_BUILD_BUG_ON_MSG*/
+
+#ifndef UL_BUILD_BUG_ON_MSG
+#define UL_BUILD_BUG_ON_MSG(condition, msg) \
+  UL_BUILD_BUG_ON_MSG_LINE(condition, msg, __LINE__)
+#endif /*UL_BUILD_BUG_ON_MSG*/
+
+#ifndef UL_BUILD_BUG_ON
+#define UL_BUILD_BUG_ON(condition) \
+  UL_BUILD_BUG_ON_MSG(condition, "Build time check " #condition " failed")
+#endif /*UL_BUILD_BUG_ON*/
+
+#if !defined(UL_OFFSETOF) && defined(__GNUC__) && __GNUC__ >= 4
+#define UL_OFFSETOF(_type, _member) __builtin_offsetof(_type, _member)
+#endif /*UL_OFFSETOF*/
+
 #ifndef UL_OFFSETOF
 /* offset of structure field */
 #define UL_OFFSETOF(_type,_member) \
@@ -34,6 +68,24 @@ extern "C" {
 #endif /*__GNUC__*/
 #endif /*UL_CONTAINEROF*/
 
+#ifndef UL_ALIGNOF_FIELD
+#define        UL_ALIGNOF_FIELD(_type) UL_OFFSETOF(struct {char _fld0; _type _fld1;}, _fld1)
+#endif /*UL_ALIGNOF*/
+
+#ifndef UL_ALIGNOF_TYPE
+#ifdef  __GNUC__
+/* The compiler provided alignment for performance can differ from ABI struct one */
+#define        UL_ALIGNOF_TYPE(_type) __alignof__(_type)
+#else /*__GNUC__*/
+#define UL_ALIGNOF_TYPE UL_ALIGNOF_FIELD
+#endif /*__GNUC__*/
+#endif /*UL_ALIGNOF*/
+
+#ifndef UL_ALIGNOF
+#define        UL_ALIGNOF UL_ALIGNOF_FIELD
+#endif /*UL_ALIGNOF*/
+
+
 #ifndef UL_NOPSTATEMENT
 #define UL_NOPSTATEMENT do { } while(0)
 #endif
@@ -114,6 +166,58 @@ extern "C" {
 #endif
 #endif /*UL_ATTR_REENTRANT*/
 
+/* The cast idea based on libHX by Jan Engelhardt */
+#define UL_TYPEOF_REFX(ref_asterisks, ptr_type) \
+  typeof(ref_asterisks(union { int z; typeof(ptr_type) x; }){0}.x)
+
+/* Const and volatile qualifiers removal cast */
+#ifdef __cplusplus
+#define UL_CAST_UNQX(ref_asterisks, new_type, expr) \
+            (const_cast<new_type>(expr))
+#else /* Standard C code */
+#ifdef __GNUC__
+#if  ((__GNUC__ * 1000 + __GNUC_MINOR__) >= 4004)
+extern void* UL_CAST_UNQX_types_not_compatible(void)
+  __attribute__((error ("UL_CAST_UNQX types differ not only by volatile and const")));
+#else
+extern void UL_CAST_UNQX_types_not_compatible(void);
+#endif
+#define UL_CAST_UNQX(ref_asterisks, new_type, expr) ( \
+  __builtin_choose_expr(__builtin_types_compatible_p \
+    (UL_TYPEOF_REFX(ref_asterisks, expr), \
+     UL_TYPEOF_REFX(ref_asterisks, new_type)), \
+    (new_type)(expr), \
+    UL_CAST_UNQX_types_not_compatible() \
+  ) \
+)
+#define UL_CAST_UNQX_NULL_ALLOWED(ref_asterisks, new_type, expr) ( \
+  __builtin_choose_expr(!__builtin_types_compatible_p(void *, typeof(expr)), \
+    UL_CAST_UNQX(ref_asterisks, new_type, expr), \
+    expr \
+  ) \
+)
+#else /*__GNUC__*/
+#define UL_CAST_UNQX(ref_asterisks, new_type, expr) ((new_type)(expr))
+#endif /*__GNUC__*/
+#endif /*__cplusplus*/
+
+#define UL_CAST_UNQ1(new_type, expr) \
+  UL_CAST_UNQX(*, new_type, expr)
+
+#define UL_CAST_UNQ2(new_type, expr) \
+  UL_CAST_UNQX(**, new_type, expr)
+
+#define UL_CAST_UNQ3(new_type, expr) \
+  UL_CAST_UNQX(**, new_type, expr)
+
+#ifndef UL_CAST_UNQX_NULL_ALLOWED
+#define UL_CAST_UNQX_NULL_ALLOWED(ref_asterisks, new_type, expr) \
+  UL_CAST_UNQX(ref_asterisks, new_type, expr)
+#endif /*UL_CAST_UNQX_NULL_ALLOWED*/
+
+#define UL_CAST_UNQ1_NULL_ALLOWED(new_type, expr) \
+  UL_CAST_UNQX_NULL_ALLOWED(*, new_type, expr)
+
 #ifdef __cplusplus
 } /* extern "C"*/
 #endif