[PATCH 03/31] virCapabilitiesAllocMachines: Use NULL-terminated list as argument and return count

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/conf/capabilities.c        | 12 +++++++-----
 src/conf/capabilities.h        |  2 +-
 src/libxl/libxl_capabilities.c |  9 +++++----
 tests/testutilsqemu.c          | 23 +++--------------------
 tests/testutilsxen.c           | 26 +++++++-------------------
 5 files changed, 23 insertions(+), 49 deletions(-)

diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
index 9144ced498..6d2f6e282d 100644
--- a/src/conf/capabilities.c
+++ b/src/conf/capabilities.c
@@ -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]);
     }
diff --git a/src/conf/capabilities.h b/src/conf/capabilities.h
index ef6e8ab685..07f7a3ef58 100644
--- a/src/conf/capabilities.h
+++ b/src/conf/capabilities.h
@@ -261,7 +261,7 @@ virCapabilitiesHostNUMAAddCell(virCapsHostNUMA *caps,

 virCapsGuestMachine **
 virCapabilitiesAllocMachines(const char *const *names,
-                             int nnames);
+                             int *nnames);
 void
 virCapabilitiesFreeMachines(virCapsGuestMachine **machines,
                             int nmachines);
diff --git a/src/libxl/libxl_capabilities.c b/src/libxl/libxl_capabilities.c
index 82c38d1b26..2f4bc8bde8 100644
--- a/src/libxl/libxl_capabilities.c
+++ b/src/libxl/libxl_capabilities.c
@@ -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);
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index d5922f302d..f6d49bc193 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -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;
 }


diff --git a/tests/testutilsxen.c b/tests/testutilsxen.c
index 821ee49d94..3484cccbf2 100644
--- a/tests/testutilsxen.c
+++ b/tests/testutilsxen.c
@@ -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;
 }


-- 
2.39.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux