NeoMutt  2024-02-01-35-geee02f
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
qsort_r.c File Reference

Context-free sorting function. More...

#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include "qsort_r.h"
#include <assert.h>
+ Include dependency graph for qsort_r.c:

Go to the source code of this file.

Functions

static int relay_compar (const void *a, const void *b)
 Shim to pass context through to real comparator.
 
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.
 

Variables

static sort_t GlobalCompar = NULL
 Original comparator in fallback implementation.
 
static void * GlobalData = NULL
 Original opaque data in fallback implementation.
 

Detailed Description

Context-free sorting function.

Authors
  • Eric Blake
  • Richard Russon

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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file qsort_r.c.

Function Documentation

◆ relay_compar()

static int relay_compar ( const void *  a,
const void *  b 
)
static

Shim to pass context through to real comparator.

Parameters
aFirst item to be compared
bSecond item to be compared
Return values
<0a sorts before b
0a and b sort equally (sort stability not guaranteed)
>0a sorts after b

Definition at line 50 of file qsort_r.c.

51{
52 return GlobalCompar(a, b, GlobalData);
53}
static sort_t GlobalCompar
Original comparator in fallback implementation.
Definition: qsort_r.c:38
static void * GlobalData
Original opaque data in fallback implementation.
Definition: qsort_r.c:40
+ Here is the caller graph for this function:

◆ mutt_qsort_r()

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.

Parameters
baseStart of the array to be sorted
nmembNumber of elements in the array
sizeSize of each array element
comparComparison function, return <0/0/>0 to compare two elements
sdataOpaque argument to pass to compar
Note
This implementation might not be re-entrant: signal handlers and compar must not call mutt_qsort_r().

Definition at line 67 of file qsort_r.c.

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. */
77 assert(!GlobalCompar && !GlobalData);
78 GlobalCompar = compar;
79 GlobalData = sdata;
80 qsort(base, nmemb, size, relay_compar);
81 GlobalCompar = NULL;
82 GlobalData = NULL;
83#endif
84}
static int relay_compar(const void *a, const void *b)
Shim to pass context through to real comparator.
Definition: qsort_r.c:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ GlobalCompar

sort_t GlobalCompar = NULL
static

Original comparator in fallback implementation.

Definition at line 38 of file qsort_r.c.

◆ GlobalData

void* GlobalData = NULL
static

Original opaque data in fallback implementation.

Definition at line 40 of file qsort_r.c.