qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stuart Brady <sdb@zubnet.me.uk>
To: qemu-devel@nongnu.org
Cc: Stuart Brady <sdb@zubnet.me.uk>
Subject: [Qemu-devel] [PATCH 5/5] Convert remaining calls to g_malloc(sizeof(type)) to g_new()
Date: Thu, 20 Oct 2011 09:03:40 +0100	[thread overview]
Message-ID: <1319097820-4788-5-git-send-email-sdb@zubnet.me.uk> (raw)
In-Reply-To: <1319097820-4788-1-git-send-email-sdb@zubnet.me.uk>

Convert calls to g_malloc() and g_malloc0() to g_new() and g_new0()
respectively, in cases where the size passed to g_malloc() is specified
as sizeof(type).

Coccinelle did not match these when matching assignments involving an
expression corresponding to the type used for determining the size to
allocate.  Such cases deserve more careful review, in case the types do
not match, so are submitted separately.

This was achieved using Coccinelle with the following semantic patch:

@@ type T; @@
-g_malloc(sizeof(T))
+g_new(T, 1)

@@ type T; @@
-g_malloc0(sizeof(T))
+g_new0(T, 1)

@@ type T; expression N; @@
-g_malloc(sizeof(T) * N)
+g_new(T, N)

@@ type T; expression N; @@
-g_malloc0(sizeof(T) * N)
+g_new0(T, N)

Note: changes to omap_l4_io_readb_fn, etc in hw/omap_l4.c have been
withheld since the size of the wrong type of pointer is specified.
This should not cause a problem as all types of pointer will have the
same size on any architecture that we care about, but this should be
fixed properly in a separate patch.

Also note that the allocation of seek_data in ga/guest-agent-commands.c
is not fixed because it should use GuestFileSeek, not GuestFileRead.
This should be fixed separately.

Finally, cris-dis.c appears to perform allocations using the wrong
pointer type.  This should also be fixed separately.

Signed-off-by: Stuart Brady <sdb@zubnet.me.uk>
---
 block/qcow2-snapshot.c |    2 +-
 block/vvfat.c          |    2 +-
 bsd-user/syscall.c     |    2 +-
 cpus.c                 |    8 ++++----
 hw/ide/ahci.c          |    2 +-
 hw/intel-hda.c         |    2 +-
 hw/lsi53c895a.c        |    2 +-
 hw/virtio-serial-bus.c |    4 ++--
 hw/virtio.c            |    2 +-
 linux-user/elfload.c   |    2 +-
 monitor.c              |    2 +-
 savevm.c               |    4 ++--
 test-qmp-commands.c    |    2 +-
 ui/vnc.c               |    2 +-
 xen-all.c              |    2 +-
 15 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c
index 4c170d86..1c2470d 100644
--- a/block/qcow2-snapshot.c
+++ b/block/qcow2-snapshot.c
@@ -76,7 +76,7 @@ int qcow2_read_snapshots(BlockDriverState *bs)
     }
 
     offset = s->snapshots_offset;
-    s->snapshots = g_malloc0(s->nb_snapshots * sizeof(QCowSnapshot));
+    s->snapshots = g_new0(QCowSnapshot, s->nb_snapshots);
     for(i = 0; i < s->nb_snapshots; i++) {
         offset = align_offset(offset, 8);
         if (bdrv_pread(bs->file, offset, &h, sizeof(h)) != sizeof(h))
diff --git a/block/vvfat.c b/block/vvfat.c
index 7e9e35a..1ed9641 100644
--- a/block/vvfat.c
+++ b/block/vvfat.c
@@ -2782,7 +2782,7 @@ static int enable_write_target(BDRVVVFATState *s)
 
     s->bs->backing_hd = calloc(sizeof(BlockDriverState), 1);
     s->bs->backing_hd->drv = &vvfat_write_target;
-    s->bs->backing_hd->opaque = g_malloc(sizeof(void*));
+    s->bs->backing_hd->opaque = g_new(void *, 1);
     *(void**)s->bs->backing_hd->opaque = s;
 
     return 0;
diff --git a/bsd-user/syscall.c b/bsd-user/syscall.c
index 18b43f1..e75ca81 100644
--- a/bsd-user/syscall.c
+++ b/bsd-user/syscall.c
@@ -231,7 +231,7 @@ static abi_long do_freebsd_sysctl(abi_ulong namep, int32_t namelen, abi_ulong ol
     void *hnamep, *holdp, *hnewp = NULL;
     size_t holdlen;
     abi_ulong oldlen = 0;
-    int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
+    int32_t *snamep = g_new(int32_t, namelen), *p, *q, i;
     uint32_t kind = 0;
 
     if (oldlenp)
diff --git a/cpus.c b/cpus.c
index 8978779..cbe7531 100644
--- a/cpus.c
+++ b/cpus.c
@@ -818,8 +818,8 @@ static void qemu_tcg_init_vcpu(void *_env)
 
     /* share a single thread for all cpus with TCG */
     if (!tcg_cpu_thread) {
-        env->thread = g_malloc0(sizeof(QemuThread));
-        env->halt_cond = g_malloc0(sizeof(QemuCond));
+        env->thread = g_new0(QemuThread, 1);
+        env->halt_cond = g_new0(QemuCond, 1);
         qemu_cond_init(env->halt_cond);
         tcg_halt_cond = env->halt_cond;
         qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
@@ -835,8 +835,8 @@ static void qemu_tcg_init_vcpu(void *_env)
 
 static void qemu_kvm_start_vcpu(CPUState *env)
 {
-    env->thread = g_malloc0(sizeof(QemuThread));
-    env->halt_cond = g_malloc0(sizeof(QemuCond));
+    env->thread = g_new0(QemuThread, 1);
+    env->halt_cond = g_new0(QemuCond, 1);
     qemu_cond_init(env->halt_cond);
     qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
     while (env->created == 0) {
diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index 1c7e3a0..7f176e3 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -1164,7 +1164,7 @@ void ahci_init(AHCIState *s, DeviceState *qdev, int ports)
     int i;
 
     s->ports = ports;
-    s->dev = g_malloc0(sizeof(AHCIDevice) * ports);
+    s->dev = g_new0(AHCIDevice, ports);
     ahci_reg_init(s);
     /* XXX BAR size should be 1k, but that breaks, so bump it to 4k for now */
     memory_region_init_io(&s->mem, &ahci_mem_ops, s, "ahci", AHCI_MEM_BAR_SIZE);
diff --git a/hw/intel-hda.c b/hw/intel-hda.c
index 4272204..47a35e4 100644
--- a/hw/intel-hda.c
+++ b/hw/intel-hda.c
@@ -468,7 +468,7 @@ static void intel_hda_parse_bdl(IntelHDAState *d, IntelHDAStream *st)
     addr = intel_hda_addr(st->bdlp_lbase, st->bdlp_ubase);
     st->bentries = st->lvi +1;
     g_free(st->bpl);
-    st->bpl = g_malloc(sizeof(bpl) * st->bentries);
+    st->bpl = g_new(bpl, st->bentries);
     for (i = 0; i < st->bentries; i++, addr += 16) {
         cpu_physical_memory_read(addr, buf, 16);
         st->bpl[i].addr  = le64_to_cpu(*(uint64_t *)buf);
diff --git a/hw/lsi53c895a.c b/hw/lsi53c895a.c
index e077ec0..110fafd 100644
--- a/hw/lsi53c895a.c
+++ b/hw/lsi53c895a.c
@@ -778,7 +778,7 @@ static void lsi_do_command(LSIState *s)
     }
 
     assert(s->current == NULL);
-    s->current = g_malloc0(sizeof(lsi_request));
+    s->current = g_new0(lsi_request, 1);
     s->current->tag = s->select_tag;
     s->current->req = scsi_req_new(dev, s->current->tag, s->current_lun, buf,
                                    s->current);
diff --git a/hw/virtio-serial-bus.c b/hw/virtio-serial-bus.c
index a4825b9..9842576 100644
--- a/hw/virtio-serial-bus.c
+++ b/hw/virtio-serial-bus.c
@@ -862,8 +862,8 @@ VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
     QTAILQ_INIT(&vser->ports);
 
     vser->bus.max_nr_ports = conf->max_virtserial_ports;
-    vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
-    vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
+    vser->ivqs = g_new(VirtQueue *, conf->max_virtserial_ports);
+    vser->ovqs = g_new(VirtQueue *, conf->max_virtserial_ports);
 
     /* Add a queue for host to guest transfers for port 0 (backward compat) */
     vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
diff --git a/hw/virtio.c b/hw/virtio.c
index 7011b5b..c03c50a 100644
--- a/hw/virtio.c
+++ b/hw/virtio.c
@@ -871,7 +871,7 @@ VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
     vdev->isr = 0;
     vdev->queue_sel = 0;
     vdev->config_vector = VIRTIO_NO_VECTOR;
-    vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
+    vdev->vq = g_new0(VirtQueue, VIRTIO_PCI_QUEUE_MAX);
     vdev->vm_running = runstate_is_running();
     for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
         vdev->vq[i].vector = VIRTIO_NO_VECTOR;
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 56abc8c..fdd83cf 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2488,7 +2488,7 @@ static int fill_note_info(struct elf_note_info *info,
 
     QTAILQ_INIT(&info->thread_list);
 
-    info->notes = g_malloc0(NUMNOTES * sizeof (struct memelfnote));
+    info->notes = g_new0(struct memelfnote, NUMNOTES);
     if (info->notes == NULL)
         return (-ENOMEM);
     info->prstatus = g_malloc0(sizeof (*info->prstatus));
diff --git a/monitor.c b/monitor.c
index 87ce79f..fe45c24 100644
--- a/monitor.c
+++ b/monitor.c
@@ -5124,7 +5124,7 @@ void monitor_init(CharDriverState *chr, int flags)
     }
 
     if (monitor_ctrl_mode(mon)) {
-        mon->mc = g_malloc0(sizeof(MonitorControl));
+        mon->mc = g_new0(MonitorControl, 1);
         /* Control mode requires special handlers */
         qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
                               monitor_control_event, mon);
diff --git a/savevm.c b/savevm.c
index 51f71f7..cce4e7d 100644
--- a/savevm.c
+++ b/savevm.c
@@ -1132,7 +1132,7 @@ int register_savevm_live(DeviceState *dev,
             pstrcat(se->idstr, sizeof(se->idstr), "/");
             g_free(id);
 
-            se->compat = g_malloc0(sizeof(CompatEntry));
+            se->compat = g_new0(CompatEntry, 1);
             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
             se->compat->instance_id = instance_id == -1 ?
                          calculate_compat_instance_id(idstr) : instance_id;
@@ -1243,7 +1243,7 @@ int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
             pstrcat(se->idstr, sizeof(se->idstr), "/");
             g_free(id);
 
-            se->compat = g_malloc0(sizeof(CompatEntry));
+            se->compat = g_new0(CompatEntry, 1);
             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
             se->compat->instance_id = instance_id == -1 ?
                          calculate_compat_instance_id(vmsd->name) : instance_id;
diff --git a/test-qmp-commands.c b/test-qmp-commands.c
index f44b6df..d0d8a83 100644
--- a/test-qmp-commands.c
+++ b/test-qmp-commands.c
@@ -120,7 +120,7 @@ static void test_dealloc_types(void)
 
     ud1list = g_new0(UserDefOneList, 1);
     ud1list->value = ud1a;
-    ud1list->next = g_malloc0(sizeof(UserDefOneList));
+    ud1list->next = g_new0(UserDefOneList, 1);
     ud1list->next->value = ud1b;
 
     qapi_free_UserDefOneList(ud1list);
diff --git a/ui/vnc.c b/ui/vnc.c
index 4e28e34..51235dd 100644
--- a/ui/vnc.c
+++ b/ui/vnc.c
@@ -2515,7 +2515,7 @@ static void vnc_connect(VncDisplay *vd, int csock, int skipauth)
 
     vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect));
     for (i = 0; i < VNC_STAT_ROWS; ++i) {
-        vs->lossy_rect[i] = g_malloc0(VNC_STAT_COLS * sizeof (uint8_t));
+        vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS);
     }
 
     VNC_DEBUG("New client on socket %d\n", csock);
diff --git a/xen-all.c b/xen-all.c
index 415fde6..ec001aa 100644
--- a/xen-all.c
+++ b/xen-all.c
@@ -924,7 +924,7 @@ int xen_hvm_init(void)
         hw_error("map buffered IO page returned error %d", errno);
     }
 
-    state->ioreq_local_port = g_malloc0(smp_cpus * sizeof (evtchn_port_t));
+    state->ioreq_local_port = g_new0(evtchn_port_t, smp_cpus);
 
     /* FIXME: how about if we overflow the page here? */
     for (i = 0; i < smp_cpus; i++) {
-- 
1.7.4.1

  parent reply	other threads:[~2011-10-20  8:04 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-20  8:03 [Qemu-devel] [PATCH 1/5] Convert calls to g_malloc() using casts to g_new() Stuart Brady
2011-10-20  8:03 ` [Qemu-devel] [PATCH 2/5] Convert use of ptr = g_malloc(sizeof(*ptr)) " Stuart Brady
2011-10-20  8:03 ` [Qemu-devel] [PATCH 3/5] Convert use of ptr = g_malloc(sizeof(type)) " Stuart Brady
2011-10-20  8:03 ` [Qemu-devel] [PATCH 4/5] Convert remaining calls to g_malloc(sizeof(type)) using casts " Stuart Brady
2011-10-20  8:03 ` Stuart Brady [this message]
2011-10-20  9:05   ` [Qemu-devel] [PATCH 5/5] Convert remaining calls to g_malloc(sizeof(type)) " Paolo Bonzini
2011-10-21  0:26     ` Stuart Brady
2011-10-21  7:37       ` Paolo Bonzini
2011-10-21 17:56         ` Stuart Brady
2011-10-23 13:45       ` Blue Swirl
2011-10-20  8:55 ` [Qemu-devel] [PATCH 1/5] Convert calls to g_malloc() using casts " Paolo Bonzini
2011-10-21  0:34   ` Stuart Brady

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1319097820-4788-5-git-send-email-sdb@zubnet.me.uk \
    --to=sdb@zubnet.me.uk \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).