]> xenbits.xensource.com Git - libvirt.git/commitdiff
virCapabilitiesAllocMachines: Use NULL-terminated list as argument and return count
authorPeter Krempa <pkrempa@redhat.com>
Fri, 17 Mar 2023 13:12:11 +0000 (14:12 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 3 Apr 2023 07:19:07 +0000 (09:19 +0200)
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>
src/conf/capabilities.c
src/conf/capabilities.h
src/libxl/libxl_capabilities.c
tests/testutilsqemu.c
tests/testutilsxen.c

index 9e1bb0862092b4e095150e425fddd213ba8636d2..8bddbf79c3a9af5b1b5dde68fce9c29f635674b8 100644 (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]);
     }
index ef6e8ab6855095e4d07feee39a4bfe18e62c9edd..07f7a3ef581f3a949a37b803fbb31058d43c88b7 100644 (file)
@@ -261,7 +261,7 @@ virCapabilitiesHostNUMAAddCell(virCapsHostNUMA *caps,
 
 virCapsGuestMachine **
 virCapabilitiesAllocMachines(const char *const *names,
-                             int nnames);
+                             int *nnames);
 void
 virCapabilitiesFreeMachines(virCapsGuestMachine **machines,
                             int nmachines);
index 9fdc208f205d2cabc0615c13f7e25db1c6ca3a4f..5ee6fe3f67c58392849777be94e5a5a11b57b83f 100644 (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);
index d5922f302df5a9e9194ba152035e98e81c643d81..f6d49bc193d861d6b501d985760625a9daeb634e 100644 (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;
 }
 
 
index 821ee49d94a2e20302e0ec6c905bc13908cccfa2..3484cccbf2d89df43d987f525eee4fe368e5d371 100644 (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;
 }