[libvirt] [PATCH 3/8] add hash table rebalancing in virHashUpdateEntry

Christophe Fergeau teuf at gnome.org
Sun Feb 13 21:45:19 UTC 2011


The code in virHashUpdateEntry and virHashAddEntry is really
similar. However, the latter rebalances the hash table when
one of its buckets contains too many elements while the former
does not. Fix this discrepancy.
---
 src/util/hash.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)

diff --git a/src/util/hash.c b/src/util/hash.c
index 893fe96..8e337eb 100644
--- a/src/util/hash.c
+++ b/src/util/hash.c
@@ -322,7 +322,7 @@ int
 virHashUpdateEntry(virHashTablePtr table, const char *name,
                    void *userdata, virHashDeallocator f)
 {
-    unsigned long key;
+    unsigned long key, len = 0;
     virHashEntryPtr entry;
     virHashEntryPtr insert;
 
@@ -344,6 +344,7 @@ virHashUpdateEntry(virHashTablePtr table, const char *name,
                 insert->payload = userdata;
                 return (0);
             }
+            len++;
         }
         if (STREQ(insert->name, name)) {
             if (f)
@@ -376,6 +377,10 @@ virHashUpdateEntry(virHashTablePtr table, const char *name,
     if (insert != NULL) {
         insert->next = entry;
     }
+
+    if (len > MAX_HASH_LEN)
+        virHashGrow(table, MAX_HASH_LEN * table->size);
+
     return (0);
 }
 
-- 
1.7.4




More information about the libvir-list mailing list