Simplify virtio ring num-available calculation.

Submitted by:	Chris Torek, torek at torek dot net
This commit is contained in:
Peter Grehan 2013-03-06 07:28:20 +00:00
parent 0cfbcf8c7b
commit 6be7c5e31c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=247871
2 changed files with 21 additions and 11 deletions

View File

@ -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);
}

View File

@ -172,12 +172,17 @@ hq_num_avail(struct vring_hqueue *hq)
{
int 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);
}