[libvirt PATCH 1/7] nodedev: add internal virNodeDeviceObjListFind()

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

 



This is a generic function that you can provide your own predicate
function to search for a particular device. It will be used in an
upcoming commit.

Signed-off-by: Jonathon Jongsma <jjongsma@xxxxxxxxxx>
---
 src/conf/virnodedeviceobj.c | 53 +++++++++++++++++++++++++++++++------
 src/conf/virnodedeviceobj.h | 11 +++++---
 src/libvirt_private.syms    |  1 +
 3 files changed, 54 insertions(+), 11 deletions(-)

diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c
index b213592b56..6e7b354f96 100644
--- a/src/conf/virnodedeviceobj.c
+++ b/src/conf/virnodedeviceobj.c
@@ -1026,19 +1026,19 @@ virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj,
 }
 
 
-struct virNodeDeviceObjListRemoveHelperData
+typedef struct
 {
-    virNodeDeviceObjListRemoveIterator callback;
+    virNodeDeviceObjListPredicate predicate;
     void *opaque;
-};
+} PredicateHelperData;
 
 static int virNodeDeviceObjListRemoveHelper(void *key G_GNUC_UNUSED,
                                             void *value,
                                             void *opaque)
 {
-    struct virNodeDeviceObjListRemoveHelperData *data = opaque;
+    PredicateHelperData *data = opaque;
 
-    return data->callback(value, data->opaque);
+    return data->predicate(value, data->opaque);
 }
 
 
@@ -1054,11 +1054,11 @@ static int virNodeDeviceObjListRemoveHelper(void *key G_GNUC_UNUSED,
  */
 void
 virNodeDeviceObjListForEachRemove(virNodeDeviceObjList *devs,
-                                  virNodeDeviceObjListRemoveIterator callback,
+                                  virNodeDeviceObjListPredicate callback,
                                   void *opaque)
 {
-    struct virNodeDeviceObjListRemoveHelperData data = {
-        .callback = callback,
+    PredicateHelperData data = {
+        .predicate = callback,
         .opaque = opaque
     };
 
@@ -1068,3 +1068,40 @@ virNodeDeviceObjListForEachRemove(virNodeDeviceObjList *devs,
                                 &data);
     virObjectRWUnlock(devs);
 }
+
+
+static int virNodeDeviceObjListFindHelper(const void *payload,
+                                          const char *name G_GNUC_UNUSED,
+                                          const void *opaque)
+{
+    PredicateHelperData *data = (PredicateHelperData *) opaque;
+    virNodeDeviceObj *obj = (virNodeDeviceObj *) payload;
+
+    return data->predicate(obj, data->opaque);
+}
+
+
+/**
+ * virNodeDeviceObjListFind
+ * @devs: Pointer to object list
+ * @predicate: function to test the device for a certain property
+ * @opaque: Opaque data to use as argument to helper
+ *
+ * For each object in @devs, call the @predicate helper using @opaque as
+ * an argument until it returns TRUE. The list may not be modified while
+ * iterating.
+ */
+virNodeDeviceObj *
+virNodeDeviceObjListFind(virNodeDeviceObjList *devs,
+                         virNodeDeviceObjListPredicate predicate,
+                         void *opaque)
+{
+    PredicateHelperData data = {
+        .predicate = predicate,
+        .opaque = opaque
+    };
+
+    return virNodeDeviceObjListSearch(devs,
+                                      virNodeDeviceObjListFindHelper,
+                                      &data);
+}
diff --git a/src/conf/virnodedeviceobj.h b/src/conf/virnodedeviceobj.h
index 7353e4619b..0cb78748a4 100644
--- a/src/conf/virnodedeviceobj.h
+++ b/src/conf/virnodedeviceobj.h
@@ -136,9 +136,14 @@ void
 virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj,
                               bool persistent);
 
-typedef bool (*virNodeDeviceObjListRemoveIterator)(virNodeDeviceObj *obj,
-                                                   const void *opaque);
+typedef bool (*virNodeDeviceObjListPredicate)(virNodeDeviceObj *obj,
+                                              const void *opaque);
 
 void virNodeDeviceObjListForEachRemove(virNodeDeviceObjList *devs,
-                                       virNodeDeviceObjListRemoveIterator callback,
+                                       virNodeDeviceObjListPredicate callback,
                                        void *opaque);
+
+virNodeDeviceObj *
+virNodeDeviceObjListFind(virNodeDeviceObjList *devs,
+                         virNodeDeviceObjListPredicate callback,
+                         void *opaque);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4bc2974e7f..9d16d7c6b3 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1287,6 +1287,7 @@ virNodeDeviceObjIsActive;
 virNodeDeviceObjIsPersistent;
 virNodeDeviceObjListAssignDef;
 virNodeDeviceObjListExport;
+virNodeDeviceObjListFind;
 virNodeDeviceObjListFindByName;
 virNodeDeviceObjListFindBySysfsPath;
 virNodeDeviceObjListFindMediatedDeviceByUUID;
-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux