diff options
author | Stefan Hajnoczi <stefanha@redhat.com> | 2014-07-09 10:05:49 +0200 |
---|---|---|
committer | Kevin Wolf <kwolf@redhat.com> | 2014-07-14 12:03:20 +0200 |
commit | f897bf751fbd95e4015b95d202c706548586813a (patch) | |
tree | 38023ac92b901f2cd4c7aa45b34549a08cfba9ca /hw/virtio/dataplane | |
parent | 869d66af53d8e04709456c9cae5cca7c560d4b93 (diff) |
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 <stefanha@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'hw/virtio/dataplane')
-rw-r--r-- | hw/virtio/dataplane/vring.c | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/hw/virtio/dataplane/vring.c b/hw/virtio/dataplane/vring.c index 5d17d39c17..67cb2b823e 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; } |