NeoMutt  2022-04-29-247-gc6aae8
Teaching an old dog new tricks
DOXYGEN
qsort_r.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stddef.h>
31#include <stdlib.h>
32#include "qsort_r.h"
33
42typedef int (*qsort_compar_t)(const void *a, const void *b);
43
44#if !defined(HAVE_QSORT_S) && !defined(HAVE_QSORT_R)
45#include <assert.h>
49static void *global_data = NULL;
50
59static int relay_compar(const void *a, const void *b)
60{
61 return global_compar(a, b, global_data);
62}
63#endif
64
76void mutt_qsort_r(void *base, size_t nmemb, size_t size, qsort_r_compar_t compar, void *arg)
77{
78#ifdef HAVE_QSORT_S
79 /* FreeBSD 13, where qsort_r had incompatible signature but qsort_s works */
80 qsort_s(base, nmemb, size, compar, arg);
81#elif defined(HAVE_QSORT_R)
82 /* glibc, POSIX (https://www.austingroupbugs.net/view.php?id=900) */
83 qsort_r(base, nmemb, size, compar, arg);
84#else
85 /* This fallback is not re-entrant. */
86 assert((global_compar == NULL) && (global_data == NULL));
87 global_compar = compar;
88 global_data = arg;
89 qsort(base, nmemb, size, relay_compar);
90 global_compar = NULL;
91 global_data = NULL;
92#endif
93}
static int relay_compar(const void *a, const void *b)
Shim to pass context through to real comparator.
Definition: qsort_r.c:59
static qsort_r_compar_t global_compar
Original comparator in fallback implementation.
Definition: qsort_r.c:47
int(* qsort_compar_t)(const void *a, const void *b)
Prototype for generic comparison function, compatible with qsort()
Definition: qsort_r.c:42
void mutt_qsort_r(void *base, size_t nmemb, size_t size, qsort_r_compar_t compar, void *arg)
Sort an array, where the comparator has access to opaque data rather than requiring global variables.
Definition: qsort_r.c:76
static void * global_data
Original opaque data in fallback implementation.
Definition: qsort_r.c:49
Context-free sorting function.
int(* qsort_r_compar_t)(const void *a, const void *b, void *arg)
Prototype for generic comparison function, compatible with qsort_r()
Definition: qsort_r.h:37