[libvirt PATCH v2 04/15] network: replace VIR_ALLOC/REALLOC with g_new0/g_renew

Laine Stump posted 15 patches 4 years, 4 months ago
[libvirt PATCH v2 04/15] network: replace VIR_ALLOC/REALLOC with g_new0/g_renew
Posted by Laine Stump 4 years, 4 months ago
Signed-off-by: Laine Stump <laine@redhat.com>
---
 src/network/bridge_driver.c | 29 ++++++++++-------------------
 1 file changed, 10 insertions(+), 19 deletions(-)

diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index 713763130b..ab359acdb5 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -177,8 +177,7 @@ networkDnsmasqDefNamespaceParseOptions(networkDnsmasqXmlNsDefPtr nsdef,
     if (nnodes == 0)
         return 0;
 
-    if (VIR_ALLOC_N(nsdef->options, nnodes) < 0)
-        return -1;
+    nsdef->options = g_new0(char *, nnodes);
 
     for (i = 0; i < nnodes; i++) {
         if (!(nsdef->options[nsdef->noptions++] = virXMLPropString(nodes[i], "value"))) {
@@ -196,12 +195,9 @@ static int
 networkDnsmasqDefNamespaceParse(xmlXPathContextPtr ctxt,
                                 void **data)
 {
-    networkDnsmasqXmlNsDefPtr nsdata = NULL;
+    networkDnsmasqXmlNsDefPtr nsdata = g_new0(networkDnsmasqXmlNsDef, 1);
     int ret = -1;
 
-    if (VIR_ALLOC(nsdata) < 0)
-        return -1;
-
     if (networkDnsmasqDefNamespaceParseOptions(nsdata, ctxt))
         goto cleanup;
 
@@ -733,8 +729,7 @@ networkStateInitialize(bool privileged,
         return -1;
     }
 
-    if (VIR_ALLOC(network_driver) < 0)
-        goto error;
+    network_driver = g_new0(virNetworkDriverState, 1);
 
     network_driver->lockFD = -1;
     if (virMutexInit(&network_driver->lock) < 0) {
@@ -2753,8 +2748,7 @@ networkCreateInterfacePool(virNetworkDefPtr netdef)
         goto cleanup;
     }
 
-    if (VIR_ALLOC_N(netdef->forward.ifs, numVirtFns) < 0)
-        goto cleanup;
+    netdef->forward.ifs = g_new0(virNetworkForwardIfDef, numVirtFns);
 
     for (i = 0; i < numVirtFns; i++) {
         virPCIDeviceAddressPtr thisVirtFn = virtFns[i];
@@ -4323,8 +4317,7 @@ networkGetDHCPLeases(virNetworkPtr net,
             continue;
 
         if (need_results) {
-            if (VIR_ALLOC(lease) < 0)
-                goto error;
+            lease = g_new0(virNetworkDHCPLease, 1);
 
             lease->expirytime = expirytime_tmp;
 
@@ -4378,9 +4371,8 @@ networkGetDHCPLeases(virNetworkPtr net,
 
     if (leases_ret) {
         /* NULL terminated array */
-        ignore_value(VIR_REALLOC_N(leases_ret, nleases + 1));
-        *leases = leases_ret;
-        leases_ret = NULL;
+        leases_ret = g_renew(virNetworkDHCPLeasePtr, leases_ret,  nleases + 1);
+        *leases = g_steal_pointer(&leases_ret);
     }
 
     rv = nleases;
@@ -5612,10 +5604,9 @@ networkPortSetParameters(virNetworkPortPtr port,
     if (!(dir = virNetworkObjGetPortStatusDir(obj, driver->stateDir)))
         goto cleanup;
 
-    if ((VIR_ALLOC(bandwidth) < 0) ||
-        (VIR_ALLOC(bandwidth->in) < 0) ||
-        (VIR_ALLOC(bandwidth->out) < 0))
-        goto cleanup;
+    bandwidth = g_new0(virNetDevBandwidth, 1);
+    bandwidth->in = g_new0(virNetDevBandwidthRate, 1);
+    bandwidth->out = g_new0(virNetDevBandwidthRate, 1);
 
     for (i = 0; i < nparams; i++) {
         virTypedParameterPtr param = &params[i];
-- 
2.25.4

Re: [libvirt PATCH v2 04/15] network: replace VIR_ALLOC/REALLOC with g_new0/g_renew
Posted by Ján Tomko 4 years, 4 months ago
On a Tuesday in 2020, Laine Stump wrote:
>Signed-off-by: Laine Stump <laine@redhat.com>
>---
> src/network/bridge_driver.c | 29 ++++++++++-------------------
> 1 file changed, 10 insertions(+), 19 deletions(-)
>
>diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
>index 713763130b..ab359acdb5 100644
>--- a/src/network/bridge_driver.c
>+++ b/src/network/bridge_driver.c
>@@ -177,8 +177,7 @@ networkDnsmasqDefNamespaceParseOptions(networkDnsmasqXmlNsDefPtr nsdef,
>     if (nnodes == 0)
>         return 0;
>
>-    if (VIR_ALLOC_N(nsdef->options, nnodes) < 0)
>-        return -1;
>+    nsdef->options = g_new0(char *, nnodes);
>
>     for (i = 0; i < nnodes; i++) {
>         if (!(nsdef->options[nsdef->noptions++] = virXMLPropString(nodes[i], "value"))) {
>@@ -196,12 +195,9 @@ static int
> networkDnsmasqDefNamespaceParse(xmlXPathContextPtr ctxt,
>                                 void **data)
> {
>-    networkDnsmasqXmlNsDefPtr nsdata = NULL;
>+    networkDnsmasqXmlNsDefPtr nsdata = g_new0(networkDnsmasqXmlNsDef, 1);
>     int ret = -1;
>
>-    if (VIR_ALLOC(nsdata) < 0)
>-        return -1;
>-
>     if (networkDnsmasqDefNamespaceParseOptions(nsdata, ctxt))
>         goto cleanup;
>
>@@ -733,8 +729,7 @@ networkStateInitialize(bool privileged,
>         return -1;
>     }
>
>-    if (VIR_ALLOC(network_driver) < 0)
>-        goto error;
>+    network_driver = g_new0(virNetworkDriverState, 1);
>
>     network_driver->lockFD = -1;
>     if (virMutexInit(&network_driver->lock) < 0) {
>@@ -2753,8 +2748,7 @@ networkCreateInterfacePool(virNetworkDefPtr netdef)
>         goto cleanup;
>     }
>
>-    if (VIR_ALLOC_N(netdef->forward.ifs, numVirtFns) < 0)
>-        goto cleanup;
>+    netdef->forward.ifs = g_new0(virNetworkForwardIfDef, numVirtFns);
>
>     for (i = 0; i < numVirtFns; i++) {
>         virPCIDeviceAddressPtr thisVirtFn = virtFns[i];
>@@ -4323,8 +4317,7 @@ networkGetDHCPLeases(virNetworkPtr net,
>             continue;
>
>         if (need_results) {
>-            if (VIR_ALLOC(lease) < 0)
>-                goto error;
>+            lease = g_new0(virNetworkDHCPLease, 1);
>
>             lease->expirytime = expirytime_tmp;
>
>@@ -4378,9 +4371,8 @@ networkGetDHCPLeases(virNetworkPtr net,
>
>     if (leases_ret) {
>         /* NULL terminated array */
>-        ignore_value(VIR_REALLOC_N(leases_ret, nleases + 1));
>-        *leases = leases_ret;
>-        leases_ret = NULL;
>+        leases_ret = g_renew(virNetworkDHCPLeasePtr, leases_ret,  nleases + 1);

Double space before nleases.

Also, this is a faithful conversion, but neither VIR_REALLOC_N nor
g_renew guarantee that the memory will be zeroed.

>+        *leases = g_steal_pointer(&leases_ret);
>     }
>
>     rv = nleases;

With the double space removed:
Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano