[Spice-devel] [PATCH 05/11] make red_pipe_item_init_full more typesafe
Frediano Ziglio
fziglio at redhat.com
Fri May 20 13:01:43 UTC 2016
Use a proper type for free callback
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
server/cursor-channel.c | 10 ++++++----
server/dcc.c | 8 +++++---
server/main-channel.c | 10 ++++++----
server/red-channel.c | 6 ++++--
server/red-pipe-item.c | 4 ++--
server/red-pipe-item.h | 10 +++++++---
server/reds.c | 8 +++++---
server/smartcard.c | 5 +++--
server/stream.c | 15 +++++++++------
9 files changed, 47 insertions(+), 29 deletions(-)
diff --git a/server/cursor-channel.c b/server/cursor-channel.c
index 949cbc4..c257654 100644
--- a/server/cursor-channel.c
+++ b/server/cursor-channel.c
@@ -81,7 +81,7 @@ struct CursorChannelClient {
#include "cache-item.tmpl.c"
#undef CLIENT_CURSOR_CACHE
-static void cursor_pipe_item_free(RedCursorPipeItem *pipe_item);
+static red_pipe_item_free_t cursor_pipe_item_free;
static CursorItem *cursor_item_new(QXLInstance *qxl, RedCursorCmd *cmd)
{
@@ -138,7 +138,7 @@ static RedPipeItem *new_cursor_pipe_item(RedChannelClient *rcc, void *data, int
RedCursorPipeItem *item = spice_malloc0(sizeof(RedCursorPipeItem));
red_pipe_item_init_full(&item->base, RED_PIPE_ITEM_TYPE_CURSOR,
- (GDestroyNotify)cursor_pipe_item_free);
+ cursor_pipe_item_free);
item->cursor_item = data;
item->cursor_item->refs++;
return &item->base;
@@ -204,9 +204,11 @@ void cursor_channel_disconnect(CursorChannel *cursor_channel)
}
-static void cursor_pipe_item_free(RedCursorPipeItem *pipe_item)
+static void cursor_pipe_item_free(RedPipeItem *base)
{
- spice_return_if_fail(pipe_item);
+ spice_return_if_fail(base);
+
+ RedCursorPipeItem *pipe_item = SPICE_CONTAINEROF(base, RedCursorPipeItem, base);
spice_assert(!red_pipe_item_is_linked(&pipe_item->base));
diff --git a/server/dcc.c b/server/dcc.c
index b9e4eb3..33357cd 100644
--- a/server/dcc.c
+++ b/server/dcc.c
@@ -306,7 +306,7 @@ static RedDrawablePipeItem *red_drawable_pipe_item_new(DisplayChannelClient *dcc
ring_item_init(&dpi->base);
ring_add(&drawable->pipes, &dpi->base);
red_pipe_item_init_full(&dpi->dpi_pipe_item, RED_PIPE_ITEM_TYPE_DRAW,
- (GDestroyNotify)red_drawable_pipe_item_free);
+ red_drawable_pipe_item_free);
drawable->refs++;
return dpi;
}
@@ -517,8 +517,10 @@ void dcc_stream_agent_clip(DisplayChannelClient* dcc, StreamAgent *agent)
red_channel_client_pipe_add(RED_CHANNEL_CLIENT(dcc), (RedPipeItem *)item);
}
-static void red_monitors_config_item_free(RedMonitorsConfigItem *item)
+static void red_monitors_config_item_free(RedPipeItem *base)
{
+ RedMonitorsConfigItem *item = SPICE_CONTAINEROF(base, RedMonitorsConfigItem, pipe_item);
+
monitors_config_unref(item->monitors_config);
free(item);
}
@@ -532,7 +534,7 @@ static RedMonitorsConfigItem *red_monitors_config_item_new(RedChannel* channel,
mci->monitors_config = monitors_config;
red_pipe_item_init_full(&mci->pipe_item, RED_PIPE_ITEM_TYPE_MONITORS_CONFIG,
- (GDestroyNotify)red_monitors_config_item_free);
+ red_monitors_config_item_free);
return mci;
}
diff --git a/server/main-channel.c b/server/main-channel.c
index 70aaff7..e89af97 100644
--- a/server/main-channel.c
+++ b/server/main-channel.c
@@ -247,8 +247,9 @@ static RedPipeItem *main_agent_tokens_item_new(RedChannelClient *rcc, uint32_t n
return &item->base;
}
-static void main_agent_data_item_free(RedAgentDataPipeItem *item)
+static void main_agent_data_item_free(RedPipeItem *base)
{
+ RedAgentDataPipeItem *item = SPICE_CONTAINEROF(base, RedAgentDataPipeItem, base);
item->free_data(item->data, item->opaque);
free(item);
}
@@ -260,7 +261,7 @@ static RedPipeItem *main_agent_data_item_new(RedChannelClient *rcc, uint8_t* dat
RedAgentDataPipeItem *item = spice_malloc(sizeof(RedAgentDataPipeItem));
red_pipe_item_init_full(&item->base, RED_PIPE_ITEM_TYPE_MAIN_AGENT_DATA,
- (GDestroyNotify)main_agent_data_item_free);
+ main_agent_data_item_free);
item->data = data;
item->len = len;
item->free_data = free_data;
@@ -306,8 +307,9 @@ static RedPipeItem *main_uuid_item_new(MainChannelClient *mcc, const uint8_t uui
return &item->base;
}
-static void main_notify_item_free(RedNotifyPipeItem *data)
+static void main_notify_item_free(RedPipeItem *base)
{
+ RedNotifyPipeItem *data = SPICE_CONTAINEROF(base, RedNotifyPipeItem, base);
free(data->msg);
free(data);
}
@@ -318,7 +320,7 @@ static RedPipeItem *main_notify_item_new(RedChannelClient *rcc, void *data, int
const char *msg = data;
red_pipe_item_init_full(&item->base, RED_PIPE_ITEM_TYPE_MAIN_NOTIFY,
- (GDestroyNotify)main_notify_item_free);
+ main_notify_item_free);
item->msg = spice_strdup(msg);
return &item->base;
}
diff --git a/server/red-channel.c b/server/red-channel.c
index c0a9501..c422afd 100644
--- a/server/red-channel.c
+++ b/server/red-channel.c
@@ -2365,8 +2365,10 @@ int red_channel_client_wait_outgoing_item(RedChannelClient *rcc,
}
}
-static void marker_pipe_item_free(MarkerPipeItem *item)
+static void marker_pipe_item_free(RedPipeItem *base)
{
+ MarkerPipeItem *item = SPICE_CONTAINEROF(base, MarkerPipeItem, base);
+
if (item->item_in_pipe) {
*item->item_in_pipe = FALSE;
}
@@ -2392,7 +2394,7 @@ int red_channel_client_wait_pipe_item_sent(RedChannelClient *rcc,
MarkerPipeItem *mark_item = spice_new0(MarkerPipeItem, 1);
red_pipe_item_init_full(&mark_item->base, RED_PIPE_ITEM_TYPE_MARKER,
- (GDestroyNotify)marker_pipe_item_free);
+ marker_pipe_item_free);
item_in_pipe = TRUE;
mark_item->item_in_pipe = &item_in_pipe;
red_channel_client_pipe_add_after(rcc, &mark_item->base, item);
diff --git a/server/red-pipe-item.c b/server/red-pipe-item.c
index d9c60aa..cc0ab5a 100644
--- a/server/red-pipe-item.c
+++ b/server/red-pipe-item.c
@@ -44,10 +44,10 @@ void red_pipe_item_unref(gpointer object)
void red_pipe_item_init_full(RedPipeItem *item,
gint type,
- GDestroyNotify free_func)
+ red_pipe_item_free_t *free_func)
{
ring_item_init(&item->link);
item->type = type;
item->refcount = 1;
- item->free_func = free_func ? free_func : (GDestroyNotify)free;
+ item->free_func = free_func ? free_func : (red_pipe_item_free_t *)free;
}
diff --git a/server/red-pipe-item.h b/server/red-pipe-item.h
index 0ff4852..4b3d892 100644
--- a/server/red-pipe-item.h
+++ b/server/red-pipe-item.h
@@ -21,17 +21,21 @@
#include <glib.h>
#include <common/ring.h>
-typedef struct {
+struct RedPipeItem;
+
+typedef void red_pipe_item_free_t(struct RedPipeItem *item);
+
+typedef struct RedPipeItem {
RingItem link;
int type;
/* private */
int refcount;
- GDestroyNotify free_func;
+ red_pipe_item_free_t *free_func;
} RedPipeItem;
-void red_pipe_item_init_full(RedPipeItem *item, int type, GDestroyNotify free_func);
+void red_pipe_item_init_full(RedPipeItem *item, int type, red_pipe_item_free_t free_func);
RedPipeItem *red_pipe_item_ref(gpointer item);
void red_pipe_item_unref(gpointer item);
diff --git a/server/reds.c b/server/reds.c
index a1e589a..8a903b7 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -303,7 +303,7 @@ static uint32_t reds_qxl_ram_size(RedsState *reds);
static int calc_compression_level(RedsState *reds);
static RedVDIReadBuf *vdi_port_get_read_buf(RedCharDeviceVDIPort *dev);
-static void vdi_port_read_buf_free(RedVDIReadBuf *buf);
+static red_pipe_item_free_t vdi_port_read_buf_free;
static ChannelSecurityOptions *reds_find_channel_security(RedsState *reds, int id)
{
@@ -790,7 +790,7 @@ static void vdi_read_buf_init(RedVDIReadBuf *buf)
* from the base class and are not going to use the type
*/
red_pipe_item_init_full(&buf->parent, -1,
- (GDestroyNotify)vdi_port_read_buf_free);
+ vdi_port_read_buf_free);
}
static RedVDIReadBuf *vdi_port_get_read_buf(RedCharDeviceVDIPort *dev)
@@ -811,8 +811,10 @@ static RedVDIReadBuf *vdi_port_get_read_buf(RedCharDeviceVDIPort *dev)
return buf;
}
-static void vdi_port_read_buf_free(RedVDIReadBuf *buf)
+static void vdi_port_read_buf_free(RedPipeItem *base)
{
+ RedVDIReadBuf *buf = SPICE_CONTAINEROF(base, RedVDIReadBuf, parent);
+
g_warn_if_fail(buf->parent.refcount == 0);
ring_add(&buf->dev->priv->read_bufs, (RingItem *)buf);
diff --git a/server/smartcard.c b/server/smartcard.c
index c39aeae..2acddcf 100644
--- a/server/smartcard.c
+++ b/server/smartcard.c
@@ -537,8 +537,9 @@ static void smartcard_push_error(RedChannelClient *rcc, uint32_t reader_id, VSCE
smartcard_channel_client_pipe_add_push(rcc, &error_item->base);
}
-static void smartcard_free_vsc_msg_item(RedMsgItem *item)
+static void smartcard_free_vsc_msg_item(RedPipeItem *base)
{
+ RedMsgItem *item = SPICE_CONTAINEROF(base, RedMsgItem, base);
free(item->vheader);
free(item);
}
@@ -549,7 +550,7 @@ static RedMsgItem *smartcard_get_vsc_msg_item(RedChannelClient *rcc,
RedMsgItem *msg_item = spice_new0(RedMsgItem, 1);
red_pipe_item_init_full(&msg_item->base, RED_PIPE_ITEM_TYPE_SMARTCARD_DATA,
- (GDestroyNotify)smartcard_free_vsc_msg_item);
+ smartcard_free_vsc_msg_item);
msg_item->vheader = vheader;
return msg_item;
}
diff --git a/server/stream.c b/server/stream.c
index 4c733de..74df254 100644
--- a/server/stream.c
+++ b/server/stream.c
@@ -76,7 +76,7 @@ static RedPipeItem *stream_create_destroy_item_new(StreamAgent *agent, gint type
StreamCreateDestroyItem *item = spice_new0(StreamCreateDestroyItem, 1);
red_pipe_item_init_full(&item->base, type,
- (GDestroyNotify)stream_create_destroy_item_release);
+ stream_create_destroy_item_release);
agent->stream->refs++;
item->agent = agent;
return &item->base;
@@ -161,9 +161,10 @@ void stream_agent_unref(DisplayChannel *display, StreamAgent *agent)
stream_unref(display, agent->stream);
}
-void red_stream_clip_item_free(RedStreamClipItem *item)
+void red_stream_clip_item_free(RedPipeItem *base)
{
- g_return_if_fail(item != NULL);
+ g_return_if_fail(base != NULL);
+ RedStreamClipItem *item = SPICE_CONTAINEROF(base, RedStreamClipItem, base);
DisplayChannel *display = DCC_TO_DC(item->stream_agent->dcc);
g_return_if_fail(item->base.refcount == 0);
@@ -177,7 +178,7 @@ RedStreamClipItem *red_stream_clip_item_new(StreamAgent *agent)
{
RedStreamClipItem *item = spice_new(RedStreamClipItem, 1);
red_pipe_item_init_full((RedPipeItem *)item, RED_PIPE_ITEM_TYPE_STREAM_CLIP,
- (GDestroyNotify)red_stream_clip_item_free);
+ red_stream_clip_item_free);
item->stream_agent = agent;
agent->stream->refs++;
@@ -785,8 +786,10 @@ void stream_agent_stop(StreamAgent *agent)
}
}
-static void red_upgrade_item_free(RedUpgradeItem *item)
+static void red_upgrade_item_free(RedPipeItem *base)
{
+ RedUpgradeItem *item = SPICE_CONTAINEROF(base, RedUpgradeItem, base);
+
g_return_if_fail(item != NULL);
g_return_if_fail(item->base.refcount == 0);
@@ -836,7 +839,7 @@ static void dcc_detach_stream_gracefully(DisplayChannelClient *dcc,
rcc = RED_CHANNEL_CLIENT(dcc);
upgrade_item = spice_new(RedUpgradeItem, 1);
red_pipe_item_init_full(&upgrade_item->base, RED_PIPE_ITEM_TYPE_UPGRADE,
- (GDestroyNotify)red_upgrade_item_free);
+ red_upgrade_item_free);
upgrade_item->drawable = stream->current;
upgrade_item->drawable->refs++;
n_rects = pixman_region32_n_rects(&upgrade_item->drawable->tree_item.base.rgn);
--
2.7.4
More information about the Spice-devel
mailing list