qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc()
@ 2014-12-04 13:46 Markus Armbruster
  2014-12-04 13:46 ` [Qemu-devel] [PATCH 1/4] x86: Drop superfluous conditionals around g_free() Markus Armbruster
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Markus Armbruster @ 2014-12-04 13:46 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini, rth

Markus Armbruster (4):
  x86: Drop superfluous conditionals around g_free()
  x86: Fuse g_malloc(); memset() into g_malloc0()
  x86: Use g_new() & friends where that makes obvious sense
  x86: Drop some superfluous casts from void *

 hw/i386/pc.c            |  3 +--
 hw/i386/pc_sysfw.c      |  4 +---
 target-i386/arch_dump.c | 16 ++++------------
 target-i386/cpu.c       |  2 +-
 target-i386/kvm.c       |  4 ++--
 5 files changed, 9 insertions(+), 20 deletions(-)

-- 
1.9.3

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Qemu-devel] [PATCH 1/4] x86: Drop superfluous conditionals around g_free()
  2014-12-04 13:46 [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc() Markus Armbruster
@ 2014-12-04 13:46 ` Markus Armbruster
  2014-12-04 13:46 ` [Qemu-devel] [PATCH 2/4] x86: Fuse g_malloc(); memset() into g_malloc0() Markus Armbruster
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Markus Armbruster @ 2014-12-04 13:46 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini, rth

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 hw/i386/pc_sysfw.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c
index 75913c5..662d997 100644
--- a/hw/i386/pc_sysfw.c
+++ b/hw/i386/pc_sysfw.c
@@ -204,9 +204,7 @@ static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
         fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
         exit(1);
     }
-    if (filename) {
-        g_free(filename);
-    }
+    g_free(filename);
 
     /* map the last 128KB of the BIOS in ISA space */
     isa_bios_size = bios_size;
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Qemu-devel] [PATCH 2/4] x86: Fuse g_malloc(); memset() into g_malloc0()
  2014-12-04 13:46 [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc() Markus Armbruster
  2014-12-04 13:46 ` [Qemu-devel] [PATCH 1/4] x86: Drop superfluous conditionals around g_free() Markus Armbruster
@ 2014-12-04 13:46 ` Markus Armbruster
  2014-12-04 13:46 ` [Qemu-devel] [PATCH 3/4] x86: Use g_new() & friends where that makes obvious sense Markus Armbruster
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Markus Armbruster @ 2014-12-04 13:46 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini, rth

Coccinelle semantic patch:

    @@
    expression LHS, SZ;
    @@
    -       LHS = g_malloc(SZ);
    -       memset(LHS, 0, SZ);
    +       LHS = g_malloc0(SZ);

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 target-i386/arch_dump.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/target-i386/arch_dump.c b/target-i386/arch_dump.c
index 0bbed23..eccd803 100644
--- a/target-i386/arch_dump.c
+++ b/target-i386/arch_dump.c
@@ -78,9 +78,7 @@ static int x86_64_write_elf64_note(WriteCoreDumpFunction f,
     descsz = sizeof(x86_64_elf_prstatus);
     note_size = ((sizeof(Elf64_Nhdr) + 3) / 4 + (name_size + 3) / 4 +
                 (descsz + 3) / 4) * 4;
-    note = g_malloc(note_size);
-
-    memset(note, 0, note_size);
+    note = g_malloc0(note_size);
     note->n_namesz = cpu_to_le32(name_size);
     note->n_descsz = cpu_to_le32(descsz);
     note->n_type = cpu_to_le32(NT_PRSTATUS);
@@ -159,9 +157,7 @@ static int x86_write_elf64_note(WriteCoreDumpFunction f, CPUX86State *env,
     descsz = sizeof(x86_elf_prstatus);
     note_size = ((sizeof(Elf64_Nhdr) + 3) / 4 + (name_size + 3) / 4 +
                 (descsz + 3) / 4) * 4;
-    note = g_malloc(note_size);
-
-    memset(note, 0, note_size);
+    note = g_malloc0(note_size);
     note->n_namesz = cpu_to_le32(name_size);
     note->n_descsz = cpu_to_le32(descsz);
     note->n_type = cpu_to_le32(NT_PRSTATUS);
@@ -216,9 +212,7 @@ int x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
     descsz = sizeof(x86_elf_prstatus);
     note_size = ((sizeof(Elf32_Nhdr) + 3) / 4 + (name_size + 3) / 4 +
                 (descsz + 3) / 4) * 4;
-    note = g_malloc(note_size);
-
-    memset(note, 0, note_size);
+    note = g_malloc0(note_size);
     note->n_namesz = cpu_to_le32(name_size);
     note->n_descsz = cpu_to_le32(descsz);
     note->n_type = cpu_to_le32(NT_PRSTATUS);
@@ -345,9 +339,7 @@ static inline int cpu_write_qemu_note(WriteCoreDumpFunction f,
     }
     note_size = ((note_head_size + 3) / 4 + (name_size + 3) / 4 +
                 (descsz + 3) / 4) * 4;
-    note = g_malloc(note_size);
-
-    memset(note, 0, note_size);
+    note = g_malloc0(note_size);
     if (type == 0) {
         note32 = note;
         note32->n_namesz = cpu_to_le32(name_size);
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Qemu-devel] [PATCH 3/4] x86: Use g_new() & friends where that makes obvious sense
  2014-12-04 13:46 [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc() Markus Armbruster
  2014-12-04 13:46 ` [Qemu-devel] [PATCH 1/4] x86: Drop superfluous conditionals around g_free() Markus Armbruster
  2014-12-04 13:46 ` [Qemu-devel] [PATCH 2/4] x86: Fuse g_malloc(); memset() into g_malloc0() Markus Armbruster
@ 2014-12-04 13:46 ` Markus Armbruster
  2014-12-04 13:46 ` [Qemu-devel] [PATCH 4/4] x86: Drop some superfluous casts from void * Markus Armbruster
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Markus Armbruster @ 2014-12-04 13:46 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini, rth

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).

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 hw/i386/pc.c      | 3 +--
 target-i386/kvm.c | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index f31d55e..c0e55a6 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -602,8 +602,7 @@ int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
     }
 
     /* new "etc/e820" file -- include ram too */
-    e820_table = g_realloc(e820_table,
-                           sizeof(struct e820_entry) * (e820_entries+1));
+    e820_table = g_renew(struct e820_entry, e820_table, e820_entries + 1);
     e820_table[e820_entries].address = cpu_to_le64(address);
     e820_table[e820_entries].length = cpu_to_le64(length);
     e820_table[e820_entries].type = cpu_to_le32(type);
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index ccf36e8..c1559c4 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -277,7 +277,7 @@ static void kvm_hwpoison_page_add(ram_addr_t ram_addr)
             return;
         }
     }
-    page = g_malloc(sizeof(HWPoisonPage));
+    page = g_new(HWPoisonPage, 1);
     page->ram_addr = ram_addr;
     QLIST_INSERT_HEAD(&hwpoison_page_list, page, list);
 }
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Qemu-devel] [PATCH 4/4] x86: Drop some superfluous casts from void *
  2014-12-04 13:46 [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc() Markus Armbruster
                   ` (2 preceding siblings ...)
  2014-12-04 13:46 ` [Qemu-devel] [PATCH 3/4] x86: Use g_new() & friends where that makes obvious sense Markus Armbruster
@ 2014-12-04 13:46 ` Markus Armbruster
  2014-12-05 15:09 ` [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc() Eric Blake
  2014-12-05 15:40 ` Paolo Bonzini
  5 siblings, 0 replies; 7+ messages in thread
From: Markus Armbruster @ 2014-12-04 13:46 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini, rth

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 target-i386/cpu.c | 2 +-
 target-i386/kvm.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index e9df33e..e132c7e 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1530,7 +1530,7 @@ static char *x86_cpuid_get_vendor(Object *obj, Error **errp)
     CPUX86State *env = &cpu->env;
     char *value;
 
-    value = (char *)g_malloc(CPUID_VENDOR_SZ + 1);
+    value = g_malloc(CPUID_VENDOR_SZ + 1);
     x86_cpu_vendor_words2str(value, env->cpuid_vendor1, env->cpuid_vendor2,
                              env->cpuid_vendor3);
     return value;
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index c1559c4..7a2fda5 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -95,7 +95,7 @@ static struct kvm_cpuid2 *try_get_cpuid(KVMState *s, int max)
     int r, size;
 
     size = sizeof(*cpuid) + max * sizeof(*cpuid->entries);
-    cpuid = (struct kvm_cpuid2 *)g_malloc0(size);
+    cpuid = g_malloc0(size);
     cpuid->nent = max;
     r = kvm_ioctl(s, KVM_GET_SUPPORTED_CPUID, cpuid);
     if (r == 0 && cpuid->nent >= max) {
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc()
  2014-12-04 13:46 [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc() Markus Armbruster
                   ` (3 preceding siblings ...)
  2014-12-04 13:46 ` [Qemu-devel] [PATCH 4/4] x86: Drop some superfluous casts from void * Markus Armbruster
@ 2014-12-05 15:09 ` Eric Blake
  2014-12-05 15:40 ` Paolo Bonzini
  5 siblings, 0 replies; 7+ messages in thread
From: Eric Blake @ 2014-12-05 15:09 UTC (permalink / raw)
  To: Markus Armbruster, qemu-devel; +Cc: pbonzini, rth

[-- Attachment #1: Type: text/plain, Size: 719 bytes --]

On 12/04/2014 06:46 AM, Markus Armbruster wrote:
> Markus Armbruster (4):
>   x86: Drop superfluous conditionals around g_free()
>   x86: Fuse g_malloc(); memset() into g_malloc0()
>   x86: Use g_new() & friends where that makes obvious sense
>   x86: Drop some superfluous casts from void *

Reviewed-by: Eric Blake <eblake@redhat.com>

> 
>  hw/i386/pc.c            |  3 +--
>  hw/i386/pc_sysfw.c      |  4 +---
>  target-i386/arch_dump.c | 16 ++++------------
>  target-i386/cpu.c       |  2 +-
>  target-i386/kvm.c       |  4 ++--
>  5 files changed, 9 insertions(+), 20 deletions(-)
> 

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc()
  2014-12-04 13:46 [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc() Markus Armbruster
                   ` (4 preceding siblings ...)
  2014-12-05 15:09 ` [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc() Eric Blake
@ 2014-12-05 15:40 ` Paolo Bonzini
  5 siblings, 0 replies; 7+ messages in thread
From: Paolo Bonzini @ 2014-12-05 15:40 UTC (permalink / raw)
  To: Markus Armbruster, qemu-devel; +Cc: rth



On 04/12/2014 14:46, Markus Armbruster wrote:
> Markus Armbruster (4):
>   x86: Drop superfluous conditionals around g_free()
>   x86: Fuse g_malloc(); memset() into g_malloc0()
>   x86: Use g_new() & friends where that makes obvious sense
>   x86: Drop some superfluous casts from void *
> 
>  hw/i386/pc.c            |  3 +--
>  hw/i386/pc_sysfw.c      |  4 +---
>  target-i386/arch_dump.c | 16 ++++------------
>  target-i386/cpu.c       |  2 +-
>  target-i386/kvm.c       |  4 ++--
>  5 files changed, 9 insertions(+), 20 deletions(-)
> 

Applied, thanks.

Paolo

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-12-05 15:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-04 13:46 [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc() Markus Armbruster
2014-12-04 13:46 ` [Qemu-devel] [PATCH 1/4] x86: Drop superfluous conditionals around g_free() Markus Armbruster
2014-12-04 13:46 ` [Qemu-devel] [PATCH 2/4] x86: Fuse g_malloc(); memset() into g_malloc0() Markus Armbruster
2014-12-04 13:46 ` [Qemu-devel] [PATCH 3/4] x86: Use g_new() & friends where that makes obvious sense Markus Armbruster
2014-12-04 13:46 ` [Qemu-devel] [PATCH 4/4] x86: Drop some superfluous casts from void * Markus Armbruster
2014-12-05 15:09 ` [Qemu-devel] [PATCH 0/4] x86: Trivial cleanups around g_malloc() Eric Blake
2014-12-05 15:40 ` Paolo Bonzini

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).