]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
internal.h: Introduce and use VIR_IS_POW2()
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 4 Nov 2020 18:41:27 +0000 (19:41 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 4 Dec 2020 15:24:19 +0000 (16:24 +0100)
This macro checks whether given number is an integer power of
two. At the same time, I've identified two places where we check
for pow2 and I'm replacing them with the macro.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Tested-by: Han Han <hhan@redhat.com>
src/internal.h
src/qemu/qemu_validate.c
src/util/virrandom.c

index ff94e7e53e0fb85b4d2ce3438572de5691b819a7..0a03dfc46fbb7d665cd545a8b079fdf8e2a86b75 100644 (file)
         (a) = (a) ^ (b); \
     } while (0)
 
+
+/**
+ * VIR_IS_POW2:
+ *
+ * Returns true if given number is a power of two
+ */
+#define VIR_IS_POW2(x) \
+    ((x) && !((x) & ((x) - 1)))
+
+
 /**
  * virCheckFlags:
  * @supported: an OR'ed set of supported flags
index e60d39a22fe23a82a26db353e37bb29c270cbebe..6f4662b25aff231df4275a9c41382e423a90db4d 100644 (file)
@@ -1460,20 +1460,32 @@ qemuValidateDomainDeviceDefNetwork(const virDomainNetDef *net,
             return -1;
         }
 
-        if (net->driver.virtio.rx_queue_size &&
-            !virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_NET_RX_QUEUE_SIZE)) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                           _("virtio rx_queue_size option is not supported "
-                             "with this QEMU binary"));
-            return -1;
+        if (net->driver.virtio.rx_queue_size) {
+            if (!VIR_IS_POW2(net->driver.virtio.rx_queue_size)) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                               _("rx_queue_size has to be a power of two"));
+                return -1;
+            }
+            if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_NET_RX_QUEUE_SIZE)) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                               _("virtio rx_queue_size option is not supported "
+                                 "with this QEMU binary"));
+                return -1;
+            }
         }
 
-        if (net->driver.virtio.tx_queue_size &&
-            !virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_NET_TX_QUEUE_SIZE)) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                           _("virtio tx_queue_size option is not supported "
-                             "with this QEMU binary"));
-            return -1;
+        if (net->driver.virtio.tx_queue_size) {
+            if (!VIR_IS_POW2(net->driver.virtio.tx_queue_size)) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                               _("tx_queue_size has to be a power of two"));
+                return -1;
+            }
+            if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_NET_TX_QUEUE_SIZE)) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                               _("virtio tx_queue_size option is not supported "
+                                 "with this QEMU binary"));
+                return -1;
+            }
         }
 
         if (net->mtu &&
@@ -1484,16 +1496,6 @@ qemuValidateDomainDeviceDefNetwork(const virDomainNetDef *net,
             return -1;
         }
 
-        if (net->driver.virtio.rx_queue_size & (net->driver.virtio.rx_queue_size - 1)) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                           _("rx_queue_size has to be a power of two"));
-            return -1;
-        }
-        if (net->driver.virtio.tx_queue_size & (net->driver.virtio.tx_queue_size - 1)) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                           _("tx_queue_size has to be a power of two"));
-            return -1;
-        }
         if (qemuValidateDomainVirtioOptions(net->virtio, qemuCaps) < 0)
             return -1;
     }
index 6417232e3a9996f332daffabdf3895d2617a4be1..3ae1297e6b09ed68f04d245ada0c04ed02d26123 100644 (file)
@@ -89,7 +89,7 @@ double virRandom(void)
  */
 uint32_t virRandomInt(uint32_t max)
 {
-    if ((max & (max - 1)) == 0)
+    if (VIR_IS_POW2(max))
         return virRandomBits(__builtin_ffs(max) - 1);
 
     return virRandom() * max;