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

[libvirt] [PATCHv2 08/11] util: use VIR_(APPEND|DELETE)_ELEMENTS_N for hardware device lists



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 | 24 ++++++------------------
 src/util/pci.c     | 30 +++++++++---------------------
 2 files changed, 15 insertions(+), 39 deletions(-)

diff --git a/src/util/hostusb.c b/src/util/hostusb.c
index 81a9f5a..b0c87d6 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_ELEMENTS_N(list->devs, list->count, 1, &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;
-
+        if (list->devs[i]->bus == dev->bus &&
+            list->devs[i]->dev == dev->dev) {
         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 */
-        }
-
+        ignore_value(VIR_DELETE_ELEMENTS_N(list->devs, i, list->count, i));
         break;
+        }
     }
     return ret;
 }
diff --git a/src/util/pci.c b/src/util/pci.c
index 83d86b7..7d90ef6 100644
--- a/src/util/pci.c
+++ b/src/util/pci.c
@@ -74,7 +74,7 @@ struct _pciDevice {
 };
 
 struct _pciDeviceList {
-    unsigned count;
+    size_t count;
     pciDevice **devs;
 };
 
@@ -1525,13 +1525,11 @@ pciDeviceListAdd(pciDeviceList *list,
         return -1;
     }
 
-    if (VIR_REALLOC_N(list->devs, list->count+1) < 0) {
+    if (VIR_APPEND_ELEMENTS_N(list->devs, list->count, 1, &dev) < 0) {
         virReportOOMError();
         return -1;
     }
 
-    list->devs[list->count++] = dev;
-
     return 0;
 }
 
@@ -1561,24 +1559,14 @@ pciDeviceListSteal(pciDeviceList *list,
     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;
-
-        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]->domain   == dev->domain &&
+            list->devs[i]->bus      == dev->bus    &&
+            list->devs[i]->slot     == dev->slot   &&
+            list->devs[i]->function == dev->function) {
+           ret = list->devs[i];
+           ignore_value(VIR_DELETE_ELEMENTS_N(list->devs, i, list->count, 1));
+           break;
         }
-
-        break;
     }
     return ret;
 }
-- 
1.7.11.7


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