提交 38e516a5 编写于 作者: P Pavel Hrdina

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: NPavel Hrdina <phrdina@redhat.com>
Reviewed-by: NJiri Denemark <jdenemar@redhat.com>
上级 84359339
......@@ -119,7 +119,7 @@ virDomainObjListFindByIDInternal(virDomainObjListPtr doms,
{
virDomainObjPtr obj;
virObjectLock(doms);
obj = virHashSearch(doms->objs, virDomainObjListSearchID, &id);
obj = virHashSearch(doms->objs, virDomainObjListSearchID, &id, NULL);
if (ref) {
virObjectRef(obj);
virObjectUnlock(doms);
......
......@@ -208,7 +208,7 @@ virNetworkObjFindByNameLocked(virNetworkObjListPtr nets,
{
virNetworkObjPtr ret = NULL;
ret = virHashSearch(nets->objs, virNetworkObjSearchName, name);
ret = virHashSearch(nets->objs, virNetworkObjSearchName, name, NULL);
if (ret)
virObjectRef(ret);
return ret;
......@@ -980,7 +980,7 @@ virNetworkObjBridgeInUse(virNetworkObjListPtr nets,
struct virNetworkObjBridgeInUseHelperData data = {bridge, skipname};
virObjectLock(nets);
obj = virHashSearch(nets->objs, virNetworkObjBridgeInUseHelper, &data);
obj = virHashSearch(nets->objs, virNetworkObjBridgeInUseHelper, &data, NULL);
virObjectUnlock(nets);
return obj != NULL;
......
......@@ -247,7 +247,7 @@ virSecretObjListFindByUsageLocked(virSecretObjListPtr secrets,
struct virSecretSearchData data = { .usageType = usageType,
.usageID = usageID };
obj = virHashSearch(secrets->objs, virSecretObjSearchName, &data);
obj = virHashSearch(secrets->objs, virSecretObjSearchName, &data, NULL);
if (obj)
virObjectRef(obj);
return obj;
......
......@@ -5502,14 +5502,15 @@ virQEMUCapsCacheLookupByArch(virCapsPtr caps,
struct virQEMUCapsSearchData data = { .arch = arch };
virMutexLock(&cache->lock);
ret = virHashSearch(cache->binaries, virQEMUCapsCompareArch, &data);
ret = virHashSearch(cache->binaries, virQEMUCapsCompareArch, &data, NULL);
if (!ret) {
/* If the first attempt at finding capabilities has failed, try
* again using the QEMU target as lookup key instead */
target = virQEMUCapsFindTarget(virArchFromHost(), data.arch);
if (target != data.arch) {
data.arch = target;
ret = virHashSearch(cache->binaries, virQEMUCapsCompareArch, &data);
ret = virHashSearch(cache->binaries, virQEMUCapsCompareArch,
&data, NULL);
}
}
......
......@@ -702,15 +702,18 @@ virHashRemoveAll(virHashTablePtr table)
* @table: the hash table to search
* @iter: an iterator to identify the desired element
* @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
* for each element. The first element for which the iter
* 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,
virHashSearcher iter,
const void *data)
const void *data,
void **name)
{
size_t i;
......@@ -730,6 +733,8 @@ void *virHashSearch(const virHashTable *ctable,
for (entry = table->table[i]; entry; entry = entry->next) {
if (iter(entry->payload, entry->name, data)) {
table->iterating = false;
if (name)
*name = table->keyCopy(entry->name);
return entry->payload;
}
}
......@@ -824,7 +829,7 @@ bool virHashEqual(const virHashTable *table1,
virHashSize(table1) != virHashSize(table2))
return false;
virHashSearch(table1, virHashEqualSearcher, &data);
virHashSearch(table1, virHashEqualSearcher, &data, NULL);
return data.equal;
}
......@@ -194,7 +194,7 @@ bool virHashEqual(const virHashTable *table1,
int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data);
ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *data);
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. */
void virHashValueFree(void *value, const void *name);
......
......@@ -880,7 +880,8 @@ xenXMDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0)
goto cleanup;
if (!(entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID, (const void *)uuid)))
if (!(entry = virHashSearch(priv->configCache, xenXMDomainSearchForUUID,
(const void *)uuid, NULL)))
goto cleanup;
ret = virDomainDefNewFull(entry->def->name, uuid, -1);
......@@ -971,7 +972,7 @@ xenXMDomainDefineXML(virConnectPtr conn, virDomainDefPtr def)
* it has the same name
*/
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) &&
(STRNEQ(def->name, entry->def->name))) {
char uuidstr[VIR_UUID_STRING_BUFLEN];
......
......@@ -436,7 +436,7 @@ testHashSearch(const void *data ATTRIBUTE_UNUSED)
if (!(hash = testHashInit(0)))
return -1;
entry = virHashSearch(hash, testHashSearchIter, NULL);
entry = virHashSearch(hash, testHashSearchIter, NULL, NULL);
if (!entry || STRNEQ(uuids_subset[testSearchIndex], entry)) {
VIR_TEST_VERBOSE("\nvirHashSearch didn't find entry '%s'\n",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册