From 93405ae3c64212c751c24ec9d466ab605b3fadd8 Mon Sep 17 00:00:00 2001 From: Frediano Ziglio Date: Tue, 5 May 2020 04:54:46 +0100 Subject: [PATCH] Automatically release some pipe item resources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now that items are managed by constructors/destructors avoid some manual releases. Signed-off-by: Frediano Ziglio Acked-by: Julien RopĂ© --- server/dcc-send.cpp | 4 ++-- server/main-channel-client.cpp | 14 ++++---------- server/smartcard.cpp | 12 +++--------- server/spicevmc.cpp | 15 ++++----------- server/video-stream.cpp | 8 ++++---- server/video-stream.h | 4 ++-- 6 files changed, 19 insertions(+), 38 deletions(-) diff --git a/server/dcc-send.cpp b/server/dcc-send.cpp index 641b8c06..80ef2da3 100644 --- a/server/dcc-send.cpp +++ b/server/dcc-send.cpp @@ -2158,7 +2158,7 @@ static void marshall_stream_clip(DisplayChannelClient *dcc, stream_clip.id = display_channel_get_video_stream_id(DCC_TO_DC(dcc), agent->stream); stream_clip.clip.type = item->clip_type; - stream_clip.clip.rects = item->rects; + stream_clip.clip.rects = item->rects.get(); spice_marshall_msg_display_stream_clip(base_marshaller, &stream_clip); } @@ -2194,7 +2194,7 @@ static void marshall_upgrade(DisplayChannelClient *dcc, SpiceMarshaller *m, copy.base.surface_id = 0; copy.base.box = red_drawable->bbox; copy.base.clip.type = SPICE_CLIP_TYPE_RECTS; - copy.base.clip.rects = item->rects; + copy.base.clip.rects = item->rects.get(); copy.data = red_drawable->u.copy; spice_marshall_msg_display_draw_copy(m, ©, diff --git a/server/main-channel-client.cpp b/server/main-channel-client.cpp index 6423ecc0..eb82614c 100644 --- a/server/main-channel-client.cpp +++ b/server/main-channel-client.cpp @@ -87,8 +87,7 @@ struct RedUuidPipeItem: public RedPipeItemNum { }; struct RedNotifyPipeItem: public RedPipeItemNum { - ~RedNotifyPipeItem(); - char *msg; + red::glib_unique_ptr msg; }; struct RedMouseModePipeItem: public RedPipeItemNum { @@ -143,16 +142,11 @@ void MainChannelClient::on_disconnect() static void main_channel_client_push_ping(MainChannelClient *mcc, int size); -RedNotifyPipeItem::~RedNotifyPipeItem() -{ - g_free(msg); -} - static RedPipeItem *main_notify_item_new(const char *msg, int num) { RedNotifyPipeItem *item = new RedNotifyPipeItem(); - item->msg = g_strdup(msg); + item->msg.reset(g_strdup(msg)); return item; } @@ -675,9 +669,9 @@ static void main_channel_marshall_notify(RedChannelClient *rcc, notify.severity = SPICE_NOTIFY_SEVERITY_WARN; notify.visibilty = SPICE_NOTIFY_VISIBILITY_HIGH; notify.what = SPICE_WARN_GENERAL; - notify.message_len = strlen(item->msg); + notify.message_len = strlen(item->msg.get()); spice_marshall_msg_notify(m, ¬ify); - spice_marshaller_add(m, (uint8_t *)item->msg, notify.message_len + 1); + spice_marshaller_add(m, (uint8_t *)item->msg.get(), notify.message_len + 1); } static void main_channel_fill_migrate_dst_info(MainChannel *main_channel, diff --git a/server/smartcard.cpp b/server/smartcard.cpp index f968c6ed..c33844dd 100644 --- a/server/smartcard.cpp +++ b/server/smartcard.cpp @@ -68,8 +68,7 @@ struct RedCharDeviceSmartcardPrivate { }; struct RedMsgItem: public RedPipeItemNum { - ~RedMsgItem(); - VSCMsgHeader* vheader; + red::glib_unique_ptr vheader; }; static RedMsgItem *smartcard_new_vsc_msg_item(unsigned int reader_id, const VSCMsgHeader *vheader); @@ -339,7 +338,7 @@ static void smartcard_channel_send_msg(RedChannelClient *rcc, { RedMsgItem* msg_item = static_cast(item); - smartcard_channel_client_send_data(rcc, m, item, msg_item->vheader); + smartcard_channel_client_send_data(rcc, m, item, msg_item->vheader.get()); } static void smartcard_channel_send_migrate_data(SmartCardChannelClient *scc, @@ -389,16 +388,11 @@ void SmartCardChannelClient::send_item(RedPipeItem *item) begin_send_message(); } -RedMsgItem::~RedMsgItem() -{ - g_free(vheader); -} - static RedMsgItem *smartcard_new_vsc_msg_item(unsigned int reader_id, const VSCMsgHeader *vheader) { RedMsgItem *msg_item = new RedMsgItem(); - msg_item->vheader = (VSCMsgHeader*) g_memdup(vheader, sizeof(*vheader) + vheader->length); + msg_item->vheader.reset((VSCMsgHeader*) g_memdup(vheader, sizeof(*vheader) + vheader->length)); /* We patch the reader_id, since the device only knows about itself, and * we know about the sum of readers. */ msg_item->vheader->reader_id = reader_id; diff --git a/server/spicevmc.cpp b/server/spicevmc.cpp index 8919cc4f..a290bfbf 100644 --- a/server/spicevmc.cpp +++ b/server/spicevmc.cpp @@ -189,9 +189,7 @@ static red::shared_ptr red_vmc_channel_new(RedsState *reds, uint8 } struct RedPortInitPipeItem: public RedPipeItemNum { - ~RedPortInitPipeItem(); - - char* name; + red::glib_unique_ptr name; uint8_t opened; }; @@ -285,18 +283,13 @@ RedPipeItem* RedCharDeviceSpiceVmc::read_one_msg_from_device() return NULL; } -RedPortInitPipeItem::~RedPortInitPipeItem() -{ - g_free(name); -} - static void spicevmc_port_send_init(VmcChannelClient *rcc) { RedVmcChannel *channel = rcc->get_channel(); SpiceCharDeviceInstance *sin = channel->chardev_sin; RedPortInitPipeItem *item = new RedPortInitPipeItem(); - item->name = g_strdup(sin->portname); + item->name.reset(g_strdup(sin->portname)); item->opened = channel->port_opened; rcc->pipe_add_push(item); } @@ -556,8 +549,8 @@ static void spicevmc_red_channel_send_port_init(RedChannelClient *rcc, SpiceMsgPortInit init; rcc->init_send_data(SPICE_MSG_PORT_INIT); - init.name = (uint8_t *)i->name; - init.name_size = strlen(i->name) + 1; + init.name = (uint8_t *)i->name.get(); + init.name_size = strlen(i->name.get()) + 1; init.opened = i->opened; spice_marshall_msg_port_init(m, &init); } diff --git a/server/video-stream.cpp b/server/video-stream.cpp index 02d01e32..578afae7 100644 --- a/server/video-stream.cpp +++ b/server/video-stream.cpp @@ -164,7 +164,6 @@ VideoStreamClipItem::~VideoStreamClipItem() DisplayChannel *display = DCC_TO_DC(stream_agent->dcc); video_stream_agent_unref(display, stream_agent); - g_free(rects); } VideoStreamClipItem *video_stream_clip_item_new(VideoStreamAgent *agent) @@ -177,7 +176,8 @@ VideoStreamClipItem *video_stream_clip_item_new(VideoStreamAgent *agent) item->clip_type = SPICE_CLIP_TYPE_RECTS; int n_rects = pixman_region32_n_rects(&agent->clip); - item->rects = (SpiceClipRects*) g_malloc(sizeof(SpiceClipRects) + n_rects * sizeof(SpiceRect)); + item->rects.reset((SpiceClipRects*) g_malloc(sizeof(SpiceClipRects) + + n_rects * sizeof(SpiceRect))); item->rects->num_rects = n_rects; region_ret_rects(&agent->clip, item->rects->rects, n_rects); @@ -803,7 +803,6 @@ void video_stream_agent_stop(VideoStreamAgent *agent) RedUpgradeItem::~RedUpgradeItem() { drawable_unref(drawable); - g_free(rects); } /* @@ -846,7 +845,8 @@ static void dcc_detach_stream_gracefully(DisplayChannelClient *dcc, upgrade_item->drawable = stream->current; upgrade_item->drawable->refs++; n_rects = pixman_region32_n_rects(&upgrade_item->drawable->tree_item.base.rgn); - upgrade_item->rects = (SpiceClipRects*) g_malloc(sizeof(SpiceClipRects) + n_rects * sizeof(SpiceRect)); + upgrade_item->rects.reset((SpiceClipRects*) g_malloc(sizeof(SpiceClipRects) + + n_rects * sizeof(SpiceRect))); upgrade_item->rects->num_rects = n_rects; region_ret_rects(&upgrade_item->drawable->tree_item.base.rgn, upgrade_item->rects->rects, n_rects); diff --git a/server/video-stream.h b/server/video-stream.h index e35d740f..18ffe67d 100644 --- a/server/video-stream.h +++ b/server/video-stream.h @@ -53,7 +53,7 @@ struct RedUpgradeItem: public RedPipeItem { using RedPipeItem::RedPipeItem; ~RedUpgradeItem(); Drawable *drawable; - SpiceClipRects *rects; + red::glib_unique_ptr rects; }; struct RedStreamActivateReportItem: public RedPipeItem { @@ -101,7 +101,7 @@ struct VideoStreamClipItem: public RedPipeItem { ~VideoStreamClipItem(); VideoStreamAgent *stream_agent; int clip_type; - SpiceClipRects *rects; + red::glib_unique_ptr rects; }; VideoStreamClipItem *video_stream_clip_item_new(VideoStreamAgent *agent);