Use multi-variable iteration helpers to rewrite rculist loops macros.

There is an important behavior change compared with the previous
implementation: When the loop ends normally (i.e: not via "break;"),
the object pointer provided by the user is NULL. This is safer
because it's not guaranteed that it would end up pointing a valid
address.

Signed-off-by: Adrian Moreno <amore...@redhat.com>
---
 lib/rculist.h | 61 +++++++++++++++++++++++++++------------------------
 1 file changed, 32 insertions(+), 29 deletions(-)

diff --git a/lib/rculist.h b/lib/rculist.h
index 1072b87af..01873700f 100644
--- a/lib/rculist.h
+++ b/lib/rculist.h
@@ -366,34 +366,37 @@ rculist_is_singleton_protected(const struct rculist *list)
 }
 
 #define RCULIST_FOR_EACH(ITER, MEMBER, RCULIST)                         \
-    for (INIT_CONTAINER(ITER, rculist_next(RCULIST), MEMBER);           \
-         &(ITER)->MEMBER != (RCULIST);                                  \
-         ASSIGN_CONTAINER(ITER, rculist_next(&(ITER)->MEMBER), MEMBER))
-#define RCULIST_FOR_EACH_CONTINUE(ITER, MEMBER, RCULIST)                \
-    for (ASSIGN_CONTAINER(ITER, rculist_next(&(ITER)->MEMBER), MEMBER); \
-         &(ITER)->MEMBER != (RCULIST);                                  \
-         ASSIGN_CONTAINER(ITER, rculist_next(&(ITER)->MEMBER), MEMBER))
-
-#define RCULIST_FOR_EACH_REVERSE_PROTECTED(ITER, MEMBER, RCULIST)       \
-    for (INIT_CONTAINER(ITER, (RCULIST)->prev, MEMBER);                 \
-         &(ITER)->MEMBER != (RCULIST);                                  \
-         ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER))
-#define RCULIST_FOR_EACH_REVERSE_PROTECTED_CONTINUE(ITER, MEMBER, RCULIST) \
-    for (ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER);           \
-         &(ITER)->MEMBER != (RCULIST);                                  \
-         ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER))
-
-#define RCULIST_FOR_EACH_PROTECTED(ITER, MEMBER, RCULIST)               \
-    for (INIT_CONTAINER(ITER, rculist_next_protected(RCULIST), MEMBER); \
-         &(ITER)->MEMBER != (RCULIST);                                  \
-         ASSIGN_CONTAINER(ITER, rculist_next_protected(&(ITER)->MEMBER), \
-                          MEMBER))
-
-#define RCULIST_FOR_EACH_SAFE_PROTECTED(ITER, NEXT, MEMBER, RCULIST)    \
-    for (INIT_CONTAINER(ITER, rculist_next_protected(RCULIST), MEMBER); \
-         (&(ITER)->MEMBER != (RCULIST)                                  \
-          ? INIT_CONTAINER(NEXT, rculist_next_protected(&(ITER)->MEMBER), \
-                           MEMBER), 1 : 0);                             \
-         (ITER) = (NEXT))
+    for (INIT_MULTIVAR(ITER, MEMBER, rculist_next(RCULIST));            \
+         CONDITION_MULTIVAR(ITER_VAR(ITER) != (RCULIST), ITER, MEMBER); \
+         UPDATE_MULTIVAR(ITER_VAR(ITER) = rculist_next(ITER_VAR(ITER)), \
+                        ITER))
+
+#define RCULIST_FOR_EACH_CONTINUE(ITER, MEMBER, RCULIST)                      \
+    for (INIT_MULTIVAR(ITER, MEMBER, rculist_next(&(ITER)->MEMBER));          \
+         CONDITION_MULTIVAR(ITER_VAR(ITER) != (RCULIST), ITER, MEMBER);       \
+         UPDATE_MULTIVAR(ITER_VAR(ITER) = rculist_next(ITER_VAR(ITER)),       \
+                        ITER))
+
+#define RCULIST_FOR_EACH_REVERSE_PROTECTED(ITER, MEMBER, RCULIST)          \
+    for (INIT_MULTIVAR(ITER, MEMBER, (RCULIST)->prev);                     \
+         CONDITION_MULTIVAR(ITER_VAR(ITER) != (RCULIST), ITER, MEMBER);    \
+         UPDATE_MULTIVAR(ITER_VAR(ITER) = ITER_VAR(VAR).prev, ITER))
+
+#define RCULIST_FOR_EACH_REVERSE_PROTECTED_CONTINUE(ITER, MEMBER, RCULIST)    \
+    for (INIT_MULTIVAR(ITER, MEMBER, (ITER)->MEMBER.prev);                    \
+         CONDITION_MULTIVAR(ITER_VAR(ITER) != (RCULIST), ITER, MEMBER);       \
+         UPDATE_MULTIVAR(ITER_VAR(ITER) = ITER_VAR(VAR).prev, ITER))
+
+#define RCULIST_FOR_EACH_PROTECTED(ITER, MEMBER, RCULIST)                 \
+    for (INIT_MULTIVAR(ITER, MEMBER, rculist_next_protected(RCULIST));    \
+         CONDITION_MULTIVAR(ITER_VAR(ITER) != (RCULIST), ITER, MEMBER);   \
+         UPDATE_MULTIVAR(rculist_next_protected(ITER_VAR(ITER), ITER))    \
+
+#define RCULIST_FOR_EACH_SAFE_PROTECTED(ITER, MEMBER, RCULIST)                \
+    for (INIT_MULTIVAR_SAFE(ITER, MEMBER, rculist_next_protected(RCULIST));   \
+         CONDITION_MULTIVAR_SAFE(ITER_VAR(ITER) != (RCULIST),                 \
+             ITER_NEXT_VAR(ITER) = rculist_next_protected(ITER_VAR(VAR)),     \
+             ITER, MEMBER);                                                   \
+        UPDATE_MULTIVAR(ITER))
 
 #endif /* rculist.h */
-- 
2.34.1

_______________________________________________
dev mailing list
d...@openvswitch.org
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to