From 4398ceec2c31089df7743d5c67a657f51492cca0 Mon Sep 17 00:00:00 2001 From: Mina Brown Date: Wed, 26 Aug 2026 20:14:55 -0400 Subject: [PATCH] [libkeke_array.c] Use qsort_s on Windows qsort_r is not completely standardized, so we must accomodate for different platforms. The qsort_s function is available on Windows and we can use macros to transparently change behavior when required. --- libkeke_array.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libkeke_array.c b/libkeke_array.c index 6a4227d..935633b 100644 --- a/libkeke_array.c +++ b/libkeke_array.c @@ -185,7 +185,16 @@ void *keke_array_find_start(KekeArray *array, void *start, size_t size) { return NULL; } -int compar(const void *a, const void *b, void *size_v) { +/* Windows uses qsort_s rather than qsort_r and wants a BSD-style comparison function. + * This will transparently switch the comparison function based on platform. */ +#if _WIN32 +#define qsort_r qsort_s +#define compar(a, b, size_v) _compar(size_v, a, b) +int _compar(void *size_v, const void *a, const void *b) { +#else +#define compar(a, b, size_v) _compar(a, b, size_v) +int _compar(const void *a, const void *b, void *size_v) { +#endif size_t size = *(size_t *) size_v; #if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) return memcmp(a, b, size); @@ -219,11 +228,11 @@ int compar(const void *a, const void *b, void *size_v) { } void keke_array_qsort(KekeArray *array) { - qsort_r(array->content, array->len, array->type_size, compar, &array->type_size); + qsort_r(array->content, array->len, array->type_size, _compar, &array->type_size); } void keke_array_qsort_header(KekeArray *array, size_t start_header_size) { - qsort_r(array->content, array->len, array->type_size, compar, &start_header_size); + qsort_r(array->content, array->len, array->type_size, _compar, &start_header_size); } typedef int (*Rng_fun)(int);