[Spice-devel,3/3] simplify agent data pipe item

Submitted by Christophe Fergeau on Aug. 22, 2011, 1:39 p.m.

Details

Message ID 1313995160-11625-4-git-send-email-cfergeau@redhat.com
State New, archived
Headers show

Not browsing as part of any series.

Commit Message

Christophe Fergeau Aug. 22, 2011, 1:39 p.m.
Now that we have a SpiceRefcountedChunk, we can make use of it to
make the code handling the agent data pipe item much simpler and
readable.
---
 server/main_channel.c |   51 ++++++++++++------------------------------------
 1 files changed, 13 insertions(+), 38 deletions(-)

Patch hide | download patch | download mbox

diff --git a/server/main_channel.c b/server/main_channel.c
index db3c617..5347177 100644
--- a/server/main_channel.c
+++ b/server/main_channel.c
@@ -85,17 +85,9 @@  typedef struct TokensPipeItem {
     int tokens;
 } TokensPipeItem;
 
-typedef struct AgentDataPipeItemRefs {
-    int refs;
-} AgentDataPipeItemRefs;
-
 typedef struct AgentDataPipeItem {
     PipeItem base;
-    AgentDataPipeItemRefs *refs;
-    uint8_t* data;
-    size_t len;
-    spice_marshaller_item_free_func free_data;
-    void *opaque;
+    SpiceRefcountedChunk *chunk;
 } AgentDataPipeItem;
 
 typedef struct InitPipeItem {
@@ -229,26 +221,14 @@  static PipeItem *main_tokens_item_new(RedChannelClient *rcc, void *data, int num
     return &item->base;
 }
 
-typedef struct MainAgentDataItemInfo {
-    uint8_t* data;
-    size_t len;
-    spice_marshaller_item_free_func free_data;
-    void *opaque;
-    AgentDataPipeItemRefs *refs;
-} MainAgentDataItemInfo;
-
 static PipeItem *main_agent_data_item_new(RedChannelClient *rcc, void *data, int num)
 {
-    MainAgentDataItemInfo *info = data;
+    SpiceRefcountedChunk *chunk = (SpiceRefcountedChunk *)data;
     AgentDataPipeItem *item = spice_malloc(sizeof(AgentDataPipeItem));
 
     red_channel_pipe_item_init(rcc->channel, &item->base,
                                SPICE_MSG_MAIN_AGENT_DATA);
-    item->refs = info->refs;
-    item->data = info->data;
-    item->len = info->len;
-    item->free_data = info->free_data;
-    item->opaque = info->opaque;
+    item->chunk = spice_refcounted_chunk_ref (chunk);
     return &item->base;
 }
 
@@ -430,23 +410,22 @@  static void main_channel_marshall_tokens(SpiceMarshaller *m, uint32_t num_tokens
 void main_channel_push_agent_data(MainChannel *main_chan, uint8_t* data, size_t len,
            spice_marshaller_item_free_func free_data, void *opaque)
 {
-    MainAgentDataItemInfo info = {
-        .data = data,
-        .len = len,
-        .free_data = free_data,
-        .opaque = opaque,
-        .refs = spice_malloc(sizeof(AgentDataPipeItemRefs)),
-    };
+    SpiceRefcountedChunk *chunk;
 
-    info.refs->refs = main_chan->base.clients_num;
+    chunk = spice_refcounted_chunk_new (data, len, free_data, opaque);
     red_channel_pipes_new_add_push(&main_chan->base,
-        main_agent_data_item_new, &info);
+        main_agent_data_item_new, chunk);
+    spice_refcounted_chunk_unref (chunk);
 }
 
 static void main_channel_marshall_agent_data(SpiceMarshaller *m,
                                   AgentDataPipeItem *item)
 {
-    spice_marshaller_add_ref(m, item->data, item->len);
+    uint8_t *data;
+    size_t len;
+    data = spice_chunk_get_data ((SpiceChunk *)item->chunk);
+    len = spice_chunk_get_len ((SpiceChunk *)item->chunk);
+    spice_marshaller_add_ref(m, data, len);
 }
 
 static void main_channel_push_migrate_data_item(MainChannel *main_chan)
@@ -723,11 +702,7 @@  static void main_channel_release_pipe_item(RedChannelClient *rcc,
     switch (base->type) {
         case SPICE_MSG_MAIN_AGENT_DATA: {
             AgentDataPipeItem *data = (AgentDataPipeItem*)base;
-            if (!--data->refs->refs) {
-                red_printf("SPICE_MSG_MAIN_AGENT_DATA %p %p, %d", data, data->refs, data->refs->refs);
-                free(data->refs);
-                data->free_data(data->data, data->opaque);
-            }
+            spice_refcounted_chunk_unref (data->chunk);
             break;
         }
         case SPICE_MSG_MAIN_MIGRATE_SWITCH_HOST: {