Skip to content

Commit bf7e4b1

Browse files
committed
Use POSIX qsort_r on FreeBSD >= 14
Prior to FreeBSD, qsort_r's arguments were the original BSD style, where the thunk was the first argument to the comparison function. However, the GNU style, where the thunk was the last argument, was eventually adopted as the POSIX standard. FreeBSD switched to this beginning with FreeBSD 14. So when compiling on FreeBSD, we first check to see whether to use the BSD-style or GNU-style qsort_r declaration. This patch is based on noporpoise/sort_r#15
1 parent e0c2a31 commit bf7e4b1

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/neogb/sort_r.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ void sort_r(void *base, size_t nel, size_t width,
4545
#define _SORT_R_INLINE inline
4646

4747
#if (defined __APPLE__ || defined __MACH__ || defined __DARWIN__ || \
48-
defined __FreeBSD__ || defined __DragonFly__)
48+
(defined __FreeBSD__ && !defined(qsort_r)) || defined __DragonFly__)
4949
# define _SORT_R_BSD
50-
#elif (defined __MINGW32__ || defined __GLIBC__)
50+
#elif (defined __MINGW32__ || defined __GLIBC__ || \
51+
(defined (__FreeBSD__) && defined(qsort_r)))
52+
5153
# define _SORT_R_LINUX
5254
#elif (defined _WIN32 || defined _WIN64 || defined __WINDOWS__)
5355
# define _SORT_R_WINDOWS
@@ -282,7 +284,7 @@ static _SORT_R_INLINE void sort_r_simple(void *base, size_t nel, size_t w,
282284
#if defined _SORT_R_LINUX
283285

284286
typedef int(* __compar_d_fn_t)(const void *, const void *, void *);
285-
extern void qsort_r(void *base, size_t nel, size_t width,
287+
extern void (qsort_r)(void *base, size_t nel, size_t width,
286288
__compar_d_fn_t __compar, void *arg)
287289
__attribute__((nonnull (1, 4)));
288290

0 commit comments

Comments
 (0)