diff mbox series

[ovs-dev,v3,12/17] hindex: remove the next variable in safe loops

Message ID 20220216142800.3295236-13-amorenoz@redhat.com
State Superseded
Headers show
Series Fix undefined behavior in loop macros | expand

Checks

Context Check Description
ovsrobot/apply-robot warning apply and check: warning
ovsrobot/github-robot-_Build_and_Test success github build: passed

Commit Message

Adrián Moreno Feb. 16, 2022, 2:27 p.m. UTC
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 HINDEX_*_SAFE.

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.

Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 lib/conntrack.c     |  4 ++--
 lib/hindex.h        | 48 ++++++++++++++++++++++++++++++++++++---------
 tests/test-hindex.c | 31 +++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 11 deletions(-)

Comments

0-day Robot Feb. 16, 2022, 4 p.m. UTC | #1
Bleep bloop.  Greetings Adrian Moreno, I am a robot and I have tried out your patch.
Thanks for your contribution.

I encountered some error that I wasn't expecting.  See the details below.


checkpatch:
ERROR: Use ovs_assert() in place of assert()
#124 FILE: tests/test-hindex.c:296:
            assert(n == n_remaining);

ERROR: Use ovs_assert() in place of assert()
#133 FILE: tests/test-hindex.c:305:
                assert(i < n);

ERROR: Use ovs_assert() in place of assert()
#138 FILE: tests/test-hindex.c:310:
                        assert(j < n_remaining);

ERROR: Use ovs_assert() in place of assert()
#148 FILE: tests/test-hindex.c:320:
            assert(i == n);

Lines checked: 156, Warnings: 0, Errors: 4


Please check this out.  If you feel there has been an error, please email aconole@redhat.com

Thanks,
0-day Robot
Dumitru Ceara Feb. 25, 2022, 12:08 p.m. UTC | #2
On 2/16/22 15:27, Adrian Moreno wrote:
> 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 HINDEX_*_SAFE.
> 
> 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.
> 
> Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
> ---

Acked-by: Dumitru Ceara <dceara@redhat.com>
diff mbox series

Patch

diff --git a/lib/conntrack.c b/lib/conntrack.c
index 9c96b69e4..2f0777102 100644
--- a/lib/conntrack.c
+++ b/lib/conntrack.c
@@ -2857,8 +2857,8 @@  expectation_clean(struct conntrack *ct, const struct conn_key *parent_key)
 {
     ovs_rwlock_wrlock(&ct->resources_lock);
 
-    struct alg_exp_node *node, *next;
-    HINDEX_FOR_EACH_WITH_HASH_SAFE (node, next, node_ref,
+    struct alg_exp_node *node;
+    HINDEX_FOR_EACH_WITH_HASH_SAFE (node, node_ref,
                                     conn_key_hash(parent_key, ct->hash_basis),
                                     &ct->alg_expectation_refs) {
         if (!conn_key_cmp(&node->parent_key, parent_key)) {
diff --git a/lib/hindex.h b/lib/hindex.h
index fa252e6dd..61e6876a9 100644
--- a/lib/hindex.h
+++ b/lib/hindex.h
@@ -135,16 +135,32 @@  void hindex_remove(struct hindex *, struct hindex_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 HINDEX_FOR_EACH_WITH_HASH_SAFE(NODE, NEXT, MEMBER, HASH, HINDEX)    \
-    for (INIT_MULTIVAR_SAFE_LONG(NODE, NEXT, MEMBER,                        \
-                                hindex_node_with_hash(HINDEX, HASH),        \
-                                struct hindex_node);                        \
-         CONDITION_MULTIVAR_SAFE_LONG(NODE, NEXT, MEMBER,                   \
-                                      ITER_VAR(NODE) != NULL,               \
-                                      ITER_VAR(NEXT) = ITER_VAR(NODE)->s,   \
-                                      ITER_VAR(NEXT) != NULL);              \
+#define HINDEX_FOR_EACH_WITH_HASH_SAFE_LONG(NODE, NEXT, MEMBER, HASH, HINDEX) \
+    for (INIT_MULTIVAR_SAFE_LONG(NODE, NEXT, MEMBER,                          \
+                                hindex_node_with_hash(HINDEX, HASH),          \
+                                struct hindex_node);                          \
+         CONDITION_MULTIVAR_SAFE_LONG(NODE, NEXT, MEMBER,                     \
+                                      ITER_VAR(NODE) != NULL,                 \
+                                      ITER_VAR(NEXT) = ITER_VAR(NODE)->s,     \
+                                      ITER_VAR(NEXT) != NULL);                \
          UPDATE_MULTIVAR_SAFE_LONG(NODE, NEXT))
 
+/* Short version of HINDEX_FOR_EACH_WITH_HASH_SAFE */
+#define HINDEX_FOR_EACH_WITH_HASH_SAFE_SHORT(NODE, MEMBER, HASH, HINDEX)      \
+    for (INIT_MULTIVAR_SAFE_SHORT(NODE, MEMBER,                               \
+                            hindex_node_with_hash(HINDEX, HASH),              \
+                            struct hindex_node);                              \
+         CONDITION_MULTIVAR_SAFE_SHORT(NODE, MEMBER,                          \
+                                       ITER_VAR(NODE) != NULL,                \
+                                 ITER_NEXT_VAR(NODE) = ITER_VAR(NODE)->s);    \
+         UPDATE_MULTIVAR_SAFE_SHORT(NODE))
+
+#define HINDEX_FOR_EACH_WITH_HASH_SAFE(...)                                   \
+    OVERLOAD_SAFE_MACRO(HINDEX_FOR_EACH_WITH_HASH_SAFE_LONG,                  \
+                        HINDEX_FOR_EACH_WITH_HASH_SAFE_SHORT,                 \
+                        5, __VA_ARGS__)
+
+
 /* Returns the head node in 'hindex' with the given 'hash', or a null pointer
  * if no nodes have that hash value. */
 static inline struct hindex_node *
@@ -170,7 +186,7 @@  hindex_node_with_hash(const struct hindex *hindex, size_t hash)
 
 /* Safe when NODE may be freed (not needed when NODE may be removed from the
  * hash index but its members remain accessible and intact). */
-#define HINDEX_FOR_EACH_SAFE(NODE, NEXT, MEMBER, HINDEX)                      \
+#define HINDEX_FOR_EACH_SAFE_LONG(NODE, NEXT, MEMBER, HINDEX)                 \
     for (INIT_MULTIVAR_SAFE_LONG(NODE, NEXT, MEMBER, hindex_first(HINDEX),    \
                                  struct hindex_node);                         \
          CONDITION_MULTIVAR_SAFE_LONG(NODE, NEXT, MEMBER,                     \
@@ -179,6 +195,20 @@  hindex_node_with_hash(const struct hindex *hindex, size_t hash)
                                       ITER_VAR(NEXT) != NULL);                \
          UPDATE_MULTIVAR_SAFE_LONG(NODE, NEXT))
 
+/* Short version of HINDEX_FOR_EACH_SAFE */
+#define HINDEX_FOR_EACH_SAFE_SHORT(NODE, MEMBER, HINDEX)                      \
+    for (INIT_MULTIVAR_SAFE_SHORT(NODE, MEMBER, hindex_first(HINDEX),         \
+                                  struct hindex_node);                        \
+         CONDITION_MULTIVAR_SAFE_SHORT(NODE, MEMBER,                          \
+                                       ITER_VAR(NODE) != NULL,                \
+              ITER_NEXT_VAR(NODE) = hindex_next(HINDEX, ITER_VAR(NODE)));     \
+         UPDATE_MULTIVAR_SAFE_SHORT(NODE))
+
+#define HINDEX_FOR_EACH_SAFE(...)                                             \
+    OVERLOAD_SAFE_MACRO(HINDEX_FOR_EACH_SAFE_LONG,                            \
+                        HINDEX_FOR_EACH_SAFE_SHORT,                           \
+                        4, __VA_ARGS__)
+
 struct hindex_node *hindex_first(const struct hindex *);
 struct hindex_node *hindex_next(const struct hindex *,
                                 const struct hindex_node *);
diff --git a/tests/test-hindex.c b/tests/test-hindex.c
index 95e49284e..cc2b1b8bd 100644
--- a/tests/test-hindex.c
+++ b/tests/test-hindex.c
@@ -288,6 +288,37 @@  test_hindex_for_each_safe(hash_func *hash)
             assert(i == n);
             assert(next == NULL);
 
+            for (i = 0; i < n; i++) {
+                if (pattern & (1ul << i)) {
+                    n_remaining++;
+                }
+            }
+            assert(n == n_remaining);
+            hindex_destroy(&hindex);
+
+            /* Test short version (without the next variable). */
+            make_hindex(&hindex, elements, values, n, hash);
+
+            i = 0;
+            n_remaining = n;
+            HINDEX_FOR_EACH_SAFE (e, node, &hindex) {
+                assert(i < n);
+                if (pattern & (1ul << e->value)) {
+                    size_t j;
+                    hindex_remove(&hindex, &e->node);
+                    for (j = 0; ; j++) {
+                        assert(j < n_remaining);
+                        if (values[j] == e->value) {
+                            values[j] = values[--n_remaining];
+                            break;
+                        }
+                    }
+                }
+                check_hindex(&hindex, values, n_remaining, hash);
+                i++;
+            }
+            assert(i == n);
+
             for (i = 0; i < n; i++) {
                 if (pattern & (1ul << i)) {
                     n_remaining++;