util: use VIR_(APPEND|DELETE)_ELEMENT for pci/usb device lists

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.
This commit is contained in:
Laine Stump 2013-07-05 14:46:35 -04:00
parent 9ec23a6663
commit 22c6829bc2
2 changed files with 10 additions and 42 deletions

View 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;
}

View 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;
}