Re: [libvirt PATCH 5/5] tools: use g_new0 instead of VIR_ALLOC*

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 9/23/20 5:11 PM, Ján Tomko wrote:
With the exception of vsh*alloc.

Signed-off-by: Ján Tomko <jtomko@xxxxxxxxxx>
---

The 'error' label removal down there in virt-admin-completer.c was
really nice. 2 'for' loops and several VIR_FREE() calls were needed
solely because of that VIR_ALLOC_N().

Reviewed-by: Daniel Henrique Barboza <danielhb413@xxxxxxxxx>




  tools/virsh-domain-monitor.c    | 11 +++--------
  tools/virsh-domain.c            | 26 ++++++++------------------
  tools/virt-admin-completer.c    | 12 +-----------
  tools/virt-login-shell-helper.c | 13 ++++---------
  tools/vsh-table.c               | 21 +++++++--------------
  tools/vsh.c                     |  5 +----
  6 files changed, 24 insertions(+), 64 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 848efc8aa3..c8a7c0f1b7 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1234,8 +1234,7 @@ cmdDomBlkError(vshControl *ctl, const vshCmd *cmd)
      ndisks = count;
if (ndisks) {
-        if (VIR_ALLOC_N(disks, ndisks) < 0)
-            goto cleanup;
+        disks = g_new0(virDomainDiskError, ndisks);
if ((count = virDomainGetDiskErrors(dom, disks, ndisks, 0)) == -1)
              goto cleanup;
@@ -1378,10 +1377,7 @@ cmdDominfo(vshControl *ctl, const vshCmd *cmd)
              vshPrint(ctl, "%-15s %s\n", _("Security DOI:"), secmodel.doi);
/* Security labels are only valid for active domains */
-            if (VIR_ALLOC(seclabel) < 0) {
-                virshDomainFree(dom);
-                return false;
-            }
+            seclabel = g_new0(virSecurityLabel, 1);
if (virDomainGetSecurityLabel(dom, seclabel) == -1) {
                  virshDomainFree(dom);
@@ -2280,8 +2276,7 @@ cmdDomstats(vshControl *ctl, const vshCmd *cmd)
          flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_NOWAIT;
if (vshCommandOptBool(cmd, "domain")) {
-        if (VIR_ALLOC_N(domlist, 1) < 0)
-            goto cleanup;
+        domlist = g_new0(virDomainPtr, 1);
          ndoms = 1;
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 32edfc0398..dfcba04682 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -1762,8 +1762,7 @@ virshBlockJobWaitInit(vshControl *ctl,
      virshBlockJobWaitDataPtr ret;
      virshControlPtr priv = ctl->privData;
- if (VIR_ALLOC(ret) < 0)
-        return NULL;
+    ret = g_new0(virshBlockJobWaitData, 1);
ret->ctl = ctl;
      ret->dom = dom;
@@ -8177,8 +8176,7 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd)
          goto cleanup;
      }
- if (VIR_ALLOC_N(params, nparams * MIN(show_count, 128)) < 0)
-        goto cleanup;
+    params = g_new0(virTypedParameter, nparams * MIN(show_count, 128));
while (show_count) {
          int ncpus = MIN(show_count, 128);
@@ -8215,8 +8213,7 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd)
          goto cleanup;
      }
- if (VIR_ALLOC_N(params, nparams) < 0)
-        goto cleanup;
+    params = g_new0(virTypedParameter, nparams);
/* passing start_cpu == -1 gives us domain's total status */
      if ((stats_per_cpu = virDomainGetCPUStats(dom, params, nparams,
@@ -10086,14 +10083,9 @@ cmdLxcEnterNamespace(vshControl *ctl, const vshCmd *cmd)
          goto cleanup;
if (setlabel) {
-        if (VIR_ALLOC(secmodel) < 0) {
-            vshError(ctl, "%s", _("Failed to allocate security model"));
-            goto cleanup;
-        }
-        if (VIR_ALLOC(seclabel) < 0) {
-            vshError(ctl, "%s", _("Failed to allocate security label"));
-            goto cleanup;
-        }
+        secmodel = g_new0(virSecurityModel, 1);
+        seclabel = g_new0(virSecurityLabel, 1);
+
          if (virNodeGetSecurityModel(priv->conn, secmodel) < 0)
              goto cleanup;
          if (virDomainGetSecurityLabel(dom, seclabel) < 0)
@@ -13737,8 +13729,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd)
      }
if (all) {
-        if (VIR_ALLOC_N(data, VIR_DOMAIN_EVENT_ID_LAST) < 0)
-            goto cleanup;
+        data = g_new0(virshDomEventData, VIR_DOMAIN_EVENT_ID_LAST);
          for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) {
              data[i].ctl = ctl;
              data[i].loop = loop;
@@ -13748,8 +13739,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd)
              data[i].id = -1;
          }
      } else {
-        if (VIR_ALLOC_N(data, 1) < 0)
-            goto cleanup;
+        data = g_new0(virshDomEventData, 1);
          data[0].ctl = ctl;
          data[0].loop = vshCommandOptBool(cmd, "loop");
          data[0].count = &count;
diff --git a/tools/virt-admin-completer.c b/tools/virt-admin-completer.c
index 7201bb71eb..61004beeb5 100644
--- a/tools/virt-admin-completer.c
+++ b/tools/virt-admin-completer.c
@@ -46,8 +46,7 @@ vshAdmServerCompleter(vshControl *ctl,
      if ((nsrvs = virAdmConnectListServers(priv->conn, &srvs, 0)) < 0)
          return NULL;
- if (VIR_ALLOC_N(ret, nsrvs + 1) < 0)
-        goto error;
+    ret = g_new0(char *, nsrvs + 1);
for (i = 0; i < nsrvs; i++) {
          const char *name = virAdmServerGetName(srvs[i]);
@@ -59,13 +58,4 @@ vshAdmServerCompleter(vshControl *ctl,
      VIR_FREE(srvs);
return ret;
-
- error:
-    for (; i < nsrvs; i++)
-        virAdmServerFree(srvs[i]);
-    VIR_FREE(srvs);
-    for (i = 0; i < nsrvs; i++)
-        VIR_FREE(ret[i]);
-    VIR_FREE(ret);
-    return ret;
  }
diff --git a/tools/virt-login-shell-helper.c b/tools/virt-login-shell-helper.c
index cc6836382a..0e7987bf82 100644
--- a/tools/virt-login-shell-helper.c
+++ b/tools/virt-login-shell-helper.c
@@ -99,7 +99,7 @@ static int virLoginShellGetShellArgv(virConfPtr conf,
          return -1;
if (rv == 0) {
-        if (VIR_ALLOC_N(*shargv, 2) < 0)
+        *shargv = g_new0(char *, 2);
              return -1;
          (*shargv)[0] = g_strdup("/bin/sh");
          *shargvlen = 1;
@@ -302,10 +302,8 @@ main(int argc, char **argv)
if ((nfdlist = virDomainLxcOpenNamespace(dom, &fdlist, 0)) < 0)
          goto cleanup;
-    if (VIR_ALLOC(secmodel) < 0)
-        goto cleanup;
-    if (VIR_ALLOC(seclabel) < 0)
-        goto cleanup;
+    secmodel = g_new0(virSecurityModel, 1);
+    seclabel = g_new0(virSecurityLabel, 1);
      if (virNodeGetSecurityModel(conn, secmodel) < 0)
          goto cleanup;
      if (virDomainGetSecurityLabel(dom, seclabel) < 0)
@@ -331,10 +329,7 @@ main(int argc, char **argv)
          if (tmp) {
              g_strfreev(shargv);
              shargvlen = 1;
-            if (VIR_ALLOC_N(shargv[0], shargvlen + 1) < 0) {
-                VIR_FREE(tmp);
-                goto cleanup;
-            }
+            shargv = g_new0(char *, shargvlen + 1);
              shargv[0] = tmp;
              shargv[1] = NULL;
          }
diff --git a/tools/vsh-table.c b/tools/vsh-table.c
index a727ac17b5..4471368687 100644
--- a/tools/vsh-table.c
+++ b/tools/vsh-table.c
@@ -95,8 +95,7 @@ vshTableRowNew(const char *arg, va_list ap)
          goto error;
      }
- if (VIR_ALLOC(row) < 0)
-        goto error;
+    row = g_new0(vshTableRow, 1);
while (arg) {
          char *tmp = NULL;
@@ -134,8 +133,7 @@ vshTableNew(const char *arg, ...)
      vshTableRowPtr header = NULL;
      va_list ap;
- if (VIR_ALLOC(table) < 0)
-        goto error;
+    table = g_new0(vshTable, 1);
va_start(ap, arg);
      header = vshTableRowNew(arg, ap);
@@ -215,8 +213,7 @@ vshTableSafeEncode(const char *s, size_t *width)
memset(&st, 0, sizeof(st)); - if (VIR_ALLOC_N(buf, (sz * HEX_ENCODE_LENGTH) + 1) < 0)
-        return NULL;
+    buf = g_new0(char, (sz * HEX_ENCODE_LENGTH) + 1);
ret = buf;
      *width = 0;
@@ -369,17 +366,13 @@ vshTablePrint(vshTablePtr table, bool header)
      g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
      char *ret = NULL;
- if (VIR_ALLOC_N(maxwidths, table->rows[0]->ncells))
-        goto cleanup;
+    maxwidths = g_new0(size_t, table->rows[0]->ncells);
- if (VIR_ALLOC_N(widths, table->nrows))
-        goto cleanup;
+    widths = g_new0(size_t *, table->nrows);
/* retrieve widths of columns */
-    for (i = 0; i < table->nrows; i++) {
-        if (VIR_ALLOC_N(widths[i], table->rows[0]->ncells))
-            goto cleanup;
-    }
+    for (i = 0; i < table->nrows; i++)
+        widths[i] = g_new0(size_t, table->rows[0]->ncells);
if (vshTableGetColumnsWidths(table, maxwidths, widths, header) < 0)
          goto cleanup;
diff --git a/tools/vsh.c b/tools/vsh.c
index 87c409e4aa..0eddd5349d 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -162,10 +162,7 @@ vshStringToArray(const char *str,
      }
/* reserve the NULL element at the end */
-    if (VIR_ALLOC_N(arr, nstr_tokens + 1) < 0) {
-        VIR_FREE(str_copied);
-        return -1;
-    }
+    arr = g_new0(char *, nstr_tokens + 1);
/* tokenize the input string, while treating ,, as a literal comma */
      nstr_tokens = 0;





[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux