On Tue, Sep 5, 2017 at 2:41 PM Philippe Mathieu-Daudé <
address@hidden> wrote:
Hi Marc-André,
On 09/05/2017 09:14 AM, Marc-André Lureau wrote:
> Hi,
>
> I have a series of changes generated with clang-tidy qemu [1] pending
> for review [2].
>
> It translates calloc/*malloc*/*realloc() calls to
> g_new/g_newa/g_new0/g_renew() where the argument is a sizeof(T) [* N].
>
> This is the first commit to give you an idea:
>
> diff --git a/hw/timer/arm_timer.c b/hw/timer/arm_timer.c
> index 98fddd7ac1..75affcb8a6 100644
> --- a/hw/timer/arm_timer.c
> +++ b/hw/timer/arm_timer.c
> @@ -166,7 +166,7 @@ static arm_timer_state *arm_timer_init(uint32_t freq)
> arm_timer_state *s;
> QEMUBH *bh;
>
> - s = (arm_timer_state *)g_malloc0(sizeof(arm_timer_state));
> + s = g_new0(arm_timer_state, 1);
> s->freq = freq;
> s->control = TIMER_CTRL_IE;
>
> g_new() advantages (from glib doc):
> - the returned pointer is cast to a pointer to the given type.
> - care is taken to avoid overflow when calculating the size of the
> allocated block.
>
> But it is also shorter&nicer :)
>
> I have not included in the first batch the opportunity to also
> translate: alloc(sizeof(*p) * x) to g_new(typeof(*p), x), since it is
> arguably not much nicer. But for consistency, I think it would be good
"avoid overflow when calculating the size" sounds nicer.
> to use g_new(). What do you think?
Dunno if useful, but Juan Quintela had some issue:
http://lists.nongnu.org/archive/html/qemu-devel/2017-09/msg00085.html
Thanks for pointing this out.
It looks like the last iteration didn't change it after all: