[ovs-dev] [shadow 2/8] cmap: Allow CMAP_FOR_EACH to be nested without shadowing variables.

Justin Pettit jpettit at ovn.org
Wed Feb 28 08:00:57 UTC 2018


Signed-off-by: Justin Pettit <jpettit at ovn.org>
---
 lib/cmap.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/lib/cmap.h b/lib/cmap.h
index 5a72b65bec5e..2427a6293b64 100644
--- a/lib/cmap.h
+++ b/lib/cmap.h
@@ -233,11 +233,20 @@ struct cmap_cursor {
 struct cmap_cursor cmap_cursor_start(const struct cmap *);
 void cmap_cursor_advance(struct cmap_cursor *);
 
-#define CMAP_FOR_EACH(NODE, MEMBER, CMAP)                       \
-    for (struct cmap_cursor cursor__ = cmap_cursor_start(CMAP); \
-         CMAP_CURSOR_FOR_EACH__(NODE, &cursor__, MEMBER);       \
+/* Generate a unique name for the cursor with the __COUNTER__ macro to
+ * allow nesting of CMAP_FOR_EACH loops. */
+#define CURSOR_JOIN2(x,y) x##y
+#define CURSOR_JOIN(x, y) CURSOR_JOIN2(x,y)
+#define CURSOR_NAME CURSOR_JOIN(cursor_, __COUNTER__)
+
+#define CMAP_FOR_EACH__(NODE, MEMBER, CMAP, CURSOR_NAME)           \
+    for (struct cmap_cursor CURSOR_NAME = cmap_cursor_start(CMAP); \
+         CMAP_CURSOR_FOR_EACH__(NODE, &CURSOR_NAME, MEMBER);       \
         )
 
+#define CMAP_FOR_EACH(NODE, MEMBER, CMAP) \
+          CMAP_FOR_EACH__(NODE, MEMBER, CMAP, CURSOR_NAME)
+
 static inline struct cmap_node *cmap_first(const struct cmap *);
 
 /* Another, less preferred, form of iteration, for use in situations where it
-- 
2.7.4



More information about the dev mailing list