Re: [Spice-devel] [spice-gtk][PATCH] Prefer using g_malloc0()/g_free()
ack - Original Message - > As we already depend on GLib, let's use g_{malloc,new}0() instead of the > standard malloc() or the spice_{malloc,new}*() and g_free() instead of > the standard free() when possible. > Memory allocated by other libraries using malloc() should still be freed > by free(). > As a side effect of the changes, we are muting a few warnings caught by > coverity. > --- > gtk/channel-cursor.c | 2 +- > gtk/channel-display-mjpeg.c | 8 +++- > gtk/channel-display.c | 8 > gtk/channel-main.c| 12 ++-- > gtk/channel-record.c | 2 +- > gtk/channel-webdav.c | 4 ++-- > gtk/controller/test.c | 4 ++-- > gtk/decode-glz.c | 10 +- > gtk/decode-jpeg.c | 2 +- > gtk/decode-zlib.c | 2 +- > gtk/spice-channel.c | 20 ++-- > gtk/spice-client-gtk.override | 7 --- > gtk/spice-session.c | 2 +- > gtk/spice-widget.c| 2 +- > gtk/usb-device-manager.c | 2 +- > 15 files changed, 43 insertions(+), 44 deletions(-) > > diff --git a/gtk/channel-cursor.c b/gtk/channel-cursor.c > index a7d7153..ae788dc 100644 > --- a/gtk/channel-cursor.c > +++ b/gtk/channel-cursor.c > @@ -323,7 +323,7 @@ static display_cursor *set_cursor(SpiceChannel *channel, > SpiceCursor *scursor) > g_return_val_if_fail(scursor->data_size != 0, NULL); > > size = 4u * hdr->width * hdr->height; > -cursor = spice_malloc(sizeof(*cursor) + size); > +cursor = g_malloc0(sizeof(*cursor) + size); > cursor->hdr = *hdr; > cursor->default_cursor = FALSE; > cursor->refcount = 1; > diff --git a/gtk/channel-display-mjpeg.c b/gtk/channel-display-mjpeg.c > index 2ad653e..95d5b33 100644 > --- a/gtk/channel-display-mjpeg.c > +++ b/gtk/channel-display-mjpeg.c > @@ -73,11 +73,9 @@ void stream_mjpeg_data(display_stream *st) > uint8_t *lines[4]; > > stream_get_dimensions(st, &width, &height); > -dest = malloc(width * height * 4); > +dest = g_malloc0(width * height * 4); > > -if (st->out_frame) { > -free(st->out_frame); > -} > +g_free(st->out_frame); > st->out_frame = dest; > > jpeg_read_header(&st->mjpeg_cinfo, 1); > @@ -153,6 +151,6 @@ G_GNUC_INTERNAL > void stream_mjpeg_cleanup(display_stream *st) > { > jpeg_destroy_decompress(&st->mjpeg_cinfo); > -free(st->out_frame); > +g_free(st->out_frame); > st->out_frame = NULL; > } > diff --git a/gtk/channel-display.c b/gtk/channel-display.c > index 6265334..6fa97aa 100644 > --- a/gtk/channel-display.c > +++ b/gtk/channel-display.c > @@ -730,7 +730,7 @@ static void destroy_canvas(display_surface *surface) > jpeg_decoder_destroy(surface->jpeg_decoder); > > if (surface->shmid == -1) { > -free(surface->data); > +g_free(surface->data); > } > #ifdef HAVE_SYS_SHM_H > else { > @@ -986,7 +986,7 @@ static void display_handle_stream_create(SpiceChannel > *channel, SpiceMsgIn *in) > memset(c->streams + n, 0, (c->nstreams - n) * > sizeof(c->streams[0])); > } > g_return_if_fail(c->streams[op->id] == NULL); > -c->streams[op->id] = spice_new0(display_stream, 1); > +c->streams[op->id] = g_new0(display_stream, 1); > st = c->streams[op->id]; > > st->msg_create = in; > @@ -1486,7 +1486,7 @@ static void destroy_stream(SpiceChannel *channel, int > id) > g_queue_free(st->msgq); > if (st->timeout != 0) > g_source_remove(st->timeout); > -free(st); > +g_free(st); > c->streams[id] = NULL; > } > > @@ -1498,7 +1498,7 @@ static void clear_streams(SpiceChannel *channel) > for (i = 0; i < c->nstreams; i++) { > destroy_stream(channel, i); > } > -free(c->streams); > +g_free(c->streams); > c->streams = NULL; > c->nstreams = 0; > } > diff --git a/gtk/channel-main.c b/gtk/channel-main.c > index 352499c..7a299a4 100644 > --- a/gtk/channel-main.c > +++ b/gtk/channel-main.c > @@ -1050,7 +1050,7 @@ gboolean > spice_main_send_monitor_config(SpiceMainChannel *channel) > } > > size = sizeof(VDAgentMonitorsConfig) + sizeof(VDAgentMonConfig) * > monitors; > -mon = spice_malloc0(size); > +mon = g_malloc0(size); > > mon->num_of_monitors = monitors; > if (c->disable_display_position == FALSE || > @@ -1081,7 +1081,7 @@ gboolean > spice_main_send_monitor_config(SpiceMainChannel *channel) > monitors_align(mon->monitors, mon->num_of_monitors); > > agent_msg_queue(channel, VD_AGENT_MONITORS_CONFIG, size, mon); > -free(mon); > +g_free(mon); > > spice_channel_wakeup(SPICE_CHANNEL(channel), FALSE); > if (c->timer_id != 0) { > @@ -1133,7 +1133,7 @@ static void agent_announce_caps(SpiceMainChannel > *channel) > return; > > size = sizeof(VDAgentAnnounceCapabilities) + VD_AGENT_CAPS_BYTES; > -caps = spice_malloc0(size); > +c
[Spice-devel] [spice-gtk][PATCH] Prefer using g_malloc0()/g_free()
As we already depend on GLib, let's use g_{malloc,new}0() instead of the standard malloc() or the spice_{malloc,new}*() and g_free() instead of the standard free() when possible. Memory allocated by other libraries using malloc() should still be freed by free(). As a side effect of the changes, we are muting a few warnings caught by coverity. --- gtk/channel-cursor.c | 2 +- gtk/channel-display-mjpeg.c | 8 +++- gtk/channel-display.c | 8 gtk/channel-main.c| 12 ++-- gtk/channel-record.c | 2 +- gtk/channel-webdav.c | 4 ++-- gtk/controller/test.c | 4 ++-- gtk/decode-glz.c | 10 +- gtk/decode-jpeg.c | 2 +- gtk/decode-zlib.c | 2 +- gtk/spice-channel.c | 20 ++-- gtk/spice-client-gtk.override | 7 --- gtk/spice-session.c | 2 +- gtk/spice-widget.c| 2 +- gtk/usb-device-manager.c | 2 +- 15 files changed, 43 insertions(+), 44 deletions(-) diff --git a/gtk/channel-cursor.c b/gtk/channel-cursor.c index a7d7153..ae788dc 100644 --- a/gtk/channel-cursor.c +++ b/gtk/channel-cursor.c @@ -323,7 +323,7 @@ static display_cursor *set_cursor(SpiceChannel *channel, SpiceCursor *scursor) g_return_val_if_fail(scursor->data_size != 0, NULL); size = 4u * hdr->width * hdr->height; -cursor = spice_malloc(sizeof(*cursor) + size); +cursor = g_malloc0(sizeof(*cursor) + size); cursor->hdr = *hdr; cursor->default_cursor = FALSE; cursor->refcount = 1; diff --git a/gtk/channel-display-mjpeg.c b/gtk/channel-display-mjpeg.c index 2ad653e..95d5b33 100644 --- a/gtk/channel-display-mjpeg.c +++ b/gtk/channel-display-mjpeg.c @@ -73,11 +73,9 @@ void stream_mjpeg_data(display_stream *st) uint8_t *lines[4]; stream_get_dimensions(st, &width, &height); -dest = malloc(width * height * 4); +dest = g_malloc0(width * height * 4); -if (st->out_frame) { -free(st->out_frame); -} +g_free(st->out_frame); st->out_frame = dest; jpeg_read_header(&st->mjpeg_cinfo, 1); @@ -153,6 +151,6 @@ G_GNUC_INTERNAL void stream_mjpeg_cleanup(display_stream *st) { jpeg_destroy_decompress(&st->mjpeg_cinfo); -free(st->out_frame); +g_free(st->out_frame); st->out_frame = NULL; } diff --git a/gtk/channel-display.c b/gtk/channel-display.c index 6265334..6fa97aa 100644 --- a/gtk/channel-display.c +++ b/gtk/channel-display.c @@ -730,7 +730,7 @@ static void destroy_canvas(display_surface *surface) jpeg_decoder_destroy(surface->jpeg_decoder); if (surface->shmid == -1) { -free(surface->data); +g_free(surface->data); } #ifdef HAVE_SYS_SHM_H else { @@ -986,7 +986,7 @@ static void display_handle_stream_create(SpiceChannel *channel, SpiceMsgIn *in) memset(c->streams + n, 0, (c->nstreams - n) * sizeof(c->streams[0])); } g_return_if_fail(c->streams[op->id] == NULL); -c->streams[op->id] = spice_new0(display_stream, 1); +c->streams[op->id] = g_new0(display_stream, 1); st = c->streams[op->id]; st->msg_create = in; @@ -1486,7 +1486,7 @@ static void destroy_stream(SpiceChannel *channel, int id) g_queue_free(st->msgq); if (st->timeout != 0) g_source_remove(st->timeout); -free(st); +g_free(st); c->streams[id] = NULL; } @@ -1498,7 +1498,7 @@ static void clear_streams(SpiceChannel *channel) for (i = 0; i < c->nstreams; i++) { destroy_stream(channel, i); } -free(c->streams); +g_free(c->streams); c->streams = NULL; c->nstreams = 0; } diff --git a/gtk/channel-main.c b/gtk/channel-main.c index 352499c..7a299a4 100644 --- a/gtk/channel-main.c +++ b/gtk/channel-main.c @@ -1050,7 +1050,7 @@ gboolean spice_main_send_monitor_config(SpiceMainChannel *channel) } size = sizeof(VDAgentMonitorsConfig) + sizeof(VDAgentMonConfig) * monitors; -mon = spice_malloc0(size); +mon = g_malloc0(size); mon->num_of_monitors = monitors; if (c->disable_display_position == FALSE || @@ -1081,7 +1081,7 @@ gboolean spice_main_send_monitor_config(SpiceMainChannel *channel) monitors_align(mon->monitors, mon->num_of_monitors); agent_msg_queue(channel, VD_AGENT_MONITORS_CONFIG, size, mon); -free(mon); +g_free(mon); spice_channel_wakeup(SPICE_CHANNEL(channel), FALSE); if (c->timer_id != 0) { @@ -1133,7 +1133,7 @@ static void agent_announce_caps(SpiceMainChannel *channel) return; size = sizeof(VDAgentAnnounceCapabilities) + VD_AGENT_CAPS_BYTES; -caps = spice_malloc0(size); +caps = g_malloc0(size); if (!c->agent_caps_received) caps->request = 1; VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_MOUSE_STATE); @@ -1144,7 +1144,7 @@ static void agent_announce_caps(SpiceMainChannel *channel) VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_CLIPBOARD_SELECTION); agent_m