Patchwork [12/14] buffered_file: unfold buffered_append in buffered_put_buffer

login
register
mail settings
Submitter Juan Quintela
Date Sept. 21, 2012, 2:08 p.m.
Message ID <1348236500-2565-13-git-send-email-quintela@redhat.com>
Download mbox | patch
Permalink /patch/185806/
State New
Headers show

Comments

Juan Quintela - Sept. 21, 2012, 2:08 p.m.
It was the only user, and now buffered_put_buffer just do the append

Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 buffered_file.c | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)
Paolo Bonzini - Sept. 21, 2012, 5:07 p.m.
Il 21/09/2012 16:08, Juan Quintela ha scritto:
> It was the only user, and now buffered_put_buffer just do the append
> 
> Signed-off-by: Juan Quintela <quintela@redhat.com>
> ---
>  buffered_file.c | 33 ++++++++++++++-------------------
>  1 file changed, 14 insertions(+), 19 deletions(-)
> 
> diff --git a/buffered_file.c b/buffered_file.c
> index 929b911..1b261ec 100644
> --- a/buffered_file.c
> +++ b/buffered_file.c
> @@ -42,22 +42,6 @@ typedef struct QEMUFileBuffered
>      do { } while (0)
>  #endif
> 
> -static void buffered_append(QEMUFileBuffered *s,
> -                            const uint8_t *buf, size_t size)
> -{
> -    if (size > (s->buffer_capacity - s->buffer_size)) {
> -        DPRINTF("increasing buffer capacity from %zu by %zu\n",
> -                s->buffer_capacity, size + 1024);
> -
> -        s->buffer_capacity += size + 1024;
> -
> -        s->buffer = g_realloc(s->buffer, s->buffer_capacity);
> -    }
> -
> -    memcpy(s->buffer + s->buffer_size, buf, size);
> -    s->buffer_size += size;
> -}
> -
>  static int buffered_flush(QEMUFileBuffered *s)
>  {
>      size_t offset = 0;
> @@ -102,11 +86,22 @@ static int buffered_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, in
>          return error;
>      }
> 
> -    if (size > 0) {
> -        DPRINTF("buffering %d bytes\n", size - offset);
> -        buffered_append(s, buf, size);
> +    if (size <= 0) {
> +        return size;
>      }
> 
> +    if (size > (s->buffer_capacity - s->buffer_size)) {
> +        DPRINTF("increasing buffer capacity from %zu by %zu\n",
> +                s->buffer_capacity, size + 1024);
> +
> +        s->buffer_capacity += size + 1024;
> +
> +        s->buffer = g_realloc(s->buffer, s->buffer_capacity);
> +    }
> +
> +    memcpy(s->buffer + s->buffer_size, buf, size);
> +    s->buffer_size += size;
> +
>      return size;
>  }
> 

Ah, finally this arrived as well. :)

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>

Patch

diff --git a/buffered_file.c b/buffered_file.c
index 929b911..1b261ec 100644
--- a/buffered_file.c
+++ b/buffered_file.c
@@ -42,22 +42,6 @@  typedef struct QEMUFileBuffered
     do { } while (0)
 #endif

-static void buffered_append(QEMUFileBuffered *s,
-                            const uint8_t *buf, size_t size)
-{
-    if (size > (s->buffer_capacity - s->buffer_size)) {
-        DPRINTF("increasing buffer capacity from %zu by %zu\n",
-                s->buffer_capacity, size + 1024);
-
-        s->buffer_capacity += size + 1024;
-
-        s->buffer = g_realloc(s->buffer, s->buffer_capacity);
-    }
-
-    memcpy(s->buffer + s->buffer_size, buf, size);
-    s->buffer_size += size;
-}
-
 static int buffered_flush(QEMUFileBuffered *s)
 {
     size_t offset = 0;
@@ -102,11 +86,22 @@  static int buffered_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, in
         return error;
     }

-    if (size > 0) {
-        DPRINTF("buffering %d bytes\n", size - offset);
-        buffered_append(s, buf, size);
+    if (size <= 0) {
+        return size;
     }

+    if (size > (s->buffer_capacity - s->buffer_size)) {
+        DPRINTF("increasing buffer capacity from %zu by %zu\n",
+                s->buffer_capacity, size + 1024);
+
+        s->buffer_capacity += size + 1024;
+
+        s->buffer = g_realloc(s->buffer, s->buffer_capacity);
+    }
+
+    memcpy(s->buffer + s->buffer_size, buf, size);
+    s->buffer_size += size;
+
     return size;
 }