[Spice-commits] 3 commits - server/char-device.c server/char-device.h server/reds.c server/reds-private.h server/smartcard.c server/spicevmc.c

Frediano Ziglio fziglio at kemper.freedesktop.org
Wed Mar 9 17:36:38 UTC 2016


 server/char-device.c  |    9 ++++-
 server/char-device.h  |    2 +
 server/reds-private.h |   15 +--------
 server/reds.c         |   82 ++++++++++++--------------------------------------
 server/smartcard.c    |    2 -
 server/spicevmc.c     |   16 +++------
 6 files changed, 39 insertions(+), 87 deletions(-)

New commits:
commit 91bab2ff1c5a0d06af22a6f1c5300903f662c5e5
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Mon Feb 23 18:54:58 2015 +0100

    Replace RedsPrivate::mig_wait_disconnect_clients with a GList
    
    The code was introducing an intermediate RedsMigWaitDisconnectClient type to
    hold linked list elements, resulting in a memory handling behaviour very
    similar to a GList. Using GList directly makes the code shorter and more
    readable.
    
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/server/reds-private.h b/server/reds-private.h
index b2afc6b..205973a 100644
--- a/server/reds-private.h
+++ b/server/reds-private.h
@@ -120,11 +120,6 @@ typedef struct RedsMigTargetClient {
     Ring pending_links;
 } RedsMigTargetClient;
 
-typedef struct RedsMigWaitDisconnectClient {
-    RingItem link;
-    RedClient *client;
-} RedsMigWaitDisconnectClient;
-
 /* Intermediate state for on going monitors config message from a single
  * client, being passed to the guest */
 typedef struct RedsClientMonitorsConfig {
@@ -160,9 +155,10 @@ struct RedsState {
     int mig_wait_connect; /* src waits for clients to establish connection to dest
                              (before migration starts) */
     int mig_wait_disconnect; /* src waits for clients to disconnect (after migration completes) */
-    Ring mig_wait_disconnect_clients; /* List of RedsMigWaitDisconnectClient. Holds the clients
+    GList *mig_wait_disconnect_clients;/* List of RedsMigWaitDisconnectClient. Holds the clients
                                          which the src waits for their disconnection */
 
+
     int mig_inprogress;
     int expect_migrate;
     int src_do_seamless_migrate; /* per migration. Updated after the migration handshake
diff --git a/server/reds.c b/server/reds.c
index cffa873..72282a6 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -2834,11 +2834,8 @@ static void reds_mig_fill_wait_disconnect(RedsState *reds)
      * of clients that got connected to the src after migration completion.*/
     RING_FOREACH(client_item, &reds->clients) {
         RedClient *client = SPICE_CONTAINEROF(client_item, RedClient, link);
-        RedsMigWaitDisconnectClient *wait_client;
 
-        wait_client = spice_new0(RedsMigWaitDisconnectClient, 1);
-        wait_client->client = client;
-        ring_add(&reds->mig_wait_disconnect_clients, &wait_client->link);
+        reds->mig_wait_disconnect_clients = g_list_append(reds->mig_wait_disconnect_clients, client);
     }
     reds->mig_wait_disconnect = TRUE;
     reds->core->timer_start(reds->mig_timer, MIGRATE_TIMEOUT);
@@ -2846,36 +2843,18 @@ static void reds_mig_fill_wait_disconnect(RedsState *reds)
 
 static void reds_mig_cleanup_wait_disconnect(RedsState *reds)
 {
-    RingItem *wait_client_item;
-
-    while ((wait_client_item = ring_get_tail(&reds->mig_wait_disconnect_clients))) {
-        RedsMigWaitDisconnectClient *wait_client;
-
-        wait_client = SPICE_CONTAINEROF(wait_client_item, RedsMigWaitDisconnectClient, link);
-        ring_remove(wait_client_item);
-        free(wait_client);
-    }
+    g_list_free(reds->mig_wait_disconnect_clients);
     reds->mig_wait_disconnect = FALSE;
 }
 
 static void reds_mig_remove_wait_disconnect_client(RedsState *reds, RedClient *client)
 {
-    RingItem *wait_client_item;
-
-    RING_FOREACH(wait_client_item, &reds->mig_wait_disconnect_clients) {
-        RedsMigWaitDisconnectClient *wait_client;
+    g_warn_if_fail(g_list_find(reds->mig_wait_disconnect_clients, client) != NULL);
 
-        wait_client = SPICE_CONTAINEROF(wait_client_item, RedsMigWaitDisconnectClient, link);
-        if (wait_client->client == client) {
-            ring_remove(wait_client_item);
-            free(wait_client);
-            if (ring_is_empty(&reds->mig_wait_disconnect_clients)) {
-                reds_mig_cleanup(reds);
-            }
-            return;
-        }
+    reds->mig_wait_disconnect_clients = g_list_remove(reds->mig_wait_disconnect_clients, client);
+    if (reds->mig_wait_disconnect_clients == NULL) {
+       reds_mig_cleanup(reds);
     }
-    spice_warning("client not found %p", client);
 }
 
 static void reds_migrate_channels_seamless(RedsState *reds)
@@ -3337,7 +3316,7 @@ static int do_spice_init(RedsState *reds, SpiceCoreInterface *core_interface)
     ring_init(&reds->channels);
     ring_init(&reds->mig_target_clients);
     reds->char_devices = NULL;
-    ring_init(&reds->mig_wait_disconnect_clients);
+    reds->mig_wait_disconnect_clients = NULL;
     reds->vm_running = TRUE; /* for backward compatibility */
 
     if (!(reds->mig_timer = reds->core->timer_add(reds->core, migrate_timeout, reds))) {
commit 30f718aae20170d851ddf9f8626b2ed37eb5d862
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Mon Feb 23 18:37:52 2015 +0100

    Replace RedsPrivate::char_devs_state with a GList
    
    The code was introducing an intermediate SpiceCharDevStateItem type to
    hold linked list elements, resulting in a memory handling behaviour very
    similar to a GList. Using GList directly makes the code shorter and more
    readable.
    
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/server/reds-private.h b/server/reds-private.h
index 6bd6878..b2afc6b 100644
--- a/server/reds-private.h
+++ b/server/reds-private.h
@@ -125,11 +125,6 @@ typedef struct RedsMigWaitDisconnectClient {
     RedClient *client;
 } RedsMigWaitDisconnectClient;
 
-typedef struct SpiceCharDeviceStateItem {
-    RingItem link;
-    SpiceCharDeviceState *st;
-} SpiceCharDeviceStateItem;
-
 /* Intermediate state for on going monitors config message from a single
  * client, being passed to the guest */
 typedef struct RedsClientMonitorsConfig {
@@ -187,7 +182,7 @@ struct RedsState {
     SpiceTimer *mig_timer;
 
     int vm_running;
-    Ring char_devs_states; /* list of SpiceCharDeviceStateItem */
+    GList *char_devices; /* list of SpiceCharDeviceState */
     int seamless_migration_enabled; /* command line arg */
     int keepalive_timeout;
 
diff --git a/server/reds.c b/server/reds.c
index 3b72cc7..cffa873 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -3065,28 +3065,13 @@ SPICE_GNUC_VISIBLE const char** spice_server_char_device_recognized_subtypes(voi
 
 static void reds_char_device_add_state(RedsState *reds, SpiceCharDeviceState *st)
 {
-    SpiceCharDeviceStateItem *item = spice_new0(SpiceCharDeviceStateItem, 1);
-
-    item->st = st;
-
-    ring_add(&reds->char_devs_states, &item->link);
+    reds->char_devices = g_list_append(reds->char_devices, st);
 }
 
 static void reds_char_device_remove_state(RedsState *reds, SpiceCharDeviceState *st)
 {
-    RingItem *item;
-
-    RING_FOREACH(item, &reds->char_devs_states) {
-        SpiceCharDeviceStateItem *st_item;
-
-        st_item = SPICE_CONTAINEROF(item, SpiceCharDeviceStateItem, link);
-        if (st_item->st == st) {
-            ring_remove(item);
-            free(st_item);
-            return;
-        }
-    }
-    spice_error("char dev state not found %p", st);
+    g_warn_if_fail(g_list_find(reds->char_devices, st) != NULL);
+    reds->char_devices = g_list_remove(reds->char_devices, st);
 }
 
 void reds_on_char_device_state_destroy(RedsState *reds, SpiceCharDeviceState *dev)
@@ -3351,7 +3336,7 @@ static int do_spice_init(RedsState *reds, SpiceCoreInterface *core_interface)
     reds->main_dispatcher = main_dispatcher_new(reds, reds->core);
     ring_init(&reds->channels);
     ring_init(&reds->mig_target_clients);
-    ring_init(&reds->char_devs_states);
+    reds->char_devices = NULL;
     ring_init(&reds->mig_wait_disconnect_clients);
     reds->vm_running = TRUE; /* for backward compatibility */
 
@@ -4001,30 +3986,24 @@ SPICE_GNUC_VISIBLE int spice_server_migrate_switch(SpiceServer *s)
 
 SPICE_GNUC_VISIBLE void spice_server_vm_start(SpiceServer *s)
 {
-    RingItem *item;
+    GList *it;
 
     spice_assert(s == reds);
     reds->vm_running = TRUE;
-    RING_FOREACH(item, &reds->char_devs_states) {
-        SpiceCharDeviceStateItem *st_item;
-
-        st_item = SPICE_CONTAINEROF(item, SpiceCharDeviceStateItem, link);
-        spice_char_device_start(st_item->st);
+    for (it = reds->char_devices; it != NULL; it = it->next) {
+        spice_char_device_start((SpiceCharDeviceState *)it->data);
     }
     reds_on_vm_start(reds);
 }
 
 SPICE_GNUC_VISIBLE void spice_server_vm_stop(SpiceServer *s)
 {
-    RingItem *item;
+    GList *it;
 
     spice_assert(s == reds);
     reds->vm_running = FALSE;
-    RING_FOREACH(item, &reds->char_devs_states) {
-        SpiceCharDeviceStateItem *st_item;
-
-        st_item = SPICE_CONTAINEROF(item, SpiceCharDeviceStateItem, link);
-        spice_char_device_stop(st_item->st);
+    for (it = reds->char_devices; it != NULL; it = it->next) {
+        spice_char_device_stop((SpiceCharDeviceState *)it->data);
     }
     reds_on_vm_stop(reds);
 }
commit e5c89b0b6035e0af79beb13c23a27d32cbf13df1
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Fri Feb 20 18:19:24 2015 +0100

    Introduce spice_char_device_get_interface()
    
    Hides awkward casting/dereferencing to go from a
    SpiceCharDeviceInstance to a SpiceCharDeviceInterface
    
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/server/char-device.c b/server/char-device.c
index 9858ab3..cda2d60 100644
--- a/server/char-device.c
+++ b/server/char-device.c
@@ -460,7 +460,7 @@ static int spice_char_device_write_to_device(SpiceCharDeviceState *dev)
         reds_core_timer_cancel(dev->reds, dev->write_to_dev_timer);
     }
 
-    sif = SPICE_CONTAINEROF(dev->sin->base.sif, SpiceCharDeviceInterface, base);
+    sif = spice_char_device_get_interface(dev->sin);
     while (dev->running) {
         uint32_t write_len;
 
@@ -695,7 +695,7 @@ SpiceCharDeviceState *spice_char_device_state_create(SpiceCharDeviceInstance *si
     ring_init(&char_dev->write_bufs_pool);
     ring_init(&char_dev->clients);
 
-    sif = SPICE_CONTAINEROF(char_dev->sin->base.sif, SpiceCharDeviceInterface, base);
+    sif = spice_char_device_get_interface(char_dev->sin);
     if (sif->base.minor_version <= 2 ||
         !(sif->flags & SPICE_CHAR_DEVICE_NOTIFY_WRITABLE)) {
         char_dev->write_to_dev_timer = reds_core_timer_add(reds, spice_char_dev_write_retry, char_dev);
@@ -1044,3 +1044,8 @@ SpiceServer* spice_char_device_get_server(SpiceCharDeviceState *dev)
 {
     return dev->reds;
 }
+
+SpiceCharDeviceInterface *spice_char_device_get_interface(SpiceCharDeviceInstance *instance)
+{
+   return SPICE_CONTAINEROF(instance->base.sif, SpiceCharDeviceInterface, base);
+}
diff --git a/server/char-device.h b/server/char-device.h
index db72bfe..7c78524 100644
--- a/server/char-device.h
+++ b/server/char-device.h
@@ -219,4 +219,6 @@ SpiceCharDeviceState *spicevmc_device_connect(struct RedsState *reds,
 void spicevmc_device_disconnect(struct RedsState *reds,
                                 SpiceCharDeviceInstance *char_device);
 
+SpiceCharDeviceInterface *spice_char_device_get_interface(SpiceCharDeviceInstance *instance);
+
 #endif // CHAR_DEVICE_H_
diff --git a/server/reds.c b/server/reds.c
index 213d753..3b72cc7 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -437,7 +437,7 @@ static void reds_reset_vdp(RedsState *reds)
         spice_char_device_reset(state->base);
     }
 
-    sif = SPICE_CONTAINEROF(reds->vdagent->base.sif, SpiceCharDeviceInterface, base);
+    sif = spice_char_device_get_interface(reds->vdagent);
     if (sif->state) {
         sif->state(reds->vdagent, 0);
     }
@@ -712,7 +712,7 @@ static SpiceCharDeviceMsgToClient *vdi_port_read_one_msg_from_device(SpiceCharDe
         return NULL;
     }
     spice_assert(reds->vdagent == sin);
-    sif = SPICE_CONTAINEROF(reds->vdagent->base.sif, SpiceCharDeviceInterface, base);
+    sif = spice_char_device_get_interface(reds->vdagent);
     while (reds->vdagent) {
         switch (state->read_state) {
         case VDI_PORT_READ_STATE_READ_HEADER:
@@ -2978,7 +2978,7 @@ static SpiceCharDeviceState *attach_to_red_agent(RedsState *reds, SpiceCharDevic
     reds->vdagent = sin;
     reds_update_mouse_mode(reds);
 
-    sif = SPICE_CONTAINEROF(reds->vdagent->base.sif, SpiceCharDeviceInterface, base);
+    sif = spice_char_device_get_interface(reds->vdagent);
     if (sif->state) {
         sif->state(reds->vdagent, 1);
     }
diff --git a/server/smartcard.c b/server/smartcard.c
index c7b1f30..e1e8e2b 100644
--- a/server/smartcard.c
+++ b/server/smartcard.c
@@ -133,7 +133,7 @@ SpiceCharDeviceMsgToClient *smartcard_read_msg_from_device(SpiceCharDeviceInstan
                                                            void *opaque)
 {
     SmartCardDeviceState *state = opaque;
-    SpiceCharDeviceInterface *sif = SPICE_CONTAINEROF(sin->base.sif, SpiceCharDeviceInterface, base);
+    SpiceCharDeviceInterface *sif = spice_char_device_get_interface(sin);
     VSCMsgHeader *vheader = (VSCMsgHeader*)state->buf;
     int n;
     int remaining;
diff --git a/server/spicevmc.c b/server/spicevmc.c
index 35d1393..929ec0e 100644
--- a/server/spicevmc.c
+++ b/server/spicevmc.c
@@ -112,7 +112,7 @@ static SpiceCharDeviceMsgToClient *spicevmc_chardev_read_msg_from_dev(SpiceCharD
     SpiceVmcPipeItem *msg_item;
     int n;
 
-    sif = SPICE_CONTAINEROF(sin->base.sif, SpiceCharDeviceInterface, base);
+    sif = spice_char_device_get_interface(sin);
 
     if (!state->rcc) {
         return NULL;
@@ -212,7 +212,6 @@ static int spicevmc_red_channel_client_config_socket(RedChannelClient *rcc)
 static void spicevmc_red_channel_client_on_disconnect(RedChannelClient *rcc)
 {
     SpiceVmcState *state;
-    SpiceCharDeviceInstance *sin;
     SpiceCharDeviceInterface *sif;
 
     if (!rcc) {
@@ -220,8 +219,6 @@ static void spicevmc_red_channel_client_on_disconnect(RedChannelClient *rcc)
     }
 
     state = SPICE_CONTAINEROF(rcc->channel, SpiceVmcState, channel);
-    sin = state->chardev_sin;
-    sif = SPICE_CONTAINEROF(sin->base.sif, SpiceCharDeviceInterface, base);
 
     if (state->recv_from_client_buf) { /* partial message which wasn't pushed to device */
         spice_char_device_write_buffer_release(state->chardev_st, state->recv_from_client_buf);
@@ -243,8 +240,9 @@ static void spicevmc_red_channel_client_on_disconnect(RedChannelClient *rcc)
         red_channel_client_destroy(rcc);
 
     state->rcc = NULL;
+    sif = spice_char_device_get_interface(state->chardev_sin);
     if (sif->state) {
-        sif->state(sin, 0);
+        sif->state(state->chardev_sin, 0);
     }
 }
 
@@ -287,12 +285,10 @@ static int spicevmc_red_channel_client_handle_message(RedChannelClient *rcc,
                                                       uint8_t *msg)
 {
     SpiceVmcState *state;
-    SpiceCharDeviceInstance *sin;
     SpiceCharDeviceInterface *sif;
 
     state = spicevmc_red_channel_client_get_state(rcc);
-    sin = state->chardev_sin;
-    sif = SPICE_CONTAINEROF(sin->base.sif, SpiceCharDeviceInterface, base);
+    sif = spice_char_device_get_interface(state->chardev_sin);
 
     switch (type) {
     case SPICE_MSGC_SPICEVMC_DATA:
@@ -307,7 +303,7 @@ static int spicevmc_red_channel_client_handle_message(RedChannelClient *rcc,
             return FALSE;
         }
         if (sif->base.minor_version >= 2 && sif->event != NULL)
-            sif->event(sin, *msg);
+            sif->event(state->chardev_sin, *msg);
         break;
     default:
         return red_channel_client_handle_message(rcc, size, type, msg);
@@ -467,7 +463,6 @@ static void spicevmc_connect(RedChannel *channel, RedClient *client,
 
     state = SPICE_CONTAINEROF(channel, SpiceVmcState, channel);
     sin = state->chardev_sin;
-    sif = SPICE_CONTAINEROF(sin->base.sif, SpiceCharDeviceInterface, base);
 
     if (state->rcc) {
         spice_printerr("channel client %d:%d (%p) already connected, refusing second connection",
@@ -499,6 +494,7 @@ static void spicevmc_connect(RedChannel *channel, RedClient *client,
         return;
     }
 
+    sif = spice_char_device_get_interface(state->chardev_sin);
     if (sif->state) {
         sif->state(sin, 1);
     }


More information about the Spice-commits mailing list