[ovs-dev] [ovsdb monitors 2/8] shash: Refactor shash_add_nocopy(), shash_find().

Ben Pfaff blp at nicira.com
Wed Jun 30 23:49:21 UTC 2010


By breaking out the hash calculation we can enable operations that need
to do both to avoid duplicating the hash calculations.  A following commit
will add such an operation.
---
 lib/shash.c |   34 ++++++++++++++++++++++++----------
 1 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/lib/shash.c b/lib/shash.c
index c4d3ccb..e2b1fe2 100644
--- a/lib/shash.c
+++ b/lib/shash.c
@@ -19,6 +19,9 @@
 #include <assert.h>
 #include "hash.h"
 
+static struct shash_node *shash_find__(const struct shash *,
+                                       const char *name, size_t hash);
+
 static size_t
 hash_name(const char *name)
 {
@@ -100,21 +103,27 @@ shash_count(const struct shash *shash)
     return hmap_count(&shash->map);
 }
 
-/* It is the caller's responsibility to avoid duplicate names, if that is
- * desirable. */
-struct shash_node *
-shash_add_nocopy(struct shash *sh, char *name, const void *data)
+static struct shash_node *
+shash_add_nocopy__(struct shash *sh, char *name, const void *data, size_t hash)
 {
     struct shash_node *node = xmalloc(sizeof *node);
     node->name = name;
     node->data = (void *) data;
-    hmap_insert(&sh->map, &node->node, hash_name(name));
+    hmap_insert(&sh->map, &node->node, hash);
     return node;
 }
 
 /* It is the caller's responsibility to avoid duplicate names, if that is
  * desirable. */
 struct shash_node *
+shash_add_nocopy(struct shash *sh, char *name, const void *data)
+{
+    return shash_add_nocopy__(sh, name, data, hash_name(name));
+}
+
+/* It is the caller's responsibility to avoid duplicate names, if that is
+ * desirable. */
+struct shash_node *
 shash_add(struct shash *sh, const char *name, const void *data)
 {
     return shash_add_nocopy(sh, xstrdup(name), data);
@@ -146,14 +155,12 @@ shash_delete(struct shash *sh, struct shash_node *node)
     free(node);
 }
 
-/* If there are duplicates, returns a random element. */
-struct shash_node *
-shash_find(const struct shash *sh, const char *name)
+static struct shash_node *
+shash_find__(const struct shash *sh, const char *name, size_t hash)
 {
     struct shash_node *node;
 
-    HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node,
-                             hash_name(name), &sh->map) {
+    HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node, hash, &sh->map) {
         if (!strcmp(node->name, name)) {
             return node;
         }
@@ -161,6 +168,13 @@ shash_find(const struct shash *sh, const char *name)
     return NULL;
 }
 
+/* If there are duplicates, returns a random element. */
+struct shash_node *
+shash_find(const struct shash *sh, const char *name)
+{
+    return shash_find__(sh, name, hash_name(name));
+}
+
 void *
 shash_find_data(const struct shash *sh, const char *name)
 {
-- 
1.7.1





More information about the dev mailing list