NeoMutt  2023-11-03-85-g512e01
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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
34#if !defined(HAVE_QSORT_S) && !defined(HAVE_QSORT_R)
35#include <assert.h>
37static sort_t GlobalCompar = NULL;
39static void *GlobalData = NULL;
40
49static int relay_compar(const void *a, const void *b)
50{
51 return GlobalCompar(a, b, GlobalData);
52}
53#endif
54
66void mutt_qsort_r(void *base, size_t nmemb, size_t size, sort_t compar, void *sdata)
67{
68#ifdef HAVE_QSORT_S
69 /* FreeBSD 13, where qsort_r had incompatible signature but qsort_s works */
70 qsort_s(base, nmemb, size, compar, sdata);
71#elif defined(HAVE_QSORT_R)
72 /* glibc, POSIX (https://www.austingroupbugs.net/view.php?id=900) */
73 qsort_r(base, nmemb, size, compar, sdata);
74#else
75 /* This fallback is not re-entrant. */
76 assert(!GlobalCompar && !GlobalData);
77 GlobalCompar = compar;
78 GlobalData = sdata;
79 qsort(base, nmemb, size, relay_compar);
80 GlobalCompar = NULL;
81 GlobalData = NULL;
82#endif
83}
static sort_t GlobalCompar
Original comparator in fallback implementation.
Definition: qsort_r.c:37
static int relay_compar(const void *a, const void *b)
Shim to pass context through to real comparator.
Definition: qsort_r.c:49
static void * GlobalData
Original opaque data in fallback implementation.
Definition: qsort_r.c:39
void mutt_qsort_r(void *base, size_t nmemb, size_t size, sort_t compar, void *sdata)
Sort an array, where the comparator has access to opaque data rather than requiring global variables.
Definition: qsort_r.c:66
Context-free sorting function.
int(* sort_t)(const void *a, const void *b, void *sdata)
Definition: qsort_r.h:40