[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.
This commit is contained in:
2026-08-26 20:14:55 -04:00
parent 4afca8e8c5
commit 4398ceec2c
+12 -3
View File
@@ -185,7 +185,16 @@ void *keke_array_find_start(KekeArray *array, void *start, size_t size) {
return NULL; 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; size_t size = *(size_t *) size_v;
#if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) #if defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
return memcmp(a, b, size); 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) { 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) { 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); typedef int (*Rng_fun)(int);