Received: from eggs.gnu.org ([2001:4830:134:3::10]:54101) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZUVkL-00050o-JV for qemu-devel@nongnu.org; Wed, 26 Aug 2015 04:07:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZUVkI-0008E1-SV for qemu-devel@nongnu.org; Wed, 26 Aug 2015 04:07:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44741) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZUVkI-0008Dl-O7 for qemu-devel@nongnu.org; Wed, 26 Aug 2015 04:07:46 -0400 From: Markus Armbruster References: <1440524394-15640-1-git-send-email-armbru@redhat.com> <55DCBF79.7040709@redhat.com> Date: Wed, 26 Aug 2015 09:44:26 +0200 In-Reply-To: <55DCBF79.7040709@redhat.com> (Eric Blake's message of "Tue, 25 Aug 2015 13:18:17 -0600") Message-ID: <87bndufq91.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH] arm: Use g_new() & friends where that makes obvious sense List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: peter.maydell@linaro.org, peter.crosthwaite@xilinx.com, qemu-devel@nongnu.org Eric Blake writes: > On 08/25/2015 11:39 AM, Markus Armbruster wrote: >> g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, >> for two reasons. One, it catches multiplication overflowing size_t. >> Two, it returns T * rather than void *, which lets the compiler catch >> more type errors. >> >> This commit only touches allocations with size arguments of the form >> sizeof(T). >> >> Coccinelle semantic patch: >> >> @@ >> type T; >> @@ >> -g_malloc(sizeof(T)) >> +g_new(T, 1) >> @@ >> type T; > > I find it slightly easier to read coccinelle if there is a blank line > before every second @@, to call attention to metatype vs. changes > (coccinelle doesn't care either way). > > And it's probably possible to write the coccinelle more succinctly, by > grouping similar rules together using something like this (although I > didn't actually test it): > > @@ > type T; > @@ > ( > g_malloc > | > g_try_malloc > | > g_malloc0 > | > g_try_malloc0 > ) > -(sizeof(T)) > +(T, 1) > > but it doesn't matter in the long run. I tend to write really stupid semantic patches, because when I try to write a clever one, I usually relearn I'm not nearly clever enough for that. Amazing tool all the same. >> Signed-off-by: Markus Armbruster >> --- > > Both the coccinelle rule and the resulting conversion look sane. > Reviewed-by: Eric Blake > >> +++ b/hw/gpio/omap_gpio.c >> @@ -710,8 +710,8 @@ static int omap2_gpio_init(SysBusDevice *sbd) >> } else { >> s->modulecount = 6; >> } >> - s->modules = g_malloc0(s->modulecount * sizeof(struct omap2_gpio_s)); >> - s->handler = g_malloc0(s->modulecount * 32 * sizeof(qemu_irq)); >> + s->modules = g_new0(struct omap2_gpio_s, s->modulecount); >> + s->handler = g_new0(qemu_irq, s->modulecount * 32); >> qdev_init_gpio_in(dev, omap2_gpio_set, s->modulecount * 32); >> qdev_init_gpio_out(dev, s->handler, s->modulecount * 32); > > Thankfully, the '* 32' here does not overflow even pre-patch, since > s->modulecount was set to a maximum of 6 in the preceding if/else block. Thanks!