trivial improvement for usr.sbin/bhyve/pci_virtio_block.c

Chris Torek torek at torek.net
Fri Mar 1 00:49:41 UTC 2013


I was looking through the bhyve code and noticed an obvious
easy (if trivial) code improvement.

Tested "standalone" rather than inside bhyve (with both gcc and
clang, on FreeBSD 9.0).

Not sure where/how diffs should go, so I figured I would send this
here as a test. :-)

Chris

Index: pci_virtio_block.c
===================================================================
--- pci_virtio_block.c	(revision 247510)
+++ pci_virtio_block.c	(working copy)
@@ -164,14 +164,19 @@
 static int
 hq_num_avail(struct vring_hqueue *hq)
 {
-	int ndesc;
+	uint16_t ndesc;
 
-	if (*hq->hq_avail_idx >= hq->hq_cur_aidx)
-		ndesc = *hq->hq_avail_idx - hq->hq_cur_aidx;
-	else
-		ndesc = UINT16_MAX - hq->hq_cur_aidx + *hq->hq_avail_idx + 1;
+	/*
+	 * We're just computing (a-b) in GF(2^16).
+	 *
+	 * The only glitch here is that in standard C,
+	 * uint16_t promotes to (signed) int when int has
+	 * more than 16 bits (pretty much always now), so
+	 * we have to force it back to unsigned.
+	 */
+	ndesc = (unsigned)*hq->hq_avail_idx - (unsigned)hq->hq_cur_aidx;
 
-	assert(ndesc >= 0 && ndesc <= hq->hq_size);
+	assert(ndesc <= hq->hq_size);
 
 	return (ndesc);
 }


More information about the freebsd-virtualization mailing list