[libvirt] [PATCH 7/9] libxlGetDHCPInterfaces: Switch to GLib

Michal Privoznik mprivozn at redhat.com
Wed Dec 4 09:55:32 UTC 2019


If we use glib alloc functions, we can drop the 'cleanup' label
and @rv variable and also simplify the code a bit.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/libxl/libxl_driver.c | 41 +++++++++++-----------------------------
 1 file changed, 11 insertions(+), 30 deletions(-)

diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 6064436681..7467111618 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -6241,9 +6241,6 @@ libxlGetDHCPInterfaces(virDomainObjPtr vm,
     g_autoptr(virConnect) conn = NULL;
     virDomainInterfacePtr *ifaces_ret = NULL;
     size_t ifaces_count = 0;
-    int rv = -1;
-    int n_leases = 0;
-    virNetworkDHCPLeasePtr *leases = NULL;
     size_t i;
 
     if (!(conn = virGetConnectNetwork()))
@@ -6252,6 +6249,8 @@ libxlGetDHCPInterfaces(virDomainObjPtr vm,
     for (i = 0; i < vm->def->nnets; i++) {
         g_autoptr(virNetwork) network = NULL;
         char macaddr[VIR_MAC_STRING_BUFLEN];
+        virNetworkDHCPLeasePtr *leases = NULL;
+        int n_leases = 0;
         virDomainInterfacePtr iface = NULL;
         size_t j;
 
@@ -6270,21 +6269,16 @@ libxlGetDHCPInterfaces(virDomainObjPtr vm,
             goto error;
 
         if (n_leases) {
-            if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0)
-                goto error;
+            ifaces_ret = g_renew(typeof(*ifaces_ret), ifaces_ret, ifaces_count + 1);
+            ifaces_ret[ifaces_count] = g_new0(typeof(**ifaces_ret), 1);
+            iface = ifaces_ret[ifaces_count];
+            ifaces_count++;
 
-            if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0)
-                goto error;
-
-            iface = ifaces_ret[ifaces_count - 1];
             /* Assuming each lease corresponds to a separate IP */
             iface->naddrs = n_leases;
 
-            if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0)
-                goto error;
-
+            iface->addrs = g_new0(typeof(*iface->addrs), iface->naddrs);
             iface->name = g_strdup(vm->def->nets[i]->ifname);
-
             iface->hwaddr = g_strdup(macaddr);
         }
 
@@ -6293,30 +6287,17 @@ libxlGetDHCPInterfaces(virDomainObjPtr vm,
             virDomainIPAddressPtr ip_addr = &iface->addrs[j];
 
             ip_addr->addr = g_strdup(lease->ipaddr);
-
             ip_addr->type = lease->type;
             ip_addr->prefix = lease->prefix;
-        }
 
-        for (j = 0; j < n_leases; j++)
             virNetworkDHCPLeaseFree(leases[j]);
+        }
 
         VIR_FREE(leases);
     }
 
-    *ifaces = ifaces_ret;
-    ifaces_ret = NULL;
-    rv = ifaces_count;
-
- cleanup:
-    virObjectUnref(network);
-    if (leases) {
-        for (i = 0; i < n_leases; i++)
-            virNetworkDHCPLeaseFree(leases[i]);
-    }
-    VIR_FREE(leases);
-
-    return rv;
+    *ifaces = g_steal_pointer(&ifaces_ret);
+    return ifaces_count;
 
  error:
     if (ifaces_ret) {
@@ -6325,7 +6306,7 @@ libxlGetDHCPInterfaces(virDomainObjPtr vm,
     }
     VIR_FREE(ifaces_ret);
 
-    goto cleanup;
+    return -1;
 }
 
 
-- 
2.23.0




More information about the libvir-list mailing list