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: * virCapabilitiesAllocMachines:
* @machines: machine variants for emulator ('pc', or 'isapc', etc) * @machines: NULL-terminated list of machine variants for emulator ('pc', or 'isapc', etc)
* @nmachines: number of machine variants for emulator * @nmachines: filled with number of machine variants for emulator
* *
* Allocate a table of virCapsGuestMachine *from the supplied table * Allocate a table of virCapsGuestMachine *from the supplied table
* of machine names. * of machine names.
*/ */
virCapsGuestMachine ** virCapsGuestMachine **
virCapabilitiesAllocMachines(const char *const *names, int nnames) virCapabilitiesAllocMachines(const char *const *names,
int *nnames)
{ {
virCapsGuestMachine **machines; virCapsGuestMachine **machines;
size_t i; 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] = g_new0(virCapsGuestMachine, 1);
machines[i]->name = g_strdup(names[i]); machines[i]->name = g_strdup(names[i]);
} }

View File

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

View File

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

View File

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

View File

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