]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: use VIR_(APPEND|DELETE)_ELEMENT for pci/usb device lists
authorLaine Stump <laine@laine.org>
Fri, 5 Jul 2013 18:46:35 +0000 (14:46 -0400)
committerLaine Stump <laine@laine.org>
Thu, 11 Jul 2013 02:52:12 +0000 (22:52 -0400)
Eliminate memmove() by using VIR_*_ELEMENT API instead.

In both pci and usb cases, the count that held the size of the list
was unsigned int so it had to be changed to size_t.

src/util/virpci.c
src/util/virusb.c

index d7ebeace49564238a0a1bd488506ccfbe4aad208..ed55b80f31591cf8b6092cb25c24b0ffdaf7c69a 100644 (file)
@@ -80,7 +80,7 @@ struct _virPCIDevice {
 struct _virPCIDeviceList {
     virObjectLockable parent;
 
-    unsigned int count;
+    size_t count;
     virPCIDevicePtr *devs;
 };
 
@@ -1669,13 +1669,7 @@ virPCIDeviceListAdd(virPCIDeviceListPtr list,
                        _("Device %s is already in use"), dev->name);
         return -1;
     }
-
-    if (VIR_REALLOC_N(list->devs, list->count+1) < 0)
-        return -1;
-
-    list->devs[list->count++] = dev;
-
-    return 0;
+    return VIR_APPEND_ELEMENT(list->devs, list->count, dev, true);
 }
 
 
@@ -1723,17 +1717,7 @@ virPCIDeviceListStealIndex(virPCIDeviceListPtr 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;
 }
 
index a82754e076a7ddd8c3f64ba475030547995885c5..60222f4f9703bc56ace4417f84ce5bf10fe69790 100644 (file)
@@ -60,7 +60,7 @@ struct _virUSBDevice {
 
 struct _virUSBDeviceList {
     virObjectLockable parent;
-    unsigned int count;
+    size_t count;
     virUSBDevicePtr *devs;
 };
 
@@ -450,13 +450,7 @@ virUSBDeviceListAdd(virUSBDeviceListPtr list,
                        dev->name);
         return -1;
     }
-
-    if (VIR_REALLOC_N(list->devs, list->count+1) < 0)
-        return -1;
-
-    list->devs[list->count++] = dev;
-
-    return 0;
+    return VIR_APPEND_ELEMENT(list->devs, list->count, dev, true);
 }
 
 virUSBDevicePtr
@@ -484,22 +478,12 @@ virUSBDeviceListSteal(virUSBDeviceListPtr list,
     size_t 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;
 }