[PATCH 1/6] virBitmapNewEmpty: Use g_new0 to allocate and remove error checking

Peter Krempa pkrempa at redhat.com
Thu Mar 19 19:35:58 UTC 2020


virBitmapNewEmpty can't fail now so we can make it obvious and fix all
callers.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virbitmap.c  | 14 +++-----------
 src/util/virhostcpu.c |  6 ++----
 src/util/virtpm.c     |  3 +--
 tests/virbitmaptest.c |  8 ++------
 4 files changed, 8 insertions(+), 23 deletions(-)

diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
index baf77fe556..d38a2dd7e9 100644
--- a/src/util/virbitmap.c
+++ b/src/util/virbitmap.c
@@ -110,17 +110,12 @@ virBitmapNew(size_t size)
  *
  * Allocate an empty bitmap. It can be used with self-expanding APIs.
  *
- * Returns a pointer to the allocated bitmap or NULL if memory cannot be
- * allocated. Reports libvirt errors.
+ * Returns a pointer to the allocated bitmap.
  */
 virBitmapPtr
 virBitmapNewEmpty(void)
 {
-    virBitmapPtr ret;
-
-    ignore_value(VIR_ALLOC(ret));
-
-    return ret;
+    return g_new0(virBitmap, 1);
 }


@@ -610,16 +605,13 @@ virBitmapParse(const char *str,
 virBitmapPtr
 virBitmapParseUnlimited(const char *str)
 {
-    virBitmapPtr bitmap;
+    virBitmapPtr bitmap = virBitmapNewEmpty();
     bool neg = false;
     const char *cur = str;
     char *tmp;
     size_t i;
     int start, last;

-    if (!(bitmap = virBitmapNewEmpty()))
-        return NULL;
-
     if (!str)
         goto error;

diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
index 67d3e3308b..41033b7473 100644
--- a/src/util/virhostcpu.c
+++ b/src/util/virhostcpu.c
@@ -336,8 +336,7 @@ virHostCPUParseNode(const char *node,
         goto cleanup;

     /* enumerate sockets in the node */
-    if (!(sockets_map = virBitmapNewEmpty()))
-        goto cleanup;
+    sockets_map = virBitmapNewEmpty();

     while ((direrr = virDirRead(cpudir, &cpudirent, node)) > 0) {
         if (sscanf(cpudirent->d_name, "cpu%u", &cpu) != 1)
@@ -373,8 +372,7 @@ virHostCPUParseNode(const char *node,
         goto cleanup;

     for (i = 0; i < sock_max; i++)
-        if (!(cores_maps[i] = virBitmapNewEmpty()))
-            goto cleanup;
+        cores_maps[i] = virBitmapNewEmpty();

     /* Iterate over all CPUs in the node, in ascending order */
     for (cpu = 0; cpu < npresent_cpus; cpu++) {
diff --git a/src/util/virtpm.c b/src/util/virtpm.c
index 505a4230dd..c734bf941a 100644
--- a/src/util/virtpm.c
+++ b/src/util/virtpm.c
@@ -183,8 +183,7 @@ virTPMExecGetCaps(virCommandPtr cmd,
     if (virCommandRun(cmd, &exitstatus) < 0)
         return NULL;

-    if (!(bitmap = virBitmapNewEmpty()))
-        return NULL;
+    bitmap = virBitmapNewEmpty();

     /* older version does not support --print-capabilties -- that's fine */
     if (exitstatus != 0) {
diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c
index 2808d9c880..1c7dc1d610 100644
--- a/tests/virbitmaptest.c
+++ b/tests/virbitmaptest.c
@@ -193,8 +193,7 @@ test4(const void *data G_GNUC_UNUSED)

     /* 0. empty set */

-    if (!(bitmap = virBitmapNewEmpty()))
-        goto error;
+    bitmap = virBitmapNewEmpty();

     if (virBitmapNextSetBit(bitmap, -1) != -1)
         goto error;
@@ -632,12 +631,9 @@ test11(const void *opaque)
 static int
 test12(const void *opaque G_GNUC_UNUSED)
 {
-    virBitmapPtr map = NULL;
+    virBitmapPtr map = virBitmapNewEmpty();
     int ret = -1;

-    if (!(map = virBitmapNewEmpty()))
-        return -1;
-
     TEST_MAP(0, "");

     if (virBitmapSetBitExpand(map, 128) < 0)
-- 
2.24.1




More information about the libvir-list mailing list