The convenience macros replace manual alloc/movement except in cases
where the list has been pre-allocated to the necessary size, *and* a new
item is just being appended - in that case it's just as concise (and
arguably easier to interpret) to leave it as is - a simple direct
assignment ("list[count++] = item").
Note that when the pointer being appended to the array will continue
to be used by its original name after the append is done, we must use
VIR_APPEND_ELEMENT_COPY, so that the original pointer isn't
cleared. This was only necessary in one case (virDomainNetInsert(),
since the same pointer may also be sent to virDomainHostdevInsert())
---
src/conf/domain_conf.c | 105 +++++++------------------------------------------
1 file changed, 14 insertions(+), 91 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 6aa5f79..321d6dd 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7840,9 +7840,8 @@ virDomainChrTargetTypeToString(int deviceType,
int
virDomainHostdevInsert(virDomainDefPtr def, virDomainHostdevDefPtr hostdev)
{
- if (VIR_REALLOC_N(def->hostdevs, def->nhostdevs + 1) < 0)
+ if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0)
return -1;
- def->hostdevs[def->nhostdevs++] = hostdev;
return 0;
}
@@ -7851,19 +7850,7 @@ virDomainHostdevRemove(virDomainDefPtr def, size_t i)
{
virDomainHostdevDefPtr hostdev = def->hostdevs[i];
- if (def->nhostdevs > 1) {
- memmove(def->hostdevs + i,
- def->hostdevs + i + 1,
- sizeof(*def->hostdevs) *
- (def->nhostdevs - (i + 1)));
- def->nhostdevs--;
- if (VIR_REALLOC_N(def->hostdevs, def->nhostdevs) < 0) {
- /* ignore, harmless */
- }
- } else {
- VIR_FREE(def->hostdevs);
- def->nhostdevs = 0;
- }
+ VIR_DELETE_ELEMENT(def->hostdevs, i, def->nhostdevs);
return hostdev;
}
@@ -8018,13 +8005,8 @@ void virDomainDiskInsertPreAlloced(virDomainDefPtr def,
if (insertAt == -1)
insertAt = def->ndisks;
- if (insertAt < def->ndisks)
- memmove(def->disks + insertAt + 1,
- def->disks + insertAt,
- (sizeof(def->disks[0]) * (def->ndisks-insertAt)));
-
- def->disks[insertAt] = disk;
- def->ndisks++;
+ ignore_value(VIR_INSERT_ELEMENT_INPLACE(def->disks, insertAt,
+ def->ndisks, disk));
}
@@ -8033,19 +8015,7 @@ virDomainDiskRemove(virDomainDefPtr def, size_t i)
{
virDomainDiskDefPtr disk = def->disks[i];
- if (def->ndisks > 1) {
- memmove(def->disks + i,
- def->disks + i + 1,
- sizeof(*def->disks) *
- (def->ndisks - (i + 1)));
- def->ndisks--;
- if (VIR_REALLOC_N(def->disks, def->ndisks) < 0) {
- /* ignore, harmless */
- }
- } else {
- VIR_FREE(def->disks);
- def->ndisks = 0;
- }
+ VIR_DELETE_ELEMENT(def->disks, i, def->ndisks);
return disk;
}
@@ -8072,10 +8042,8 @@ virDomainHasDiskMirror(virDomainObjPtr vm)
int virDomainNetInsert(virDomainDefPtr def, virDomainNetDefPtr net)
{
- if (VIR_REALLOC_N(def->nets, def->nnets + 1) < 0)
+ if (VIR_APPEND_ELEMENT_COPY(def->nets, def->nnets, net) < 0)
return -1;
- def->nets[def->nnets] = net;
- def->nnets++;
if (net->type == VIR_DOMAIN_NET_TYPE_HOSTDEV) {
/* hostdev net devices must also exist in the hostdevs array */
return virDomainHostdevInsert(def, &net->data.hostdev.def);
@@ -8148,18 +8116,7 @@ virDomainNetRemove(virDomainDefPtr def, size_t i)
}
}
}
- if (def->nnets > 1) {
- memmove(def->nets + i,
- def->nets + i + 1,
- sizeof(*def->nets) * (def->nnets - (i + 1)));
- def->nnets--;
- if (VIR_REALLOC_N(def->nets, def->nnets) < 0) {
- /* ignore harmless */
- }
- } else {
- VIR_FREE(def->nets);
- def->nnets = 0;
- }
+ VIR_DELETE_ELEMENT(def->nets, i, def->nnets);
return net;
}
@@ -8206,13 +8163,8 @@ void virDomainControllerInsertPreAlloced(virDomainDefPtr def,
if (insertAt == -1)
insertAt = def->ncontrollers;
- if (insertAt < def->ncontrollers)
- memmove(def->controllers + insertAt + 1,
- def->controllers + insertAt,
- (sizeof(def->controllers[0]) * (def->ncontrollers-insertAt)));
-
- def->controllers[insertAt] = controller;
- def->ncontrollers++;
+ ignore_value(VIR_INSERT_ELEMENT_INPLACE(def->controllers, insertAt,
+ def->ncontrollers, controller));
}
int
@@ -8236,20 +8188,7 @@ virDomainControllerRemove(virDomainDefPtr def, size_t i)
{
virDomainControllerDefPtr controller = def->controllers[i];
- if (def->ncontrollers > 1) {
- memmove(def->controllers + i,
- def->controllers + i + 1,
- sizeof(*def->controllers) *
- (def->ncontrollers - (i + 1)));
- def->ncontrollers--;
- if (VIR_REALLOC_N(def->controllers, def->ncontrollers) < 0) {
- /* ignore, harmless */
- }
- } else {
- VIR_FREE(def->controllers);
- def->ncontrollers = 0;
- }
-
+ VIR_DELETE_ELEMENT(def->controllers, i, def->ncontrollers);
return controller;
}
@@ -8311,16 +8250,7 @@ virDomainLeaseRemoveAt(virDomainDefPtr def, size_t i)
virDomainLeaseDefPtr lease = def->leases[i];
- if (def->nleases > 1) {
- memmove(def->leases + i,
- def->leases + i + 1,
- sizeof(*def->leases) *
- (def->nleases - (i + 1)));
- VIR_SHRINK_N(def->leases, def->nleases, 1);
- } else {
- VIR_FREE(def->leases);
- def->nleases = 0;
- }
+ VIR_DELETE_ELEMENT(def->leases, i, def->nleases);
return lease;
}
@@ -8607,15 +8537,11 @@ static int virDomainDefMaybeAddController(virDomainDefPtr def,
cont->opts.vioserial.vectors = -1;
}
-
- if (VIR_REALLOC_N(def->controllers, def->ncontrollers+1) < 0) {
+ if (VIR_APPEND_ELEMENT(def->controllers, def->ncontrollers, cont) < 0) {
VIR_FREE(cont);
virReportOOMError();
return -1;
}
- def->controllers[def->ncontrollers] = cont;
- def->ncontrollers++;
-
return 0;
}
@@ -9923,12 +9849,10 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps,
input->bus = VIR_DOMAIN_INPUT_BUS_XEN;
}
- if (VIR_REALLOC_N(def->inputs, def->ninputs + 1) < 0) {
+ if (VIR_APPEND_ELEMENT(def->inputs, def->ninputs, input) < 0) {
virDomainInputDefFree(input);
goto no_memory;
}
- def->inputs[def->ninputs] = input;
- def->ninputs++;
}
@@ -9980,11 +9904,10 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps,
}
video->vram = virDomainVideoDefaultRAM(def, video->type);
video->heads = 1;
- if (VIR_ALLOC_N(def->videos, 1) < 0) {
+ if (VIR_APPEND_ELEMENT(def->videos, def->nvideos, video) < 0) {
virDomainVideoDefFree(video);
goto no_memory;
}
- def->videos[def->nvideos++] = video;
}
/* analysis of the host devices */
--
1.7.11.7