From f897bf751fbd95e4015b95d202c706548586813a Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Wed, 9 Jul 2014 10:05:49 +0200 Subject: virtio-blk: embed VirtQueueElement in VirtIOBlockReq The memory allocation between hw/block/virtio-blk.c, hw/block/dataplane/virtio-blk.c, and hw/virtio/dataplane/vring.c is messy. Structs are allocated in different files than they are freed in. This is risky and makes memory leaks easier. Embed VirtQueueElement in VirtIOBlockReq to reduce the amount of memory allocation we need to juggle. This also makes vring.c and virtio.c slightly more similar. Signed-off-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf --- hw/virtio/dataplane/vring.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'hw/virtio/dataplane') diff --git a/hw/virtio/dataplane/vring.c b/hw/virtio/dataplane/vring.c index 5d17d39..67cb2b8 100644 --- a/hw/virtio/dataplane/vring.c +++ b/hw/virtio/dataplane/vring.c @@ -301,14 +301,16 @@ static void vring_unmap_element(VirtQueueElement *elem) * Stolen from linux/drivers/vhost/vhost.c. */ int vring_pop(VirtIODevice *vdev, Vring *vring, - VirtQueueElement **p_elem) + VirtQueueElement *elem) { struct vring_desc desc; unsigned int i, head, found = 0, num = vring->vr.num; uint16_t avail_idx, last_avail_idx; - VirtQueueElement *elem = NULL; int ret; + /* Initialize elem so it can be safely unmapped */ + elem->in_num = elem->out_num = 0; + /* If there was a fatal error then refuse operation */ if (vring->broken) { ret = -EFAULT; @@ -340,10 +342,8 @@ int vring_pop(VirtIODevice *vdev, Vring *vring, * the index we've seen. */ head = vring->vr.avail->ring[last_avail_idx % num]; - elem = g_slice_new(VirtQueueElement); elem->index = head; - elem->in_num = elem->out_num = 0; - + /* If their number is silly, that's an error. */ if (unlikely(head >= num)) { error_report("Guest says index %u > %u is available", head, num); @@ -391,7 +391,6 @@ int vring_pop(VirtIODevice *vdev, Vring *vring, /* On success, increment avail index. */ vring->last_avail_idx++; - *p_elem = elem; return head; out: @@ -399,11 +398,7 @@ out: if (ret == -EFAULT) { vring->broken = true; } - if (elem) { - vring_unmap_element(elem); - g_slice_free(VirtQueueElement, elem); - } - *p_elem = NULL; + vring_unmap_element(elem); return ret; } -- cgit v1.1