Patchwork [33/41] qemu-file: simplify and export qemu_ftell

login
register
mail settings
Submitter Paolo Bonzini
Date Feb. 15, 2013, 5:47 p.m.
Message ID <1360950433-17106-34-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/220795/
State New
Headers show

Comments

Paolo Bonzini - Feb. 15, 2013, 5:47 p.m.
Force a flush when qemu_ftell is called.  This simplifies the buffer magic
(it also breaks qemu_ftell for input QEMUFiles, but we never use it).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 savevm.c |   20 ++++++++------------
 1 files changed, 8 insertions(+), 12 deletions(-)
Orit Wasserman - Feb. 21, 2013, 8:14 a.m.
On 02/15/2013 07:47 PM, Paolo Bonzini wrote:
> Force a flush when qemu_ftell is called.  This simplifies the buffer magic
> (it also breaks qemu_ftell for input QEMUFiles, but we never use it).
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  savevm.c |   20 ++++++++------------
>  1 files changed, 8 insertions(+), 12 deletions(-)
> 
> diff --git a/savevm.c b/savevm.c
> index b6cf415..e4dc01d 100644
> --- a/savevm.c
> +++ b/savevm.c
> @@ -119,8 +119,8 @@ struct QEMUFile {
>      void *opaque;
>      int is_write;
>  
> -    int64_t buf_offset; /* start of buffer when writing, end of buffer
> -                           when reading */
> +    int64_t pos; /* start of buffer when writing, end of buffer
> +                    when reading */
>      int buf_index;
>      int buf_size; /* 0 when writing */
>      uint8_t buf[IO_BUF_SIZE];
> @@ -505,9 +505,9 @@ static void qemu_fflush(QEMUFile *f)
>          return;
>      }
>      if (f->is_write && f->buf_index > 0) {
> -        ret = f->ops->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
> +        ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
>          if (ret >= 0) {
> -            f->buf_offset += f->buf_index;
> +            f->pos += f->buf_index;
>          }
>          f->buf_index = 0;
>      }
> @@ -534,11 +534,11 @@ static void qemu_fill_buffer(QEMUFile *f)
>      f->buf_index = 0;
>      f->buf_size = pending;
>  
> -    len = f->ops->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
> +    len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
>                          IO_BUF_SIZE - pending);
>      if (len > 0) {
>          f->buf_size += len;
> -        f->buf_offset += len;
> +        f->pos += len;
>      } else if (len == 0) {
>          qemu_file_set_error(f, -EIO);
>      } else if (len != -EAGAIN)
> @@ -718,12 +718,8 @@ int qemu_get_byte(QEMUFile *f)
>  
>  int64_t qemu_ftell(QEMUFile *f)
>  {
> -    /* buf_offset excludes buffer for writing but includes it for reading */
> -    if (f->is_write) {
> -        return f->buf_offset + f->buf_index;
> -    } else {
> -        return f->buf_offset - f->buf_size + f->buf_index;
> -    }
> +    qemu_fflush(f);
> +    return f->pos;
>  }
>  
>  int qemu_file_rate_limit(QEMUFile *f)
> 
Reviewed-by: Orit Wasserman <owasserm@redhat.com>
Juan Quintela - Feb. 22, 2013, 11:23 a.m.
Paolo Bonzini <pbonzini@redhat.com> wrote:
> Force a flush when qemu_ftell is called.  This simplifies the buffer magic
> (it also breaks qemu_ftell for input QEMUFiles, but we never use it).
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Acked-by: Juan Quintela <quintela@redhat.com>

Patch

diff --git a/savevm.c b/savevm.c
index b6cf415..e4dc01d 100644
--- a/savevm.c
+++ b/savevm.c
@@ -119,8 +119,8 @@  struct QEMUFile {
     void *opaque;
     int is_write;
 
-    int64_t buf_offset; /* start of buffer when writing, end of buffer
-                           when reading */
+    int64_t pos; /* start of buffer when writing, end of buffer
+                    when reading */
     int buf_index;
     int buf_size; /* 0 when writing */
     uint8_t buf[IO_BUF_SIZE];
@@ -505,9 +505,9 @@  static void qemu_fflush(QEMUFile *f)
         return;
     }
     if (f->is_write && f->buf_index > 0) {
-        ret = f->ops->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
+        ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
         if (ret >= 0) {
-            f->buf_offset += f->buf_index;
+            f->pos += f->buf_index;
         }
         f->buf_index = 0;
     }
@@ -534,11 +534,11 @@  static void qemu_fill_buffer(QEMUFile *f)
     f->buf_index = 0;
     f->buf_size = pending;
 
-    len = f->ops->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
+    len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
                         IO_BUF_SIZE - pending);
     if (len > 0) {
         f->buf_size += len;
-        f->buf_offset += len;
+        f->pos += len;
     } else if (len == 0) {
         qemu_file_set_error(f, -EIO);
     } else if (len != -EAGAIN)
@@ -718,12 +718,8 @@  int qemu_get_byte(QEMUFile *f)
 
 int64_t qemu_ftell(QEMUFile *f)
 {
-    /* buf_offset excludes buffer for writing but includes it for reading */
-    if (f->is_write) {
-        return f->buf_offset + f->buf_index;
-    } else {
-        return f->buf_offset - f->buf_size + f->buf_index;
-    }
+    qemu_fflush(f);
+    return f->pos;
 }
 
 int qemu_file_rate_limit(QEMUFile *f)