summary refs log tree commit diff stats
path: root/util/uuid.c
diff options
context:
space:
mode:
authorAlbert Esteve <aesteve@redhat.com>2023-10-02 08:57:03 +0200
committerMichael S. Tsirkin <mst@redhat.com>2023-10-04 18:15:06 -0400
commita6ceee591acdb9c9c772bf59544a57891308222e (patch)
tree79cadd8681bb2324c370cf64b17b2b2e3855df31 /util/uuid.c
parent70f88436aa5a8aeddd33e4d06270146af5d5bb52 (diff)
downloadfocaccia-qemu-a6ceee591acdb9c9c772bf59544a57891308222e.tar.gz
focaccia-qemu-a6ceee591acdb9c9c772bf59544a57891308222e.zip
util/uuid: add a hash function
Add hash function to uuid module using the
djb2 hash algorithm.

Add a couple simple unit tests for the hash
function, checking collisions for similar UUIDs.

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Signed-off-by: Albert Esteve <aesteve@redhat.com>
Message-Id: <20231002065706.94707-2-aesteve@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Diffstat (limited to 'util/uuid.c')
-rw-r--r--util/uuid.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/util/uuid.c b/util/uuid.c
index b1108dde78..d71aa79e5e 100644
--- a/util/uuid.c
+++ b/util/uuid.c
@@ -116,3 +116,17 @@ QemuUUID qemu_uuid_bswap(QemuUUID uuid)
     bswap16s(&uuid.fields.time_high_and_version);
     return uuid;
 }
+
+/* djb2 hash algorithm */
+uint32_t qemu_uuid_hash(const void *uuid)
+{
+    QemuUUID *qid = (QemuUUID *) uuid;
+    uint32_t h = 5381;
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(qid->data); i++) {
+        h = (h << 5) + h + qid->data[i];
+    }
+
+    return h;
+}