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>
This commit is contained in:
Albert Esteve 2023-10-02 08:57:03 +02:00 committed by Michael S. Tsirkin
parent 70f88436aa
commit a6ceee591a
3 changed files with 43 additions and 0 deletions

View file

@ -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;
}