[PATCH 2/4] util: Slightly refactor PCI list functions

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

 



In order to be able to steal PCI device by its index in the list.
---
 src/libvirt_private.syms |  2 ++
 src/util/pci.c           | 60 +++++++++++++++++++++++++++++-------------------
 src/util/pci.h           |  4 ++++
 3 files changed, 42 insertions(+), 24 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 41e2629..625490f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1000,10 +1000,12 @@ pciDeviceListAdd;
 pciDeviceListCount;
 pciDeviceListDel;
 pciDeviceListFind;
+pciDeviceListFindIndex;
 pciDeviceListFree;
 pciDeviceListGet;
 pciDeviceListNew;
 pciDeviceListSteal;
+pciDeviceListStealIndex;
 pciDeviceNetName;
 pciDeviceReAttachInit;
 pciDeviceSetManaged;
diff --git a/src/util/pci.c b/src/util/pci.c
index 191f99d..3ebf6f7 100644
--- a/src/util/pci.c
+++ b/src/util/pci.c
@@ -1554,35 +1554,36 @@ pciDeviceListCount(pciDeviceList *list)
 }
 
 pciDevice *
-pciDeviceListSteal(pciDeviceList *list,
-                   pciDevice *dev)
+pciDeviceListStealIndex(pciDeviceList *list,
+                        int idx)
 {
-    pciDevice *ret = NULL;
-    int i;
-
-    for (i = 0; i < list->count; i++) {
-        if (list->devs[i]->domain   != dev->domain ||
-            list->devs[i]->bus      != dev->bus    ||
-            list->devs[i]->slot     != dev->slot   ||
-            list->devs[i]->function != dev->function)
-            continue;
+    pciDevice *ret;
 
-        ret = list->devs[i];
+    if (idx < 0)
+        return NULL;
 
-        if (i != --list->count)
-            memmove(&list->devs[i],
-                    &list->devs[i+1],
-                    sizeof(*list->devs) * (list->count-i));
+    ret = list->devs[idx];
 
-        if (VIR_REALLOC_N(list->devs, list->count) < 0) {
-            ; /* not fatal */
-        }
+    if (idx != --list->count) {
+        memmove(&list->devs[idx],
+                &list->devs[idx + 1],
+                sizeof(*list->devs) * (list->count - idx));
+    }
 
-        break;
+    if (VIR_REALLOC_N(list->devs, list->count) < 0) {
+        ; /* not fatal */
     }
+
     return ret;
 }
 
+pciDevice *
+pciDeviceListSteal(pciDeviceList *list,
+                   pciDevice *dev)
+{
+    return pciDeviceListStealIndex(list, pciDeviceListFindIndex(list, dev));
+}
+
 void
 pciDeviceListDel(pciDeviceList *list,
                  pciDevice *dev)
@@ -1592,8 +1593,8 @@ pciDeviceListDel(pciDeviceList *list,
         pciFreeDevice(ret);
 }
 
-pciDevice *
-pciDeviceListFind(pciDeviceList *list, pciDevice *dev)
+int
+pciDeviceListFindIndex(pciDeviceList *list, pciDevice *dev)
 {
     int i;
 
@@ -1602,8 +1603,19 @@ pciDeviceListFind(pciDeviceList *list, pciDevice *dev)
             list->devs[i]->bus      == dev->bus    &&
             list->devs[i]->slot     == dev->slot   &&
             list->devs[i]->function == dev->function)
-            return list->devs[i];
-    return NULL;
+            return i;
+    return -1;
+}
+
+pciDevice *
+pciDeviceListFind(pciDeviceList *list, pciDevice *dev)
+{
+    int i;
+
+    if ((i = pciDeviceListFindIndex(list, dev)) >= 0)
+        return list->devs[i];
+    else
+        return NULL;
 }
 
 
diff --git a/src/util/pci.h b/src/util/pci.h
index d3cc85d..814c24e 100644
--- a/src/util/pci.h
+++ b/src/util/pci.h
@@ -75,10 +75,14 @@ pciDevice *    pciDeviceListGet (pciDeviceList *list,
 int            pciDeviceListCount (pciDeviceList *list);
 pciDevice *    pciDeviceListSteal (pciDeviceList *list,
                                    pciDevice *dev);
+pciDevice *    pciDeviceListStealIndex(pciDeviceList *list,
+                                       int idx);
 void           pciDeviceListDel  (pciDeviceList *list,
                                   pciDevice *dev);
 pciDevice *    pciDeviceListFind (pciDeviceList *list,
                                   pciDevice *dev);
+int            pciDeviceListFindIndex(pciDeviceList *list,
+                                      pciDevice *dev);
 
 /*
  * Callback that will be invoked once for each file
-- 
1.8.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[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]