[vlc-commits] [Git][videolan/vlc][master] 5 commits: vlc_common: add some function attribute defines

Steve Lhomme (@robUx4) gitlab at videolan.org
Fri May 20 05:22:50 UTC 2022



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
072f13a8 by Steve Lhomme at 2022-05-20T05:07:42+00:00
vlc_common: add some function attribute defines

They should exist for gcc and clang.

We could also use __has_c_attribute() and the [[]] C23 notation when available.

- - - - -
4e881071 by Steve Lhomme at 2022-05-20T05:07:42+00:00
vlc_threads: use defines for the builtin checks

- - - - -
0ef98375 by Steve Lhomme at 2022-05-20T05:07:42+00:00
vlc_common: define more attribute based defines using __has_attribute

Leave VLC_DEPRECATED_ENUM, VLC_FORMAT and VLC_FORMAT_ARG for now as they are
more compiler specific.

- - - - -
c9cf3e67 by Steve Lhomme at 2022-05-20T05:07:42+00:00
vlc_thread: check the delay values when __builtin_constant_p is defined

It exists in very old gcc and clang.

VLC_WARN_CALL/VLC_ERROR_CALL may not warn though.

Don't define check_delay/check_deadline otherwise and don't force the
vlc_tick_sleep/vlc_tick_wait calls.

- - - - -
a45953d0 by Steve Lhomme at 2022-05-20T05:07:42+00:00
vlc_thread: ensure thread names are not bigger than 16 bytes

Some platforms may not accept longer names or will truncate it, so we stick to
that size.

It should work with gcc at least. It also works with LLVM14.

- - - - -


8 changed files:

- include/vlc_common.h
- include/vlc_threads.h
- src/darwin/thread.c
- src/freebsd/thread.c
- src/linux/thread.c
- src/os2/thread.c
- src/posix/thread.c
- src/win32/thread.c


Changes:

=====================================
include/vlc_common.h
=====================================
@@ -90,24 +90,34 @@
 #endif
 
 /* Function attributes for compiler warnings */
-#ifdef __GNUC__
-# define VLC_DEPRECATED __attribute__((deprecated))
-# if VLC_GCC_VERSION(6,0)
-#  define VLC_DEPRECATED_ENUM __attribute__((deprecated))
+#if defined __has_attribute
+# if __has_attribute(warning)
+#  define VLC_WARN_CALL(w)  VLC_NOINLINE_FUNC __attribute__((warning((w))))
 # else
-#  define VLC_DEPRECATED_ENUM
+#  define VLC_WARN_CALL(w)
 # endif
 
-# if defined( _WIN32 ) && !defined( __clang__ )
-#  define VLC_FORMAT(x,y) __attribute__ ((format(gnu_printf,x,y)))
+# if __has_attribute(error)
+#  define VLC_ERROR_CALL(e)  VLC_NOINLINE_FUNC __attribute__((error((e))))
 # else
-#  define VLC_FORMAT(x,y) __attribute__ ((format(printf,x,y)))
+#  define VLC_ERROR_CALL(e)
 # endif
-# define VLC_FORMAT_ARG(x) __attribute__ ((format_arg(x)))
-# define VLC_MALLOC __attribute__ ((malloc))
-# define VLC_USED __attribute__ ((warn_unused_result))
 
-#else
+# if __has_attribute(unused)
+#  define VLC_UNUSED_FUNC  __attribute__((unused))
+# else
+#  define VLC_UNUSED_FUNC
+# endif
+
+# if __has_attribute(noinline)
+#  define VLC_NOINLINE_FUNC  __attribute__((noinline))
+# else
+#  define VLC_NOINLINE_FUNC
+# endif
+
+# if __has_attribute(deprecated)
+#  define VLC_DEPRECATED  __attribute__((deprecated))
+# else
 /**
  * Deprecated functions or compound members annotation
  *
@@ -117,8 +127,67 @@
  *
  * Use \ref VLC_DEPRECATED_ENUM instead for enumeration members.
  */
-# define VLC_DEPRECATED
+#  define VLC_DEPRECATED
+# endif
+
+# if __has_attribute(malloc)
+#  define VLC_MALLOC  __attribute__((malloc))
+# else
+/**
+ * Heap allocated result function annotation
+ *
+ * Use this macro to annotate a function that returns a pointer to memory that
+ * cannot alias any other valid pointer.
+ *
+ * This is primarily used for functions that return a pointer to heap-allocated
+ * memory, but it can be used for other applicable purposes.
+ *
+ * \warning Do not use this annotation if the returned pointer can in any way
+ * alias a valid pointer at the time the function exits. This could lead to
+ * very weird memory corruption bugs.
+ */
+#  define VLC_MALLOC
+# endif
+
+# if __has_attribute(warn_unused_result)
+#  define VLC_USED  __attribute__((warn_unused_result))
+# else
+/**
+ * Used result function annotation
+ *
+ * Use this macro to annotate a function whose result must be used.
+ *
+ * There are several cases where this is useful:
+ * - If a function has no side effects (or no useful side effects), such that
+ *   the only useful purpose of calling said function is to obtain its
+ *   return value.
+ * - If ignoring the function return value would lead to a resource leak
+ *   (including but not limited to a memory leak).
+ * - If a function cannot be used correctly without checking its return value.
+ *   For instance, if the function can fail at any time.
+ *
+ * The compiler may warn if the return value of a function call is ignored.
+ */
+#  define VLC_USED
+# endif
+#endif
+
 
+#ifdef __GNUC__
+# if VLC_GCC_VERSION(6,0)
+#  define VLC_DEPRECATED_ENUM __attribute__((deprecated))
+# else
+#  define VLC_DEPRECATED_ENUM
+# endif
+
+# if defined( _WIN32 ) && !defined( __clang__ )
+#  define VLC_FORMAT(x,y) __attribute__ ((format(gnu_printf,x,y)))
+# else
+#  define VLC_FORMAT(x,y) __attribute__ ((format(printf,x,y)))
+# endif
+# define VLC_FORMAT_ARG(x) __attribute__ ((format_arg(x)))
+
+#else
 /**
  * Deprecated enum member annotation
  *
@@ -151,39 +220,6 @@
  * This is primarily intended for localization functions such as gettext().
  */
 # define VLC_FORMAT_ARG(x)
-
-/**
- * Heap allocated result function annotation
- *
- * Use this macro to annotate a function that returns a pointer to memory that
- * cannot alias any other valid pointer.
- *
- * This is primarily used for functions that return a pointer to heap-allocated
- * memory, but it can be used for other applicable purposes.
- *
- * \warning Do not use this annotation if the returned pointer can in any way
- * alias a valid pointer at the time the function exits. This could lead to
- * very weird memory corruption bugs.
- */
-# define VLC_MALLOC
-
-/**
- * Used result function annotation
- *
- * Use this macro to annotate a function whose result must be used.
- *
- * There are several cases where this is useful:
- * - If a function has no side effects (or no useful side effects), such that
- *   the only useful purpose of calling said function is to obtain its
- *   return value.
- * - If ignoring the function return value would lead to a resource leak
- *   (including but not limited to a memory leak).
- * - If a function cannot be used correctly without checking its return value.
- *   For instance, if the function can fail at any time.
- *
- * The compiler may warn if the return value of a function call is ignored.
- */
-# define VLC_USED
 #endif
 
 #if defined (__ELF__) || defined (__MACH__) || defined (__wasm__)


=====================================
include/vlc_threads.h
=====================================
@@ -631,6 +631,20 @@ VLC_API void *vlc_threadvar_get(vlc_threadvar_t);
 VLC_API int vlc_clone(vlc_thread_t *th, void *(*entry)(void *),
                       void *data) VLC_USED;
 
+#if defined(__GNUC__)
+static
+VLC_UNUSED_FUNC
+VLC_WARN_CALL("thread name too big")
+const char * vlc_thread_name_too_big( const char * thread_name )
+{
+    return thread_name;
+}
+
+# define check_name_length( thread_name ) \
+    ((__builtin_constant_p(strlen(thread_name) > 15)) ? \
+        vlc_thread_name_too_big(thread_name) : thread_name)
+#endif
+
 /**
  * Set the thread name of the current thread.
  *
@@ -640,6 +654,9 @@ VLC_API int vlc_clone(vlc_thread_t *th, void *(*entry)(void *),
  *       nul character. If larger, the name will be truncated.
  */
 VLC_API void vlc_thread_set_name(const char *name);
+#if defined(check_name_length)
+# define vlc_thread_set_name(name)  vlc_thread_set_name(check_name_length(name))
+#endif
 
 /**
  * Marks a thread as cancelled.
@@ -765,16 +782,16 @@ VLC_API void vlc_tick_sleep(vlc_tick_t delay);
 #define VLC_HARD_MIN_SLEEP  VLC_TICK_FROM_MS(10)   /* 10 milliseconds = 1 tick at 100Hz */
 #define VLC_SOFT_MIN_SLEEP  VLC_TICK_FROM_SEC(9)   /* 9 seconds */
 
-#if defined (__GNUC__) && !defined (__clang__)
+#if defined(__GNUC__)
+
 /* Linux has 100, 250, 300 or 1000Hz
  *
  * HZ=100 by default on FreeBSD, but some architectures use a 1000Hz timer
  */
 
 static
-__attribute__((unused))
-__attribute__((noinline))
-__attribute__((error("sorry, cannot sleep for such short a time")))
+VLC_UNUSED_FUNC
+VLC_ERROR_CALL("sorry, cannot sleep for such short a time")
 vlc_tick_t impossible_delay( vlc_tick_t delay )
 {
     (void) delay;
@@ -782,9 +799,8 @@ vlc_tick_t impossible_delay( vlc_tick_t delay )
 }
 
 static
-__attribute__((unused))
-__attribute__((noinline))
-__attribute__((warning("use proper event handling instead of short delay")))
+VLC_UNUSED_FUNC
+VLC_WARN_CALL("use proper event handling instead of short delay")
 vlc_tick_t harmful_delay( vlc_tick_t delay )
 {
     return delay;
@@ -800,9 +816,8 @@ vlc_tick_t harmful_delay( vlc_tick_t delay )
            : d))
 
 static
-__attribute__((unused))
-__attribute__((noinline))
-__attribute__((error("deadlines can not be constant")))
+VLC_UNUSED_FUNC
+VLC_ERROR_CALL("deadlines can not be constant")
 vlc_tick_t impossible_deadline( vlc_tick_t deadline )
 {
     return deadline;
@@ -810,13 +825,14 @@ vlc_tick_t impossible_deadline( vlc_tick_t deadline )
 
 # define check_deadline( d ) \
     (__builtin_constant_p(d) ? impossible_deadline(d) : d)
-#else
-# define check_delay(d) (d)
-# define check_deadline(d) (d)
 #endif
 
+#if defined(check_delay)
 #define vlc_tick_sleep(d) vlc_tick_sleep(check_delay(d))
+#endif
+#if defined(check_deadline)
 #define vlc_tick_wait(d) vlc_tick_wait(check_deadline(d))
+#endif
 
 /**
  * \defgroup timer Asynchronous/threaded timers


=====================================
src/darwin/thread.c
=====================================
@@ -25,7 +25,7 @@
 #include <vlc_common.h>
 #include <vlc_threads.h>
 
-void vlc_thread_set_name(const char *name)
+void (vlc_thread_set_name)(const char *name)
 {
     pthread_setname_np(name);
 }


=====================================
src/freebsd/thread.c
=====================================
@@ -45,7 +45,7 @@ unsigned long vlc_thread_id(void)
      return tid;
 }
 
-void vlc_thread_set_name(const char *name)
+void (vlc_thread_set_name)(const char *name)
 {
     pthread_set_name_np(pthread_self(), name);
 }


=====================================
src/linux/thread.c
=====================================
@@ -51,7 +51,7 @@ unsigned long vlc_thread_id(void)
      return tid;
 }
 
-void vlc_thread_set_name(const char *name)
+void (vlc_thread_set_name)(const char *name)
 {
     prctl(PR_SET_NAME, name);
 }


=====================================
src/os2/thread.c
=====================================
@@ -509,7 +509,7 @@ unsigned long vlc_thread_id (void)
     return _gettid();
 }
 
-void vlc_thread_set_name(const char *name)
+void (vlc_thread_set_name)(const char *name)
 {
     VLC_UNUSED(name);
 }


=====================================
src/posix/thread.c
=====================================
@@ -204,7 +204,7 @@ VLC_WEAK unsigned long vlc_thread_id(void)
      return (uintptr_t)(void *)&dummy;
 }
 
-VLC_WEAK void vlc_thread_set_name(const char *name)
+VLC_WEAK void (vlc_thread_set_name)(const char *name)
 {
     VLC_UNUSED(name);
 }


=====================================
src/win32/thread.c
=====================================
@@ -407,7 +407,7 @@ unsigned long vlc_thread_id (void)
     return GetCurrentThreadId ();
 }
 
-void vlc_thread_set_name(const char *name)
+void (vlc_thread_set_name)(const char *name)
 {
     if (SetThreadDescription_)
     {



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/2390f360d663c44a97821444765520b2acf0a91a...a45953d02f9b40eae0799a1a739bc2dd79bd5d34

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/2390f360d663c44a97821444765520b2acf0a91a...a45953d02f9b40eae0799a1a739bc2dd79bd5d34
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list