asort.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. * asort - sort an array of elements
  8. * @base: pointer to data to sort
  9. * @num: number of elements
  10. * @cmp: pointer to comparison function
  11. * @ctx: a context pointer for the cmp function
  12. *
  13. * This function does a sort on the given array. The resulting array
  14. * will be in ascending sorted order by the provided comparison function.
  15. *
  16. * The @cmp function should exactly match the type of the @base and
  17. * @ctx arguments. Otherwise it can take three const void *.
  18. */
  19. #define asort(base, num, cmp, ctx) \
  20. _asort((base), (num), sizeof(*(base)), \
  21. typesafe_cb_cast(int (*)(const void *, const void *, void *), \
  22. int (*)(const __typeof__(*(base)) *, \
  23. const __typeof__(*(base)) *, \
  24. __typeof__(ctx)), \
  25. (cmp)), \
  26. (ctx))
  27. #if HAVE_QSORT_R_PRIVATE_LAST
  28. #define _asort(b, n, s, cmp, ctx) qsort_r(b, n, s, cmp, ctx)
  29. #else
  30. void _asort(void *base, size_t nmemb, size_t size,
  31. int(*compar)(const void *, const void *, void *),
  32. void *ctx);
  33. #endif
  34. #endif /* CCAN_ASORT_H */