Skip to contentSkip to conversation

x86: Trivial cleanups around g_malloc()

PATCH Applied Markus Armbruster 3 participants 7 messages Dec 4, 2014 – Dec 5, 2014

lore
Share & tools
lore
#4 lore raw

PATCH 4/4 x86: Drop some superfluous casts from void *

In reply to Markus Armbruster
---
 target-i386/cpu.c | 2 +-
 target-i386/kvm.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
target-i386/cpu.c +1 -1
index e9df33e..e132c7e 100644
@@ -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;
target-i386/kvm.c +1 -1
index c1559c4..7a2fda5 100644
@@ -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) {
Signature · 1 line
1.9.3
#0 lore raw

cover x86: Trivial cleanups around g_malloc()

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(-)
Signature · 1 line
1.9.3
#2 lore raw

PATCH 2/4 x86: Fuse g_malloc(); memset() into g_malloc0()

Coccinelle semantic patch:

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

---
 target-i386/arch_dump.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
target-i386/arch_dump.c +4 -12
index 0bbed23..eccd803 100644
@@ -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);
Signature · 1 line
1.9.3
#3 lore raw

PATCH 3/4 x86: Use g_new() & friends where that makes obvious sense

In reply to Markus Armbruster

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

---
 hw/i386/pc.c      | 3 +--
 target-i386/kvm.c | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
hw/i386/pc.c +1 -2
index f31d55e..c0e55a6 100644
@@ -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);
target-i386/kvm.c +1 -1
index ccf36e8..c1559c4 100644
@@ -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);
 }
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/4 x86: Drop superfluous conditionals around g_free()

In reply to Markus Armbruster
---
 hw/i386/pc_sysfw.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hw/i386/pc_sysfw.c +1 -3
index 75913c5..662d997 100644
@@ -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;
Signature · 1 line
1.9.3
Reviewed-by on the v1 series
#5 lore raw
In reply to Markus Armbruster
Quoted 5 lines · Markus Armbruster

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 *

Quoted 8 lines
 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(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw
In reply to Markus Armbruster
Quoted 6 lines · Markus Armbruster

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 *

Quoted 7 lines
 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