]> git.ozlabs.org Git - ccan/blob - ccan/asort/asort.h
tdb2: test lock timeout plugin code.
[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        typesafe_cb_cast(int (*)(const void *, const void *, void *),    \
23                         int (*)(const __typeof__(*(base)) *,            \
24                                 const __typeof__(*(base)) *,            \
25                                 __typeof__(ctx)),                       \
26                         (cmp)),                                         \
27        (ctx))
28
29 #if HAVE_QSORT_R_PRIVATE_LAST
30 #define _asort(b, n, s, cmp, ctx) qsort_r(b, n, s, cmp, ctx)
31 #else
32 void _asort(void *base, size_t nmemb, size_t size,
33             int(*compar)(const void *, const void *, void *),
34             void *ctx);
35 #endif
36
37 #endif /* CCAN_ASORT_H */