]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/search/: Split APIs
authorAlejandro Colomar <alx@kernel.org>
Sat, 15 Nov 2025 20:42:56 +0000 (21:42 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 5 Dec 2025 14:34:32 +0000 (08:34 -0600)
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 <alx@kernel.org>
lib/search/l/lfind.h
lib/search/l/lsearch.h
lib/search/sort/qsort.h

index acc61ae4c27bcce5a4c312e0891f7646330dd772..2206b1551eaea1d60971e1b9a331108f5e666c2f 100644 (file)
 #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);
index 57306a517d0ea8e9b6d1f8f9725fbc13778fcbe3..37c677378d32156649ac73bace81a23dfc2f151d 100644 (file)
 #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
index 543d608055894fd913e8acb07f888c68ad87cac9..ada38a6f5c6018fb6847b173e9846e9f9823b9d8 100644 (file)
 #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