portability: add qsort_r

cute-signatures
Timo Teräs 2022-03-21 13:04:36 +02:00
parent c6b9297bcb
commit 925b435faf
3 changed files with 34 additions and 0 deletions

View File

@ -9,6 +9,7 @@ check_functions = [
['strlcpy', 'strlcpy.c', 'NEED_STRLCPY', 'string.h'], ['strlcpy', 'strlcpy.c', 'NEED_STRLCPY', 'string.h'],
['pipe2', 'pipe2.c', 'NEED_PIPE2', 'unistd.h'], ['pipe2', 'pipe2.c', 'NEED_PIPE2', 'unistd.h'],
['mknodat', 'mknodat.c', 'NEED_MKNODAT', 'sys/stat.h'], ['mknodat', 'mknodat.c', 'NEED_MKNODAT', 'sys/stat.h'],
['qsort_r', 'qsort_r.c', 'NEED_QSORT_R', 'stdlib.h'],
] ]

26
portability/qsort_r.c Normal file
View File

@ -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;
}

7
portability/stdlib.h Normal file
View File

@ -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