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

Laine Stump laine at laine.org
Fri Dec 7 18:16:43 UTC 2012


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..2eaf7a7 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..921826f 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




More information about the libvir-list mailing list