[vlc-devel] [PATCH V3 4/7] posix: implement vlc_qsort with POSIX or FREEBSD qsort_r

Thomas Guillem thomas at gllm.fr
Tue Jan 22 14:30:08 CET 2019


---
 src/Makefile.am   |  2 ++
 src/posix/utils.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
 create mode 100644 src/posix/utils.c

diff --git a/src/Makefile.am b/src/Makefile.am
index a98f5fdac1..11a3f16597 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -425,6 +425,8 @@ libvlccore_la_SOURCES += \
 	os2/rand.c \
 	os2/thread.c
 else
+# All except OS2/WIN32
+libvlccore_la_SOURCES += posix/utils.c
 if HAVE_NACL
 libvlccore_la_SOURCES += \
 	android/error.c \
diff --git a/src/posix/utils.c b/src/posix/utils.c
new file mode 100644
index 0000000000..1fe908bae4
--- /dev/null
+++ b/src/posix/utils.c
@@ -0,0 +1,63 @@
+/******************************************************************************
+ * utils.c: POSIX utils back-end
+ ******************************************************************************
+ * Copyright © 2019 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <vlc_common.h>
+#include <vlc_utils.h>
+
+#if HAVE_BROKEN_QSORT_R
+
+struct compat_arg
+{
+    int (*compar)(const void *, const void *, void *);
+    void *arg;
+};
+
+static int compar_compat(void *arg, const void *a, const void *b)
+{
+    struct compat_arg *compat_arg = arg;
+    return compat_arg->compar(a, b, compat_arg->arg);
+}
+
+/* Follow the FreeBSD prototype */
+void vlc_qsort(void *base, size_t nmemb, size_t size,
+               int (*compar)(const void *, const void *, void *),
+               void *arg)
+{
+    struct compat_arg compat_arg = {
+        .compar = compar,
+        .arg = arg
+    };
+    qsort_r(base, nmemb, size, &compat_arg, compar_compat);
+}
+
+#else
+
+/* Follow the POSIX prototype */
+void vlc_qsort(void *base, size_t nmemb, size_t size,
+               int (*compar)(const void *, const void *, void *),
+               void *arg)
+{
+    qsort_r(base, nmemb, size, compar, arg);
+}
+#endif
-- 
2.20.1



More information about the vlc-devel mailing list