list.h: Add iterator macros
authorHugo Landau <hlandau@openssl.org>
Thu, 9 Nov 2023 10:27:13 +0000 (10:27 +0000)
committerHugo Landau <hlandau@openssl.org>
Thu, 21 Dec 2023 08:11:59 +0000 (08:11 +0000)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22674)

include/internal/list.h

index de2a102cd448a2bb63f179d0e5ae541f2f5a2728..902047641f7bb381ba8406db6c54c2c2a4b78ca6 100644 (file)
 #  define OSSL_LIST_DBG(x) x;
 # endif
 
+# define LIST_FOREACH_FROM(p, name, init)                                   \
+    for ((p) = (init);                                                      \
+         (p) != NULL;                                                       \
+         (p) = ossl_list_##name##_next(p))
+# define LIST_FOREACH(p, name, l)                                           \
+    LIST_FOREACH_FROM(p, name, ossl_list_##name##_head(l))
+
+# define LIST_FOREACH_REV_FROM(p, name, init)                               \
+    for ((p) = (init);                                                      \
+         (p) != NULL;                                                       \
+         (p) = ossl_list_##name##_prev(p))
+# define LIST_FOREACH_REV(p, name, l)                                       \
+    LIST_FOREACH_FROM(p, name, ossl_list_##name##_tail(l))
+
+# define LIST_FOREACH_DELSAFE_FROM(p, pn, name, init)                       \
+    for ((p) = (init);                                                      \
+         (p) != NULL && (((pn) = ossl_list_##name##_next(p)), 1);           \
+         (p) = (pn))
+#define LIST_FOREACH_DELSAFE(p, pn, name, l)                                \
+    LIST_FOREACH_DELSAFE_FROM(p, pn, name, ossl_list_##name##_head(l))
+
+# define LIST_FOREACH_REV_DELSAFE_FROM(p, pn, name, init)                   \
+    for ((p) = (init);                                                      \
+         (p) != NULL && (((pn) = ossl_list_##name##_prev(p)), 1);           \
+         (p) = (pn))
+# define LIST_FOREACH_REV_DELSAFE(p, pn, name, l)                           \
+    LIST_FOREACH_REV_DELSAFE_FROM(p, pn, name, ossl_list_##name##_tail(l))
+
 /* Define a list structure */
 # define OSSL_LIST(name) OSSL_LIST_ ## name