[PATCH] Introduce virBitmapIsBitSet

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

 



A helper that never returns an error and treats bits out of bitmap range
as false.

Use it everywhere we use ignore_value on virBitmapGetBit, or loop over
the bitmap size.
---
 src/conf/domain_conf.c        |  4 +---
 src/conf/node_device_conf.c   |  4 +---
 src/conf/snapshot_conf.c      |  6 ++----
 src/conf/storage_conf.c       |  4 +---
 src/libvirt_private.syms      |  1 +
 src/libxl/libxl_domain.c      |  4 +---
 src/libxl/libxl_driver.c      |  5 +----
 src/nodeinfo.c                |  6 +-----
 src/qemu/qemu_capabilities.c  |  7 +------
 src/storage/storage_backend.c |  4 +---
 src/util/virbitmap.c          | 18 ++++++++++++++++++
 src/util/virbitmap.h          |  5 +++++
 src/util/vircgroup.c          |  4 +---
 src/util/virdnsmasq.c         |  6 +-----
 src/util/virportallocator.c   | 17 +++--------------
 src/util/virprocess.c         |  9 ++-------
 src/xen/xen_driver.c          |  4 +---
 src/xen/xend_internal.c       |  5 +----
 18 files changed, 43 insertions(+), 70 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 127fc91..ae8688e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -3107,7 +3107,6 @@ virDomainDefRejectDuplicateControllers(virDomainDefPtr def)
     virDomainControllerDefPtr cont;
     size_t nbitmaps = 0;
     int ret = -1;
-    bool b;
     size_t i;
 
     memset(max_idx, -1, sizeof(max_idx));
@@ -3133,8 +3132,7 @@ virDomainDefRejectDuplicateControllers(virDomainDefPtr def)
         if (max_idx[cont->type] == -1)
             continue;
 
-        ignore_value(virBitmapGetBit(bitmaps[cont->type], cont->idx, &b));
-        if (b) {
+        if (virBitmapIsBitSet(bitmaps[cont->type], cont->idx)) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Multiple '%s' controllers with index '%d'"),
                            virDomainControllerTypeToString(cont->type),
diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c
index f9c9b6f..2899a06 100644
--- a/src/conf/node_device_conf.c
+++ b/src/conf/node_device_conf.c
@@ -439,9 +439,7 @@ char *virNodeDeviceDefFormat(const virNodeDeviceDef *def)
             virInterfaceLinkFormat(&buf, &data->net.lnk);
             if (data->net.features) {
                 for (i = 0; i < VIR_NET_DEV_FEAT_LAST; i++) {
-                    bool b;
-                    ignore_value(virBitmapGetBit(data->net.features, i, &b));
-                    if (b) {
+                    if (virBitmapIsBitSet(data->net.features, i)) {
                         virBufferAsprintf(&buf, "<feature name='%s'/>\n",
                                           virNetDevFeatureTypeToString(i));
                     }
diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c
index a0667c2..dc5436f 100644
--- a/src/conf/snapshot_conf.c
+++ b/src/conf/snapshot_conf.c
@@ -465,7 +465,6 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr def,
     virBitmapPtr map = NULL;
     size_t i;
     int ndisks;
-    bool inuse;
 
     if (!def->dom) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -500,7 +499,7 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr def,
             goto cleanup;
         }
 
-        if (virBitmapGetBit(map, idx, &inuse) < 0 || inuse) {
+        if (virBitmapIsBitSet(map, idx)) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("disk '%s' specified twice"),
                            disk->name);
@@ -553,8 +552,7 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr def,
     for (i = 0; i < def->dom->ndisks; i++) {
         virDomainSnapshotDiskDefPtr disk;
 
-        ignore_value(virBitmapGetBit(map, i, &inuse));
-        if (inuse)
+        if (virBitmapIsBitSet(map, i))
             continue;
         disk = &def->disks[ndisks++];
         if (VIR_ALLOC(disk->src) < 0)
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 4c1f05d..b070448 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -1567,7 +1567,6 @@ virStorageVolTargetDefFormat(virStorageVolOptionsPtr options,
 
     if (options->featureToString && def->features) {
         size_t i;
-        bool b;
         bool empty = virBitmapIsAllClear(def->features);
 
         if (empty) {
@@ -1578,8 +1577,7 @@ virStorageVolTargetDefFormat(virStorageVolOptionsPtr options,
         }
 
         for (i = 0; i < VIR_STORAGE_FILE_FEATURE_LAST; i++) {
-            ignore_value(virBitmapGetBit(def->features, i, &b));
-            if (b)
+            if (virBitmapIsBitSet(def->features, i))
                 virBufferAsprintf(buf, "<%s/>\n",
                                   options->featureToString(i));
         }
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7262c95..b5fe88c 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1070,6 +1070,7 @@ virBitmapFree;
 virBitmapGetBit;
 virBitmapIsAllClear;
 virBitmapIsAllSet;
+virBitmapIsBitSet;
 virBitmapLastSetBit;
 virBitmapNew;
 virBitmapNewCopy;
diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c
index 9af5758..66ca988 100644
--- a/src/libxl/libxl_domain.c
+++ b/src/libxl/libxl_domain.c
@@ -1093,9 +1093,7 @@ libxlDomainSetVcpuAffinities(libxlDriverPrivatePtr driver, virDomainObjPtr vm)
         cpumask = def->cputune.vcpupin[vcpu]->cpumask;
 
         for (i = 0; i < virBitmapSize(cpumask); ++i) {
-            bool bit;
-            ignore_value(virBitmapGetBit(cpumask, i, &bit));
-            if (bit)
+            if (virBitmapIsBitSet(cpumask, i))
                 VIR_USE_CPU(cpumap, i);
         }
 
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 88fa6ff..d4a8b9d 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -2051,7 +2051,6 @@ libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps,
     virBitmapPtr cpumask = NULL;
     int maxcpu, hostcpus, vcpu, pcpu, n, ret = -1;
     unsigned char *cpumap;
-    bool pinned;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                   VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -2100,9 +2099,7 @@ libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps,
         cpumask = vcpupin_list[n]->cpumask;
         cpumap = VIR_GET_CPUMAP(cpumaps, maplen, vcpu);
         for (pcpu = 0; pcpu < maxcpu; pcpu++) {
-            if (virBitmapGetBit(cpumask, pcpu, &pinned) < 0)
-                goto cleanup;
-            if (!pinned)
+            if (!virBitmapIsBitSet(cpumask, pcpu))
                 VIR_UNUSE_CPU(cpumap, pcpu);
         }
     }
diff --git a/src/nodeinfo.c b/src/nodeinfo.c
index 3a27c22..05256cc 100644
--- a/src/nodeinfo.c
+++ b/src/nodeinfo.c
@@ -1911,11 +1911,7 @@ nodeCapsInitNUMA(virCapsPtr caps)
         cpu = 0;
 
         for (i = 0; i < virBitmapSize(cpumap); i++) {
-            bool cpustate;
-            if (virBitmapGetBit(cpumap, i, &cpustate) < 0)
-                continue;
-
-            if (cpustate) {
+            if (virBitmapIsBitSet(cpumap, i)) {
                 if (virNodeCapsFillCPUInfo(i, cpus + cpu++) < 0) {
                     topology_failed = true;
                     virResetLastError();
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index c513d46..ccf22f0 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2048,12 +2048,7 @@ bool
 virQEMUCapsGet(virQEMUCapsPtr qemuCaps,
                virQEMUCapsFlags flag)
 {
-    bool b;
-
-    if (!qemuCaps || virBitmapGetBit(qemuCaps->flags, flag, &b) < 0)
-        return false;
-    else
-        return b;
+    return qemuCaps && virBitmapIsBitSet(qemuCaps->flags, flag);
 }
 
 
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index a4e75f5a5..9322571 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -807,7 +807,6 @@ virStorageBackendCreateQemuImgOpts(char **opts,
                                    virBitmapPtr features)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
-    bool b;
     size_t i;
 
     if (backingType)
@@ -823,8 +822,7 @@ virStorageBackendCreateQemuImgOpts(char **opts,
         virBufferAsprintf(&buf, "compat=%s,", compat);
     if (features && format == VIR_STORAGE_FILE_QCOW2) {
         for (i = 0; i < VIR_STORAGE_FILE_FEATURE_LAST; i++) {
-            ignore_value(virBitmapGetBit(features, i, &b));
-            if (b) {
+            if (virBitmapIsBitSet(features, i)) {
                 switch ((virStorageFileFeature) i) {
                 case VIR_STORAGE_FILE_FEATURE_LAZY_REFCOUNTS:
                     if (STREQ_NULLABLE(compat, "0.10")) {
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
index b531be5..5322bce 100644
--- a/src/util/virbitmap.c
+++ b/src/util/virbitmap.c
@@ -179,6 +179,24 @@ static bool virBitmapIsSet(virBitmapPtr bitmap, size_t b)
 }
 
 /**
+ * virBitmapIsBitSet:
+ * @bitmap: Pointer to bitmap
+ * @b: bit position to get
+ *
+ * Get setting of bit position @b in @bitmap.
+ *
+ * If @b is in the range of @bitmap, returns the value of the bit.
+ * Otherwise false is returned.
+ */
+bool virBitmapIsBitSet(virBitmapPtr bitmap, size_t b)
+{
+    if (bitmap->max_bit <= b)
+        return false;
+
+    return virBitmapIsSet(bitmap, b);
+}
+
+/**
  * virBitmapGetBit:
  * @bitmap: Pointer to bitmap
  * @b: bit position to get
diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h
index 136e819..d326c6a 100644
--- a/src/util/virbitmap.h
+++ b/src/util/virbitmap.h
@@ -62,6 +62,11 @@ int virBitmapClearBit(virBitmapPtr bitmap, size_t b)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
 
 /*
+ * Get bit @b in @bitmap. Returns false if b is out of range.
+ */
+bool virBitmapIsBitSet(virBitmapPtr bitmap, size_t b)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
+/*
  * Get setting of bit position @b in @bitmap and store in @result
  */
 int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 6957e81..e54a079 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -3085,9 +3085,7 @@ virCgroupGetPercpuStats(virCgroupPtr group,
     need_cpus = MIN(total_cpus, start_cpu + ncpus);
 
     for (i = 0; i < need_cpus; i++) {
-        bool present;
-        ignore_value(virBitmapGetBit(cpumap, i, &present));
-        if (!present) {
+        if (!virBitmapIsBitSet(cpumap, i)) {
             cpu_time = 0;
         } else if (virStrToLong_ull(pos, &pos, 10, &cpu_time) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c
index 97652c0..6a2a2fb 100644
--- a/src/util/virdnsmasq.c
+++ b/src/util/virdnsmasq.c
@@ -874,10 +874,6 @@ dnsmasqCapsGetVersion(dnsmasqCapsPtr caps)
 bool
 dnsmasqCapsGet(dnsmasqCapsPtr caps, dnsmasqCapsFlags flag)
 {
-    bool b;
 
-    if (!caps || virBitmapGetBit(caps->flags, flag, &b) < 0)
-        return false;
-    else
-        return b;
+    return caps && virBitmapIsBitSet(caps->flags, flag);
 }
diff --git a/src/util/virportallocator.c b/src/util/virportallocator.c
index 578debf..fcd4f74 100644
--- a/src/util/virportallocator.c
+++ b/src/util/virportallocator.c
@@ -184,14 +184,7 @@ int virPortAllocatorAcquire(virPortAllocatorPtr pa,
     for (i = pa->start; i <= pa->end && !*port; i++) {
         bool used = false, v6used = false;
 
-        if (virBitmapGetBit(pa->bitmap,
-                            i - pa->start, &used) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Failed to query port %zu"), i);
-            goto cleanup;
-        }
-
-        if (used)
+        if (virBitmapIsBitSet(pa->bitmap, i - pa->start))
             continue;
 
         if (!(pa->flags & VIR_PORT_ALLOCATOR_SKIP_BIND_CHECK)) {
@@ -269,12 +262,8 @@ int virPortAllocatorSetUsed(virPortAllocatorPtr pa,
     }
 
     if (value) {
-        bool used = false;
-        if (virBitmapGetBit(pa->bitmap, port - pa->start, &used) < 0)
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Failed to query port %d"), port);
-
-        if (used || virBitmapSetBit(pa->bitmap, port - pa->start) < 0) {
+        if (virBitmapIsBitSet(pa->bitmap, port - pa->start) ||
+            virBitmapSetBit(pa->bitmap, port - pa->start) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Failed to reserve port %d"), port);
             goto cleanup;
diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index 342bf40..aa00a99 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -407,7 +407,6 @@ virProcessKillPainfully(pid_t pid, bool force)
 int virProcessSetAffinity(pid_t pid, virBitmapPtr map)
 {
     size_t i;
-    bool set = false;
     VIR_DEBUG("Set process affinity on %lld\n", (long long)pid);
 # ifdef CPU_ALLOC
     /* New method dynamically allocates cpu mask, allowing unlimted cpus */
@@ -433,9 +432,7 @@ int virProcessSetAffinity(pid_t pid, virBitmapPtr map)
 
     CPU_ZERO_S(masklen, mask);
     for (i = 0; i < virBitmapSize(map); i++) {
-        if (virBitmapGetBit(map, i, &set) < 0)
-            return -1;
-        if (set)
+        if (virBitmapIsBitSet(map, i))
             CPU_SET_S(i, masklen, mask);
     }
 
@@ -457,9 +454,7 @@ int virProcessSetAffinity(pid_t pid, virBitmapPtr map)
 
     CPU_ZERO(&mask);
     for (i = 0; i < virBitmapSize(map); i++) {
-        if (virBitmapGetBit(map, i, &set) < 0)
-            return -1;
-        if (set)
+        if (virBitmapIsBitSet(map, i))
             CPU_SET(i, &mask);
     }
 
diff --git a/src/xen/xen_driver.c b/src/xen/xen_driver.c
index 5e6ef68..7577881 100644
--- a/src/xen/xen_driver.c
+++ b/src/xen/xen_driver.c
@@ -232,9 +232,7 @@ xenDomainUsedCpus(virDomainPtr dom, virDomainDefPtr def)
                                                   cpumap, cpumaplen)) >= 0) {
         for (n = 0; n < ncpus; n++) {
             for (m = 0; m < priv->nbNodeCpus; m++) {
-                bool used;
-                ignore_value(virBitmapGetBit(cpulist, m, &used));
-                if ((!used) &&
+                if (!virBitmapIsBitSet(cpulist, m) &&
                     (VIR_CPU_USABLE(cpumap, cpumaplen, n, m))) {
                     ignore_value(virBitmapSetBit(cpulist, m));
                     nb++;
diff --git a/src/xen/xend_internal.c b/src/xen/xend_internal.c
index ab03c1c..1722c3f 100644
--- a/src/xen/xend_internal.c
+++ b/src/xen/xend_internal.c
@@ -1089,10 +1089,7 @@ sexpr_to_xend_topology(const struct sexpr *root, virCapsPtr caps)
         }
 
         for (n = 0, cpu = 0; cpu < numCpus; cpu++) {
-            bool used;
-
-            ignore_value(virBitmapGetBit(cpuset, cpu, &used));
-            if (used)
+            if (virBitmapIsBitSet(cpuset, cpu))
                 cpuInfo[n++].id = cpu;
         }
         virBitmapFree(cpuset);
-- 
2.0.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[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]