Patchwork [v3,07/12] vnc: add buffer_free()

login
register
mail settings
Submitter Corentin Chary
Date May 19, 2010, 7:24 a.m.
Message ID <1274253852-16068-8-git-send-email-corentincj@iksaif.net>
Download mbox | patch
Permalink /patch/52960/
State New
Headers show

Comments

Corentin Chary - May 19, 2010, 7:24 a.m.
Add a buffer_free() helper to free vnc buffers and
remove some duplicated code in vnc_disconnect_finish().

Signed-off-by: Corentin Chary <corentincj@iksaif.net>
---
 vnc.c |   18 ++++++++++--------
 vnc.h |    1 +
 2 files changed, 11 insertions(+), 8 deletions(-)

Patch

diff --git a/vnc.c b/vnc.c
index a5a0456..8f256b3 100644
--- a/vnc.c
+++ b/vnc.c
@@ -506,6 +506,14 @@  void buffer_reset(Buffer *buffer)
         buffer->offset = 0;
 }
 
+void buffer_free(Buffer *buffer)
+{
+    qemu_free(buffer->buffer);
+    buffer->offset = 0;
+    buffer->capacity = 0;
+    buffer->buffer = NULL;
+}
+
 void buffer_append(Buffer *buffer, const void *data, size_t len)
 {
     memcpy(buffer->buffer + buffer->offset, data, len);
@@ -911,14 +919,8 @@  static void vnc_disconnect_finish(VncState *vs)
 {
     vnc_qmp_event(vs, QEVENT_VNC_DISCONNECTED);
 
-    if (vs->input.buffer) {
-        qemu_free(vs->input.buffer);
-        vs->input.buffer = NULL;
-    }
-    if (vs->output.buffer) {
-        qemu_free(vs->output.buffer);
-        vs->output.buffer = NULL;
-    }
+    buffer_free(&vs->input);
+    buffer_free(&vs->output);
 
     qobject_decref(vs->info);
 
diff --git a/vnc.h b/vnc.h
index b2d8738..abd9f27 100644
--- a/vnc.h
+++ b/vnc.h
@@ -376,6 +376,7 @@  void buffer_reserve(Buffer *buffer, size_t len);
 int buffer_empty(Buffer *buffer);
 uint8_t *buffer_end(Buffer *buffer);
 void buffer_reset(Buffer *buffer);
+void buffer_free(Buffer *buffer);
 void buffer_append(Buffer *buffer, const void *data, size_t len);