portability: add qsort_r
parent
c6b9297bcb
commit
925b435faf
|
@ -9,6 +9,7 @@ check_functions = [
|
|||
['strlcpy', 'strlcpy.c', 'NEED_STRLCPY', 'string.h'],
|
||||
['pipe2', 'pipe2.c', 'NEED_PIPE2', 'unistd.h'],
|
||||
['mknodat', 'mknodat.c', 'NEED_MKNODAT', 'sys/stat.h'],
|
||||
['qsort_r', 'qsort_r.c', 'NEED_QSORT_R', 'stdlib.h'],
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
struct qsortr_ctx {
|
||||
int (*compar)(const void *, const void *, void *);
|
||||
void *arg;
|
||||
};
|
||||
|
||||
static __thread struct qsortr_ctx *__ctx;
|
||||
|
||||
static int cmp_wrapper(const void *a, const void *b)
|
||||
{
|
||||
return __ctx->compar(a, b, __ctx->arg);
|
||||
}
|
||||
|
||||
void qsort_r(void *base, size_t nmemb, size_t size,
|
||||
int (*compar)(const void *, const void *, void *),
|
||||
void *arg)
|
||||
{
|
||||
struct qsortr_ctx ctx = {
|
||||
.compar = compar,
|
||||
.arg = arg,
|
||||
};
|
||||
__ctx = &ctx;
|
||||
qsort(base, nmemb, size, cmp_wrapper);
|
||||
__ctx = 0;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
#include_next <stdlib.h>
|
||||
|
||||
#ifdef NEED_QSORT_R
|
||||
void qsort_r(void *base, size_t nmemb, size_t size,
|
||||
int (*compar)(const void *, const void *, void *),
|
||||
void *arg);
|
||||
#endif
|
Loading…
Reference in New Issue