[PATCH 02/31] libxlCapsInitGuests: Rework insane use of ternary operators

Peter Krempa pkrempa at redhat.com
Thu Mar 30 13:37:56 UTC 2023


Get rid of nested ternaries by adding a few helper variables and more
explicit if conditions to fill them appropriately.

Note that 'virCapabilitiesAllocMachines' doesn't require return value
check any more as it can't fail.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/libxl/libxl_capabilities.c | 38 ++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/src/libxl/libxl_capabilities.c b/src/libxl/libxl_capabilities.c
index c2975d637e..82c38d1b26 100644
--- a/src/libxl/libxl_capabilities.c
+++ b/src/libxl/libxl_capabilities.c
@@ -463,26 +463,32 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCaps *caps)

     for (i = 0; i < nr_guest_archs; ++i) {
         virCapsGuest *guest;
-        char const *const xen_machines[] = {
-            guest_archs[i].hvm ? "xenfv" :
-                (guest_archs[i].pvh ? "xenpvh" : "xenpv")};
         virCapsGuestMachine **machines;
+        virDomainOSType ostype = VIR_DOMAIN_OSTYPE_XEN;
+        const char *loader = NULL;

-        if ((machines = virCapabilitiesAllocMachines(xen_machines, 1)) == NULL)
-            return -1;
+        if (guest_archs[i].hvm) {
+            char const *const xen_machines[] = { "xenfv", NULL };
+
+            ostype = VIR_DOMAIN_OSTYPE_HVM;
+            loader = LIBXL_FIRMWARE_DIR "/hvmloader";
+
+            machines = virCapabilitiesAllocMachines(xen_machines, 1);
+        } else if (guest_archs[i].pvh) {
+            char const *const xen_machines[] = { "xenpvh", NULL };
+
+            ostype = VIR_DOMAIN_OSTYPE_XENPVH;
+            machines = virCapabilitiesAllocMachines(xen_machines, 1);
+        } else {
+            char const *const xen_machines[] = { "xenpv", NULL };
+
+            ostype = VIR_DOMAIN_OSTYPE_XEN;
+            machines = virCapabilitiesAllocMachines(xen_machines, 1);
+        }

-        guest = virCapabilitiesAddGuest(caps,
-                                        guest_archs[i].hvm ? VIR_DOMAIN_OSTYPE_HVM :
-                                        (guest_archs[i].pvh ? VIR_DOMAIN_OSTYPE_XENPVH :
-                                         VIR_DOMAIN_OSTYPE_XEN),
-                                        guest_archs[i].arch,
+        guest = virCapabilitiesAddGuest(caps, ostype, guest_archs[i].arch,
                                         LIBXL_EXECBIN_DIR "/qemu-system-i386",
-                                        (guest_archs[i].hvm ?
-                                         LIBXL_FIRMWARE_DIR "/hvmloader" :
-                                         NULL),
-                                        1,
-                                        machines);
-        machines = NULL;
+                                        loader, 1, machines);

         virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_XEN,
                                       NULL, NULL, 0, NULL);
-- 
2.39.2



More information about the libvir-list mailing list