From: Alejandro Colomar Date: Thu, 14 Nov 2024 14:21:00 +0000 (+0100) Subject: lib/search/l/: LFIND(): Add macro X-Git-Tag: 4.17.3~48 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e7f5f564c5cba53e8826b6c6448e394d9e9fa887;p=thirdparty%2Fshadow.git lib/search/l/: LFIND(): Add macro The use of typeof() for the function pointer argument was suggested by Jorenar. This improves readability of these complex types. Co-authored-by: Martin Uecker Cc: Jorenar Reviewed-by: Serge Hallyn Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index c474e465d..f34325a52 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -157,6 +157,8 @@ libshadow_la_SOURCES = \ salt.c \ search/cmp/cmp.c \ search/cmp/cmp.h \ + search/l/lfind.c \ + search/l/lfind.h \ selinux.c \ semanage.c \ setugid.c \ diff --git a/lib/search/l/lfind.c b/lib/search/l/lfind.c new file mode 100644 index 000000000..b41ea10a0 --- /dev/null +++ b/lib/search/l/lfind.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "search/l/lfind.h" + +#include + + +extern 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/lfind.h b/lib/search/l/lfind.h new file mode 100644 index 000000000..2a6f6f6ef --- /dev/null +++ b/lib/search/l/lfind.h @@ -0,0 +1,44 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_SEARCH_L_LFIND_H_ +#define SHADOW_INCLUDE_LIB_SEARCH_L_LFIND_H_ + + +#include + +#include +#include + +#include "must_be.h" +#include "search/cmp/cmp.h" + +#include + + +#define LFIND(k, a, n) \ +({ \ + __auto_type k_ = k; \ + __auto_type a_ = a; \ + \ + static_assert(is_same_typeof(k_, a_), ""); \ + \ + (typeof(k_)) lfind_(k_, a_, n, sizeof(*k_), CMP(typeof(k_))); \ +}) + + +inline void *lfind_(const void *k, const void *a, size_t n, size_t ksize, + typeof(int (const void *k, const void *elt)) *cmp); + + +inline void * +lfind_(const void *k, const void *a, size_t n, size_t ksize, + typeof(int (const void *k, const void *elt)) *cmp) +{ + // lfind(3) wants a pointer to n for historic reasons. + return lfind(k, a, &n, ksize, cmp); +} + + +#endif // include guard