src/array_list.c

changeset 1695
d21cec66facc
parent 1684
89a07d18f86c
--- a/src/array_list.c	Wed Dec 31 16:01:08 2025 +0100
+++ b/src/array_list.c	Wed Dec 31 16:05:38 2025 +0100
@@ -370,11 +370,31 @@
 }
 #endif
 
+#if defined(WITH_QSORT_R) && defined(__APPLE__)
+// macOS uses a different comparefunc signature for qsort_r
+typedef struct QsortCmpFuncWrapper {
+    cx_compare_func2 fn;
+    void *context;
+} QsortCmpFuncWrapper;
+
+static int sort_comparefunc(void *context, const void *left, const void *right){
+    QsortCmpFuncWrapper *w = context;
+    return w->fn(left, right, w->context);
+}
+#endif
+
 void cx_array_qsort_c(void *array, size_t nmemb, size_t size,
         cx_compare_func2 fn, void *context) {
 #ifdef WITH_QSORT_R
+#ifndef __APPLE__
     qsort_r(array, nmemb, size, fn, context);
 #else
+    QsortCmpFuncWrapper wrapper;
+    wrapper.fn = fn;
+    wrapper.context = context;
+    qsort_r(array, nmemb, size, &wrapper, sort_comparefunc);
+#endif
+#else
     cx_array_fn_for_qsort = fn;
     cx_array_context_for_qsort = context;
     qsort(array, nmemb, size, cx_array_qsort_wrapper);

mercurial