util: alloc: Reimplement VIR_APPEND_ELEMENT_COPY using virAppendElement

Use virAppendElement instead of virInsertElementsN to implement
VIR_APPEND_ELEMENT_COPY which allows us to remove error handling as the
only relevant errors were removed when switching to aborting memory
allocation functions.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2021-08-03 14:14:20 +02:00
parent 0215139bc6
commit 592517636f
12 changed files with 27 additions and 84 deletions

View File

@ -16573,8 +16573,7 @@ virDomainMemoryInsert(virDomainDef *def,
return -1;
}
if (VIR_APPEND_ELEMENT_COPY(def->mems, def->nmems, mem) < 0)
return -1;
VIR_APPEND_ELEMENT_COPY(def->mems, def->nmems, mem);
virDomainDefSetMemoryTotal(def, memory + mem->size);
@ -17141,10 +17140,7 @@ virDomainDefAddController(virDomainDef *def, int type, int idx, int model)
cont->idx = idx;
cont->model = model;
if (VIR_APPEND_ELEMENT_COPY(def->controllers, def->ncontrollers, cont) < 0) {
VIR_FREE(cont);
return NULL;
}
VIR_APPEND_ELEMENT_COPY(def->controllers, def->ncontrollers, cont);
return cont;
}
@ -22780,15 +22776,9 @@ virDomainIOThreadIDAdd(virDomainDef *def,
iothrid->iothread_id = iothread_id;
if (VIR_APPEND_ELEMENT_COPY(def->iothreadids, def->niothreadids,
iothrid) < 0)
goto error;
VIR_APPEND_ELEMENT_COPY(def->iothreadids, def->niothreadids, iothrid);
return iothrid;
error:
virDomainIOThreadIDDefFree(iothrid);
return NULL;
}
@ -29304,13 +29294,9 @@ virDomainGraphicsListenAppendAddress(virDomainGraphicsDef *def,
glisten.address = g_strdup(address);
if (VIR_APPEND_ELEMENT_COPY(def->listens, def->nListens, glisten) < 0)
goto error;
VIR_APPEND_ELEMENT_COPY(def->listens, def->nListens, glisten);
return 0;
error:
VIR_FREE(glisten.address);
return -1;
}
@ -29326,14 +29312,9 @@ virDomainGraphicsListenAppendSocket(virDomainGraphicsDef *def,
glisten.socket = g_strdup(socketPath);
if (VIR_APPEND_ELEMENT_COPY(def->listens, def->nListens, glisten) < 0)
goto error;
VIR_APPEND_ELEMENT_COPY(def->listens, def->nListens, glisten);
return 0;
error:
VIR_FREE(glisten.socket);
return -1;
}

View File

@ -394,11 +394,9 @@ virNWFilterRuleDefAddString(virNWFilterRuleDef *nwf,
const char *string,
size_t maxstrlen)
{
char *tmp;
char *tmp = g_strndup(string, maxstrlen);
tmp = g_strndup(string, maxstrlen);
if (VIR_APPEND_ELEMENT_COPY(nwf->strings, nwf->nstrings, tmp) < 0)
VIR_FREE(tmp);
VIR_APPEND_ELEMENT_COPY(nwf->strings, nwf->nstrings, tmp);
return tmp;
}
@ -2696,11 +2694,7 @@ virNWFilterDefParseXML(xmlXPathContextPtr ctxt)
}
if (entry->rule || entry->include) {
if (VIR_APPEND_ELEMENT_COPY(ret->filterEntries,
ret->nentries, entry) < 0) {
virNWFilterEntryFree(entry);
goto cleanup;
}
VIR_APPEND_ELEMENT_COPY(ret->filterEntries, ret->nentries, entry);
} else {
virNWFilterEntryFree(entry);
}

View File

@ -371,12 +371,8 @@ virNWFilterObjListAssignDef(virNWFilterObjList *nwfilters,
if (!(obj = virNWFilterObjNew()))
return NULL;
if (VIR_APPEND_ELEMENT_COPY(nwfilters->objs,
nwfilters->count, obj) < 0) {
virNWFilterObjUnlock(obj);
virNWFilterObjFree(obj);
return NULL;
}
VIR_APPEND_ELEMENT_COPY(nwfilters->objs, nwfilters->count, obj);
obj->def = def;
return obj;

View File

@ -514,9 +514,8 @@ virCPUx86DataAddItem(virCPUx86Data *data,
if ((existing = virCPUx86DataGet(data, item))) {
virCPUx86DataItemSetBits(existing, item);
} else {
if (VIR_APPEND_ELEMENT_COPY(data->items, data->len,
*((virCPUx86DataItem *)item)) < 0)
return -1;
VIR_APPEND_ELEMENT_COPY(data->items, data->len,
*((virCPUx86DataItem *)item));
qsort(data->items, data->len,
sizeof(virCPUx86DataItem), virCPUx86DataSorter);
@ -1143,11 +1142,8 @@ x86FeatureParse(xmlXPathContextPtr ctxt,
return -1;
}
if (!feature->migratable &&
VIR_APPEND_ELEMENT_COPY(map->migrate_blockers,
map->nblockers,
feature) < 0)
return -1;
if (!feature->migratable)
VIR_APPEND_ELEMENT_COPY(map->migrate_blockers, map->nblockers, feature);
if (VIR_APPEND_ELEMENT(map->features, map->nfeatures, feature) < 0)
return -1;

View File

@ -318,8 +318,7 @@ virLogHandlerNewPostExecRestart(virJSONValue *object,
if (!(file = virLogHandlerLogFilePostExecRestart(handler, child)))
goto error;
if (VIR_APPEND_ELEMENT_COPY(handler->files, handler->nfiles, file) < 0)
goto error;
VIR_APPEND_ELEMENT_COPY(handler->files, handler->nfiles, file);
if ((file->watch = virEventAddHandle(file->pipefd,
VIR_EVENT_HANDLE_READABLE,
@ -401,8 +400,7 @@ virLogHandlerDomainOpenLogFile(virLogHandler *handler,
DEFAULT_MODE)) == NULL)
goto error;
if (VIR_APPEND_ELEMENT_COPY(handler->files, handler->nfiles, file) < 0)
goto error;
VIR_APPEND_ELEMENT_COPY(handler->files, handler->nfiles, file);
if ((file->watch = virEventAddHandle(file->pipefd,
VIR_EVENT_HANDLE_READABLE,

View File

@ -206,8 +206,7 @@ openvzReadNetworkConf(virDomainDef *def,
if (virDomainNetAppendIPAddress(net, token, AF_UNSPEC, 0) < 0)
goto error;
if (VIR_APPEND_ELEMENT_COPY(def->nets, def->nnets, net) < 0)
goto error;
VIR_APPEND_ELEMENT_COPY(def->nets, def->nnets, net);
token = strtok_r(NULL, " ", &saveptr);
}
@ -275,7 +274,7 @@ openvzReadNetworkConf(virDomainDef *def,
}
}
ignore_value(VIR_APPEND_ELEMENT_COPY(def->nets, def->nnets, net));
VIR_APPEND_ELEMENT_COPY(def->nets, def->nnets, net);
}
}

View File

@ -4001,11 +4001,7 @@ qemuDomainDefAddDefaultDevices(virQEMUDriver *driver,
if (j == def->npanics) {
virDomainPanicDef *panic = g_new0(virDomainPanicDef, 1);
if (VIR_APPEND_ELEMENT_COPY(def->panics,
def->npanics, panic) < 0) {
VIR_FREE(panic);
return -1;
}
VIR_APPEND_ELEMENT_COPY(def->panics, def->npanics, panic);
}
}

View File

@ -1236,8 +1236,7 @@ qemuDomainAttachNetDevice(virQEMUDriver *driver,
virDomainNetGetActualHostdev(net)) < 0) {
goto cleanup;
}
if (VIR_APPEND_ELEMENT_COPY(vm->def->nets, vm->def->nnets, net) < 0)
goto cleanup;
VIR_APPEND_ELEMENT_COPY(vm->def->nets, vm->def->nnets, net);
/* the rest of the setup doesn't apply to hostdev interfaces, so
* we can skip straight to the cleanup (nothing there applies to
@ -1272,8 +1271,7 @@ qemuDomainAttachNetDevice(virQEMUDriver *driver,
* locked memory limit). This means we will need to remove it if
* there is a failure.
*/
if (VIR_APPEND_ELEMENT_COPY(vm->def->nets, vm->def->nnets, net) < 0)
goto cleanup;
VIR_APPEND_ELEMENT_COPY(vm->def->nets, vm->def->nnets, net);
switch (actualType) {
case VIR_DOMAIN_NET_TYPE_BRIDGE:

View File

@ -234,8 +234,8 @@ int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false)
#define VIR_APPEND_ELEMENT_COPY(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false)
virAppendElement(&(ptr), sizeof(*(ptr)), &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false)
#define VIR_APPEND_ELEMENT_INPLACE(ptr, count, newelem) \
virAppendElement(&(ptr), sizeof(*(ptr)), &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), \

View File

@ -313,13 +313,9 @@ virFirewallAddRuleFullV(virFirewall *firewall,
ADD_ARG(rule, str);
if (group->addingRollback) {
ignore_value(VIR_APPEND_ELEMENT_COPY(group->rollback,
group->nrollback,
rule));
VIR_APPEND_ELEMENT_COPY(group->rollback, group->nrollback, rule);
} else {
ignore_value(VIR_APPEND_ELEMENT_COPY(group->action,
group->naction,
rule));
VIR_APPEND_ELEMENT_COPY(group->action, group->naction, rule);
}

View File

@ -578,8 +578,7 @@ prlsdkAddDomainVideoInfoVm(PRL_HANDLE sdkdom, virDomainDef *def)
video = g_new0(virDomainVideoDef, 1);
accel = g_new0(virDomainVideoAccelDef, 1);
if (VIR_APPEND_ELEMENT_COPY(def->videos, def->nvideos, video) < 0)
goto error;
VIR_APPEND_ELEMENT_COPY(def->videos, def->nvideos, video);
video->type = VIR_DOMAIN_VIDEO_TYPE_VGA;
video->vram = videoRam << 10; /* from mbibytes to kbibytes */
@ -587,11 +586,6 @@ prlsdkAddDomainVideoInfoVm(PRL_HANDLE sdkdom, virDomainDef *def)
video->accel = accel;
return 0;
error:
VIR_FREE(accel);
virDomainVideoDefFree(video);
return -1;
}
static int

View File

@ -269,12 +269,7 @@ virNWFilterDefToInst(const char *xml,
if (!def)
return -1;
if (VIR_APPEND_ELEMENT_COPY(inst->filters,
inst->nfilters,
def) < 0) {
virNWFilterDefFree(def);
goto cleanup;
}
VIR_APPEND_ELEMENT_COPY(inst->filters, inst->nfilters, def);
for (i = 0; i < def->nentries; i++) {
if (def->filterEntries[i]->rule) {