All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/8] util: Add L_IN_SET macros
@ 2021-01-06 19:54 Andrew Zaborowski
  2021-01-06 19:54 ` [PATCH 2/8] unit: Add an L_IN_SET test Andrew Zaborowski
                   ` (7 more replies)
  0 siblings, 8 replies; 16+ messages in thread
From: Andrew Zaborowski @ 2021-01-06 19:54 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 1836 bytes --]

This can be used with enum types or ints as in L_IN_SET(-err, EIO, EINVAL).
I'm also adding an L_IN_STRSET() for string comparisons.  This
macro is not equivalent to (x == (val1) || x == (val2) || ...)
because val2 will be evaluated even if (x == (val1)) was true.  There
seems to be no way to avoid that without adding another numbr of macros
dependent on the maximum number of arguments expected.  The "x" in the
above example is evaluated once.

We may have to add a warning about everything being cast to the type of
the "x", I'm not sure how important of a problem this is.
---
 ell/util.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/ell/util.h b/ell/util.h
index 0b3e1a8..11708c2 100644
--- a/ell/util.h
+++ b/ell/util.h
@@ -315,6 +315,26 @@ const char *l_util_get_debugfs_path(void);
        while (__result == -1L && errno == EINTR);  \
        __result; }))
 
+#define _L_IN_SET_CMP(val, type, cmp, ...) __extension__ ({		\
+		const type __v = (val);					\
+		const typeof(__v) __elems[] = {__VA_ARGS__};		\
+		unsigned int __i;					\
+		static const unsigned int __n = L_ARRAY_SIZE(__elems);	\
+		bool __r = false;					\
+		for (__i = 0; __i < __n && !__r; __i++)			\
+			__r = (cmp);					\
+		__r;							\
+	})
+
+/* Warning: evaluates all set elements even after @val has matched one */
+#define L_IN_SET(val, ...)	\
+	_L_IN_SET_CMP((val), __auto_type, __v == __elems[__i], ##__VA_ARGS__)
+
+#define L_IN_STRSET(val, ...)						\
+	_L_IN_SET_CMP((val), const char *, __v == __elems[__i] ||	\
+				(__v && __elems[__i] &&			\
+				 !strcmp(__v, __elems[__i])), ##__VA_ARGS__)
+
 /*
  * Taken from https://github.com/chmike/cst_time_memcmp, adding a volatile to
  * ensure the compiler does not try to optimize the constant time behavior.
-- 
2.27.0

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2021-01-09  2:46 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-06 19:54 [PATCH 1/8] util: Add L_IN_SET macros Andrew Zaborowski
2021-01-06 19:54 ` [PATCH 2/8] unit: Add an L_IN_SET test Andrew Zaborowski
2021-01-06 19:54 ` [PATCH 3/8] pem: Move PKCS private key parsing to cert.c Andrew Zaborowski
2021-01-06 19:54 ` [PATCH 4/8] pkcs5: Rename to cert-crypto Andrew Zaborowski
2021-01-06 19:54 ` [PATCH 5/8] cert: Add l_cert_load_container_file Andrew Zaborowski
2021-01-08  4:37   ` Denis Kenzior
2021-01-09  0:51     ` Andrew Zaborowski
2021-01-09  2:39       ` Denis Kenzior
2021-01-06 19:54 ` [PATCH 6/8] cert: Add PKCS#12 loading support Andrew Zaborowski
2021-01-08  4:59   ` Denis Kenzior
2021-01-09  1:04     ` Andrew Zaborowski
2021-01-09  2:29       ` Denis Kenzior
2021-01-09  2:46         ` Andrew Zaborowski
2021-01-06 19:54 ` [PATCH 7/8] unit: Update tests after l_pkcs5_* renaming Andrew Zaborowski
2021-01-06 19:54 ` [PATCH 8/8] unit: Add l_cert_load_container_file tests Andrew Zaborowski
2021-01-07 20:01 ` [PATCH 1/8] util: Add L_IN_SET macros Denis Kenzior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.