From 2527cbb869d02606cbf9bf5a54d79a5246920bca Mon Sep 17 00:00:00 2001 From: grehan Date: Wed, 6 Mar 2013 07:28:20 +0000 Subject: Simplify virtio ring num-available calculation. Submitted by: Chris Torek, torek at torek dot net --- usr.sbin/bhyve/pci_virtio_block.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'usr.sbin/bhyve/pci_virtio_block.c') diff --git a/usr.sbin/bhyve/pci_virtio_block.c b/usr.sbin/bhyve/pci_virtio_block.c index 31ff2e6..0c34666 100644 --- a/usr.sbin/bhyve/pci_virtio_block.c +++ b/usr.sbin/bhyve/pci_virtio_block.c @@ -164,14 +164,19 @@ pci_vtblk_iosize(struct pci_devinst *pi) 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(216). + * + * 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); } -- cgit v1.1