NeoMutt  2022-04-29-145-g9b6a0e
Teaching an old dog new tricks
DOXYGEN
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.

Typedefs

typedef int(* qsort_compar_t) (const void *a, const void *b)
 Prototype for generic comparison function, compatible with qsort() More...
 

Functions

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

Variables

static qsort_r_compar_t global_compar = NULL
 Original comparator in fallback implementation. More...
 
static void * global_data = NULL
 Original opaque data in fallback implementation. More...
 

Detailed Description

Context-free sorting function.

Authors
  • Eric Blake

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.

Typedef Documentation

◆ qsort_compar_t

typedef int(* qsort_compar_t) (const void *a, const void *b)

Prototype for generic comparison function, compatible with qsort()

Parameters
aFirst item
bSecond item
Return values
<0a precedes b
0a and b are identical
>0b precedes a

Definition at line 42 of 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 59 of file qsort_r.c.

60 {
61  return global_compar(a, b, global_data);
62 }
static qsort_r_compar_t global_compar
Original comparator in fallback implementation.
Definition: qsort_r.c:47
static void * global_data
Original opaque data in fallback implementation.
Definition: qsort_r.c:49
+ Here is the caller graph for this function:

◆ mutt_qsort_r()

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.

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
argOpaque 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 76 of file qsort_r.c.

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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ global_compar

qsort_r_compar_t global_compar = NULL
static

Original comparator in fallback implementation.

Definition at line 47 of file qsort_r.c.

◆ global_data

void* global_data = NULL
static

Original opaque data in fallback implementation.

Definition at line 49 of file qsort_r.c.