From defd31358e71b4b4e1327f3f04fcd298f21fd08a Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Wed, 9 Oct 2019 15:34:54 +0200 Subject: [PATCH] util: hash: Add new constructor 'virHashNew' Add a simpler constructor for hash tables which specifically does not require specifying the initial hash size and uses simpler freeing function. The initial hash table size usually is not important as the hash table is growing when it reaches certain number of entries in one bucket. Additionally many callers pass in a random small number for ad-hoc table use so using a central one will simplify things. Signed-off-by: Peter Krempa ACKed-by: Eric Blake --- src/libvirt_private.syms | 1 + src/util/virhash.c | 21 +++++++++++++++++++++ src/util/virhash.h | 1 + 3 files changed, 23 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 875ad8a7c7..ee8bb84041 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2071,6 +2071,7 @@ virHashForEach; virHashFree; virHashGetItems; virHashLookup; +virHashNew; virHashRemoveAll; virHashRemoveEntry; virHashRemoveSet; diff --git a/src/util/virhash.c b/src/util/virhash.c index 32de4dabad..9384d28002 100644 --- a/src/util/virhash.c +++ b/src/util/virhash.c @@ -169,6 +169,27 @@ virHashTablePtr virHashCreateFull(ssize_t size, } +/** + * virHashNew: + * @dataFree: callback to free data + * + * Create a new virHashTablePtr. + * + * Returns the newly created object, or NULL if an error occurred. + */ +virHashTablePtr +virHashNew(virHashDataFreeSimple dataFree) +{ + return virHashCreateFull(32, + NULL, + dataFree, + virHashStrCode, + virHashStrEqual, + virHashStrCopy, + virHashStrFree); +} + + /** * virHashCreate: * @size: the size of the hash table diff --git a/src/util/virhash.h b/src/util/virhash.h index 94fe8e23e4..d7de0618cb 100644 --- a/src/util/virhash.h +++ b/src/util/virhash.h @@ -107,6 +107,7 @@ typedef void (*virHashKeyFree)(void *name); /* * Constructor and destructor. */ +virHashTablePtr virHashNew(virHashDataFreeSimple dataFree); virHashTablePtr virHashCreate(ssize_t size, virHashDataFree dataFree); virHashAtomicPtr virHashAtomicNew(ssize_t size, -- GitLab