[PATCH 12/12] cpu_ppc64.c: use g_autoptr() whenever possible

Daniel Henrique Barboza danielhb413 at gmail.com
Wed Sep 2 20:25:46 UTC 2020


Using g_autoptr() in virCPUDef pointers allows for more
cleanups in ppc64Compute() and virCPUppc64Baseline()

Signed-off-by: Daniel Henrique Barboza <danielhb413 at gmail.com>
---
 src/cpu/cpu_ppc64.c | 55 +++++++++++++++++----------------------------
 1 file changed, 20 insertions(+), 35 deletions(-)

diff --git a/src/cpu/cpu_ppc64.c b/src/cpu/cpu_ppc64.c
index 0c140b33e4..9e6f1e856e 100644
--- a/src/cpu/cpu_ppc64.c
+++ b/src/cpu/cpu_ppc64.c
@@ -421,15 +421,14 @@ ppc64Compute(virCPUDefPtr host,
     g_autoptr(virCPUppc64Map) map = NULL;
     g_autoptr(virCPUppc64Model) host_model = NULL;
     g_autoptr(virCPUppc64Model) guest_model = NULL;
-    virCPUDefPtr cpu = NULL;
-    virCPUCompareResult ret = VIR_CPU_COMPARE_ERROR;
+    g_autoptr(virCPUDef) cpu = NULL;
     virArch arch;
     size_t i;
 
     /* Ensure existing configurations are handled correctly */
     if (!(cpu = virCPUDefCopy(other)) ||
         virCPUppc64ConvertLegacy(cpu) < 0)
-        goto cleanup;
+        return VIR_CPU_COMPARE_ERROR;
 
     if (cpu->arch != VIR_ARCH_NONE) {
         bool found = false;
@@ -448,8 +447,7 @@ ppc64Compute(virCPUDefPtr host,
                 *message = g_strdup_printf(_("CPU arch %s does not match host arch"),
                                            virArchToString(cpu->arch));
 
-            ret = VIR_CPU_COMPARE_INCOMPATIBLE;
-            goto cleanup;
+            return VIR_CPU_COMPARE_INCOMPATIBLE;
         }
         arch = cpu->arch;
     } else {
@@ -466,16 +464,15 @@ ppc64Compute(virCPUDefPtr host,
                                        cpu->vendor);
         }
 
-        ret = VIR_CPU_COMPARE_INCOMPATIBLE;
-        goto cleanup;
+        return VIR_CPU_COMPARE_INCOMPATIBLE;
     }
 
     if (!(map = ppc64LoadMap()))
-        goto cleanup;
+        return VIR_CPU_COMPARE_ERROR;
 
     /* Host CPU information */
     if (!(host_model = ppc64ModelFromCPU(host, map)))
-        goto cleanup;
+        return VIR_CPU_COMPARE_ERROR;
 
     if (cpu->type == VIR_CPU_TYPE_GUEST) {
         /* Guest CPU information */
@@ -485,10 +482,8 @@ ppc64Compute(virCPUDefPtr host,
             /* host-model only:
              * we need to take compatibility modes into account */
             tmp = ppc64CheckCompatibilityMode(host->model, cpu->model);
-            if (tmp != VIR_CPU_COMPARE_IDENTICAL) {
-                ret = tmp;
-                goto cleanup;
-            }
+            if (tmp != VIR_CPU_COMPARE_IDENTICAL)
+                return tmp;
             G_GNUC_FALLTHROUGH;
 
         case VIR_CPU_MODE_HOST_PASSTHROUGH:
@@ -509,7 +504,7 @@ ppc64Compute(virCPUDefPtr host,
     }
 
     if (!guest_model)
-        goto cleanup;
+        return VIR_CPU_COMPARE_ERROR;
 
     if (STRNEQ(guest_model->name, host_model->name)) {
         VIR_DEBUG("host CPU model does not match required CPU model %s",
@@ -520,19 +515,14 @@ ppc64Compute(virCPUDefPtr host,
                                        guest_model->name);
         }
 
-        ret = VIR_CPU_COMPARE_INCOMPATIBLE;
-        goto cleanup;
+        return VIR_CPU_COMPARE_INCOMPATIBLE;
     }
 
     if (guestData)
         if (!(*guestData = ppc64MakeCPUData(arch, &guest_model->data)))
-            goto cleanup;
+            return VIR_CPU_COMPARE_ERROR;
 
-    ret = VIR_CPU_COMPARE_IDENTICAL;
-
- cleanup:
-    virCPUDefFree(cpu);
-    return ret;
+    return VIR_CPU_COMPARE_IDENTICAL;
 }
 
 static virCPUCompareResult
@@ -666,16 +656,16 @@ virCPUppc64Baseline(virCPUDefPtr *cpus,
     g_autoptr(virCPUppc64Map) map = NULL;
     const virCPUppc64Model *model;
     const virCPUppc64Vendor *vendor = NULL;
-    virCPUDefPtr cpu = NULL;
+    g_autoptr(virCPUDef) cpu = NULL;
     size_t i;
 
     if (!(map = ppc64LoadMap()))
-        goto error;
+        return NULL;
 
     if (!(model = ppc64ModelFind(map, cpus[0]->model))) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Unknown CPU model %s"), cpus[0]->model);
-        goto error;
+        return NULL;
     }
 
     for (i = 0; i < ncpus; i++) {
@@ -695,7 +685,7 @@ virCPUppc64Baseline(virCPUDefPtr *cpus,
         if (STRNEQ(cpus[i]->model, model->name)) {
             virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                            _("CPUs are incompatible"));
-            goto error;
+            return NULL;
         }
 
         if (!cpus[i]->vendor)
@@ -704,7 +694,7 @@ virCPUppc64Baseline(virCPUDefPtr *cpus,
         if (!(vnd = ppc64VendorFind(map, cpus[i]->vendor))) {
             virReportError(VIR_ERR_OPERATION_FAILED,
                            _("Unknown CPU vendor %s"), cpus[i]->vendor);
-            goto error;
+            return NULL;
         }
 
         if (model->vendor) {
@@ -714,13 +704,13 @@ virCPUppc64Baseline(virCPUDefPtr *cpus,
                                  "vendor %s"),
                                model->vendor->name, model->name,
                                vnd->name);
-                goto error;
+                return NULL;
             }
         } else if (vendor) {
             if (vendor != vnd) {
                 virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                                _("CPU vendors do not match"));
-                goto error;
+                return NULL;
             }
         } else {
             vendor = vnd;
@@ -738,12 +728,7 @@ virCPUppc64Baseline(virCPUDefPtr *cpus,
     cpu->match = VIR_CPU_MATCH_EXACT;
     cpu->fallback = VIR_CPU_FALLBACK_FORBID;
 
-    return cpu;
-
- error:
-    virCPUDefFree(cpu);
-    cpu = NULL;
-    return NULL;
+    return g_steal_pointer(&cpu);
 }
 
 static int
-- 
2.26.2




More information about the libvir-list mailing list