From df66eb2eeb7189ab27be49aa8637938cdcf43cb6 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Fri, 6 Jul 2018 10:18:11 -0400 Subject: [PATCH 1/3] Revert "lib: A small optimization for the hash iterate and walk functions" This reverts commit fc61644e440c875eefa222ab34d726c6281ca806. --- lib/hash.c | 12 +----------- vtysh/vtysh.c | 1 + 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/hash.c b/lib/hash.c index 37f6cdcc8f..ee5401b236 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -241,21 +241,15 @@ void hash_iterate(struct hash *hash, void (*func)(struct hash_backet *, void *), unsigned int i; struct hash_backet *hb; struct hash_backet *hbnext; - uint32_t count = 0; - for (i = 0; i < hash->size; i++) { + for (i = 0; i < hash->size; i++) for (hb = hash->index[i]; hb; hb = hbnext) { /* get pointer to next hash backet here, in case (*func) * decides to delete hb by calling hash_release */ hbnext = hb->next; (*func)(hb, arg); - count++; - } - if (count == hash->count) - return; - } } void hash_walk(struct hash *hash, int (*func)(struct hash_backet *, void *), @@ -265,7 +259,6 @@ void hash_walk(struct hash *hash, int (*func)(struct hash_backet *, void *), struct hash_backet *hb; struct hash_backet *hbnext; int ret = HASHWALK_CONTINUE; - uint32_t count = 0; for (i = 0; i < hash->size; i++) { for (hb = hash->index[i]; hb; hb = hbnext) { @@ -276,10 +269,7 @@ void hash_walk(struct hash *hash, int (*func)(struct hash_backet *, void *), ret = (*func)(hb, arg); if (ret == HASHWALK_ABORT) return; - count++; } - if (count == hash->count) - return; } } diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index b56eaa899f..82e744ac4d 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -2144,6 +2144,7 @@ DEFUN (vtysh_show_poll, "Thread Poll Information\n") { unsigned int i; + int idx = 0; int ret = CMD_SUCCESS; char line[100]; From 8b52179d2ae6eecad2155fb04ec99610077618f9 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Fri, 6 Jul 2018 10:18:50 -0400 Subject: [PATCH 2/3] lib: Add some documentation Add some documentation to hash_walk/iterate to tell people to not do something stupid. Signed-off-by: Donald Sharp --- lib/hash.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/hash.h b/lib/hash.h index c7e670b723..12c214e469 100644 --- a/lib/hash.h +++ b/lib/hash.h @@ -232,7 +232,9 @@ extern void *hash_release(struct hash *hash, void *data); * Iterate over the elements in a hash table. * * It is safe to delete items passed to the iteration function from the hash - * table during iteration. + * table during iteration. Please note that adding entries to the hash + * during the walk will cause undefined behavior in that some new entries + * will be walked and some will not. So do not do this. * * hash * hash table to operate on @@ -250,7 +252,9 @@ extern void hash_iterate(struct hash *hash, * Iterate over the elements in a hash table, stopping on condition. * * It is safe to delete items passed to the iteration function from the hash - * table during iteration. + * table during iteration. Please note that adding entries to the hash + * during the walk will cause undefined behavior in that some new entries + * will be walked and some will not. So do not do this. * * hash * hash table to operate on From d76345fa669174c16c6ea746cdf51d77126eea35 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Fri, 6 Jul 2018 10:22:34 -0400 Subject: [PATCH 3/3] vtysh: Remove unused variable Signed-off-by: Donald Sharp --- vtysh/vtysh.c | 1 - 1 file changed, 1 deletion(-) diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index 82e744ac4d..b56eaa899f 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -2144,7 +2144,6 @@ DEFUN (vtysh_show_poll, "Thread Poll Information\n") { unsigned int i; - int idx = 0; int ret = CMD_SUCCESS; char line[100];