diff mbox series

[for-4.0,3/9] ppc405_boards: use g_new(T, n) instead of g_malloc(sizeof(T) * n)

Message ID 154332392916.541746.3772338098005086592.stgit@bahia.lab.toulouse-stg.fr.ibm.com
State New
Headers show
Series ppc: get rid of g_malloc(sizeof(T) * n) | expand

Commit Message

Greg Kurz Nov. 27, 2018, 1:05 p.m. UTC
Because it is a recommended coding practice (see HACKING).

Signed-off-by: Greg Kurz <groug@kaod.org>
---
 hw/ppc/ppc405_boards.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Philippe Mathieu-Daudé Nov. 27, 2018, 1:41 p.m. UTC | #1
On 27/11/18 14:05, Greg Kurz wrote:
> Because it is a recommended coding practice (see HACKING).
> 
> Signed-off-by: Greg Kurz <groug@kaod.org>
> ---
>  hw/ppc/ppc405_boards.c |    4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/ppc/ppc405_boards.c b/hw/ppc/ppc405_boards.c
> index 3be3fe4432b4..f35b412c88dd 100644
> --- a/hw/ppc/ppc405_boards.c
> +++ b/hw/ppc/ppc405_boards.c
> @@ -149,7 +149,7 @@ static void ref405ep_init(MachineState *machine)
>      MemoryRegion *bios;
>      MemoryRegion *sram = g_new(MemoryRegion, 1);
>      ram_addr_t bdloc;
> -    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
> +    MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
>      hwaddr ram_bases[2], ram_sizes[2];
>      target_ulong sram_size;
>      long bios_size;
> @@ -446,7 +446,7 @@ static void taihu_405ep_init(MachineState *machine)
>      qemu_irq *pic;
>      MemoryRegion *sysmem = get_system_memory();
>      MemoryRegion *bios;
> -    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
> +    MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
>      MemoryRegion *ram = g_malloc0(sizeof(*ram));

Why not change both lines here?

       MemoryRegion *ram = g_new0(MemoryRegion, 1);

>      hwaddr ram_bases[2], ram_sizes[2];
>      long bios_size;
> 
>
Greg Kurz Nov. 27, 2018, 1:52 p.m. UTC | #2
On Tue, 27 Nov 2018 14:41:53 +0100
Philippe Mathieu-Daudé <philmd@redhat.com> wrote:

> On 27/11/18 14:05, Greg Kurz wrote:
> > Because it is a recommended coding practice (see HACKING).
> > 
> > Signed-off-by: Greg Kurz <groug@kaod.org>
> > ---
> >  hw/ppc/ppc405_boards.c |    4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/hw/ppc/ppc405_boards.c b/hw/ppc/ppc405_boards.c
> > index 3be3fe4432b4..f35b412c88dd 100644
> > --- a/hw/ppc/ppc405_boards.c
> > +++ b/hw/ppc/ppc405_boards.c
> > @@ -149,7 +149,7 @@ static void ref405ep_init(MachineState *machine)
> >      MemoryRegion *bios;
> >      MemoryRegion *sram = g_new(MemoryRegion, 1);
> >      ram_addr_t bdloc;
> > -    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
> > +    MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
> >      hwaddr ram_bases[2], ram_sizes[2];
> >      target_ulong sram_size;
> >      long bios_size;
> > @@ -446,7 +446,7 @@ static void taihu_405ep_init(MachineState *machine)
> >      qemu_irq *pic;
> >      MemoryRegion *sysmem = get_system_memory();
> >      MemoryRegion *bios;
> > -    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
> > +    MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
> >      MemoryRegion *ram = g_malloc0(sizeof(*ram));  
> 
> Why not change both lines here?
> 
>        MemoryRegion *ram = g_new0(MemoryRegion, 1);
> 

Because HACKING says:

Declarations like T *v = g_malloc(sizeof(*v)) are acceptable, though.

but if there's a consensus on fixing these as well, I'll happily do
it :)

> >      hwaddr ram_bases[2], ram_sizes[2];
> >      long bios_size;
> > 
> >
Philippe Mathieu-Daudé Nov. 27, 2018, 2:03 p.m. UTC | #3
On 27/11/18 14:52, Greg Kurz wrote:
> On Tue, 27 Nov 2018 14:41:53 +0100
> Philippe Mathieu-Daudé <philmd@redhat.com> wrote:
> 
>> On 27/11/18 14:05, Greg Kurz wrote:
>>> Because it is a recommended coding practice (see HACKING).
>>>
>>> Signed-off-by: Greg Kurz <groug@kaod.org>
>>> ---
>>>  hw/ppc/ppc405_boards.c |    4 ++--
>>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/hw/ppc/ppc405_boards.c b/hw/ppc/ppc405_boards.c
>>> index 3be3fe4432b4..f35b412c88dd 100644
>>> --- a/hw/ppc/ppc405_boards.c
>>> +++ b/hw/ppc/ppc405_boards.c
>>> @@ -149,7 +149,7 @@ static void ref405ep_init(MachineState *machine)
>>>      MemoryRegion *bios;
>>>      MemoryRegion *sram = g_new(MemoryRegion, 1);
>>>      ram_addr_t bdloc;
>>> -    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
>>> +    MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
>>>      hwaddr ram_bases[2], ram_sizes[2];
>>>      target_ulong sram_size;
>>>      long bios_size;
>>> @@ -446,7 +446,7 @@ static void taihu_405ep_init(MachineState *machine)
>>>      qemu_irq *pic;
>>>      MemoryRegion *sysmem = get_system_memory();
>>>      MemoryRegion *bios;
>>> -    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
>>> +    MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
>>>      MemoryRegion *ram = g_malloc0(sizeof(*ram));  
>>
>> Why not change both lines here?
>>
>>        MemoryRegion *ram = g_new0(MemoryRegion, 1);
>>
> 
> Because HACKING says:
> 
> Declarations like T *v = g_malloc(sizeof(*v)) are acceptable, though.

Yes, I agree, but it is weird to have 2 similar lines and only change 1,
for code consistency I'd change both...

> 
> but if there's a consensus on fixing these as well, I'll happily do
> it :)
> 
>>>      hwaddr ram_bases[2], ram_sizes[2];
>>>      long bios_size;
>>>
>>>   
>
diff mbox series

Patch

diff --git a/hw/ppc/ppc405_boards.c b/hw/ppc/ppc405_boards.c
index 3be3fe4432b4..f35b412c88dd 100644
--- a/hw/ppc/ppc405_boards.c
+++ b/hw/ppc/ppc405_boards.c
@@ -149,7 +149,7 @@  static void ref405ep_init(MachineState *machine)
     MemoryRegion *bios;
     MemoryRegion *sram = g_new(MemoryRegion, 1);
     ram_addr_t bdloc;
-    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
+    MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
     hwaddr ram_bases[2], ram_sizes[2];
     target_ulong sram_size;
     long bios_size;
@@ -446,7 +446,7 @@  static void taihu_405ep_init(MachineState *machine)
     qemu_irq *pic;
     MemoryRegion *sysmem = get_system_memory();
     MemoryRegion *bios;
-    MemoryRegion *ram_memories = g_malloc(2 * sizeof(*ram_memories));
+    MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
     MemoryRegion *ram = g_malloc0(sizeof(*ram));
     hwaddr ram_bases[2], ram_sizes[2];
     long bios_size;