]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Slightly refactor PCI list functions
authorJiri Denemark <jdenemar@redhat.com>
Tue, 4 Dec 2012 07:30:46 +0000 (08:30 +0100)
committerJiri Denemark <jdenemar@redhat.com>
Wed, 5 Dec 2012 12:45:34 +0000 (13:45 +0100)
In order to be able to steal PCI device by its index in the list.

src/libvirt_private.syms
src/util/pci.c
src/util/pci.h

index df54f1e73c9df721f3afa9f5c56cb1e702313cb2..946bb20dcdc197164b56d768f7d7aa77d1dc5cf5 100644 (file)
@@ -1000,10 +1000,12 @@ pciDeviceListAdd;
 pciDeviceListCount;
 pciDeviceListDel;
 pciDeviceListFind;
+pciDeviceListFindIndex;
 pciDeviceListFree;
 pciDeviceListGet;
 pciDeviceListNew;
 pciDeviceListSteal;
+pciDeviceListStealIndex;
 pciDeviceNetName;
 pciDeviceReAttachInit;
 pciDeviceSetManaged;
index 191f99dc9f1d151f9942b229caa325ca1506b3ee..e410245c554e171b93f90e8d786989c49387dcdc 100644 (file)
@@ -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 || idx >= list->count)
+        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;
 }
 
 
index d3cc85d82a62b5c30dfbe9587c85fdac9f2d4bef..814c24e31d5bf9134b774b19e1df9389db9c990d 100644 (file)
@@ -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