[PATCH 7/7] qemu_capabilities.c: use g_autoptr() in virQEMUCapsInitHostCPUModel()

Daniel Henrique Barboza posted 7 patches 4 years, 2 months ago
There is a newer version of this series
[PATCH 7/7] qemu_capabilities.c: use g_autoptr() in virQEMUCapsInitHostCPUModel()
Posted by Daniel Henrique Barboza 4 years, 2 months ago
All 'virCPUDef' pointers can be auto-freed and the 'cleanup' label
removed.

Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 src/qemu/qemu_capabilities.c | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 5257fe64b2..dbf08d681b 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3597,11 +3597,11 @@ virQEMUCapsInitHostCPUModel(virQEMUCaps *qemuCaps,
                             virArch hostArch,
                             virDomainVirtType type)
 {
-    virCPUDef *cpu = NULL;
-    virCPUDef *cpuExpanded = NULL;
-    virCPUDef *migCPU = NULL;
-    virCPUDef *hostCPU = NULL;
-    virCPUDef *fullCPU = NULL;
+    g_autoptr(virCPUDef) cpu = NULL;
+    g_autoptr(virCPUDef) cpuExpanded = NULL;
+    g_autoptr(virCPUDef) migCPU = NULL;
+    g_autoptr(virCPUDef) hostCPU = NULL;
+    g_autoptr(virCPUDef) fullCPU = NULL;
     size_t i;
     int rc;
 
@@ -3676,19 +3676,14 @@ virQEMUCapsInitHostCPUModel(virQEMUCaps *qemuCaps,
             goto error;
     }
 
-    virQEMUCapsSetHostModel(qemuCaps, type, cpu, migCPU, fullCPU);
+    virQEMUCapsSetHostModel(qemuCaps, type, g_steal_pointer(&cpu),
+                            g_steal_pointer(&migCPU),
+                            g_steal_pointer(&fullCPU));
 
- cleanup:
-    virCPUDefFree(cpuExpanded);
-    virCPUDefFree(hostCPU);
     return;
 
  error:
-    virCPUDefFree(cpu);
-    virCPUDefFree(migCPU);
-    virCPUDefFree(fullCPU);
     virResetLastError();
-    goto cleanup;
 }
 
 
-- 
2.31.1

Re: [PATCH 7/7] qemu_capabilities.c: use g_autoptr() in virQEMUCapsInitHostCPUModel()
Posted by Ján Tomko 4 years, 2 months ago
On a Thursday in 2021, Daniel Henrique Barboza wrote:
>All 'virCPUDef' pointers can be auto-freed and the 'cleanup' label
>removed.
>
>Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
>---
> src/qemu/qemu_capabilities.c | 21 ++++++++-------------
> 1 file changed, 8 insertions(+), 13 deletions(-)
>
>diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
>index 5257fe64b2..dbf08d681b 100644
>--- a/src/qemu/qemu_capabilities.c
>+++ b/src/qemu/qemu_capabilities.c
>@@ -3597,11 +3597,11 @@ virQEMUCapsInitHostCPUModel(virQEMUCaps *qemuCaps,
>                             virArch hostArch,
>                             virDomainVirtType type)
> {
>-    virCPUDef *cpu = NULL;
>-    virCPUDef *cpuExpanded = NULL;
>-    virCPUDef *migCPU = NULL;
>-    virCPUDef *hostCPU = NULL;
>-    virCPUDef *fullCPU = NULL;
>+    g_autoptr(virCPUDef) cpu = NULL;
>+    g_autoptr(virCPUDef) cpuExpanded = NULL;
>+    g_autoptr(virCPUDef) migCPU = NULL;

migCPU is also freed manually if virQEMUCapsInitCPUModel fails.
However in both callers of virQEMUCapsInitCPUModel the cpu argument is
allocated right above by virQEMUCapsNewHostCPUModel().

By moving the allocation inside virQEMUCapsInitCPUModel, the
virCPUDefFree can be removed.

Jano

>+    g_autoptr(virCPUDef) hostCPU = NULL;
>+    g_autoptr(virCPUDef) fullCPU = NULL;
>     size_t i;
>     int rc;
>