[vlc-devel] [PATCH v2] compat: add qsort_r()
Romain Vimont
rom1v at videolabs.io
Tue Nov 20 15:36:41 CET 2018
Add qsort_r() based on qsort() with Thread Local Storage to store the
comparison context.
---
compat/qsort_r.c | 47 ++++++++++++++++++++++++++++++++++++++++++++
configure.ac | 2 +-
include/vlc_fixups.h | 7 +++++++
3 files changed, 55 insertions(+), 1 deletion(-)
create mode 100644 compat/qsort_r.c
diff --git a/compat/qsort_r.c b/compat/qsort_r.c
new file mode 100644
index 0000000000..9519ddafc6
--- /dev/null
+++ b/compat/qsort_r.c
@@ -0,0 +1,47 @@
+/**
+ * @file qsort_r.c
+ * @brief replacement for GNU qsort_r()
+ */
+/*****************************************************************************
+ * Copyright (C) 2018 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 <stdlib.h>
+
+static thread_local struct qsort_r_context
+{
+ int (*compar)(const void *, const void *, void *);
+ void *arg;
+} qsort_r_ctx;
+
+static int qsort_cmp(const void *lhs, const void *rhs)
+{
+ return qsort_r_ctx.compar(lhs, rhs, qsort_r_ctx.arg);
+}
+
+void qsort_r(void *base, size_t nmemb, size_t size,
+ int (*compar)(const void *, const void *, void *),
+ void *arg)
+{
+ qsort_r_ctx.compar = compar;
+ qsort_r_ctx.arg = arg;
+ qsort(base, nmemb, size, qsort_cmp);
+}
diff --git a/configure.ac b/configure.ac
index 8cf88a4ec6..1f33f5dc88 100644
--- a/configure.ac
+++ b/configure.ac
@@ -581,7 +581,7 @@ need_libc=false
dnl Check for usual libc functions
AC_CHECK_FUNCS([accept4 daemon fcntl flock fstatvfs fork getenv getpwuid_r isatty memalign mkostemp mmap open_memstream newlocale openat pipe2 pread posix_fadvise posix_madvise posix_memalign setlocale stricmp strnicmp strptime uselocale])
-AC_REPLACE_FUNCS([aligned_alloc atof atoll dirfd fdopendir flockfile fsync getdelim getpid lfind lldiv memrchr nrand48 poll recvmsg rewind sendmsg setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab tdestroy tfind timegm timespec_get strverscmp pathconf])
+AC_REPLACE_FUNCS([aligned_alloc atof atoll dirfd fdopendir flockfile fsync getdelim getpid lfind lldiv memrchr nrand48 poll qsort_r recvmsg rewind sendmsg setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab tdestroy tfind timegm timespec_get strverscmp pathconf])
AC_REPLACE_FUNCS([gettimeofday])
AC_CHECK_FUNC(fdatasync,,
[AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
index ad8f692271..c7f06063c5 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -468,6 +468,13 @@ struct msghdr;
ssize_t sendmsg(int, const struct msghdr *, int);
#endif
+/* qsort_r */
+#ifndef HAVE_QSORT_R
+void qsort_r(void *base, size_t nmemb, size_t size,
+ int (*compar)(const void *, const void *, void *),
+ void *arg);
+#endif
+
/* search.h */
#ifndef HAVE_SEARCH_H
typedef struct entry {
--
2.19.1
More information about the vlc-devel
mailing list