util: hash: Introduce virHashHasEntry

Add a helper that checks whether an entry with given name exists but
does not touch the userdata.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
ACKed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Peter Krempa 2019-10-17 14:45:47 +02:00
parent defd31358e
commit 36d934e7ae
3 changed files with 42 additions and 9 deletions

View File

@ -2070,6 +2070,7 @@ virHashEqual;
virHashForEach; virHashForEach;
virHashFree; virHashFree;
virHashGetItems; virHashGetItems;
virHashHasEntry;
virHashLookup; virHashLookup;
virHashNew; virHashNew;
virHashRemoveAll; virHashRemoveAll;

View File

@ -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: * virHashLookup:
* @table: the hash table * @table: the hash table
@ -457,18 +477,29 @@ virHashAtomicUpdate(virHashAtomicPtr table,
void * void *
virHashLookup(const virHashTable *table, const void *name) virHashLookup(const virHashTable *table, const void *name)
{ {
size_t key; virHashEntryPtr entry = virHashGetEntry(table, name);
virHashEntryPtr entry;
if (!table || !name) if (!entry)
return NULL; 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 entry->payload;
} }
return NULL;
/**
* 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);
} }

View File

@ -150,6 +150,7 @@ ssize_t virHashRemoveAll(virHashTablePtr table);
* Retrieve the userdata. * Retrieve the userdata.
*/ */
void *virHashLookup(const virHashTable *table, const void *name); void *virHashLookup(const virHashTable *table, const void *name);
bool virHashHasEntry(const virHashTable *table, const void *name);
/* /*
* Retrieve & remove the userdata. * Retrieve & remove the userdata.