From: Laszlo Ersek <lersek@redhat.com>
To: Li Qiang <liq3ea@163.com>, mst@redhat.com, kraxel@redhat.com
Cc: liq3ea@gmail.com, qemu-devel@nongnu.org
Subject: Re: [PATCH] hw: virtio-gpu: remove duplicated 'virtqueue_pop'
Date: Thu, 13 Aug 2020 22:20:48 +0200 [thread overview]
Message-ID: <47b9f12f-2874-1e1c-2fc1-7ea1a81cc401@redhat.com> (raw)
In-Reply-To: <20200813153614.39917-1-liq3ea@163.com>
On 08/13/20 17:36, Li Qiang wrote:
> Just use 'while (true)' to avoid duplicated.
> No function change.
>
> Signed-off-by: Li Qiang <liq3ea@163.com>
> ---
> hw/display/virtio-gpu.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
> index 5f0dd7c150..9cef313f5e 100644
> --- a/hw/display/virtio-gpu.c
> +++ b/hw/display/virtio-gpu.c
> @@ -869,13 +869,15 @@ static void virtio_gpu_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
> }
> #endif
>
> - cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
> - while (cmd) {
> + while (true) {
> + cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
> + if (!cmd) {
> + break;
> + }
> cmd->vq = vq;
> cmd->error = 0;
> cmd->finished = false;
> QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
> - cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
> }
>
> virtio_gpu_process_cmdq(g);
>
There are (at least) three styles:
(1)
thing = get_next();
while (is_valid(thing)) {
...
thing = get_next();
}
(2)
while (true) {
thing = get_next();
if (!is_valid(thing)) {
break;
}
...
}
(3)
while (is_valid(thing = get_next())) {
...
}
My opinion:
- If the get_next() invocation is simple, then style (1) is perfectly fine.
- Style (2) is the worst of all.
- If style (1) is not appropriate for whatever reason, then style (3) is frequently a good replacement. Style (3) is sometimes rejected by coding style documents though. Style (3) is not usable if is_valid() is a function-like macro that does not evaluate its argument exactly once. Frequently, is_valid() is simply open-coded with C operators (using extra parens), for example:
while ((cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command)))) {
or more verbosely
while ((cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command))) !=
NULL) {
If we really dislike style (1), then I'd propose style (3). I think the present patch (style (2)) is a step back.
Just my opinion of course; I don't feel too strongly about this.
Laszlo
next prev parent reply other threads:[~2020-08-13 20:21 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-13 15:36 [PATCH] hw: virtio-gpu: remove duplicated 'virtqueue_pop' Li Qiang
2020-08-13 20:20 ` Laszlo Ersek [this message]
2020-08-14 0:52 ` Li Qiang
2020-08-17 7:01 ` Gerd Hoffmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=47b9f12f-2874-1e1c-2fc1-7ea1a81cc401@redhat.com \
--to=lersek@redhat.com \
--cc=kraxel@redhat.com \
--cc=liq3ea@163.com \
--cc=liq3ea@gmail.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).