[Qemu-devel] [PATCH v3 12/25] xen: Let buffer_append() return the size consumed

Philippe Mathieu-Daudé posted 25 patches 6 years, 8 months ago
Maintainers: Anthony Perard <anthony.perard@citrix.com>, Li Zhijian <lizhijian@cn.fujitsu.com>, Jason Wang <jasowang@redhat.com>, Cornelia Huck <cohuck@redhat.com>, Amit Shah <amit@kernel.org>, "Michael S. Tsirkin" <mst@redhat.com>, Paul Durrant <paul.durrant@citrix.com>, Halil Pasic <pasic@linux.ibm.com>, Christian Borntraeger <borntraeger@de.ibm.com>, David Gibson <david@gibson.dropbear.id.au>, Zhang Chen <zhangckid@gmail.com>, Corey Minyard <minyard@acm.org>, Gerd Hoffmann <kraxel@redhat.com>, Stefano Stabellini <sstabellini@kernel.org>, Paolo Bonzini <pbonzini@redhat.com>, Stefan Berger <stefanb@linux.ibm.com>, Samuel Thibault <samuel.thibault@ens-lyon.org>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>
[Qemu-devel] [PATCH v3 12/25] xen: Let buffer_append() return the size consumed
Posted by Philippe Mathieu-Daudé 6 years, 8 months ago
The buffer.size and buffer.consumed fields are only updated within the
buffer_append() body. We can simply let buffer_append() return the
difference (the buffer consumed).

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 hw/char/xen_console.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
index 083b2c8e2a..1a30014a11 100644
--- a/hw/char/xen_console.c
+++ b/hw/char/xen_console.c
@@ -48,7 +48,7 @@ struct XenConsole {
     int               backlog;
 };
 
-static void buffer_append(struct XenConsole *con)
+static ssize_t buffer_append(struct XenConsole *con)
 {
     struct buffer *buffer = &con->buffer;
     XENCONS_RING_IDX cons, prod, size;
@@ -59,8 +59,9 @@ static void buffer_append(struct XenConsole *con)
     xen_mb();
 
     size = prod - cons;
-    if ((size == 0) || (size > sizeof(intf->out)))
-        return;
+    if ((size == 0) || (size > sizeof(intf->out))) {
+        goto out;
+    }
 
     if ((buffer->capacity - buffer->size) < size) {
         buffer->capacity += (size + 1024);
@@ -89,6 +90,9 @@ static void buffer_append(struct XenConsole *con)
         if (buffer->consumed > buffer->max_capacity - over)
             buffer->consumed = buffer->max_capacity - over;
     }
+
+ out:
+    return buffer->size - buffer->consumed;
 }
 
 static void buffer_advance(struct buffer *buffer, size_t len)
@@ -281,8 +285,7 @@ static void con_event(struct XenLegacyDevice *xendev)
     struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
     ssize_t size;
 
-    buffer_append(con);
-    size = con->buffer.size - con->buffer.consumed;
+    size = buffer_append(con);
     if (size) {
         xencons_send(con, size);
     }
-- 
2.20.1


Re: [Qemu-devel] [PATCH v3 12/25] xen: Let buffer_append() return the size consumed
Posted by Marc-André Lureau 6 years, 8 months ago
Hi

On Wed, Feb 20, 2019 at 2:06 AM Philippe Mathieu-Daudé
<philmd@redhat.com> wrote:
>
> The buffer.size and buffer.consumed fields are only updated within the
> buffer_append() body. We can simply let buffer_append() return the
> difference (the buffer consumed).
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>

This is weird, why not introduce a buffer_size() function instead?

> ---
>  hw/char/xen_console.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
> index 083b2c8e2a..1a30014a11 100644
> --- a/hw/char/xen_console.c
> +++ b/hw/char/xen_console.c
> @@ -48,7 +48,7 @@ struct XenConsole {
>      int               backlog;
>  };
>
> -static void buffer_append(struct XenConsole *con)
> +static ssize_t buffer_append(struct XenConsole *con)
>  {
>      struct buffer *buffer = &con->buffer;
>      XENCONS_RING_IDX cons, prod, size;
> @@ -59,8 +59,9 @@ static void buffer_append(struct XenConsole *con)
>      xen_mb();
>
>      size = prod - cons;
> -    if ((size == 0) || (size > sizeof(intf->out)))
> -        return;
> +    if ((size == 0) || (size > sizeof(intf->out))) {
> +        goto out;
> +    }
>
>      if ((buffer->capacity - buffer->size) < size) {
>          buffer->capacity += (size + 1024);
> @@ -89,6 +90,9 @@ static void buffer_append(struct XenConsole *con)
>          if (buffer->consumed > buffer->max_capacity - over)
>              buffer->consumed = buffer->max_capacity - over;
>      }
> +
> + out:
> +    return buffer->size - buffer->consumed;
>  }
>
>  static void buffer_advance(struct buffer *buffer, size_t len)
> @@ -281,8 +285,7 @@ static void con_event(struct XenLegacyDevice *xendev)
>      struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
>      ssize_t size;
>
> -    buffer_append(con);
> -    size = con->buffer.size - con->buffer.consumed;
> +    size = buffer_append(con);
>      if (size) {
>          xencons_send(con, size);
>      }
> --
> 2.20.1
>