NeoMutt  2024-04-25-89-g194907
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
qsort_r.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stddef.h>
32#include <stdlib.h>
33#include "mutt/lib.h" // IWYU pragma: keep
34#include "qsort_r.h"
35
36#if !defined(HAVE_QSORT_S) && !defined(HAVE_QSORT_R)
38static sort_t GlobalCompar = NULL;
40static void *GlobalData = NULL;
41
50static int relay_compar(const void *a, const void *b)
51{
52 return GlobalCompar(a, b, GlobalData);
53}
54#endif
55
67void mutt_qsort_r(void *base, size_t nmemb, size_t size, sort_t compar, void *sdata)
68{
69#ifdef HAVE_QSORT_S
70 /* FreeBSD 13, where qsort_r had incompatible signature but qsort_s works */
71 qsort_s(base, nmemb, size, compar, sdata);
72#elif defined(HAVE_QSORT_R)
73 /* glibc, POSIX (https://www.austingroupbugs.net/view.php?id=900) */
74 qsort_r(base, nmemb, size, compar, sdata);
75#else
76 /* This fallback is not re-entrant. */
78 GlobalCompar = compar;
79 GlobalData = sdata;
80 qsort(base, nmemb, size, relay_compar);
81 GlobalCompar = NULL;
82 GlobalData = NULL;
83#endif
84}
Convenience wrapper for the library headers.
static sort_t GlobalCompar
Original comparator in fallback implementation.
Definition: qsort_r.c:38
static int relay_compar(const void *a, const void *b)
Shim to pass context through to real comparator.
Definition: qsort_r.c:50
static void * GlobalData
Original opaque data in fallback implementation.
Definition: qsort_r.c:40
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:67
Context-free sorting function.
int(* sort_t)(const void *a, const void *b, void *sdata)
Definition: qsort_r.h:41
#define ASSERT(COND)
Definition: signal2.h:58