qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hw: virtio-gpu: remove duplicated 'virtqueue_pop'
@ 2020-08-13 15:36 Li Qiang
  2020-08-13 20:20 ` Laszlo Ersek
  0 siblings, 1 reply; 4+ messages in thread
From: Li Qiang @ 2020-08-13 15:36 UTC (permalink / raw)
  To: mst, kraxel; +Cc: Li Qiang, liq3ea, qemu-devel

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);
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] hw: virtio-gpu: remove duplicated 'virtqueue_pop'
  2020-08-13 15:36 [PATCH] hw: virtio-gpu: remove duplicated 'virtqueue_pop' Li Qiang
@ 2020-08-13 20:20 ` Laszlo Ersek
  2020-08-14  0:52   ` Li Qiang
  2020-08-17  7:01   ` Gerd Hoffmann
  0 siblings, 2 replies; 4+ messages in thread
From: Laszlo Ersek @ 2020-08-13 20:20 UTC (permalink / raw)
  To: Li Qiang, mst, kraxel; +Cc: liq3ea, qemu-devel

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hw: virtio-gpu: remove duplicated 'virtqueue_pop'
  2020-08-13 20:20 ` Laszlo Ersek
@ 2020-08-14  0:52   ` Li Qiang
  2020-08-17  7:01   ` Gerd Hoffmann
  1 sibling, 0 replies; 4+ messages in thread
From: Li Qiang @ 2020-08-14  0:52 UTC (permalink / raw)
  To: Laszlo Ersek; +Cc: Qemu Developers, Li Qiang, Gerd Hoffmann, Michael S. Tsirkin

Laszlo Ersek <lersek@redhat.com> 于2020年8月14日周五 上午4:21写道:
>
> 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.
>

I have no strong opinion about the style(2) and style(3), just don't
like the dup of style(1).

Anyway, let Gerd do the choice. AFAICS, the qemu uses a lot of
stype(2)  to populate virtio requests.

Thanks,
Li Qiang

> Just my opinion of course; I don't feel too strongly about this.
>
> Laszlo
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hw: virtio-gpu: remove duplicated 'virtqueue_pop'
  2020-08-13 20:20 ` Laszlo Ersek
  2020-08-14  0:52   ` Li Qiang
@ 2020-08-17  7:01   ` Gerd Hoffmann
  1 sibling, 0 replies; 4+ messages in thread
From: Gerd Hoffmann @ 2020-08-17  7:01 UTC (permalink / raw)
  To: Laszlo Ersek; +Cc: liq3ea, Li Qiang, qemu-devel, mst

  Hi,

> - If the get_next() invocation is simple, then style (1) is perfectly fine.

Fully agree.  Duplicating a single line is perfectly fine if it is
simple enough that you can hardly get it wrong.

> - Style (2) is the worst of all.

Yes, especially because the break is three lines not two due to qemu
code style.

> - 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:

> or more verbosely
> 
>     while ((cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command))) !=
>            NULL) {

Style 3 tends to generate long lines, which in turn need line breaks
like this.  Also many parens don't help making code more readable.
So I'd prefer to just leave things as-is.

take care,
  Gerd



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-08-17  7:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-08-13 15:36 [PATCH] hw: virtio-gpu: remove duplicated 'virtqueue_pop' Li Qiang
2020-08-13 20:20 ` Laszlo Ersek
2020-08-14  0:52   ` Li Qiang
2020-08-17  7:01   ` Gerd Hoffmann

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).