]> git.sesse.net Git - vlc/blobdiff - include/vlc_common.h
clz: count leading zeroes
[vlc] / include / vlc_common.h
index e500cdaefc3c16a3c360e86da8da42e72835a7b4..29055a7d5168e0ed84a3eedda94b1304a4c45db6 100644 (file)
 # include <stdbool.h>
 #endif
 
+/* Try to fix format strings for all versions of mingw and mingw64 */
+#if defined( _WIN32 ) && defined( __USE_MINGW_ANSI_STDIO )
+ #undef PRId64
+ #define PRId64 "lld"
+ #undef PRIi64
+ #define PRIi64 "lli"
+ #undef PRIu64
+ #define PRIu64 "llu"
+ #undef PRIo64
+ #define PRIo64 "llo"
+ #undef PRIx64
+ #define PRIx64 "llx"
+ #define snprintf        __mingw_snprintf
+ #define vsnprintf       __mingw_vsnprintf
+#endif
+
 /* Format string sanity checks */
 #ifdef __GNUC__
-#   define LIBVLC_FORMAT(x,y) __attribute__ ((format(printf,x,y)))
+#   if defined( _WIN32 ) && (__GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 4 ) )
+#     define LIBVLC_FORMAT(x,y) __attribute__ ((format(gnu_printf,x,y)))
+#   else
+#     define LIBVLC_FORMAT(x,y) __attribute__ ((format(printf,x,y)))
+#   endif
 #   define LIBVLC_FORMAT_ARG(x) __attribute__ ((format_arg(x)))
 #   define LIBVLC_USED __attribute__ ((warn_unused_result))
 #   define LIBVLC_MALLOC __attribute__ ((malloc))
 #   define LIBVLC_MALLOC
 #endif
 
+/* Branch prediction */
+#ifdef __GNUC__
+#   define likely(p)   __builtin_expect(!!(p), 1)
+#   define unlikely(p) __builtin_expect(!!(p), 0)
+#else
+#   define likely(p)   (!!(p))
+#   define unlikely(p) (!!(p))
+#endif
+
 /*****************************************************************************
  * Basic types definitions
  *****************************************************************************/
@@ -183,6 +212,7 @@ typedef struct config_category_t config_category_t;
 typedef struct input_thread_t input_thread_t;
 typedef struct input_thread_sys_t input_thread_sys_t;
 typedef struct input_item_t input_item_t;
+typedef struct input_item_node_t input_item_node_t;
 typedef struct access_t access_t;
 typedef struct access_sys_t access_sys_t;
 typedef struct stream_t     stream_t;
@@ -507,13 +537,9 @@ typedef int ( * vlc_callback_t ) ( vlc_object_t *,      /* variable's object */
     int  i_flags;                                                           \
                                                                             \
     /* Object properties */                                                 \
-    volatile bool b_error;                  /**< set by the object */ \
     volatile bool b_die;                   /**< set by the outside */ \
     bool b_force;      /**< set by the outside (eg. module_need()) */ \
                                                                             \
-    /** Just a reminder so that people don't cast garbage */                \
-    bool be_sure_to_add_VLC_COMMON_MEMBERS_to_struct;                       \
-                                                                            \
     /* Stuff related to the libvlc structure */                             \
     libvlc_int_t *p_libvlc;                  /**< (root of all evil) - 1 */ \
                                                                             \
@@ -523,8 +549,15 @@ typedef int ( * vlc_callback_t ) ( vlc_object_t *,      /* variable's object */
 
 /* VLC_OBJECT: attempt at doing a clever cast */
 #ifdef __GNUC__
-# define VLC_OBJECT( x ) \
-    (((vlc_object_t *)(x))+0*(((__typeof__(x))0)->be_sure_to_add_VLC_COMMON_MEMBERS_to_struct))
+# ifndef __cplusplus
+#  define VLC_OBJECT( x ) \
+    __builtin_choose_expr(__builtin_offsetof(__typeof__(*x), psz_object_type), \
+                          (void)0 /* screw you */, (vlc_object_t *)(x))
+# else
+#  define VLC_OBJECT( x ) \
+    ((vlc_object_t *)(x) \
+      + 0 * __builtin_offsetof(__typeof__(*x), psz_object_type))
+# endif
 #else
 # define VLC_OBJECT( x ) ((vlc_object_t *)(x))
 #endif
@@ -588,6 +621,29 @@ static inline uint8_t clip_uint8_vlc( int32_t a )
     else           return a;
 }
 
+/* Count leading zeroes */
+LIBVLC_USED
+static inline unsigned clz (unsigned x)
+{
+#ifdef __GNUC_
+    return __builtin_clz (x);
+#else
+    unsigned i = sizeof (x) * 8;
+
+    while (x)
+    {
+        x = x >> 1;
+        i--;
+    }
+    return i;
+#endif
+}
+
+#define clz8( x ) (clz(x) - ((sizeof(unsigned) - sizeof (uint8_t)) * 8))
+#define clz16( x ) (clz(x) - ((sizeof(unsigned) - sizeof (uint16_t)) * 8))
+/* XXX: this assumes that int is 32-bits or more */
+#define clz32( x ) (clz(x) - ((sizeof(unsigned) - sizeof (uint32_t)) * 8))
+
 /* Free and set set the variable to NULL */
 #define FREENULL(a) do { free( a ); a = NULL; } while(0)
 
@@ -789,6 +845,8 @@ VLC_EXPORT( int, __vlc_execve, ( vlc_object_t *p_object, int i_argc, char *const
 /* dir wrappers (defined in src/extras/libc.c) */
 VLC_EXPORT(int, vlc_wclosedir, ( void *_p_dir ));
 
+VLC_EXPORT( void, vlc_tdestroy, ( void *, void (*)(void *) ) );
+
 /* Fast large memory copy and memory set */
 VLC_EXPORT( void *, vlc_memcpy, ( void *, const void *, size_t ) );
 VLC_EXPORT( void *, vlc_memset, ( void *, int, size_t ) );
@@ -805,15 +863,32 @@ static inline const char *vlc_pgettext( const char *ctx, const char *id )
     return (tr == ctx) ? id : tr;
 }
 
+/*****************************************************************************
+ * Loosy memory allocation functions. Do not use in new code.
+ *****************************************************************************/
+static inline void *xmalloc (size_t len)
+{
+    void *ptr = malloc (len);
+    if (unlikely (ptr == NULL))
+        abort ();
+    return ptr;
+}
+
+static inline void *xrealloc (void *ptr, size_t len)
+{
+    void *nptr = realloc (ptr, len);
+    if (unlikely (nptr == NULL))
+        abort ();
+    return nptr;
+}
+
 /*****************************************************************************
  * libvlc features
  *****************************************************************************/
 VLC_EXPORT( const char *, VLC_Version, ( void ) LIBVLC_USED );
 VLC_EXPORT( const char *, VLC_CompileBy, ( void ) LIBVLC_USED );
 VLC_EXPORT( const char *, VLC_CompileHost, ( void ) LIBVLC_USED );
-VLC_EXPORT( const char *, VLC_CompileDomain, ( void ) LIBVLC_USED );
 VLC_EXPORT( const char *, VLC_Compiler, ( void ) LIBVLC_USED );
-VLC_EXPORT( const char *, VLC_Error, ( int ) LIBVLC_USED );
 
 /*****************************************************************************
  * Additional vlc stuff