--- a/src/array_list.c Wed Dec 17 20:13:08 2025 +0100 +++ b/src/array_list.c Thu Dec 18 12:11:30 2025 +0100 @@ -26,6 +26,10 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#ifdef WITH_MEMRCHR +#define _GNU_SOURCE +#endif + #include "cx/array_list.h" #include "cx/compare.h" #include <assert.h> @@ -349,6 +353,25 @@ n, allow_duplicates, cx_acmp_wrap, &wrapper); } +#ifndef WITH_QSORT_R +static thread_local cx_compare_func2 cx_array_fn_for_qsort; +static thread_local void *cx_array_context_for_qsort; +static int cx_array_qsort_wrapper(const void *l, const void *r) { + return cx_array_fn_for_qsort(l, r, cx_array_context_for_qsort); +} +#endif + +void cx_array_qsort_c(void *array, size_t nmemb, size_t size, + cx_compare_func2 fn, void *context) { +#ifdef WITH_QSORT_R + qsort_r(array, nmemb, size, fn, context); +#else + cx_array_fn_for_qsort = fn; + cx_array_context_for_qsort = context; + qsort(array, nmemb, size, cx_array_qsort_wrapper); +#endif +} + CxIterator cx_array_iterator_(CxArray *array, size_t elem_size) { return cxIterator(array->data, elem_size, array->size); } @@ -849,19 +872,12 @@ return list->collection.size; } -// TODO: remove this hack once we have a solution for qsort() / qsort_s() -static _Thread_local CxList *cx_hack_for_qsort_list; -static int cx_hack_cmp_for_qsort(const void *l, const void *r) { - return cx_list_compare_wrapper(l, r, cx_hack_for_qsort_list); -} - static void cx_arl_sort(struct cx_list_s *list) { - // TODO: think about if we can somehow use qsort()_s - cx_hack_for_qsort_list = list; - qsort(((cx_array_list *) list)->data, + cx_array_qsort_c(((cx_array_list *) list)->data, list->collection.size, list->collection.elem_size, - cx_hack_cmp_for_qsort + cx_list_compare_wrapper, + list ); }