]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
add hash table rebalancing in virHashUpdateEntry
authorChristophe Fergeau <teuf@gnome.org>
Thu, 17 Feb 2011 21:14:56 +0000 (22:14 +0100)
committerEric Blake <eblake@redhat.com>
Thu, 17 Feb 2011 23:45:25 +0000 (16:45 -0700)
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

index 41df7ae4f37575e627bc6e32f4a349e74358422f..59f226b944e4031d83ca0fc4b4aed2984937e7b6 100644 (file)
@@ -331,7 +331,7 @@ int
 virHashUpdateEntry(virHashTablePtr table, const char *name,
                    void *userdata, virHashDeallocator f)
 {
-    unsigned long key;
+    unsigned long key, len = 0;
     virHashEntryPtr entry;
     virHashEntryPtr insert;
     char *new_name;
@@ -354,6 +354,7 @@ virHashUpdateEntry(virHashTablePtr table, const char *name,
                 insert->payload = userdata;
                 return (0);
             }
+            len++;
         }
         if (STREQ(insert->name, name)) {
             if (f)
@@ -386,6 +387,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);
 }