summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAdrian Moreno <amorenoz@redhat.com>2022-03-23 12:56:17 +0100
committerIlya Maximets <i.maximets@ovn.org>2022-03-30 16:59:02 +0200
commit9e56549c2bba79e644de2d3876b363553175210c (patch)
treebf0bf39f86c3e63eda7171f727a6ca3dcc69922b /include
parent860e69a8c3d6994dc000b37d682bd16cd2925bef (diff)
downloadopenvswitch-9e56549c2bba79e644de2d3876b363553175210c.tar.gz
hmap: use short version of safe loops if possible.
Using SHORT version of the *_SAFE loops makes the code cleaner and less error prone. So, use the SHORT version and remove the extra variable when possible for hmap and all its derived types. In order to be able to use both long and short versions without changing the name of the macro for all the clients, overload the existing name and select the appropriate version depending on the number of arguments. Acked-by: Dumitru Ceara <dceara@redhat.com> Acked-by: Eelco Chaudron <echaudro@redhat.com> Signed-off-by: Adrian Moreno <amorenoz@redhat.com> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
Diffstat (limited to 'include')
-rw-r--r--include/openvswitch/hmap.h26
-rw-r--r--include/openvswitch/shash.h15
2 files changed, 35 insertions, 6 deletions
diff --git a/include/openvswitch/hmap.h b/include/openvswitch/hmap.h
index 68c284cf1..beb48295b 100644
--- a/include/openvswitch/hmap.h
+++ b/include/openvswitch/hmap.h
@@ -178,18 +178,36 @@ bool hmap_contains(const struct hmap *, const struct hmap_node *);
/* Safe when NODE may be freed (not needed when NODE may be removed from the
* hash map but its members remain accessible and intact). */
-#define HMAP_FOR_EACH_SAFE(NODE, NEXT, MEMBER, HMAP) \
- HMAP_FOR_EACH_SAFE_INIT (NODE, NEXT, MEMBER, HMAP, (void) NEXT)
+#define HMAP_FOR_EACH_SAFE_LONG(NODE, NEXT, MEMBER, HMAP) \
+ HMAP_FOR_EACH_SAFE_LONG_INIT (NODE, NEXT, MEMBER, HMAP, (void) NEXT)
-#define HMAP_FOR_EACH_SAFE_INIT(NODE, NEXT, MEMBER, HMAP, ...) \
+#define HMAP_FOR_EACH_SAFE_LONG_INIT(NODE, NEXT, MEMBER, HMAP, ...) \
for (INIT_MULTIVAR_SAFE_LONG_EXP(NODE, NEXT, MEMBER, hmap_first(HMAP), \
- struct hmap_node, __VA_ARGS__); \
+ struct hmap_node, __VA_ARGS__); \
CONDITION_MULTIVAR_SAFE_LONG(NODE, NEXT, MEMBER, \
ITER_VAR(NODE) != NULL, \
ITER_VAR(NEXT) = hmap_next(HMAP, ITER_VAR(NODE)), \
ITER_VAR(NEXT) != NULL); \
UPDATE_MULTIVAR_SAFE_LONG(NODE, NEXT))
+/* Short versions of HMAP_FOR_EACH_SAFE. */
+#define HMAP_FOR_EACH_SAFE_SHORT(NODE, MEMBER, HMAP) \
+ HMAP_FOR_EACH_SAFE_SHORT_INIT (NODE, MEMBER, HMAP, (void) 0)
+
+#define HMAP_FOR_EACH_SAFE_SHORT_INIT(NODE, MEMBER, HMAP, ...) \
+ for (INIT_MULTIVAR_SAFE_SHORT_EXP(NODE, MEMBER, hmap_first(HMAP), \
+ struct hmap_node, __VA_ARGS__); \
+ CONDITION_MULTIVAR_SAFE_SHORT(NODE, MEMBER, \
+ ITER_VAR(NODE) != NULL, \
+ ITER_NEXT_VAR(NODE) = hmap_next(HMAP, ITER_VAR(NODE))); \
+ UPDATE_MULTIVAR_SAFE_SHORT(NODE))
+
+#define HMAP_FOR_EACH_SAFE(...) \
+ OVERLOAD_SAFE_MACRO(HMAP_FOR_EACH_SAFE_LONG, \
+ HMAP_FOR_EACH_SAFE_SHORT, \
+ 4, __VA_ARGS__)
+
+
/* Continues an iteration from just after NODE. */
#define HMAP_FOR_EACH_CONTINUE(NODE, MEMBER, HMAP) \
HMAP_FOR_EACH_CONTINUE_INIT(NODE, MEMBER, HMAP, (void) 0)
diff --git a/include/openvswitch/shash.h b/include/openvswitch/shash.h
index c249e13e1..4e7badd4d 100644
--- a/include/openvswitch/shash.h
+++ b/include/openvswitch/shash.h
@@ -41,13 +41,24 @@ struct shash {
BUILD_ASSERT_TYPE(SHASH_NODE, struct shash_node *), \
BUILD_ASSERT_TYPE(SHASH, struct shash *))
-#define SHASH_FOR_EACH_SAFE(SHASH_NODE, NEXT, SHASH) \
- HMAP_FOR_EACH_SAFE_INIT ( \
+#define SHASH_FOR_EACH_SAFE_SHORT(SHASH_NODE, SHASH) \
+ HMAP_FOR_EACH_SAFE_SHORT_INIT ( \
+ SHASH_NODE, node, &(SHASH)->map, \
+ BUILD_ASSERT_TYPE(SHASH_NODE, struct shash_node *), \
+ BUILD_ASSERT_TYPE(SHASH, struct shash *))
+
+#define SHASH_FOR_EACH_SAFE_LONG(SHASH_NODE, NEXT, SHASH) \
+ HMAP_FOR_EACH_SAFE_LONG_INIT ( \
SHASH_NODE, NEXT, node, &(SHASH)->map, \
BUILD_ASSERT_TYPE(SHASH_NODE, struct shash_node *), \
BUILD_ASSERT_TYPE(NEXT, struct shash_node *), \
BUILD_ASSERT_TYPE(SHASH, struct shash *))
+#define SHASH_FOR_EACH_SAFE(...) \
+ OVERLOAD_SAFE_MACRO(SHASH_FOR_EACH_SAFE_LONG, \
+ SHASH_FOR_EACH_SAFE_SHORT, \
+ 3, __VA_ARGS__)
+
void shash_init(struct shash *);
void shash_destroy(struct shash *);
void shash_destroy_free_data(struct shash *);