From: "Marc-André Lureau" <mlureau@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: Michael Roth <mdroth@linux.vnet.ibm.com>,
marcandre lureau <marcandre.lureau@redhat.com>,
qemu-devel@nongnu.org, pbonzini@redhat.com
Subject: Re: [Qemu-devel] [PATCH for-2.7 v3 03/36] qga: free the whole blacklist
Date: Thu, 4 Aug 2016 10:59:14 -0400 (EDT) [thread overview]
Message-ID: <1223869164.1103430.1470322754832.JavaMail.zimbra@redhat.com> (raw)
In-Reply-To: <87r3a4d2ea.fsf@dusky.pond.sub.org>
Hi
----- Original Message -----
> Marc-André Lureau <mlureau@redhat.com> writes:
>
> > Hi
> >
> > ----- Original Message -----
> >> Copying the QGA maintainer.
> >>
> >> marcandre.lureau@redhat.com writes:
> >>
> >> > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> >> >
> >> > Free the list, not just the elements.
> >> >
> >> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> >> > ---
> >> > include/glib-compat.h | 9 +++++++++
> >> > qga/main.c | 8 ++------
> >> > 2 files changed, 11 insertions(+), 6 deletions(-)
> >> >
> >> > diff --git a/include/glib-compat.h b/include/glib-compat.h
> >> > index 01aa7b3..6d643b2 100644
> >> > --- a/include/glib-compat.h
> >> > +++ b/include/glib-compat.h
> >> > @@ -260,4 +260,13 @@ static inline void g_hash_table_add(GHashTable
> >> > *hash_table, gpointer key)
> >> > } while (0)
> >> > #endif
> >> >
> >> > +/*
> >> > + * A GFunc function helper freeing the first argument (not part of
> >> > glib)
> >>
> >> Well, it's not part of GLib, because non-obsolete GLib has no use for
> >> it! You'd simply pass g_free directly to a _free_full() function
> >> instead of passing a silly wrapper to a _foreach() function.
> >>
> >> The commit does a bit more than just plug a leak, it also provides a new
> >> helper for general use. Mention in the commit message?
> >>
> >> I wonder how many more of these silly g_free() wrappers we have. A
> >> quick grep reports hits in string-input-visitor.c and
> >> qobject/json-streamer.c. If you add one to a header, you get to hunt
> >> them down :)
> >>
> >> > + */
> >> > +static inline void qemu_g_func_free(gpointer data,
> >> > + gpointer user_data)
> >> > +{
> >> > + g_free(data);
> >> > +}
> >> > +
> >> > #endif
> >> > diff --git a/qga/main.c b/qga/main.c
> >> > index 4c3b2c7..868508b 100644
> >> > --- a/qga/main.c
> >> > +++ b/qga/main.c
> >> > @@ -1175,6 +1175,8 @@ static void config_free(GAConfig *config)
> >> > #ifdef CONFIG_FSFREEZE
> >> > g_free(config->fsfreeze_hook);
> >> > #endif
> >> > + g_list_foreach(config->blacklist, qemu_g_func_free, NULL);
> >> > + g_list_free(config->blacklist);
> >>
> >> Modern GLib code doesn't need silly wrappers:
> >>
> >> g_list_free_full(config->blacklist, g_free);
> >>
> >> Unfortunately, this requires 2.28, and we're stull stuck at 2.22.
> >> Please explain this in the commit message.
> >>
> >> Even better, provide a replacement in glib-compat.h #if
> >> !GLIB_CHECK_VERSION(2, 28, 0). Will facilitate ditching the work-around
> >> when we upgrade to 2.28.
> >
> > ok
> >
> >>
> >> > g_free(config);
> >> > }
> >> >
> >> > @@ -1310,11 +1312,6 @@ static int run_agent(GAState *s, GAConfig
> >> > *config)
> >> > return EXIT_SUCCESS;
> >> > }
> >> >
> >> > -static void free_blacklist_entry(gpointer entry, gpointer unused)
> >> > -{
> >> > - g_free(entry);
> >> > -}
> >> > -
> >> > int main(int argc, char **argv)
> >> > {
> >> > int ret = EXIT_SUCCESS;
> >> > @@ -1379,7 +1376,6 @@ end:
> >> > if (s->channel) {
> >> > ga_channel_free(s->channel);
> >> > }
> >> > - g_list_foreach(config->blacklist, free_blacklist_entry, NULL);
> >> > g_free(s->pstate_filepath);
> >> > g_free(s->state_filepath_isfrozen);
> >>
> >> If you at least explain why not g_list_free_full() in the commit
> >> message, you can add
> >> Reviewed-by: Markus Armbruster <armbru@redhat.com>
> >>
> >> But I'd like to encourage you to explore providing a replacement for
> >> g_list_free_full().
> >
> > Such replacement would make use of a (GFunc) cast, glib implementation is
> > calling g_list_foreach (list, (GFunc) free_func, NULL), but Eric didn't
> > want to have such cast in qemu code. He suggested to have the static
> > inline helper in
> > https://lists.gnu.org/archive/html/qemu-devel/2016-07/msg06561.html
>
> Pointlessly dirty:
>
> g_list_foreach(list, (GFunc)g_free, NULL)
>
> Trade dirty for cumbersome:
>
> void free_wrapper(gpointer data, gpointer unused)
> {
> g_free(data)
> }
>
> g_list_foreach(list, free_wrapper, NULL);
>
> But this is C. In C, simple things are best done the simple way. Even
> when that means we don't get to show off how amazingly well we've been
> educated on higher order functions:
>
> for (node = list; node; node = next) {
> next = node->next;
> g_free(node);
> }
>
> Simple, stupid, and not dirty.
If only we were paid to write more lines of code ;) Anyway, that's fine by me (it's work after all, I'll write elegant code for fun time ;)
> Quote: "Note that it is considered perfectly acceptable to access
> list->next directly."
Funny that quote is only in GList, but GSList also documents and exposes the struct.
next prev parent reply other threads:[~2016-08-04 14:59 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-03 14:55 [Qemu-devel] [PATCH for-2.7 v3 00/36] Various memory leak fixes marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 01/36] build-sys: fix building with make CFLAGS=.. argument marcandre.lureau
2016-08-03 15:58 ` Paolo Bonzini
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 02/36] tests: fix test-qga leaks marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 03/36] qga: free the whole blacklist marcandre.lureau
2016-08-04 13:25 ` Markus Armbruster
2016-08-04 13:47 ` Marc-André Lureau
2016-08-04 14:39 ` Markus Armbruster
2016-08-04 14:59 ` Marc-André Lureau [this message]
2016-08-05 7:06 ` Markus Armbruster
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 04/36] qga: free remaining leaking state marcandre.lureau
2016-08-04 13:38 ` Markus Armbruster
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 05/36] tests: fix test-cutils leaks marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 06/36] tests: fix test-vmstate leaks marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 07/36] tests: fix test-iov leaks marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 08/36] qdist: fix entries memory leak marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 09/36] tests: fix check-qom-interface leaks marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 10/36] tests: fix check-qom-proplist leaks marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 11/36] tests: fix small leak in test-io-channel-command marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 12/36] tests: fix leak in test-string-input-visitor marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 13/36] portio: keep references on portio marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 14/36] numa: do not leak NumaOptions marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 15/36] pc: simplify passing qemu_irq marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 16/36] pc: don't leak a20_line marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 17/36] machine: use class base init generated name marcandre.lureau
2016-08-04 13:56 ` Markus Armbruster
2016-08-04 14:31 ` Marc-André Lureau
2016-08-04 15:26 ` Markus Armbruster
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 18/36] acpi-build: fix array leak marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 19/36] char: free the tcp connection data when closing marcandre.lureau
2016-08-03 15:32 ` Paolo Bonzini
2016-08-03 15:40 ` Marc-André Lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 20/36] char: free MuxDriver " marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 21/36] tests: fix qom-test leaks marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 22/36] pc: free i8259 marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 23/36] pc: keep gsi reference marcandre.lureau
2016-08-04 14:45 ` Markus Armbruster
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 24/36] ahci: free irqs array marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 25/36] sd: free timer marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 26/36] qjson: free str marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 27/36] virtio-input: free config list marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 28/36] ipmi: free extern timer marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 29/36] usb: free USBDevice.strings marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 30/36] usb: free leaking path marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 31/36] bus: simplify name handling marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 32/36] tests: add qtest_add_data_func_full marcandre.lureau
2016-08-04 14:54 ` Markus Armbruster
2016-08-04 15:05 ` Marc-André Lureau
2016-08-05 7:12 ` Markus Armbruster
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 33/36] tests: pc-cpu-test leaks fixes marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 34/36] tests: fix rsp leak in postcopy-test marcandre.lureau
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 35/36] ahci: fix sglist leak on retry marcandre.lureau
2016-08-04 14:46 ` Markus Armbruster
2016-08-04 21:16 ` John Snow
2016-08-03 14:55 ` [Qemu-devel] [PATCH for-2.7 v3 36/36] tests: fix postcopy-test leaks marcandre.lureau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1223869164.1103430.1470322754832.JavaMail.zimbra@redhat.com \
--to=mlureau@redhat.com \
--cc=armbru@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).