virQEMUCapsGetMachineTypesCaps: Use GPtrArray

This simplyfies the code a bit and removes one "goto", one "VIR_FREE",
and one "VIR_INSERT_ELEMENT_COPY".

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Tim Wiederhake 2021-07-08 13:10:38 +02:00
parent 3ea1ec7fa2
commit b2435f10ac

View File

@ -942,6 +942,7 @@ virQEMUCapsGetMachineTypesCaps(virQEMUCaps *qemuCaps,
{ {
size_t i; size_t i;
virQEMUCapsAccel *accel; virQEMUCapsAccel *accel;
g_autoptr(GPtrArray) array = NULL;
/* Guest capabilities do not report TCG vs. KVM caps separately. We just /* Guest capabilities do not report TCG vs. KVM caps separately. We just
* take the set of machine types we probed first. */ * take the set of machine types we probed first. */
@ -953,13 +954,13 @@ virQEMUCapsGetMachineTypesCaps(virQEMUCaps *qemuCaps,
*machines = NULL; *machines = NULL;
*nmachines = accel->nmachineTypes; *nmachines = accel->nmachineTypes;
if (*nmachines) if (*nmachines == 0)
*machines = g_new0(virCapsGuestMachine *, accel->nmachineTypes); return 0;
array = g_ptr_array_sized_new(*nmachines);
for (i = 0; i < accel->nmachineTypes; i++) { for (i = 0; i < accel->nmachineTypes; i++) {
virCapsGuestMachine *mach; virCapsGuestMachine *mach = g_new0(virCapsGuestMachine, 1);
mach = g_new0(virCapsGuestMachine, 1);
(*machines)[i] = mach;
if (accel->machineTypes[i].alias) { if (accel->machineTypes[i].alias) {
mach->name = g_strdup(accel->machineTypes[i].alias); mach->name = g_strdup(accel->machineTypes[i].alias);
mach->canonical = g_strdup(accel->machineTypes[i].name); mach->canonical = g_strdup(accel->machineTypes[i].name);
@ -968,6 +969,7 @@ virQEMUCapsGetMachineTypesCaps(virQEMUCaps *qemuCaps,
} }
mach->maxCpus = accel->machineTypes[i].maxCpus; mach->maxCpus = accel->machineTypes[i].maxCpus;
mach->deprecated = accel->machineTypes[i].deprecated; mach->deprecated = accel->machineTypes[i].deprecated;
g_ptr_array_add(array, mach);
} }
/* Make sure all canonical machine types also have their own entry so that /* Make sure all canonical machine types also have their own entry so that
@ -975,18 +977,19 @@ virQEMUCapsGetMachineTypesCaps(virQEMUCaps *qemuCaps,
* supported machine types. * supported machine types.
*/ */
i = 0; i = 0;
while (i < *nmachines) { while (i < array->len) {
size_t j; size_t j;
bool found = false; bool found = false;
virCapsGuestMachine *machine = (*machines)[i]; virCapsGuestMachine *machine = g_ptr_array_index(array, i);
if (!machine->canonical) { if (!machine->canonical) {
i++; i++;
continue; continue;
} }
for (j = 0; j < *nmachines; j++) { for (j = 0; j < array->len; j++) {
if (STREQ(machine->canonical, (*machines)[j]->name)) { virCapsGuestMachine *mach = g_ptr_array_index(array, j);
if (STREQ(machine->canonical, mach->name)) {
found = true; found = true;
break; break;
} }
@ -995,25 +998,21 @@ virQEMUCapsGetMachineTypesCaps(virQEMUCaps *qemuCaps,
if (!found) { if (!found) {
virCapsGuestMachine *mach; virCapsGuestMachine *mach;
mach = g_new0(virCapsGuestMachine, 1); mach = g_new0(virCapsGuestMachine, 1);
if (VIR_INSERT_ELEMENT_COPY(*machines, i, *nmachines, mach) < 0) {
VIR_FREE(mach);
goto error;
}
mach->name = g_strdup(machine->canonical); mach->name = g_strdup(machine->canonical);
mach->maxCpus = machine->maxCpus; mach->maxCpus = machine->maxCpus;
mach->deprecated = machine->deprecated; mach->deprecated = machine->deprecated;
g_ptr_array_insert(array, i, mach);
i++; i++;
} }
i++; i++;
} }
return 0; *nmachines = array->len;
*machines = g_new0(virCapsGuestMachine *, array->len);
for (i = 0; i < array->len; ++i)
(*machines)[i] = g_ptr_array_index(array, i);
error: return 0;
virCapabilitiesFreeMachines(*machines, *nmachines);
*nmachines = 0;
*machines = NULL;
return -1;
} }