diff --git a/src/util/virpci.c b/src/util/virpci.c index d7ebeace49..ed55b80f31 100644 --- a/src/util/virpci.c +++ b/src/util/virpci.c @@ -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; } diff --git a/src/util/virusb.c b/src/util/virusb.c index a82754e076..60222f4f97 100644 --- a/src/util/virusb.c +++ b/src/util/virusb.c @@ -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; }