[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v2 1/6] 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>
---
 src/conf/virdomainobjlist.c  |  2 +-
 src/conf/virnetworkobj.c     |  4 ++--
 src/conf/virsecretobj.c      |  2 +-
 src/qemu/qemu_capabilities.c |  5 +++--
 src/util/virhash.c           | 11 ++++++++---
 src/util/virhash.h           |  2 +-
 src/xen/xm_internal.c        |  5 +++--
 tests/virhashtest.c          |  2 +-
 8 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/src/conf/virdomainobjlist.c b/src/conf/virdomainobjlist.c
index c121382bcb..7bd4cd29b9 100644
--- a/src/conf/virdomainobjlist.c
+++ b/src/conf/virdomainobjlist.c
@@ -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);
diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c
index 88e42b5b3e..ccde72e727 100644
--- a/src/conf/virnetworkobj.c
+++ b/src/conf/virnetworkobj.c
@@ -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;
diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c
index e3bcbe593d..792ceb1f15 100644
--- a/src/conf/virsecretobj.c
+++ b/src/conf/virsecretobj.c
@@ -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;
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 04aa8d53c0..dbebf61961 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -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);
         }
     }
 
diff --git a/src/util/virhash.c b/src/util/virhash.c
index a8e0edfd3b..7fa2992f18 100644
--- a/src/util/virhash.c
+++ b/src/util/virhash.c
@@ -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;
 }
diff --git a/src/util/virhash.h b/src/util/virhash.h
index 61c172b9e0..00b2550e70 100644
--- a/src/util/virhash.h
+++ b/src/util/virhash.h
@@ -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);
diff --git a/src/xen/xm_internal.c b/src/xen/xm_internal.c
index fba814aa19..123f379572 100644
--- a/src/xen/xm_internal.c
+++ b/src/xen/xm_internal.c
@@ -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];
diff --git a/tests/virhashtest.c b/tests/virhashtest.c
index dbf07ae7f7..9407f98c4b 100644
--- a/tests/virhashtest.c
+++ b/tests/virhashtest.c
@@ -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",
-- 
2.13.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]