From: Stuart Brady <sdb@zubnet.me.uk>
To: qemu-devel@nongnu.org
Cc: Blue Swirl <blauwirbel@gmail.com>, Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [Qemu-devel] [PATCH 5/5] Convert remaining calls to g_malloc(sizeof(type)) to g_new()
Date: Fri, 21 Oct 2011 18:56:24 +0100 [thread overview]
Message-ID: <20111021175624.GA10163@zubnet.me.uk> (raw)
In-Reply-To: <4EA1211E.2020709@redhat.com>
On Fri, Oct 21, 2011 at 09:37:02AM +0200, Paolo Bonzini wrote:
> On 10/21/2011 02:26 AM, Stuart Brady wrote:
> >>> They all look okay, perhaps the include path you passed to
> >>> Coccinelle is incomplete?
> >Ah, good point! I'm not sure what include dirs are needed, though...
> >anyone have any advice?
> >
> >Blue Swirl, I gather you're one of the few other people to have used
> >Coccinelle with Qemu's source...
>
> I played a bit yesterday and it turns out that Coccinelle is a bit
> limited WRT handling headers, because they are very expensive. I
> used "-I . -I +build -I hw" but it didn't help much.
>
> Stuart/Blue, do you have a macro file? Mine was simply "#define
> coroutine_fn".
I didn't even have that, but Coccinelle didn't seem to mind...
It did occur to me that since a lot of Qemu's source is recompiled with
different macro definitions for different targets, we need to be really
careful about what we do regarding includes. Hopefully the names of
types that are used won't vary between targets, though.
Submitting what Coccinelle could process successfully and fixing up the
rest manually seemed reasonable, but I'd like to be as confident as
possible of these changes.
BTW, I'd thought that noone would ever do E = (T *)g_malloc(sizeof(*E)),
but from looking hw/blizzard.c, hw/cbus.c and hw/nseries.c, it seems
that this isn't quite the case afterall! I'll be sure to include this
in my second attempt, once QEMU 1.0 has been released.
One thing that did not occur to me is use of E = malloc(sizeof(*E1)) or
E = malloc(sizeof(T1)) where E is of type void *, but E1 or T1 is not
what was intended.
I'm also somewhat astonished to find that sizeof(void) and sizeof(*E)
where E is of type void * both compile! It would probably make sense to
check for these.
Any remaining calls to g_malloc() would be then be reviewed to make sure
that they're all correct.
We could also perhaps search for places where free() is called on memory
that is allocated with g_malloc(), as g_free() should be used instead.
---
Some background on my thinking before sending the patch series:
(T *)g_malloc(sizeof(T)) can obviously be safely replaced with
g_new(T, 1) since that's what g_new(T, 1) expands to.
Replacing E = g_malloc(sizeof(*E)) with E = g_new(T, 1) adds a cast, but
the cast does not provide any extra safety, since sizeof(*T) is pretty
much certain to be the correct size (unless T = void *). There seems
to be some agreement that this is more readable, though.
Replacing E = g_malloc(sizeof(T)) without a cast with E = g_new(T, 1)
effectively just adds a cast to T *, which might result in additional
compilation warnings (which are turned into errors) but should have no
other effect, so this should be perfectly safe.
Other cases where g_malloc(sizeof(*E)) or g_malloc(sizeof(T)) is used
will either be due to Coccinelle not understanding the types, or due to
a bug in Qemu, and both of these cases need special consideration.
Cheers,
--
Stuart
next prev parent reply other threads:[~2011-10-21 17:56 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-10-20 8:03 [Qemu-devel] [PATCH 1/5] Convert calls to g_malloc() using casts to g_new() Stuart Brady
2011-10-20 8:03 ` [Qemu-devel] [PATCH 2/5] Convert use of ptr = g_malloc(sizeof(*ptr)) " Stuart Brady
2011-10-20 8:03 ` [Qemu-devel] [PATCH 3/5] Convert use of ptr = g_malloc(sizeof(type)) " Stuart Brady
2011-10-20 8:03 ` [Qemu-devel] [PATCH 4/5] Convert remaining calls to g_malloc(sizeof(type)) using casts " Stuart Brady
2011-10-20 8:03 ` [Qemu-devel] [PATCH 5/5] Convert remaining calls to g_malloc(sizeof(type)) " Stuart Brady
2011-10-20 9:05 ` Paolo Bonzini
2011-10-21 0:26 ` Stuart Brady
2011-10-21 7:37 ` Paolo Bonzini
2011-10-21 17:56 ` Stuart Brady [this message]
2011-10-23 13:45 ` Blue Swirl
2011-10-20 8:55 ` [Qemu-devel] [PATCH 1/5] Convert calls to g_malloc() using casts " Paolo Bonzini
2011-10-21 0:34 ` Stuart Brady
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=20111021175624.GA10163@zubnet.me.uk \
--to=sdb@zubnet.me.uk \
--cc=blauwirbel@gmail.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).