Skip to content

Commit 08ca420

Browse files
committed
Enable qosrt_r() on more OS's
qsort_r() is now part of POSIX.1-2024. Enable qsort_r() on Android API level 36 AKA Android 16, Hurd (glibc), Haiku, modern FreeBSD 14 versions or newer and modern NetBSD -current or 11 and newer.
1 parent 290e692 commit 08ca420

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

lib/dictBuilder/cover.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
/*-*************************************
2222
* Dependencies
2323
***************************************/
24-
/* qsort_r is an extension.
25-
*
26-
* Android NDK does not ship qsort_r().
27-
*/
28-
#if (defined(__linux__) && !defined(__ANDROID__)) || defined(__CYGWIN__) || defined(__MSYS__)
24+
/* qsort_r is part of POSIX.1-2024, but is an extension with some OS's. */
25+
#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__)
2926
# ifndef _GNU_SOURCE
3027
# define _GNU_SOURCE
3128
# endif
@@ -43,6 +40,10 @@
4340
# define ZDICT_STATIC_LINKING_ONLY
4441
#endif
4542

43+
#if defined(__FreeBSD__) || defined(__NetBSD__)
44+
# include <sys/param.h>
45+
#endif
46+
4647
#include "../common/debug.h" /* DEBUG_STATIC_ASSERT */
4748
#include "../common/mem.h" /* read */
4849
#include "../common/pool.h" /* POOL_ctx */
@@ -70,7 +71,7 @@
7071
*/
7172
#define ZDICT_QSORT_MIN 0
7273
#define ZDICT_QSORT_C90 ZDICT_QSORT_MIN
73-
#define ZDICT_QSORT_GNU 1
74+
#define ZDICT_QSORT_POSIX 1
7475
#define ZDICT_QSORT_APPLE 2
7576
#define ZDICT_QSORT_MSVC 3
7677
#define ZDICT_QSORT_C11 ZDICT_QSORT_MAX
@@ -79,8 +80,11 @@
7980
#ifndef ZDICT_QSORT
8081
# if defined(__APPLE__)
8182
# define ZDICT_QSORT ZDICT_QSORT_APPLE /* uses qsort_r() with a different order for parameters */
82-
# elif (defined(__linux__) && !defined(__ANDROID__)) || defined(__CYGWIN__) || defined(__MSYS__)
83-
# define ZDICT_QSORT ZDICT_QSORT_GNU /* uses qsort_r() */
83+
# elif (defined(__linux__) && (!defined(__ANDROID__) || __ANDROID_API__ >= 36)) || \
84+
defined(__CYGWIN__) || defined(__MSYS__) || defined(__GNU__) || defined(__HAIKU__) || \
85+
(defined(__FreeBSD__) && __FreeBSD_version >= 1400072) || \
86+
(defined(__NetBSD__) && __NetBSD_Version__ >= 1099001300)
87+
# define ZDICT_QSORT ZDICT_QSORT_POSIX /* uses POSIX.1-2024 / GNU qsort_r() */
8488
# elif defined(_WIN32) && defined(_MSC_VER)
8589
# define ZDICT_QSORT ZDICT_QSORT_MSVC /* uses qsort_s() with a different order for parameters */
8690
# elif defined(STDC_LIB_EXT1) && (STDC_LIB_EXT1 > 0) /* C11 Annex K */
@@ -314,7 +318,7 @@ static int COVER_cmp8(COVER_ctx_t *ctx, const void *lp, const void *rp) {
314318
*/
315319
#if (ZDICT_QSORT == ZDICT_QSORT_MSVC) || (ZDICT_QSORT == ZDICT_QSORT_APPLE)
316320
static int WIN_CDECL COVER_strict_cmp(void* g_coverCtx, const void* lp, const void* rp) {
317-
#elif (ZDICT_QSORT == ZDICT_QSORT_GNU) || (ZDICT_QSORT == ZDICT_QSORT_C11)
321+
#elif (ZDICT_QSORT == ZDICT_QSORT_POSIX) || (ZDICT_QSORT == ZDICT_QSORT_C11)
318322
static int COVER_strict_cmp(const void *lp, const void *rp, void *g_coverCtx) {
319323
#else /* C90 fallback.*/
320324
static int COVER_strict_cmp(const void *lp, const void *rp) {
@@ -330,7 +334,7 @@ static int COVER_strict_cmp(const void *lp, const void *rp) {
330334
*/
331335
#if (ZDICT_QSORT == ZDICT_QSORT_MSVC) || (ZDICT_QSORT == ZDICT_QSORT_APPLE)
332336
static int WIN_CDECL COVER_strict_cmp8(void* g_coverCtx, const void* lp, const void* rp) {
333-
#elif (ZDICT_QSORT == ZDICT_QSORT_GNU) || (ZDICT_QSORT == ZDICT_QSORT_C11)
337+
#elif (ZDICT_QSORT == ZDICT_QSORT_POSIX) || (ZDICT_QSORT == ZDICT_QSORT_C11)
334338
static int COVER_strict_cmp8(const void *lp, const void *rp, void *g_coverCtx) {
335339
#else /* C90 fallback.*/
336340
static int COVER_strict_cmp8(const void *lp, const void *rp) {
@@ -354,7 +358,7 @@ static void stableSort(COVER_ctx_t *ctx)
354358
qsort_r(ctx->suffix, ctx->suffixSize, sizeof(U32),
355359
ctx,
356360
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
357-
#elif (ZDICT_QSORT == ZDICT_QSORT_GNU)
361+
#elif (ZDICT_QSORT == ZDICT_QSORT_POSIX)
358362
qsort_r(ctx->suffix, ctx->suffixSize, sizeof(U32),
359363
(ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp),
360364
ctx);

0 commit comments

Comments
 (0)