Add a helper that checks whether an entry with given name exists but
does not touch the userdata.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/virhash.c | 49 ++++++++++++++++++++++++++++++++--------
src/util/virhash.h | 1 +
3 files changed, 42 insertions(+), 9 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c155f51174..588f0a4356 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2065,6 +2065,7 @@ virHashEqual;
virHashForEach;
virHashFree;
virHashGetItems;
+virHashHasEntry;
virHashLookup;
virHashNew;
virHashRemoveAll;
diff --git a/src/util/virhash.c b/src/util/virhash.c
index de03032bf1..df07e37e42 100644
--- a/src/util/virhash.c
+++ b/src/util/virhash.c
@@ -445,6 +445,26 @@ virHashAtomicUpdate(virHashAtomicPtr table,
}
+static virHashEntryPtr
+virHashGetEntry(const virHashTable *table,
+ const void *name)
+{
+ size_t key;
+ virHashEntryPtr entry;
+
+ if (!table || !name)
+ return NULL;
+
+ key = virHashComputeKey(table, name);
+ for (entry = table->table[key]; entry; entry = entry->next) {
+ if (table->keyEqual(entry->name, name))
+ return entry;
+ }
+
+ return NULL;
+}
+
+
/**
* virHashLookup:
* @table: the hash table
@@ -457,18 +477,29 @@ virHashAtomicUpdate(virHashAtomicPtr table,
void *
virHashLookup(const virHashTable *table, const void *name)
{
- size_t key;
- virHashEntryPtr entry;
+ virHashEntryPtr entry = virHashGetEntry(table, name);
- if (!table || !name)
+ if (!entry)
return NULL;
- key = virHashComputeKey(table, name);
- for (entry = table->table[key]; entry; entry = entry->next) {
- if (table->keyEqual(entry->name, name))
- return entry->payload;
- }
- return NULL;
+ return entry->payload;
+}
+
+
+/**
+ * virHashHasEntry:
+ * @table: the hash table
+ * @name: the name of the userdata
+ *
+ * Find whether entry specified by @name exists.
+ *
+ * Returns true if the entry exists and false otherwise
+ */
+bool
+virHashHasEntry(const virHashTable *table,
+ const void *name)
+{
+ return !!virHashGetEntry(table, name);
}
diff --git a/src/util/virhash.h b/src/util/virhash.h
index d7de0618cb..8087965ee9 100644
--- a/src/util/virhash.h
+++ b/src/util/virhash.h
@@ -150,6 +150,7 @@ ssize_t virHashRemoveAll(virHashTablePtr table);
* Retrieve the userdata.
*/
void *virHashLookup(const virHashTable *table, const void *name);
+bool virHashHasEntry(const virHashTable *table, const void *name);
/*
* Retrieve & remove the userdata.
--
2.21.0