util/virhash: add name parameter to virHashSearch

While searching for an element using a function it may be
desirable to know the element key for future operation.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
This commit is contained in:
Pavel Hrdina 2017-06-13 15:56:14 +02:00
parent 84359339c5
commit 38e516a524
8 changed files with 20 additions and 13 deletions

View File

@ -119,7 +119,7 @@ virDomainObjListFindByIDInternal(virDomainObjListPtr doms,
{ {
virDomainObjPtr obj; virDomainObjPtr obj;
virObjectLock(doms); virObjectLock(doms);
obj = virHashSearch(doms->objs, virDomainObjListSearchID, &id); obj = virHashSearch(doms->objs, virDomainObjListSearchID, &id, NULL);
if (ref) { if (ref) {
virObjectRef(obj); virObjectRef(obj);
virObjectUnlock(doms); virObjectUnlock(doms);

View File

@ -208,7 +208,7 @@ virNetworkObjFindByNameLocked(virNetworkObjListPtr nets,
{ {
virNetworkObjPtr ret = NULL; virNetworkObjPtr ret = NULL;
ret = virHashSearch(nets->objs, virNetworkObjSearchName, name); ret = virHashSearch(nets->objs, virNetworkObjSearchName, name, NULL);
if (ret) if (ret)
virObjectRef(ret); virObjectRef(ret);
return ret; return ret;
@ -980,7 +980,7 @@ virNetworkObjBridgeInUse(virNetworkObjListPtr nets,
struct virNetworkObjBridgeInUseHelperData data = {bridge, skipname}; struct virNetworkObjBridgeInUseHelperData data = {bridge, skipname};
virObjectLock(nets); virObjectLock(nets);
obj = virHashSearch(nets->objs, virNetworkObjBridgeInUseHelper, &data); obj = virHashSearch(nets->objs, virNetworkObjBridgeInUseHelper, &data, NULL);
virObjectUnlock(nets); virObjectUnlock(nets);
return obj != NULL; return obj != NULL;

View File

@ -247,7 +247,7 @@ virSecretObjListFindByUsageLocked(virSecretObjListPtr secrets,
struct virSecretSearchData data = { .usageType = usageType, struct virSecretSearchData data = { .usageType = usageType,
.usageID = usageID }; .usageID = usageID };
obj = virHashSearch(secrets->objs, virSecretObjSearchName, &data); obj = virHashSearch(secrets->objs, virSecretObjSearchName, &data, NULL);
if (obj) if (obj)
virObjectRef(obj); virObjectRef(obj);
return obj; return obj;

View File

@ -5502,14 +5502,15 @@ virQEMUCapsCacheLookupByArch(virCapsPtr caps,
struct virQEMUCapsSearchData data = { .arch = arch }; struct virQEMUCapsSearchData data = { .arch = arch };
virMutexLock(&cache->lock); virMutexLock(&cache->lock);
ret = virHashSearch(cache->binaries, virQEMUCapsCompareArch, &data); ret = virHashSearch(cache->binaries, virQEMUCapsCompareArch, &data, NULL);
if (!ret) { if (!ret) {
/* If the first attempt at finding capabilities has failed, try /* If the first attempt at finding capabilities has failed, try
* again using the QEMU target as lookup key instead */ * again using the QEMU target as lookup key instead */
target = virQEMUCapsFindTarget(virArchFromHost(), data.arch); target = virQEMUCapsFindTarget(virArchFromHost(), data.arch);
if (target != data.arch) { if (target != data.arch) {
data.arch = target; data.arch = target;
ret = virHashSearch(cache->binaries, virQEMUCapsCompareArch, &data); ret = virHashSearch(cache->binaries, virQEMUCapsCompareArch,
&data, NULL);
} }
} }

View File

@ -702,15 +702,18 @@ virHashRemoveAll(virHashTablePtr table)
* @table: the hash table to search * @table: the hash table to search
* @iter: an iterator to identify the desired element * @iter: an iterator to identify the desired element
* @data: extra opaque information passed to the iter * @data: extra opaque information passed to the iter
* @name: the name of found user data, pass NULL to ignore
* *
* Iterates over the hash table calling the 'iter' callback * Iterates over the hash table calling the 'iter' callback
* for each element. The first element for which the iter * for each element. The first element for which the iter
* returns non-zero will be returned by this function. * returns non-zero will be returned by this function.
* The elements are processed in a undefined order * The elements are processed in a undefined order. Caller is
* responsible for freeing the @name.
*/ */
void *virHashSearch(const virHashTable *ctable, void *virHashSearch(const virHashTable *ctable,
virHashSearcher iter, virHashSearcher iter,
const void *data) const void *data,
void **name)
{ {
size_t i; size_t i;
@ -730,6 +733,8 @@ void *virHashSearch(const virHashTable *ctable,
for (entry = table->table[i]; entry; entry = entry->next) { for (entry = table->table[i]; entry; entry = entry->next) {
if (iter(entry->payload, entry->name, data)) { if (iter(entry->payload, entry->name, data)) {
table->iterating = false; table->iterating = false;
if (name)
*name = table->keyCopy(entry->name);
return entry->payload; return entry->payload;
} }
} }
@ -824,7 +829,7 @@ bool virHashEqual(const virHashTable *table1,
virHashSize(table1) != virHashSize(table2)) virHashSize(table1) != virHashSize(table2))
return false; return false;
virHashSearch(table1, virHashEqualSearcher, &data); virHashSearch(table1, virHashEqualSearcher, &data, NULL);
return data.equal; return data.equal;
} }

View File

@ -194,7 +194,7 @@ bool virHashEqual(const virHashTable *table1,
int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data); int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data); ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
void *virHashSearch(const virHashTable *table, virHashSearcher iter, void *virHashSearch(const virHashTable *table, virHashSearcher iter,
const void *data); const void *data, void **name);
/* Convenience for when VIR_FREE(value) is sufficient as a data freer. */ /* Convenience for when VIR_FREE(value) is sufficient as a data freer. */
void virHashValueFree(void *value, const void *name); void virHashValueFree(void *value, const void *name);

View File

@ -880,7 +880,8 @@ xenXMDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0) if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
goto cleanup; goto cleanup;
if (!(entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID, (const void *)uuid))) if (!(entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID,
(const void *)uuid, NULL)))
goto cleanup; goto cleanup;
ret = virDomainDefNewFull(entry->def->name, uuid, -1); ret = virDomainDefNewFull(entry->def->name, uuid, -1);
@ -971,7 +972,7 @@ xenXMDomainDefineXML(virConnectPtr conn, virDomainDefPtr def)
* it has the same name * it has the same name
*/ */
if ((entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID, if ((entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID,
(const void *)&(def->uuid))) != NULL) { (const void *)&(def->uuid), NULL)) != NULL) {
if ((entry->def != NULL) && (entry->def->name != NULL) && if ((entry->def != NULL) && (entry->def->name != NULL) &&
(STRNEQ(def->name, entry->def->name))) { (STRNEQ(def->name, entry->def->name))) {
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];

View File

@ -436,7 +436,7 @@ testHashSearch(const void *data ATTRIBUTE_UNUSED)
if (!(hash = testHashInit(0))) if (!(hash = testHashInit(0)))
return -1; return -1;
entry = virHashSearch(hash, testHashSearchIter, NULL); entry = virHashSearch(hash, testHashSearchIter, NULL, NULL);
if (!entry || STRNEQ(uuids_subset[testSearchIndex], entry)) { if (!entry || STRNEQ(uuids_subset[testSearchIndex], entry)) {
VIR_TEST_VERBOSE("\nvirHashSearch didn't find entry '%s'\n", VIR_TEST_VERBOSE("\nvirHashSearch didn't find entry '%s'\n",