]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Use new array management macros
authorOsier Yang <jyang@redhat.com>
Tue, 7 Jan 2014 14:44:27 +0000 (22:44 +0800)
committerOsier Yang <jyang@redhat.com>
Wed, 8 Jan 2014 15:00:34 +0000 (23:00 +0800)
Like commit 94a26c7e from Eric Blake, the old fuzzy code should
be replaced by the new array management macros now.

And the type of scsi->count should be changed into "size_t", and
thus virSCSIDeviceListCount should return size_t instead, similar
for vir{PCI,USB}DeviceListCount.

src/util/virpci.c
src/util/virpci.h
src/util/virscsi.c
src/util/virscsi.h
src/util/virusb.c
src/util/virusb.h

index bad6e0f8e1637722c286c7ac1730d4cf539dc755..51dbee668b8173f65e726cbd6ccb332a62e20975 100644 (file)
@@ -1752,7 +1752,7 @@ virPCIDeviceListGet(virPCIDeviceListPtr list,
     return list->devs[idx];
 }
 
-int
+size_t
 virPCIDeviceListCount(virPCIDeviceListPtr list)
 {
     return list->count;
index 0479f0bc202b5cf540cb6574adec667c9c1815b1..08bf4c37dd8e5c0ca253dcd5056292a49ba056b9 100644 (file)
@@ -86,7 +86,7 @@ int  virPCIDeviceListAdd(virPCIDeviceListPtr list,
 int virPCIDeviceListAddCopy(virPCIDeviceListPtr list, virPCIDevicePtr dev);
 virPCIDevicePtr virPCIDeviceListGet(virPCIDeviceListPtr list,
                                     int idx);
-int virPCIDeviceListCount(virPCIDeviceListPtr list);
+size_t virPCIDeviceListCount(virPCIDeviceListPtr list);
 virPCIDevicePtr virPCIDeviceListSteal(virPCIDeviceListPtr list,
                                       virPCIDevicePtr dev);
 virPCIDevicePtr virPCIDeviceListStealIndex(virPCIDeviceListPtr list,
index 7aca9e65c1f22d9d11ba5197f168c80b23bcc9db..751eaf05bf73bf69f7d20aacc331ce7138b70b66 100644 (file)
@@ -62,7 +62,7 @@ struct _virSCSIDevice {
 
 struct _virSCSIDeviceList {
     virObjectLockable parent;
-    unsigned int count;
+    size_t count;
     virSCSIDevicePtr *devs;
 };
 
@@ -356,12 +356,7 @@ virSCSIDeviceListAdd(virSCSIDeviceListPtr list,
         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);
 }
 
 virSCSIDevicePtr
@@ -373,7 +368,7 @@ virSCSIDeviceListGet(virSCSIDeviceListPtr list, int idx)
     return list->devs[idx];
 }
 
-int
+size_t
 virSCSIDeviceListCount(virSCSIDeviceListPtr list)
 {
     return list->count;
@@ -387,24 +382,14 @@ virSCSIDeviceListSteal(virSCSIDeviceListPtr list,
     size_t i;
 
     for (i = 0; i < list->count; i++) {
-        if (list->devs[i]->adapter != dev->adapter ||
-            list->devs[i]->bus != dev->bus ||
-            list->devs[i]->target != dev->target ||
-            list->devs[i]->unit != dev->unit)
-            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]->adapter == dev->adapter &&
+            list->devs[i]->bus == dev->bus &&
+            list->devs[i]->target == dev->target &&
+            list->devs[i]->unit == dev->unit) {
+            ret = list->devs[i];
+            VIR_DELETE_ELEMENT(list->devs, i, list->count);
+            break;
         }
-
-        break;
     }
 
     return ret;
index cce5df4899cbd79e610cf4f6069a652b94645e05..4c461f830d565973116571539dae60c6ab2ba753 100644 (file)
@@ -77,7 +77,7 @@ int virSCSIDeviceListAdd(virSCSIDeviceListPtr list,
                          virSCSIDevicePtr dev);
 virSCSIDevicePtr virSCSIDeviceListGet(virSCSIDeviceListPtr list,
                                       int idx);
-int virSCSIDeviceListCount(virSCSIDeviceListPtr list);
+size_t virSCSIDeviceListCount(virSCSIDeviceListPtr list);
 virSCSIDevicePtr virSCSIDeviceListSteal(virSCSIDeviceListPtr list,
                                         virSCSIDevicePtr dev);
 void virSCSIDeviceListDel(virSCSIDeviceListPtr list,
index 3c82200f2570b8bab1e8ec7aa3c0d6258d49e9e1..bb5980d7332d7ae3fe7486469a51d9d3ca0c64f3 100644 (file)
@@ -464,7 +464,7 @@ virUSBDeviceListGet(virUSBDeviceListPtr list,
     return list->devs[idx];
 }
 
-int
+size_t
 virUSBDeviceListCount(virUSBDeviceListPtr list)
 {
     return list->count;
index aa59d12cbabd126b139329ac21a49ef5db775a0b..e0a7c4cb1f5ec00bd8304f28d6ff1c0f87a55de4 100644 (file)
@@ -86,7 +86,7 @@ int virUSBDeviceListAdd(virUSBDeviceListPtr list,
                         virUSBDevicePtr dev);
 virUSBDevicePtr virUSBDeviceListGet(virUSBDeviceListPtr list,
                                     int idx);
-int virUSBDeviceListCount(virUSBDeviceListPtr list);
+size_t virUSBDeviceListCount(virUSBDeviceListPtr list);
 virUSBDevicePtr virUSBDeviceListSteal(virUSBDeviceListPtr list,
                                       virUSBDevicePtr dev);
 void virUSBDeviceListDel(virUSBDeviceListPtr list,