virCapabilitiesAllocMachines: Use NULL-terminated list as argument and return count

Simplify use of the function by determining the number of elements
inside the function.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2023-03-17 14:12:11 +01:00
parent dca563b296
commit ccee1ee088
5 changed files with 23 additions and 49 deletions

View File

@ -380,21 +380,23 @@ virCapabilitiesHostNUMAAddCell(virCapsHostNUMA *caps,
/**
* virCapabilitiesAllocMachines:
* @machines: machine variants for emulator ('pc', or 'isapc', etc)
* @nmachines: number of machine variants for emulator
* @machines: NULL-terminated list of machine variants for emulator ('pc', or 'isapc', etc)
* @nmachines: filled with number of machine variants for emulator
*
* Allocate a table of virCapsGuestMachine *from the supplied table
* of machine names.
*/
virCapsGuestMachine **
virCapabilitiesAllocMachines(const char *const *names, int nnames)
virCapabilitiesAllocMachines(const char *const *names,
int *nnames)
{
virCapsGuestMachine **machines;
size_t i;
machines = g_new0(virCapsGuestMachine *, nnames);
*nnames = g_strv_length((gchar **)names);
machines = g_new0(virCapsGuestMachine *, *nnames);
for (i = 0; i < nnames; i++) {
for (i = 0; i < *nnames; i++) {
machines[i] = g_new0(virCapsGuestMachine, 1);
machines[i]->name = g_strdup(names[i]);
}

View File

@ -261,7 +261,7 @@ virCapabilitiesHostNUMAAddCell(virCapsHostNUMA *caps,
virCapsGuestMachine **
virCapabilitiesAllocMachines(const char *const *names,
int nnames);
int *nnames);
void
virCapabilitiesFreeMachines(virCapsGuestMachine **machines,
int nmachines);

View File

@ -464,6 +464,7 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCaps *caps)
for (i = 0; i < nr_guest_archs; ++i) {
virCapsGuest *guest;
virCapsGuestMachine **machines;
int nmachines;
virDomainOSType ostype = VIR_DOMAIN_OSTYPE_XEN;
const char *loader = NULL;
@ -473,22 +474,22 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCaps *caps)
ostype = VIR_DOMAIN_OSTYPE_HVM;
loader = LIBXL_FIRMWARE_DIR "/hvmloader";
machines = virCapabilitiesAllocMachines(xen_machines, 1);
machines = virCapabilitiesAllocMachines(xen_machines, &nmachines);
} else if (guest_archs[i].pvh) {
char const *const xen_machines[] = { "xenpvh", NULL };
ostype = VIR_DOMAIN_OSTYPE_XENPVH;
machines = virCapabilitiesAllocMachines(xen_machines, 1);
machines = virCapabilitiesAllocMachines(xen_machines, &nmachines);
} else {
char const *const xen_machines[] = { "xenpv", NULL };
ostype = VIR_DOMAIN_OSTYPE_XEN;
machines = virCapabilitiesAllocMachines(xen_machines, 1);
machines = virCapabilitiesAllocMachines(xen_machines, &nmachines);
}
guest = virCapabilitiesAddGuest(caps, ostype, guest_archs[i].arch,
LIBXL_EXECBIN_DIR "/qemu-system-i386",
loader, 1, machines);
loader, nmachines, machines);
virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_XEN,
NULL, NULL, 0, NULL);

View File

@ -158,7 +158,7 @@ static int
testQemuAddGuest(virCaps *caps,
virArch arch)
{
size_t nmachines;
int nmachines;
virCapsGuestMachine **machines = NULL;
virCapsGuest *guest;
virArch emu_arch = arch;
@ -169,19 +169,11 @@ testQemuAddGuest(virCaps *caps,
if (qemu_emulators[emu_arch] == NULL)
return 0;
nmachines = g_strv_length((gchar **)qemu_machines[emu_arch]);
machines = virCapabilitiesAllocMachines(qemu_machines[emu_arch],
nmachines);
if (machines == NULL)
goto error;
machines = virCapabilitiesAllocMachines(qemu_machines[emu_arch], &nmachines);
guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM,
arch, qemu_emulators[emu_arch],
NULL, nmachines, machines);
machines = NULL;
nmachines = 0;
if (arch == VIR_ARCH_I686 ||
arch == VIR_ARCH_X86_64)
virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_CPUSELECTION);
@ -189,21 +181,12 @@ testQemuAddGuest(virCaps *caps,
virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU,
NULL, NULL, 0, NULL);
nmachines = g_strv_length((char **)qemu_machines[emu_arch]);
machines = virCapabilitiesAllocMachines(qemu_machines[emu_arch],
nmachines);
if (machines == NULL)
goto error;
machines = virCapabilitiesAllocMachines(qemu_machines[emu_arch], &nmachines);
virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM,
qemu_emulators[emu_arch],
NULL, nmachines, machines);
return 0;
error:
virCapabilitiesFreeMachines(machines, nmachines);
return -1;
}

View File

@ -16,13 +16,13 @@ testXLInitCaps(void)
virCapsGuestMachine **machines;
int nmachines;
static const char *const x86_machines[] = {
"xenfv"
"xenfv", NULL,
};
static const char *const xen_machines[] = {
"xenpv",
"xenpv", NULL,
};
static const char *const pvh_machines[] = {
"xenpvh",
"xenpvh", NULL,
};
if ((caps = virCapabilitiesNew(virArchFromHost(),
@ -31,48 +31,36 @@ testXLInitCaps(void)
caps->host.cpu = virCPUDefCopy(&cpuDefaultData);
nmachines = G_N_ELEMENTS(x86_machines);
if ((machines = virCapabilitiesAllocMachines(x86_machines, nmachines)) == NULL)
goto cleanup;
machines = virCapabilitiesAllocMachines(x86_machines, &nmachines);
guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM,
VIR_ARCH_X86_64,
"/usr/lib/xen/bin/qemu-system-i386",
"/usr/lib/xen/boot/hvmloader",
nmachines, machines);
machines = NULL;
virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_XEN,
NULL, NULL, 0, NULL);
nmachines = G_N_ELEMENTS(xen_machines);
if ((machines = virCapabilitiesAllocMachines(xen_machines, nmachines)) == NULL)
goto cleanup;
machines = virCapabilitiesAllocMachines(xen_machines, &nmachines);
guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_XEN,
VIR_ARCH_X86_64,
"/usr/lib/xen/bin/qemu-system-i386",
NULL,
nmachines, machines);
machines = NULL;
virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_XEN,
NULL, NULL, 0, NULL);
nmachines = G_N_ELEMENTS(pvh_machines);
if ((machines = virCapabilitiesAllocMachines(pvh_machines, nmachines)) == NULL)
goto cleanup;
machines = virCapabilitiesAllocMachines(pvh_machines, &nmachines);
guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_XENPVH,
VIR_ARCH_X86_64,
"/usr/lib/xen/bin/qemu-system-i386",
NULL,
nmachines, machines);
machines = NULL;
virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_XEN,
NULL, NULL, 0, NULL);
return g_steal_pointer(&caps);
cleanup:
virCapabilitiesFreeMachines(machines, nmachines);
return NULL;
}