[PATCHv4 09/17] util: use VIR_(APPEND|DELETE)_ELEMENT for pci/usb device lists

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

 



In both pci and usb cases, the count that held the size of the list
was int so it had to be changed to size_t.
---
 src/util/hostusb.c | 26 +++++++-------------------
 src/util/pci.c     | 18 +++---------------
 2 files changed, 10 insertions(+), 34 deletions(-)

diff --git a/src/util/hostusb.c b/src/util/hostusb.c
index 81a9f5a..88a7dfc 100644
--- a/src/util/hostusb.c
+++ b/src/util/hostusb.c
@@ -56,7 +56,7 @@ struct _usbDevice {
 };
 
 struct _usbDeviceList {
-    unsigned int count;
+    size_t count;
     usbDevice **devs;
 };
 
@@ -426,13 +426,11 @@ usbDeviceListAdd(usbDeviceList *list,
         return -1;
     }
 
-    if (VIR_REALLOC_N(list->devs, list->count+1) < 0) {
+    if (VIR_APPEND_ELEMENT(list->devs, list->count, dev) < 0) {
         virReportOOMError();
         return -1;
     }
 
-    list->devs[list->count++] = dev;
-
     return 0;
 }
 
@@ -461,22 +459,12 @@ usbDeviceListSteal(usbDeviceList *list,
     int i;
 
     for (i = 0; i < list->count; i++) {
-        if (list->devs[i]->bus != dev->bus ||
-            list->devs[i]->dev != dev->dev)
-            continue;
-
-        ret = list->devs[i];
-
-        if (i != list->count--)
-            memmove(&list->devs[i],
-                    &list->devs[i+1],
-                    sizeof(*list->devs) * (list->count - i));
-
-        if (VIR_REALLOC_N(list->devs, list->count) < 0) {
-            ; /* not fatal */
+        if (list->devs[i]->bus == dev->bus &&
+            list->devs[i]->dev == dev->dev) {
+            ret = list->devs[i];
+            VIR_DELETE_ELEMENT(list->devs, i, list->count);
+            break;
         }
-
-        break;
     }
     return ret;
 }
diff --git a/src/util/pci.c b/src/util/pci.c
index 5971764..ba6fe82 100644
--- a/src/util/pci.c
+++ b/src/util/pci.c
@@ -72,7 +72,7 @@ struct _pciDevice {
 };
 
 struct _pciDeviceList {
-    unsigned count;
+    size_t count;
     pciDevice **devs;
 };
 
@@ -1561,13 +1561,11 @@ pciDeviceListAdd(pciDeviceList *list,
         return -1;
     }
 
-    if (VIR_REALLOC_N(list->devs, list->count+1) < 0) {
+    if (VIR_APPEND_ELEMENT(list->devs, list->count, dev) < 0) {
         virReportOOMError();
         return -1;
     }
 
-    list->devs[list->count++] = dev;
-
     return 0;
 }
 
@@ -1599,17 +1597,7 @@ pciDeviceListStealIndex(pciDeviceList *list,
         return NULL;
 
     ret = list->devs[idx];
-
-    if (idx != --list->count) {
-        memmove(&list->devs[idx],
-                &list->devs[idx + 1],
-                sizeof(*list->devs) * (list->count - idx));
-    }
-
-    if (VIR_REALLOC_N(list->devs, list->count) < 0) {
-        ; /* not fatal */
-    }
-
+    VIR_DELETE_ELEMENT(list->devs, idx, list->count);
     return ret;
 }
 
-- 
1.7.11.7

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