]> git.ozlabs.org Git - ccan/blob - ccan/asort/asort.h
asort: Use qsort_r if the system provides it.
[ccan] / ccan / asort / asort.h
1 #ifndef CCAN_ASORT_H
2 #define CCAN_ASORT_H
3 #include "config.h"
4 #include <ccan/typesafe_cb/typesafe_cb.h>
5 #include <stdlib.h>
6
7 /**
8  * asort - sort an array of elements
9  * @base: pointer to data to sort
10  * @num: number of elements
11  * @cmp: pointer to comparison function
12  * @ctx: a context pointer for the cmp function
13  *
14  * This function does a sort on the given array.  The resulting array
15  * will be in ascending sorted order by the provided comparison function.
16  *
17  * The @cmp function should exactly match the type of the @base and
18  * @ctx arguments.  Otherwise it can take three const void *.
19  */
20 #define asort(base, num, cmp, ctx)                                      \
21 _asort((base), (num), sizeof(*(base)),                                  \
22        cast_if_type(int (*)(const void *, const void *, void *),        \
23                     (cmp), &*(cmp),                                     \
24                     int (*)(const __typeof__(*(base)) *,                \
25                             const __typeof__(*(base)) *,                \
26                             __typeof__(ctx))), (ctx))
27
28 #if HAVE_QSORT_R_PRIVATE_LAST
29 #define _asort(b, n, s, cmp, ctx) qsort_r(b, n, s, cmp, ctx)
30 #else
31 void _asort(void *base, size_t nmemb, size_t size,
32             int(*compar)(const void *, const void *, void *),
33             void *ctx);
34 #endif
35
36 #endif /* CCAN_ASORT_H */