[Spice-devel] [PATCH 5/7] RedChannel: Add FOREACH_CLIENT and use it to iterate

Frediano Ziglio fziglio at redhat.com
Sun May 22 08:02:38 UTC 2016


> 
> Remove the custom FOREACH_DCC macro and use the more generic
> FOREACH_CLIENT macro and use it for all channels.
> ---
>  server/display-channel.c | 20 ++++++++++----------
>  server/display-channel.h |  9 ---------
>  server/main-channel.c    | 32 +++++++++++++++-----------------
>  server/red-channel.c     | 40 +++++++++++++++-------------------------
>  server/red-channel.h     | 10 ++++++++++
>  server/red-worker.c      |  4 ++--
>  server/stream.c          | 12 ++++++------
>  7 files changed, 58 insertions(+), 69 deletions(-)
> 
> diff --git a/server/display-channel.c b/server/display-channel.c
> index c94bc26..615cac5 100644
> --- a/server/display-channel.c
> +++ b/server/display-channel.c
> @@ -243,7 +243,7 @@ void display_channel_surface_unref(DisplayChannel
> *display, uint32_t surface_id)
>  
>      region_destroy(&surface->draw_dirty_region);
>      surface->context.canvas = NULL;
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          dcc_destroy_surface(dcc, surface_id);
>      }
>  
> @@ -278,7 +278,7 @@ static void streams_update_visible_region(DisplayChannel
> *display, Drawable *dra
>              continue;
>          }
>  
> -        FOREACH_DCC(display, link, next, dcc) {
> +        FOREACH_CLIENT(display, link, next, dcc) {
>              agent = &dcc->stream_agents[get_stream_id(display, stream)];
>  
>              if (region_intersects(&agent->vis_region,
>              &drawable->tree_item.base.rgn)) {
> @@ -296,7 +296,7 @@ static void pipes_add_drawable(DisplayChannel *display,
> Drawable *drawable)
>      GList *link, *next;
>  
>      spice_warn_if_fail(ring_is_empty(&drawable->pipes));
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          dcc_prepend_drawable(dcc, drawable);
>      }
>  }
> @@ -320,7 +320,7 @@ static void pipes_add_drawable_after(DisplayChannel
> *display,
>      if (num_other_linked != display->common.base.clients_num) {
>          GList *link, *next;
>          spice_debug("TODO: not O(n^2)");
> -        FOREACH_DCC(display, link, next, dcc) {
> +        FOREACH_CLIENT(display, link, next, dcc) {
>              int sent = 0;
>              DRAWABLE_FOREACH_DPI_SAFE(pos_after, dpi_link, dpi_next,
>              dpi_pos_after) {
>                  if (dpi_pos_after->dcc == dcc) {
> @@ -1209,7 +1209,7 @@ void
> display_channel_free_glz_drawables_to_free(DisplayChannel *display)
>  
>      spice_return_if_fail(display);
>  
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          dcc_free_glz_drawables_to_free(dcc);
>      }
>  }
> @@ -1221,7 +1221,7 @@ void display_channel_free_glz_drawables(DisplayChannel
> *display)
>  
>      spice_return_if_fail(display);
>  
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          dcc_free_glz_drawables(dcc);
>      }
>  }
> @@ -1268,7 +1268,7 @@ void display_channel_free_some(DisplayChannel *display)
>  
>      spice_debug("#draw=%d, #glz_draw=%d", display->drawable_count,
>                  display->glz_drawable_count);
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          GlzSharedDictionary *glz_dict = dcc ? dcc->glz_dict : NULL;
>  
>          if (glz_dict) {
> @@ -1283,7 +1283,7 @@ void display_channel_free_some(DisplayChannel *display)
>          free_one_drawable(display, TRUE);
>      }
>  
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          GlzSharedDictionary *glz_dict = dcc ? dcc->glz_dict : NULL;
>  
>          if (glz_dict) {
> @@ -1758,7 +1758,7 @@ static void
> clear_surface_drawables_from_pipes(DisplayChannel *display, int surf
>      GList *link, *next;
>      DisplayChannelClient *dcc;
>  
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          if (!dcc_clear_surface_drawables_from_pipe(dcc, surface_id,
>          wait_if_used)) {
>              red_channel_client_disconnect(RED_CHANNEL_CLIENT(dcc));
>          }
> @@ -1824,7 +1824,7 @@ static void send_create_surface(DisplayChannel
> *display, int surface_id, int ima
>      DisplayChannelClient *dcc;
>      GList *link, *next;
>  
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          dcc_create_surface(dcc, surface_id);
>          if (image_ready)
>              dcc_push_surface_image(dcc, surface_id);
> diff --git a/server/display-channel.h b/server/display-channel.h
> index cec8c8e..7c1bfde 100644
> --- a/server/display-channel.h
> +++ b/server/display-channel.h
> @@ -226,15 +226,6 @@ struct DisplayChannel {
>      stat_info_t lz4_stat;
>  };
>  
> -#define FOREACH_DCC(channel, _link, _next, _data)                   \
> -    for (_link = (channel ? RED_CHANNEL(channel)->clients : NULL), \
> -         _next = (_link ? _link->next : NULL), \
> -         _data = (_link ? _link->data : NULL); \
> -         _link; \
> -         _link = _next, \
> -         _next = (_link ? _link->next : NULL), \
> -         _data = (_link ? _link->data : NULL))
> -
>  static inline int get_stream_id(DisplayChannel *display, Stream *stream)
>  {
>      return (int)(stream - display->streams_buf);
> diff --git a/server/main-channel.c b/server/main-channel.c
> index 5b5588c..00b8b32 100644
> --- a/server/main-channel.c
> +++ b/server/main-channel.c
> @@ -42,13 +42,11 @@ static void
> main_channel_client_on_disconnect(RedChannelClient *rcc)
>  
>  RedClient *main_channel_get_client_by_link_id(MainChannel *main_chan,
>  uint32_t connection_id)
>  {
> -    GList *link;
> -    MainChannelClient *mcc;
> +    GList *link, *next;
>      RedChannelClient *rcc;
>  
> -    for (link = main_chan->base.clients; link != NULL; link = link->next) {
> -        rcc = link->data;
> -        mcc = (MainChannelClient*) rcc;
> +    FOREACH_CLIENT(main_chan, link, next, rcc) {
> +        MainChannelClient *mcc = (MainChannelClient*) rcc;
>          if (main_channel_client_get_connection_id(mcc) == connection_id) {
>              return rcc->client;
>          }
> @@ -333,10 +331,10 @@ MainChannel* main_channel_new(RedsState *reds)
>  
>  static int main_channel_connect_semi_seamless(MainChannel *main_channel)
>  {
> -    GList *link;
> +    GList *link, *next;
> +    RedChannelClient *rcc;
>  
> -    for (link = main_channel->base.clients; link != NULL; link = link->next)
> {
> -        RedChannelClient *rcc = link->data;
> +    FOREACH_CLIENT(main_channel, link, next, rcc) {
>          MainChannelClient *mcc = (MainChannelClient*)rcc;
>          if (main_channel_client_connect_semi_seamless(mcc))
>              main_channel->num_clients_mig_wait++;
> @@ -346,12 +344,12 @@ static int
> main_channel_connect_semi_seamless(MainChannel *main_channel)
>  
>  static int main_channel_connect_seamless(MainChannel *main_channel)
>  {
> -    GList *link;
> +    GList *link, *next;
> +    RedChannelClient *rcc;
>  
>      spice_assert(g_list_length(main_channel->base.clients) == 1);
>  
> -    for (link = main_channel->base.clients; link != NULL; link = link->next)
> {
> -        RedChannelClient *rcc = link->data;
> +    FOREACH_CLIENT(main_channel, link, next, rcc) {
>          MainChannelClient *mcc = (MainChannelClient*)rcc;
>          main_channel_client_connect_seamless(mcc);
>          main_channel->num_clients_mig_wait++;
> @@ -388,10 +386,10 @@ int main_channel_migrate_connect(MainChannel
> *main_channel, RedsMigSpice *mig_ta
>  
>  void main_channel_migrate_cancel_wait(MainChannel *main_chan)
>  {
> -    GList *link;
> +    GList *link, *next;
> +    RedChannelClient *rcc;
>  
> -    for (link = main_chan->base.clients; link != NULL; link = link->next) {
> -        RedChannelClient *rcc = link->data;
> +    FOREACH_CLIENT(main_chan, link, next, rcc) {
>          MainChannelClient *mcc = (MainChannelClient*)rcc;
>          main_channel_client_migrate_cancel_wait(mcc);
>      }
> @@ -400,8 +398,9 @@ void main_channel_migrate_cancel_wait(MainChannel
> *main_chan)
>  
>  int main_channel_migrate_src_complete(MainChannel *main_chan, int success)
>  {
> -    GList *link;
> +    GList *link, *next;
>      int semi_seamless_count = 0;
> +    RedChannelClient *rcc;
>  
>      spice_printerr("");
>  
> @@ -410,8 +409,7 @@ int main_channel_migrate_src_complete(MainChannel
> *main_chan, int success)
>          return 0;
>      }
>  
> -    for (link = main_chan->base.clients; link != NULL; link = link->next) {
> -        RedChannelClient *rcc = link->data;
> +    FOREACH_CLIENT(main_chan, link, next, rcc) {
>          MainChannelClient *mcc = (MainChannelClient*)rcc;
>          if (main_channel_client_migrate_src_complete(mcc, success))
>              semi_seamless_count++;
> diff --git a/server/red-channel.c b/server/red-channel.c
> index 07b0a7b..e4dc4ee 100644
> --- a/server/red-channel.c
> +++ b/server/red-channel.c
> @@ -668,11 +668,10 @@ int red_channel_client_test_remote_cap(RedChannelClient
> *rcc, uint32_t cap)
>  
>  int red_channel_test_remote_common_cap(RedChannel *channel, uint32_t cap)
>  {
> -    GList *link;
> -
> -    for (link = channel->clients; link != NULL; link = link->next) {
> -        RedChannelClient *rcc = link->data;
> +    GList *link, *next;
> +    RedChannelClient *rcc;
>  
> +    FOREACH_CLIENT(channel, link, next, rcc) {
>          if (!red_channel_client_test_remote_common_cap(rcc, cap)) {
>              return FALSE;
>          }
> @@ -682,11 +681,10 @@ int red_channel_test_remote_common_cap(RedChannel
> *channel, uint32_t cap)
>  
>  int red_channel_test_remote_cap(RedChannel *channel, uint32_t cap)
>  {
> -    GList *link;
> -
> -    for (link = channel->clients; link != NULL; link = link->next) {
> -        RedChannelClient *rcc = link->data;
> +    GList *link, *next;
> +    RedChannelClient *rcc;
>  
> +    FOREACH_CLIENT(channel, link, next, rcc) {
>          if (!red_channel_client_test_remote_cap(rcc, cap)) {
>              return FALSE;
>          }
> @@ -1913,11 +1911,10 @@ int red_channel_all_blocked(RedChannel *channel)
>  
>  int red_channel_any_blocked(RedChannel *channel)
>  {
> -    GList *link;
> +    GList *link, *next;
>      RedChannelClient *rcc;
>  
> -    for (link = channel->clients; link != NULL; link = link->next) {
> -        rcc = link->data;
> +    FOREACH_CLIENT(channel, link, next, rcc) {
>          if (rcc->send_data.blocked) {
>              return TRUE;
>          }
> @@ -1972,11 +1969,10 @@ int red_channel_get_first_socket(RedChannel *channel)
>  
>  int red_channel_no_item_being_sent(RedChannel *channel)
>  {
> -    GList *link;
> +    GList *link, *next;
>      RedChannelClient *rcc;
>  
> -    for (link = channel->clients; link != NULL; link = link->next) {
> -        rcc = link->data;
> +    FOREACH_CLIENT(channel, link, next, rcc) {
>          if (!red_channel_client_no_item_being_sent(rcc)) {
>              return FALSE;
>          }
> @@ -2221,16 +2217,12 @@ static int red_channel_pipes_create_batch(RedChannel
> *channel,
>      spice_assert(creator != NULL);
>      spice_assert(pipe_add != NULL);
>  
> -    link = channel->clients;
> -    while (link != NULL) {
> -        next = link->next;
> -        rcc = link->data;
> +    FOREACH_CLIENT(channel, link, next, rcc) {
>          item = (*creator)(rcc, data, num++);
>          if (item) {
>              (*pipe_add)(rcc, item);
>              n++;
>          }
> -        link = next;
>      }
>  
>      return n;
> @@ -2273,12 +2265,11 @@ uint32_t red_channel_max_pipe_size(RedChannel
> *channel)
>  
>  uint32_t red_channel_min_pipe_size(RedChannel *channel)
>  {
> -    GList *link;
> +    GList *link, *next;
>      RedChannelClient *rcc;
>      uint32_t pipe_size = ~0;
>  
> -    for (link = channel->clients; link != NULL; link = link->next) {
> -        rcc = link->data;
> +    FOREACH_CLIENT(channel, link, next, rcc) {
>          pipe_size = MIN(pipe_size, rcc->pipe_size);
>      }
>      return pipe_size == ~0 ? 0 : pipe_size;
> @@ -2286,12 +2277,11 @@ uint32_t red_channel_min_pipe_size(RedChannel
> *channel)
>  
>  uint32_t red_channel_sum_pipes_size(RedChannel *channel)
>  {
> -    GList *link;
> +    GList *link, *next;
>      RedChannelClient *rcc;
>      uint32_t sum = 0;
>  
> -    for (link = channel->clients; link != NULL; link = link->next) {
> -        rcc = link->data;
> +    FOREACH_CLIENT(channel, link, next, rcc) {
>          sum += rcc->pipe_size;
>      }
>      return sum;
> diff --git a/server/red-channel.h b/server/red-channel.h
> index c3ed111..736e3d0 100644
> --- a/server/red-channel.h
> +++ b/server/red-channel.h
> @@ -331,6 +331,16 @@ struct RedChannel {
>  #endif
>  };
>  
> +#define FOREACH_CLIENT(channel, _link, _next, _data)                   \
> +    for (_link = (channel ? RED_CHANNEL(channel)->clients : NULL), \
> +         _next = (_link ? _link->next : NULL), \
> +         _data = (_link ? _link->data : NULL); \
> +         _link; \
> +         _link = _next, \
> +         _next = (_link ? _link->next : NULL), \
> +         _data = (_link ? _link->data : NULL))
> +
> +
>  #define RED_CHANNEL(Channel) ((RedChannel *)(Channel))
>  
>  /*
> diff --git a/server/red-worker.c b/server/red-worker.c
> index 86f1645..0c945c1 100644
> --- a/server/red-worker.c
> +++ b/server/red-worker.c
> @@ -533,7 +533,7 @@ static void guest_set_client_capabilities(RedWorker
> *worker)
>          for (i = 0 ; i < sizeof(caps_available) / sizeof(caps_available[0]);
>          ++i) {
>              SET_CAP(caps, caps_available[i]);
>          }
> -        FOREACH_DCC(worker->display_channel, link, next, dcc) {
> +        FOREACH_CLIENT(worker->display_channel, link, next, dcc) {
>              rcc = (RedChannelClient *)dcc;
>              for (i = 0 ; i < sizeof(caps_available) /
>              sizeof(caps_available[0]); ++i) {
>                  if (!red_channel_client_test_remote_cap(rcc,
>                  caps_available[i]))
> @@ -648,7 +648,7 @@ static void red_worker_push_monitors_config(RedWorker
> *worker)
>      DisplayChannelClient *dcc;
>      GList *item, *next;
>  
> -    FOREACH_DCC(worker->display_channel, item, next, dcc) {
> +    FOREACH_CLIENT(worker->display_channel, item, next, dcc) {
>          dcc_push_monitors_config(dcc);
>      }
>  }
> diff --git a/server/stream.c b/server/stream.c
> index 2ef3813..85a0255 100644
> --- a/server/stream.c
> +++ b/server/stream.c
> @@ -102,7 +102,7 @@ void stream_stop(DisplayChannel *display, Stream *stream)
>      spice_return_if_fail(!stream->current);
>  
>      spice_debug("stream %d", get_stream_id(display, stream));
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          StreamAgent *stream_agent;
>  
>          stream_agent = &dcc->stream_agents[get_stream_id(display, stream)];
> @@ -307,7 +307,7 @@ static void attach_stream(DisplayChannel *display,
> Drawable *drawable, Stream *s
>          stream->num_input_frames++;
>      }
>  
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          StreamAgent *agent;
>          QRegion clip_in_draw_dest;
>  
> @@ -385,7 +385,7 @@ static void before_reattach_stream(DisplayChannel
> *display,
>      }
>  
>  
> -    FOREACH_DCC(display, link, link_next, dcc) {
> +    FOREACH_CLIENT(display, link, link_next, dcc) {
>          double drop_factor;
>  
>          agent = &dcc->stream_agents[index];
> @@ -467,7 +467,7 @@ static void display_channel_create_stream(DisplayChannel
> *display, Drawable *dra
>      stream->input_fps_start_time = drawable->creation_time;
>      display->streams_size_total += stream->width * stream->height;
>      display->stream_count++;
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          dcc_create_stream(dcc, stream);
>      }
>      spice_debug("stream %d %dx%d (%d, %d) (%d, %d) %u fps",
> @@ -871,7 +871,7 @@ static void detach_stream_gracefully(DisplayChannel
> *display, Stream *stream,
>      GList *link, *next;
>      DisplayChannelClient *dcc;
>  
> -    FOREACH_DCC(display, link, next, dcc) {
> +    FOREACH_CLIENT(display, link, next, dcc) {
>          dcc_detach_stream_gracefully(dcc, stream, update_area_limit);
>      }
>      if (stream->current) {
> @@ -902,7 +902,7 @@ void stream_detach_behind(DisplayChannel *display,
> QRegion *region, Drawable *dr
>          int detach = 0;
>          item = ring_next(ring, item);
>  
> -        FOREACH_DCC(display, link, next, dcc) {
> +        FOREACH_CLIENT(display, link, next, dcc) {
>              StreamAgent *agent = &dcc->stream_agents[get_stream_id(display,
>              stream)];
>  
>              if (region_intersects(&agent->vis_region, region)) {

Acked-by: Frediano Ziglio <fziglio at redhat.com>

Could be squashed in the previous one.

Frediano


More information about the Spice-devel mailing list