From: Alejandro Colomar Date: Sat, 15 Nov 2025 20:42:56 +0000 (+0100) Subject: lib/search/: Split APIs X-Git-Tag: 4.19.0-rc1~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9eec34396489abe675bdd26a4b278f808baba97d;p=thirdparty%2Fshadow.git lib/search/: Split APIs Have xxx_T() macros that only add type safety to the function they wrap. Have XXX() macros that do more magic; in this case, they deduce the comparison function that is appropriate for the type. This will allow using the xxx_T() macros in more complex cases, where the function can't be deduced just from the type. Signed-off-by: Alejandro Colomar --- diff --git a/lib/search/l/lfind.h b/lib/search/l/lfind.h index acc61ae4c..2206b1551 100644 --- a/lib/search/l/lfind.h +++ b/lib/search/l/lfind.h @@ -14,13 +14,16 @@ #include "search/cmp/cmp.h" -#define LFIND(T, k, a, n) \ +// lfind_T - linear find type-safe +#define lfind_T(T, k, a, n, cmp) \ ({ \ _Generic(k, T *: 0, const T *: 0); \ _Generic(a, T *: 0, const T *: 0); \ - (T *) lfind_(k, a, n, sizeof(T), CMP(T)); \ + (T *) lfind_(k, a, n, sizeof(T), cmp); \ }) +#define LFIND(T, ...) lfind_T(T, __VA_ARGS__, CMP(T)) + inline void *lfind_(const void *k, const void *a, size_t n, size_t ksize, typeof(int (const void *k, const void *elt)) *cmp); diff --git a/lib/search/l/lsearch.h b/lib/search/l/lsearch.h index 57306a517..37c677378 100644 --- a/lib/search/l/lsearch.h +++ b/lib/search/l/lsearch.h @@ -13,12 +13,15 @@ #include "search/cmp/cmp.h" -#define LSEARCH(T, k, a, n) \ +// lsearch_T - linear search-and-insert type-safe +#define lsearch_T(T, k, a, n, cmp) \ ({ \ _Generic(k, T *: 0, const T *: 0); \ _Generic(a, T *: 0); \ - (T *) lsearch(k, a, n, sizeof(T), CMP(T)); \ + (T *) lsearch(k, a, n, sizeof(T), cmp); \ }) +#define LSEARCH(T, ...) lsearch_T(T, __VA_ARGS__, CMP(T)) + #endif // include guard diff --git a/lib/search/sort/qsort.h b/lib/search/sort/qsort.h index 543d60805..ada38a6f5 100644 --- a/lib/search/sort/qsort.h +++ b/lib/search/sort/qsort.h @@ -13,11 +13,14 @@ #include "search/cmp/cmp.h" -#define QSORT(T, a, n) do \ +// qsort_T - sort type-safe +#define qsort_T(T, a, n, cmp) do \ { \ _Generic(a, T *: 0); \ - qsort(a, n, sizeof(T), CMP(T)); \ + qsort(a, n, sizeof(T), cmp); \ } while (0) +#define QSORT(T, ...) qsort_T(T, __VA_ARGS__, CMP(T)) + #endif // include guard