]> xenbits.xensource.com Git - libvirt.git/commitdiff
util/virhash: add name parameter to virHashSearch
authorPavel Hrdina <phrdina@redhat.com>
Tue, 13 Jun 2017 13:56:14 +0000 (15:56 +0200)
committerPavel Hrdina <phrdina@redhat.com>
Thu, 20 Jul 2017 12:02:14 +0000 (14:02 +0200)
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>
src/conf/virdomainobjlist.c
src/conf/virnetworkobj.c
src/conf/virsecretobj.c
src/qemu/qemu_capabilities.c
src/util/virhash.c
src/util/virhash.h
src/xen/xm_internal.c
tests/virhashtest.c

index c121382bcbb47058d9e4b739daea0870982648bb..7bd4cd29b9c5181128e11b7624d2e56d57e8e6f1 100644 (file)
@@ -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);
index 88e42b5b3e2cbe639e11c970a2265416e81b1030..ccde72e727c40fcbec0bef82f0e669b53b46b6b5 100644 (file)
@@ -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;
index e3bcbe593dfc2d121ab17f627eb9a2762edfe899..792ceb1f15146edcec828c5394b46df49f143338 100644 (file)
@@ -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;
index 04aa8d53c0c2c90a04d2a1c95b0e4ac0fcb65099..dbebf6196105de430b8b61848bdd137b787f572e 100644 (file)
@@ -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);
         }
     }
 
index a8e0edfd3b1f652d210ad8bd62a1e25b6332836e..7fa2992f18bc585da98730a81a939e3a83091432 100644 (file)
@@ -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;
 }
index 61c172b9e05b6460782b43e2ec5a7942ee8aac59..00b2550e70abdc65abaac81fa8a75f0ae8a98dc7 100644 (file)
@@ -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);
index fba814aa196511f01e5daa9805562f206ae1118c..123f379572199f2dfa804768cd15b300eaf38379 100644 (file)
@@ -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];
index dbf07ae7f7268a8e26f545785e594575a10024a1..9407f98c4bb5f59ff77a9c37dc71225827a2fd7c 100644 (file)
@@ -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",