[vlc-devel] [PATCH] compat: add qsort_r()
Hugo Beauzée-Luyssen
hugo at beauzee.fr
Tue Nov 20 15:18:52 CET 2018
Hi,
On Tue, Nov 20, 2018, at 2:05 PM, Romain Vimont wrote:
> 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 +++++++
> src/playlist/sort.c | 1 +
> 4 files changed, 56 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..c87c5026c3
> --- /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 struct qsort_r_context
I think 'thread_local' would be a better choice, since vlc_fixups.h makes it available for more platforms
> +{
> + 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 {
> diff --git a/src/playlist/sort.c b/src/playlist/sort.c
> index b44f81a168..ef8f994a16 100644
> --- a/src/playlist/sort.c
> +++ b/src/playlist/sort.c
> @@ -23,6 +23,7 @@
> #endif
>
> #include <vlc_common.h>
> +#include <vlc_fixups.h>
This is already done by config.h
> #include <vlc_rand.h>
> #include "control.h"
> #include "item.h"
> --
> 2.19.1
>
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel
--
Hugo Beauzée-Luyssen
hugo at beauzee.fr
More information about the vlc-devel
mailing list