]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: hash: Append to hash buckets when adding new entries
authorPeter Krempa <pkrempa@redhat.com>
Tue, 16 Apr 2019 13:11:40 +0000 (15:11 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 25 Apr 2019 11:28:16 +0000 (13:28 +0200)
In cases when the hash function for a name collides with other entry
already in the hash we prepend to the bucket. This creates a 'stack
effect' on the buckets if we then iterate through the hash. Normally
this is not a problem, but in tests we want deterministic results.

Since it does not matter where we add the entry and it's usually more
probable that a different entry will be accessed next change it to
append to the end of the bucket. Luckily we already iterate throught the
bucket once thus we can easily find the last entry and just connect the
new entry after it.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
src/util/virhash.c

index 4907c1124fd556e5303d53b6a18796cfe39f9301..0e30106041ca4d607fa8aaa1f4d4f375e620d9fe 100644 (file)
@@ -316,6 +316,7 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
 {
     size_t key, len = 0;
     virHashEntryPtr entry;
+    virHashEntryPtr last = NULL;
     void *new_name;
 
     if ((table == NULL) || (name == NULL))
@@ -337,6 +338,7 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
                 return -1;
             }
         }
+        last = entry;
         len++;
     }
 
@@ -347,8 +349,11 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
 
     entry->name = new_name;
     entry->payload = userdata;
-    entry->next = table->table[key];
-    table->table[key] = entry;
+
+    if (last)
+        last->next = entry;
+    else
+        table->table[key] = entry;
 
     table->nbElems++;