Patchwork [1/5] Convert calls to g_malloc() using casts to g_new()

login
register
mail settings
Submitter Stuart Brady
Date Oct. 20, 2011, 8:03 a.m.
Message ID <1319097820-4788-1-git-send-email-sdb@zubnet.me.uk>
Download mbox | patch
Permalink /patch/120755/
State New
Headers show

Comments

Stuart Brady - Oct. 20, 2011, 8:03 a.m.
Convert calls to g_malloc() and g_malloc0() to g_new() and g_new0()
respectively, in cases where the return value is casted to the same
type as specified using sizeof() in the parameter to g_malloc() and
assigned to a variable of that type.

This was achieved using Coccinelle with the following semantic patch:

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

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

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

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

Signed-off-by: Stuart Brady <sdb@zubnet.me.uk>
---
 console.c                  |    6 +++---
 gdbstub.c                  |    2 +-
 hw/arm_timer.c             |    2 +-
 hw/bt-hci-csr.c            |    3 +--
 hw/ccid-card-emulated.c    |    4 ++--
 hw/ide/microdrive.c        |    2 +-
 hw/irq.c                   |    4 ++--
 hw/ivshmem.c               |    3 +--
 hw/mcf5206.c               |    4 ++--
 hw/mcf5208.c               |    2 +-
 hw/mcf_fec.c               |    2 +-
 hw/mips_malta.c            |    2 +-
 hw/omap1.c                 |   30 ++++++++++--------------------
 hw/omap2.c                 |   15 +++++----------
 hw/omap_clk.c              |    2 +-
 hw/omap_dma.c              |    6 ++----
 hw/omap_dss.c              |    3 +--
 hw/omap_gpmc.c             |    3 +--
 hw/omap_gptimer.c          |    3 +--
 hw/omap_i2c.c              |    6 ++----
 hw/omap_lcdc.c             |    3 +--
 hw/omap_mmc.c              |    6 ++----
 hw/omap_sdrc.c             |    3 +--
 hw/omap_spi.c              |    3 +--
 hw/omap_uart.c             |    3 +--
 hw/ps2.c                   |    4 ++--
 hw/ptimer.c                |    2 +-
 hw/pxa2xx.c                |   14 ++++++--------
 hw/pxa2xx_keypad.c         |    2 +-
 hw/pxa2xx_lcd.c            |    2 +-
 hw/pxa2xx_mmci.c           |    2 +-
 hw/pxa2xx_pcmcia.c         |    3 +--
 hw/rc4030.c                |    4 ++--
 hw/sd.c                    |    2 +-
 hw/sh_timer.c              |    4 ++--
 hw/sm501.c                 |    2 +-
 hw/stellaris.c             |    2 +-
 hw/stellaris_input.c       |    4 ++--
 hw/tc6393xb.c              |    2 +-
 hw/tsc2005.c               |    3 +--
 hw/tsc210x.c               |    6 ++----
 libcacard/cac.c            |    2 +-
 libcacard/card_7816.c      |    8 ++++----
 libcacard/event.c          |    2 +-
 libcacard/vcard.c          |    6 +++---
 libcacard/vcard_emul_nss.c |   10 +++++-----
 libcacard/vreader.c        |    7 +++----
 ui/curses.c                |    2 +-
 ui/sdl.c                   |    2 +-
 usb-redir.c                |    2 +-
 xen-mapcache.c             |    4 ++--
 51 files changed, 94 insertions(+), 131 deletions(-)
Paolo Bonzini - Oct. 20, 2011, 8:55 a.m.
On 10/20/2011 10:03 AM, Stuart Brady wrote:
> Convert calls to g_malloc() and g_malloc0() to g_new() and g_new0()
> respectively, in cases where the return value is casted to the same
> type as specified using sizeof() in the parameter to g_malloc() and
> assigned to a variable of that type.

This is likely to cause conflicts all over the place, and now is a very 
bad time to do so :( because people are trying to get their patches in 
during the soft feature freeze.  I don't oppose the change, just the timing.

Paolo
Stuart Brady - Oct. 21, 2011, 12:34 a.m.
On Thu, Oct 20, 2011 at 10:55:38AM +0200, Paolo Bonzini wrote:
> On 10/20/2011 10:03 AM, Stuart Brady wrote:
> >Convert calls to g_malloc() and g_malloc0() to g_new() and g_new0()
> >respectively, in cases where the return value is casted to the same
> >type as specified using sizeof() in the parameter to g_malloc() and
> >assigned to a variable of that type.
> 
> This is likely to cause conflicts all over the place, and now is a
> very bad time to do so :( because people are trying to get their
> patches in during the soft feature freeze.  I don't oppose the
> change, just the timing.

That makes sense.

Perhaps I should just submit a patch for the allocation of GuestFileRead
(which should be GuestFileSeek) in that case -- although since that's
benign, I could just wait a little while longer.

Cheers,

Patch

diff --git a/console.c b/console.c
index e43de92..2545252 100644
--- a/console.c
+++ b/console.c
@@ -1270,7 +1270,7 @@  static TextConsole *new_console(DisplayState *ds, console_type_t console_type)
 
 static DisplaySurface* defaultallocator_create_displaysurface(int width, int height)
 {
-    DisplaySurface *surface = (DisplaySurface*) g_malloc0(sizeof(DisplaySurface));
+    DisplaySurface *surface = g_new0(DisplaySurface, 1);
 
     int linesize = width * 4;
     qemu_alloc_display(surface, width, height, linesize,
@@ -1311,7 +1311,7 @@  void qemu_alloc_display(DisplaySurface *surface, int width, int height,
 DisplaySurface* qemu_create_displaysurface_from(int width, int height, int bpp,
                                               int linesize, uint8_t *data)
 {
-    DisplaySurface *surface = (DisplaySurface*) g_malloc0(sizeof(DisplaySurface));
+    DisplaySurface *surface = g_new0(DisplaySurface, 1);
 
     surface->width = width;
     surface->height = height;
@@ -1397,7 +1397,7 @@  DisplayState *graphic_console_init(vga_hw_update_ptr update,
     TextConsole *s;
     DisplayState *ds;
 
-    ds = (DisplayState *) g_malloc0(sizeof(DisplayState));
+    ds = g_new0(DisplayState, 1);
     ds->allocator = &default_allocator; 
     ds->surface = qemu_create_displaysurface(ds, 640, 480);
 
diff --git a/gdbstub.c b/gdbstub.c
index 4009058..717c7d9 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -1768,7 +1768,7 @@  void gdb_register_coprocessor(CPUState * env,
     GDBRegisterState **p;
     static int last_reg = NUM_CORE_REGS;
 
-    s = (GDBRegisterState *)g_malloc0(sizeof(GDBRegisterState));
+    s = g_new0(GDBRegisterState, 1);
     s->base_reg = last_reg;
     s->num_regs = num_regs;
     s->get_reg = get_reg;
diff --git a/hw/arm_timer.c b/hw/arm_timer.c
index 09a4b24..5b3335d 100644
--- a/hw/arm_timer.c
+++ b/hw/arm_timer.c
@@ -159,7 +159,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;
 
diff --git a/hw/bt-hci-csr.c b/hw/bt-hci-csr.c
index 0dcf897..ac7249c 100644
--- a/hw/bt-hci-csr.c
+++ b/hw/bt-hci-csr.c
@@ -433,8 +433,7 @@  qemu_irq *csrhci_pins_get(CharDriverState *chr)
 
 CharDriverState *uart_hci_init(qemu_irq wakeup)
 {
-    struct csrhci_s *s = (struct csrhci_s *)
-            g_malloc0(sizeof(struct csrhci_s));
+    struct csrhci_s *s = g_new0(struct csrhci_s, 1);
 
     s->chr.opaque = s;
     s->chr.chr_write = csrhci_write;
diff --git a/hw/ccid-card-emulated.c b/hw/ccid-card-emulated.c
index 092301b..759dbcb 100644
--- a/hw/ccid-card-emulated.c
+++ b/hw/ccid-card-emulated.c
@@ -169,7 +169,7 @@  static void emulated_push_event(EmulatedState *card, EmulEvent *event)
 
 static void emulated_push_type(EmulatedState *card, uint32_t type)
 {
-    EmulEvent *event = (EmulEvent *)g_malloc(sizeof(EmulEvent));
+    EmulEvent *event = g_new(EmulEvent, 1);
 
     assert(event);
     event->p.gen.type = type;
@@ -178,7 +178,7 @@  static void emulated_push_type(EmulatedState *card, uint32_t type)
 
 static void emulated_push_error(EmulatedState *card, uint64_t code)
 {
-    EmulEvent *event = (EmulEvent *)g_malloc(sizeof(EmulEvent));
+    EmulEvent *event = g_new(EmulEvent, 1);
 
     assert(event);
     event->p.error.type = EMUL_ERROR;
diff --git a/hw/ide/microdrive.c b/hw/ide/microdrive.c
index 9eee5b5..f122c33 100644
--- a/hw/ide/microdrive.c
+++ b/hw/ide/microdrive.c
@@ -530,7 +530,7 @@  static int dscm1xxxx_detach(void *opaque)
 
 PCMCIACardState *dscm1xxxx_init(DriveInfo *bdrv)
 {
-    MicroDriveState *md = (MicroDriveState *) g_malloc0(sizeof(MicroDriveState));
+    MicroDriveState *md = g_new0(MicroDriveState, 1);
     md->card.state = md;
     md->card.attach = dscm1xxxx_attach;
     md->card.detach = dscm1xxxx_detach;
diff --git a/hw/irq.c b/hw/irq.c
index 62f766e..ab654e7 100644
--- a/hw/irq.c
+++ b/hw/irq.c
@@ -44,8 +44,8 @@  qemu_irq *qemu_allocate_irqs(qemu_irq_handler handler, void *opaque, int n)
     struct IRQState *p;
     int i;
 
-    s = (qemu_irq *)g_malloc0(sizeof(qemu_irq) * n);
-    p = (struct IRQState *)g_malloc0(sizeof(struct IRQState) * n);
+    s = g_new0(qemu_irq, n);
+    p = g_new0(struct IRQState, n);
     for (i = 0; i < n; i++) {
         p->handler = handler;
         p->opaque = opaque;
diff --git a/hw/ivshmem.c b/hw/ivshmem.c
index 242fbea..6bd6ff6 100644
--- a/hw/ivshmem.c
+++ b/hw/ivshmem.c
@@ -467,8 +467,7 @@  static void ivshmem_read(void *opaque, const uint8_t * buf, int flags)
 
     if (guest_max_eventfd == 0) {
         /* one eventfd per MSI vector */
-        s->peers[incoming_posn].eventfds = (int *) g_malloc(s->vectors *
-                                                                sizeof(int));
+        s->peers[incoming_posn].eventfds = g_new(int, s->vectors);
     }
 
     /* this is an eventfd for a particular guest VM */
diff --git a/hw/mcf5206.c b/hw/mcf5206.c
index 15d6f22..6ec8786 100644
--- a/hw/mcf5206.c
+++ b/hw/mcf5206.c
@@ -132,7 +132,7 @@  static m5206_timer_state *m5206_timer_init(qemu_irq irq)
     m5206_timer_state *s;
     QEMUBH *bh;
 
-    s = (m5206_timer_state *)g_malloc0(sizeof(m5206_timer_state));
+    s = g_new0(m5206_timer_state, 1);
     bh = qemu_bh_new(m5206_timer_trigger, s);
     s->timer = ptimer_init(bh);
     s->irq = irq;
@@ -523,7 +523,7 @@  qemu_irq *mcf5206_init(uint32_t base, CPUState *env)
     qemu_irq *pic;
     int iomemtype;
 
-    s = (m5206_mbar_state *)g_malloc0(sizeof(m5206_mbar_state));
+    s = g_new0(m5206_mbar_state, 1);
     iomemtype = cpu_register_io_memory(m5206_mbar_readfn,
                                        m5206_mbar_writefn, s,
                                        DEVICE_NATIVE_ENDIAN);
diff --git a/hw/mcf5208.c b/hw/mcf5208.c
index 1c2c0c4..6cc4aa3 100644
--- a/hw/mcf5208.c
+++ b/hw/mcf5208.c
@@ -175,7 +175,7 @@  static void mcf5208_sys_init(MemoryRegion *address_space, qemu_irq *pic)
     memory_region_add_subregion(address_space, 0xfc0a8000, iomem);
     /* Timers.  */
     for (i = 0; i < 2; i++) {
-        s = (m5208_timer_state *)g_malloc0(sizeof(m5208_timer_state));
+        s = g_new0(m5208_timer_state, 1);
         bh = qemu_bh_new(m5208_timer_trigger, s);
         s->timer = ptimer_init(bh);
         memory_region_init_io(&s->iomem, &m5208_timer_ops, s,
diff --git a/hw/mcf_fec.c b/hw/mcf_fec.c
index 42a5d77..3a0e7ba 100644
--- a/hw/mcf_fec.c
+++ b/hw/mcf_fec.c
@@ -464,7 +464,7 @@  void mcf_fec_init(NICInfo *nd, target_phys_addr_t base, qemu_irq *irq)
 
     qemu_check_nic_model(nd, "mcf_fec");
 
-    s = (mcf_fec_state *)g_malloc0(sizeof(mcf_fec_state));
+    s = g_new0(mcf_fec_state, 1);
     s->irq = irq;
     s->mmio_index = cpu_register_io_memory(mcf_fec_readfn,
                                            mcf_fec_writefn, s,
diff --git a/hw/mips_malta.c b/hw/mips_malta.c
index bb49749..9c24dd0 100644
--- a/hw/mips_malta.c
+++ b/hw/mips_malta.c
@@ -432,7 +432,7 @@  static MaltaFPGAState *malta_fpga_init(MemoryRegion *address_space,
 {
     MaltaFPGAState *s;
 
-    s = (MaltaFPGAState *)g_malloc0(sizeof(MaltaFPGAState));
+    s = g_new0(MaltaFPGAState, 1);
 
     memory_region_init_io(&s->iomem, &malta_fpga_ops, s,
                           "malta-fpga", 0x100000);
diff --git a/hw/omap1.c b/hw/omap1.c
index 619812c..3e53199 100644
--- a/hw/omap1.c
+++ b/hw/omap1.c
@@ -258,8 +258,7 @@  static struct omap_mpu_timer_s *omap_mpu_timer_init(MemoryRegion *system_memory,
                 target_phys_addr_t base,
                 qemu_irq irq, omap_clk clk)
 {
-    struct omap_mpu_timer_s *s = (struct omap_mpu_timer_s *)
-            g_malloc0(sizeof(struct omap_mpu_timer_s));
+    struct omap_mpu_timer_s *s = g_new0(struct omap_mpu_timer_s, 1);
 
     s->irq = irq;
     s->clk = clk;
@@ -387,8 +386,7 @@  static struct omap_watchdog_timer_s *omap_wd_timer_init(MemoryRegion *memory,
                 target_phys_addr_t base,
                 qemu_irq irq, omap_clk clk)
 {
-    struct omap_watchdog_timer_s *s = (struct omap_watchdog_timer_s *)
-            g_malloc0(sizeof(struct omap_watchdog_timer_s));
+    struct omap_watchdog_timer_s *s = g_new0(struct omap_watchdog_timer_s, 1);
 
     s->timer.irq = irq;
     s->timer.clk = clk;
@@ -493,8 +491,7 @@  static struct omap_32khz_timer_s *omap_os_timer_init(MemoryRegion *memory,
                 target_phys_addr_t base,
                 qemu_irq irq, omap_clk clk)
 {
-    struct omap_32khz_timer_s *s = (struct omap_32khz_timer_s *)
-            g_malloc0(sizeof(struct omap_32khz_timer_s));
+    struct omap_32khz_timer_s *s = g_new0(struct omap_32khz_timer_s, 1);
 
     s->timer.irq = irq;
     s->timer.clk = clk;
@@ -1222,8 +1219,7 @@  static struct omap_tipb_bridge_s *omap_tipb_bridge_init(
     MemoryRegion *memory, target_phys_addr_t base,
     qemu_irq abort_irq, omap_clk clk)
 {
-    struct omap_tipb_bridge_s *s = (struct omap_tipb_bridge_s *)
-            g_malloc0(sizeof(struct omap_tipb_bridge_s));
+    struct omap_tipb_bridge_s *s = g_new0(struct omap_tipb_bridge_s, 1);
 
     s->abort = abort_irq;
     omap_tipb_bridge_reset(s);
@@ -2071,8 +2067,7 @@  struct omap_mpuio_s *omap_mpuio_init(MemoryRegion *memory,
                 qemu_irq kbd_int, qemu_irq gpio_int, qemu_irq wakeup,
                 omap_clk clk)
 {
-    struct omap_mpuio_s *s = (struct omap_mpuio_s *)
-            g_malloc0(sizeof(struct omap_mpuio_s));
+    struct omap_mpuio_s *s = g_new0(struct omap_mpuio_s, 1);
 
     s->irq = gpio_int;
     s->kbd_irq = kbd_int;
@@ -2263,8 +2258,7 @@  static struct omap_uwire_s *omap_uwire_init(MemoryRegion *system_memory,
                                             qemu_irq dma,
                                             omap_clk clk)
 {
-    struct omap_uwire_s *s = (struct omap_uwire_s *)
-            g_malloc0(sizeof(struct omap_uwire_s));
+    struct omap_uwire_s *s = g_new0(struct omap_uwire_s, 1);
 
     s->txirq = txirq;
     s->rxirq = rxirq;
@@ -2879,8 +2873,7 @@  static struct omap_rtc_s *omap_rtc_init(MemoryRegion *system_memory,
                                         qemu_irq timerirq, qemu_irq alarmirq,
                                         omap_clk clk)
 {
-    struct omap_rtc_s *s = (struct omap_rtc_s *)
-            g_malloc0(sizeof(struct omap_rtc_s));
+    struct omap_rtc_s *s = g_new0(struct omap_rtc_s, 1);
 
     s->irq = timerirq;
     s->alarm = alarmirq;
@@ -3410,8 +3403,7 @@  static struct omap_mcbsp_s *omap_mcbsp_init(MemoryRegion *system_memory,
                                             qemu_irq txirq, qemu_irq rxirq,
                                             qemu_irq *dma, omap_clk clk)
 {
-    struct omap_mcbsp_s *s = (struct omap_mcbsp_s *)
-            g_malloc0(sizeof(struct omap_mcbsp_s));
+    struct omap_mcbsp_s *s = g_new0(struct omap_mcbsp_s, 1);
 
     s->txirq = txirq;
     s->rxirq = rxirq;
@@ -3589,8 +3581,7 @@  static void omap_lpg_clk_update(void *opaque, int line, int on)
 static struct omap_lpg_s *omap_lpg_init(MemoryRegion *system_memory,
                                         target_phys_addr_t base, omap_clk clk)
 {
-    struct omap_lpg_s *s = (struct omap_lpg_s *)
-            g_malloc0(sizeof(struct omap_lpg_s));
+    struct omap_lpg_s *s = g_new0(struct omap_lpg_s, 1);
 
     s->tm = qemu_new_timer_ms(rt_clock, omap_lpg_tick, s);
 
@@ -3793,8 +3784,7 @@  struct omap_mpu_state_s *omap310_mpu_init(MemoryRegion *system_memory,
                 const char *core)
 {
     int i;
-    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *)
-            g_malloc0(sizeof(struct omap_mpu_state_s));
+    struct omap_mpu_state_s *s = g_new0(struct omap_mpu_state_s, 1);
     qemu_irq *cpu_irq;
     qemu_irq dma_irqs[6];
     DriveInfo *dinfo;
diff --git a/hw/omap2.c b/hw/omap2.c
index 838c32f..27f4333 100644
--- a/hw/omap2.c
+++ b/hw/omap2.c
@@ -590,8 +590,7 @@  static struct omap_eac_s *omap_eac_init(struct omap_target_agent_s *ta,
                 qemu_irq irq, qemu_irq *drq, omap_clk fclk, omap_clk iclk)
 {
     int iomemtype;
-    struct omap_eac_s *s = (struct omap_eac_s *)
-            g_malloc0(sizeof(struct omap_eac_s));
+    struct omap_eac_s *s = g_new0(struct omap_eac_s, 1);
 
     s->irq = irq;
     s->codec.rxdrq = *drq ++;
@@ -776,8 +775,7 @@  static struct omap_sti_s *omap_sti_init(struct omap_target_agent_s *ta,
                 CharDriverState *chr)
 {
     int iomemtype;
-    struct omap_sti_s *s = (struct omap_sti_s *)
-            g_malloc0(sizeof(struct omap_sti_s));
+    struct omap_sti_s *s = g_new0(struct omap_sti_s, 1);
 
     s->irq = irq;
     omap_sti_reset(s);
@@ -1789,8 +1787,7 @@  static struct omap_prcm_s *omap_prcm_init(struct omap_target_agent_s *ta,
                 struct omap_mpu_state_s *mpu)
 {
     int iomemtype;
-    struct omap_prcm_s *s = (struct omap_prcm_s *)
-            g_malloc0(sizeof(struct omap_prcm_s));
+    struct omap_prcm_s *s = g_new0(struct omap_prcm_s, 1);
 
     s->irq[0] = mpu_int;
     s->irq[1] = dsp_int;
@@ -2162,8 +2159,7 @@  static struct omap_sysctl_s *omap_sysctl_init(struct omap_target_agent_s *ta,
                 omap_clk iclk, struct omap_mpu_state_s *mpu)
 {
     int iomemtype;
-    struct omap_sysctl_s *s = (struct omap_sysctl_s *)
-            g_malloc0(sizeof(struct omap_sysctl_s));
+    struct omap_sysctl_s *s = g_new0(struct omap_sysctl_s, 1);
 
     s->mpu = mpu;
     omap_sysctl_reset(s);
@@ -2226,8 +2222,7 @@  static const struct dma_irq_map omap2_dma_irq_map[] = {
 struct omap_mpu_state_s *omap2420_mpu_init(unsigned long sdram_size,
                 const char *core)
 {
-    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *)
-            g_malloc0(sizeof(struct omap_mpu_state_s));
+    struct omap_mpu_state_s *s = g_new0(struct omap_mpu_state_s, 1);
     ram_addr_t sram_base, q2_base;
     qemu_irq *cpu_irq;
     qemu_irq dma_irqs[4];
diff --git a/hw/omap_clk.c b/hw/omap_clk.c
index 8448006..807706c 100644
--- a/hw/omap_clk.c
+++ b/hw/omap_clk.c
@@ -1239,7 +1239,7 @@  void omap_clk_init(struct omap_mpu_state_s *mpu)
     for (i = onchip_clks, count = 0; *i; i ++)
         if ((*i)->flags & flag)
             count ++;
-    mpu->clks = (struct clk *) g_malloc0(sizeof(struct clk) * (count + 1));
+    mpu->clks = g_new0(struct clk, (count + 1));
     for (i = onchip_clks, j = mpu->clks; *i; i ++)
         if ((*i)->flags & flag) {
             memcpy(j, *i, sizeof(struct clk));
diff --git a/hw/omap_dma.c b/hw/omap_dma.c
index f943d4e..a08ca87 100644
--- a/hw/omap_dma.c
+++ b/hw/omap_dma.c
@@ -1619,8 +1619,7 @@  struct soc_dma_s *omap_dma_init(target_phys_addr_t base, qemu_irq *irqs,
                 enum omap_dma_model model)
 {
     int iomemtype, num_irqs, memsize, i;
-    struct omap_dma_s *s = (struct omap_dma_s *)
-            g_malloc0(sizeof(struct omap_dma_s));
+    struct omap_dma_s *s = g_new0(struct omap_dma_s, 1);
 
     if (model <= omap_dma_3_1) {
         num_irqs = 6;
@@ -2038,8 +2037,7 @@  struct soc_dma_s *omap_dma4_init(target_phys_addr_t base, qemu_irq *irqs,
                 int chans, omap_clk iclk, omap_clk fclk)
 {
     int iomemtype, i;
-    struct omap_dma_s *s = (struct omap_dma_s *)
-            g_malloc0(sizeof(struct omap_dma_s));
+    struct omap_dma_s *s = g_new0(struct omap_dma_s, 1);
 
     s->model = omap_dma_4;
     s->chans = chans;
diff --git a/hw/omap_dss.c b/hw/omap_dss.c
index c8387a8..9251b01 100644
--- a/hw/omap_dss.c
+++ b/hw/omap_dss.c
@@ -1029,8 +1029,7 @@  struct omap_dss_s *omap_dss_init(struct omap_target_agent_s *ta,
                 omap_clk ick1, omap_clk ick2)
 {
     int iomemtype[5];
-    struct omap_dss_s *s = (struct omap_dss_s *)
-            g_malloc0(sizeof(struct omap_dss_s));
+    struct omap_dss_s *s = g_new0(struct omap_dss_s, 1);
 
     s->irq = irq;
     s->drq = drq;
diff --git a/hw/omap_gpmc.c b/hw/omap_gpmc.c
index 7fc82a2..4f0f2fc 100644
--- a/hw/omap_gpmc.c
+++ b/hw/omap_gpmc.c
@@ -809,8 +809,7 @@  struct omap_gpmc_s *omap_gpmc_init(struct omap_mpu_state_s *mpu,
                                    qemu_irq irq, qemu_irq drq)
 {
     int cs;
-    struct omap_gpmc_s *s = (struct omap_gpmc_s *)
-            g_malloc0(sizeof(struct omap_gpmc_s));
+    struct omap_gpmc_s *s = g_new0(struct omap_gpmc_s, 1);
 
     memory_region_init_io(&s->iomem, &omap_gpmc_ops, s, "omap-gpmc", 0x1000);
     memory_region_add_subregion(get_system_memory(), base, &s->iomem);
diff --git a/hw/omap_gptimer.c b/hw/omap_gptimer.c
index 704b000..18ed52a 100644
--- a/hw/omap_gptimer.c
+++ b/hw/omap_gptimer.c
@@ -464,8 +464,7 @@  struct omap_gp_timer_s *omap_gp_timer_init(struct omap_target_agent_s *ta,
                 qemu_irq irq, omap_clk fclk, omap_clk iclk)
 {
     int iomemtype;
-    struct omap_gp_timer_s *s = (struct omap_gp_timer_s *)
-            g_malloc0(sizeof(struct omap_gp_timer_s));
+    struct omap_gp_timer_s *s = g_new0(struct omap_gp_timer_s, 1);
 
     s->ta = ta;
     s->irq = irq;
diff --git a/hw/omap_i2c.c b/hw/omap_i2c.c
index 11577b1..79a9be4 100644
--- a/hw/omap_i2c.c
+++ b/hw/omap_i2c.c
@@ -425,8 +425,7 @@  struct omap_i2c_s *omap_i2c_init(target_phys_addr_t base,
                 qemu_irq irq, qemu_irq *dma, omap_clk clk)
 {
     int iomemtype;
-    struct omap_i2c_s *s = (struct omap_i2c_s *)
-            g_malloc0(sizeof(struct omap_i2c_s));
+    struct omap_i2c_s *s = g_new0(struct omap_i2c_s, 1);
 
     /* TODO: set a value greater or equal to real hardware */
     s->revision = 0x11;
@@ -447,8 +446,7 @@  struct omap_i2c_s *omap2_i2c_init(struct omap_target_agent_s *ta,
                 qemu_irq irq, qemu_irq *dma, omap_clk fclk, omap_clk iclk)
 {
     int iomemtype;
-    struct omap_i2c_s *s = (struct omap_i2c_s *)
-            g_malloc0(sizeof(struct omap_i2c_s));
+    struct omap_i2c_s *s = g_new0(struct omap_i2c_s, 1);
 
     s->revision = 0x34;
     s->irq = irq;
diff --git a/hw/omap_lcdc.c b/hw/omap_lcdc.c
index 29e6048..5e48d7a 100644
--- a/hw/omap_lcdc.c
+++ b/hw/omap_lcdc.c
@@ -437,8 +437,7 @@  struct omap_lcd_panel_s *omap_lcdc_init(target_phys_addr_t base, qemu_irq irq,
                 struct omap_dma_lcd_channel_s *dma, omap_clk clk)
 {
     int iomemtype;
-    struct omap_lcd_panel_s *s = (struct omap_lcd_panel_s *)
-            g_malloc0(sizeof(struct omap_lcd_panel_s));
+    struct omap_lcd_panel_s *s = g_new0(struct omap_lcd_panel_s, 1);
 
     s->irq = irq;
     s->dma = dma;
diff --git a/hw/omap_mmc.c b/hw/omap_mmc.c
index a1afeb5..8ac6da4 100644
--- a/hw/omap_mmc.c
+++ b/hw/omap_mmc.c
@@ -575,8 +575,7 @@  struct omap_mmc_s *omap_mmc_init(target_phys_addr_t base,
                 qemu_irq irq, qemu_irq dma[], omap_clk clk)
 {
     int iomemtype;
-    struct omap_mmc_s *s = (struct omap_mmc_s *)
-            g_malloc0(sizeof(struct omap_mmc_s));
+    struct omap_mmc_s *s = g_new0(struct omap_mmc_s, 1);
 
     s->irq = irq;
     s->dma = dma;
@@ -601,8 +600,7 @@  struct omap_mmc_s *omap2_mmc_init(struct omap_target_agent_s *ta,
                 omap_clk fclk, omap_clk iclk)
 {
     int iomemtype;
-    struct omap_mmc_s *s = (struct omap_mmc_s *)
-            g_malloc0(sizeof(struct omap_mmc_s));
+    struct omap_mmc_s *s = g_new0(struct omap_mmc_s, 1);
 
     s->irq = irq;
     s->dma = dma;
diff --git a/hw/omap_sdrc.c b/hw/omap_sdrc.c
index 1df2fd8..4b13e5d 100644
--- a/hw/omap_sdrc.c
+++ b/hw/omap_sdrc.c
@@ -152,8 +152,7 @@  static CPUWriteMemoryFunc * const omap_sdrc_writefn[] = {
 struct omap_sdrc_s *omap_sdrc_init(target_phys_addr_t base)
 {
     int iomemtype;
-    struct omap_sdrc_s *s = (struct omap_sdrc_s *)
-            g_malloc0(sizeof(struct omap_sdrc_s));
+    struct omap_sdrc_s *s = g_new0(struct omap_sdrc_s, 1);
 
     omap_sdrc_reset(s);
 
diff --git a/hw/omap_spi.c b/hw/omap_spi.c
index 6030ad9..362a259 100644
--- a/hw/omap_spi.c
+++ b/hw/omap_spi.c
@@ -314,8 +314,7 @@  struct omap_mcspi_s *omap_mcspi_init(struct omap_target_agent_s *ta, int chnum,
                 qemu_irq irq, qemu_irq *drq, omap_clk fclk, omap_clk iclk)
 {
     int iomemtype;
-    struct omap_mcspi_s *s = (struct omap_mcspi_s *)
-            g_malloc0(sizeof(struct omap_mcspi_s));
+    struct omap_mcspi_s *s = g_new0(struct omap_mcspi_s, 1);
     struct omap_mcspi_ch_s *ch = s->ch;
 
     s->irq = irq;
diff --git a/hw/omap_uart.c b/hw/omap_uart.c
index 19f8e6e..9fefe8d 100644
--- a/hw/omap_uart.c
+++ b/hw/omap_uart.c
@@ -55,8 +55,7 @@  struct omap_uart_s *omap_uart_init(target_phys_addr_t base,
                 qemu_irq txdma, qemu_irq rxdma,
                 const char *label, CharDriverState *chr)
 {
-    struct omap_uart_s *s = (struct omap_uart_s *)
-            g_malloc0(sizeof(struct omap_uart_s));
+    struct omap_uart_s *s = g_new0(struct omap_uart_s, 1);
 
     s->base = base;
     s->fclk = fclk;
diff --git a/hw/ps2.c b/hw/ps2.c
index 24228c1..fd65e5b 100644
--- a/hw/ps2.c
+++ b/hw/ps2.c
@@ -604,7 +604,7 @@  static const VMStateDescription vmstate_ps2_mouse = {
 
 void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
 {
-    PS2KbdState *s = (PS2KbdState *)g_malloc0(sizeof(PS2KbdState));
+    PS2KbdState *s = g_new0(PS2KbdState, 1);
 
     s->common.update_irq = update_irq;
     s->common.update_arg = update_arg;
@@ -617,7 +617,7 @@  void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
 
 void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg)
 {
-    PS2MouseState *s = (PS2MouseState *)g_malloc0(sizeof(PS2MouseState));
+    PS2MouseState *s = g_new0(PS2MouseState, 1);
 
     s->common.update_irq = update_irq;
     s->common.update_arg = update_arg;
diff --git a/hw/ptimer.c b/hw/ptimer.c
index b6cabd5..0a3bce7 100644
--- a/hw/ptimer.c
+++ b/hw/ptimer.c
@@ -210,7 +210,7 @@  ptimer_state *ptimer_init(QEMUBH *bh)
 {
     ptimer_state *s;
 
-    s = (ptimer_state *)g_malloc0(sizeof(ptimer_state));
+    s = g_new0(ptimer_state, 1);
     s->bh = bh;
     s->timer = qemu_new_timer_ns(vm_clock, ptimer_tick, s);
     return s;
diff --git a/hw/pxa2xx.c b/hw/pxa2xx.c
index 70d7c8a..0223966 100644
--- a/hw/pxa2xx.c
+++ b/hw/pxa2xx.c
@@ -1763,8 +1763,7 @@  static PXA2xxI2SState *pxa2xx_i2s_init(target_phys_addr_t base,
                 qemu_irq irq, qemu_irq rx_dma, qemu_irq tx_dma)
 {
     int iomemtype;
-    PXA2xxI2SState *s = (PXA2xxI2SState *)
-            g_malloc0(sizeof(PXA2xxI2SState));
+    PXA2xxI2SState *s = g_new0(PXA2xxI2SState, 1);
 
     s->irq = irq;
     s->rx_dma = rx_dma;
@@ -2024,8 +2023,7 @@  static PXA2xxFIrState *pxa2xx_fir_init(target_phys_addr_t base,
                 CharDriverState *chr)
 {
     int iomemtype;
-    PXA2xxFIrState *s = (PXA2xxFIrState *)
-            g_malloc0(sizeof(PXA2xxFIrState));
+    PXA2xxFIrState *s = g_new0(PXA2xxFIrState, 1);
 
     s->irq = irq;
     s->rx_dma = rx_dma;
@@ -2065,7 +2063,7 @@  PXA2xxState *pxa270_init(MemoryRegion *address_space,
     PXA2xxState *s;
     int iomemtype, i;
     DriveInfo *dinfo;
-    s = (PXA2xxState *) g_malloc0(sizeof(PXA2xxState));
+    s = g_new0(PXA2xxState, 1);
 
     if (revision && strncmp(revision, "pxa27", 5)) {
         fprintf(stderr, "Machine requires a PXA27x processor.\n");
@@ -2160,7 +2158,7 @@  PXA2xxState *pxa270_init(MemoryRegion *address_space,
     vmstate_register(NULL, 0, &vmstate_pxa2xx_pm, s);
 
     for (i = 0; pxa27x_ssp[i].io_base; i ++);
-    s->ssp = (SSIBus **)g_malloc0(sizeof(SSIBus *) * i);
+    s->ssp = g_new0(SSIBus *, i);
     for (i = 0; pxa27x_ssp[i].io_base; i ++) {
         DeviceState *dev;
         dev = sysbus_create_simple("pxa2xx-ssp", pxa27x_ssp[i].io_base,
@@ -2205,7 +2203,7 @@  PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
     int iomemtype, i;
     DriveInfo *dinfo;
 
-    s = (PXA2xxState *) g_malloc0(sizeof(PXA2xxState));
+    s = g_new0(PXA2xxState, 1);
 
     s->env = cpu_init("pxa255");
     if (!s->env) {
@@ -2292,7 +2290,7 @@  PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
     vmstate_register(NULL, 0, &vmstate_pxa2xx_pm, s);
 
     for (i = 0; pxa255_ssp[i].io_base; i ++);
-    s->ssp = (SSIBus **)g_malloc0(sizeof(SSIBus *) * i);
+    s->ssp = g_new0(SSIBus *, i);
     for (i = 0; pxa255_ssp[i].io_base; i ++) {
         DeviceState *dev;
         dev = sysbus_create_simple("pxa2xx-ssp", pxa255_ssp[i].io_base,
diff --git a/hw/pxa2xx_keypad.c b/hw/pxa2xx_keypad.c
index e33959d..a8f4e7c 100644
--- a/hw/pxa2xx_keypad.c
+++ b/hw/pxa2xx_keypad.c
@@ -312,7 +312,7 @@  PXA2xxKeyPadState *pxa27x_keypad_init(target_phys_addr_t base,
     int iomemtype;
     PXA2xxKeyPadState *s;
 
-    s = (PXA2xxKeyPadState *) g_malloc0(sizeof(PXA2xxKeyPadState));
+    s = g_new0(PXA2xxKeyPadState, 1);
     s->irq = irq;
 
     iomemtype = cpu_register_io_memory(pxa2xx_keypad_readfn,
diff --git a/hw/pxa2xx_lcd.c b/hw/pxa2xx_lcd.c
index b73290c..144ee5a 100644
--- a/hw/pxa2xx_lcd.c
+++ b/hw/pxa2xx_lcd.c
@@ -986,7 +986,7 @@  PXA2xxLCDState *pxa2xx_lcdc_init(target_phys_addr_t base, qemu_irq irq)
     int iomemtype;
     PXA2xxLCDState *s;
 
-    s = (PXA2xxLCDState *) g_malloc0(sizeof(PXA2xxLCDState));
+    s = g_new0(PXA2xxLCDState, 1);
     s->invalidated = 1;
     s->irq = irq;
 
diff --git a/hw/pxa2xx_mmci.c b/hw/pxa2xx_mmci.c
index 1de4979..f9f34dc 100644
--- a/hw/pxa2xx_mmci.c
+++ b/hw/pxa2xx_mmci.c
@@ -524,7 +524,7 @@  PXA2xxMMCIState *pxa2xx_mmci_init(target_phys_addr_t base,
     int iomemtype;
     PXA2xxMMCIState *s;
 
-    s = (PXA2xxMMCIState *) g_malloc0(sizeof(PXA2xxMMCIState));
+    s = g_new0(PXA2xxMMCIState, 1);
     s->irq = irq;
     s->rx_dma = rx_dma;
     s->tx_dma = tx_dma;
diff --git a/hw/pxa2xx_pcmcia.c b/hw/pxa2xx_pcmcia.c
index 74c6817..6733a39 100644
--- a/hw/pxa2xx_pcmcia.c
+++ b/hw/pxa2xx_pcmcia.c
@@ -135,8 +135,7 @@  PXA2xxPCMCIAState *pxa2xx_pcmcia_init(target_phys_addr_t base)
     int iomemtype;
     PXA2xxPCMCIAState *s;
 
-    s = (PXA2xxPCMCIAState *)
-            g_malloc0(sizeof(PXA2xxPCMCIAState));
+    s = g_new0(PXA2xxPCMCIAState, 1);
 
     /* Socket I/O Memory Space */
     iomemtype = cpu_register_io_memory(pxa2xx_pcmcia_io_readfn,
diff --git a/hw/rc4030.c b/hw/rc4030.c
index 33e1070..eab402a 100644
--- a/hw/rc4030.c
+++ b/hw/rc4030.c
@@ -789,8 +789,8 @@  static rc4030_dma *rc4030_allocate_dmas(void *opaque, int n)
     struct rc4030DMAState *p;
     int i;
 
-    s = (rc4030_dma *)g_malloc0(sizeof(rc4030_dma) * n);
-    p = (struct rc4030DMAState *)g_malloc0(sizeof(struct rc4030DMAState) * n);
+    s = g_new0(rc4030_dma, n);
+    p = g_new0(struct rc4030DMAState, n);
     for (i = 0; i < n; i++) {
         p->opaque = opaque;
         p->n = i;
diff --git a/hw/sd.c b/hw/sd.c
index 10e26ad..c6186c1 100644
--- a/hw/sd.c
+++ b/hw/sd.c
@@ -442,7 +442,7 @@  SDState *sd_init(BlockDriverState *bs, int is_spi)
 {
     SDState *sd;
 
-    sd = (SDState *) g_malloc0(sizeof(SDState));
+    sd = g_new0(SDState, 1);
     sd->buf = qemu_blockalign(bs, 512);
     sd->spi = is_spi;
     sd->enable = 1;
diff --git a/hw/sh_timer.c b/hw/sh_timer.c
index dca3c94..a37fe6c 100644
--- a/hw/sh_timer.c
+++ b/hw/sh_timer.c
@@ -188,7 +188,7 @@  static void *sh_timer_init(uint32_t freq, int feat, qemu_irq irq)
     sh_timer_state *s;
     QEMUBH *bh;
 
-    s = (sh_timer_state *)g_malloc0(sizeof(sh_timer_state));
+    s = g_new0(sh_timer_state, 1);
     s->freq = freq;
     s->feat = feat;
     s->tcor = 0xffffffff;
@@ -311,7 +311,7 @@  void tmu012_init(target_phys_addr_t base, int feat, uint32_t freq,
     tmu012_state *s;
     int timer_feat = (feat & TMU012_FEAT_EXTCLK) ? TIMER_FEAT_EXTCLK : 0;
 
-    s = (tmu012_state *)g_malloc0(sizeof(tmu012_state));
+    s = g_new0(tmu012_state, 1);
     s->feat = feat;
     s->timer[0] = sh_timer_init(freq, timer_feat, ch0_irq);
     s->timer[1] = sh_timer_init(freq, timer_feat, ch1_irq);
diff --git a/hw/sm501.c b/hw/sm501.c
index a7ed6fa..5c8aa25 100644
--- a/hw/sm501.c
+++ b/hw/sm501.c
@@ -1395,7 +1395,7 @@  void sm501_init(MemoryRegion *address_space_mem, uint32_t base,
     int sm501_2d_engine_index;
 
     /* allocate management data region */
-    s = (SM501State *)g_malloc0(sizeof(SM501State));
+    s = g_new0(SM501State, 1);
     s->base = base;
     s->local_mem_size_index
 	= get_local_mem_size_index(local_mem_bytes);
diff --git a/hw/stellaris.c b/hw/stellaris.c
index 2bf1c23..00cf3f6 100644
--- a/hw/stellaris.c
+++ b/hw/stellaris.c
@@ -670,7 +670,7 @@  static int stellaris_sys_init(uint32_t base, qemu_irq irq,
     int iomemtype;
     ssys_state *s;
 
-    s = (ssys_state *)g_malloc0(sizeof(ssys_state));
+    s = g_new0(ssys_state, 1);
     s->irq = irq;
     s->board = board;
     /* Most devices come preprogrammed with a MAC address in the user data. */
diff --git a/hw/stellaris_input.c b/hw/stellaris_input.c
index 68c600c..4633b7e 100644
--- a/hw/stellaris_input.c
+++ b/hw/stellaris_input.c
@@ -77,8 +77,8 @@  void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
     gamepad_state *s;
     int i;
 
-    s = (gamepad_state *)g_malloc0(sizeof (gamepad_state));
-    s->buttons = (gamepad_button *)g_malloc0(n * sizeof (gamepad_button));
+    s = g_new0(gamepad_state, 1);
+    s->buttons = g_new0(gamepad_button, n);
     for (i = 0; i < n; i++) {
         s->buttons[i].irq = irq[i];
         s->buttons[i].keycode = keycode[i];
diff --git a/hw/tc6393xb.c b/hw/tc6393xb.c
index c28005a..46b902b 100644
--- a/hw/tc6393xb.c
+++ b/hw/tc6393xb.c
@@ -579,7 +579,7 @@  TC6393xbState *tc6393xb_init(uint32_t base, qemu_irq irq)
         tc6393xb_writel,
     };
 
-    s = (TC6393xbState *) g_malloc0(sizeof(TC6393xbState));
+    s = g_new0(TC6393xbState, 1);
     s->irq = irq;
     s->gpio_in = qemu_allocate_irqs(tc6393xb_gpio_set, s, TC6393XB_GPIOS);
 
diff --git a/hw/tsc2005.c b/hw/tsc2005.c
index 9a500eb..c54a537 100644
--- a/hw/tsc2005.c
+++ b/hw/tsc2005.c
@@ -523,8 +523,7 @@  void *tsc2005_init(qemu_irq pintdav)
 {
     TSC2005State *s;
 
-    s = (TSC2005State *)
-            g_malloc0(sizeof(TSC2005State));
+    s = g_new0(TSC2005State, 1);
     s->x = 400;
     s->y = 240;
     s->pressure = 0;
diff --git a/hw/tsc210x.c b/hw/tsc210x.c
index 3c448a6..6da0722 100644
--- a/hw/tsc210x.c
+++ b/hw/tsc210x.c
@@ -1104,8 +1104,7 @@  uWireSlave *tsc2102_init(qemu_irq pint)
 {
     TSC210xState *s;
 
-    s = (TSC210xState *)
-            g_malloc0(sizeof(TSC210xState));
+    s = g_new0(TSC210xState, 1);
     memset(s, 0, sizeof(TSC210xState));
     s->x = 160;
     s->y = 160;
@@ -1153,8 +1152,7 @@  uWireSlave *tsc2301_init(qemu_irq penirq, qemu_irq kbirq, qemu_irq dav)
 {
     TSC210xState *s;
 
-    s = (TSC210xState *)
-            g_malloc0(sizeof(TSC210xState));
+    s = g_new0(TSC210xState, 1);
     memset(s, 0, sizeof(TSC210xState));
     s->x = 400;
     s->y = 240;
diff --git a/libcacard/cac.c b/libcacard/cac.c
index f4b0b1b..4cb8d28 100644
--- a/libcacard/cac.c
+++ b/libcacard/cac.c
@@ -288,7 +288,7 @@  cac_new_pki_applet_private(const unsigned char *cert,
 {
     CACPKIAppletData *pki_applet_data = NULL;
     VCardAppletPrivate *applet_private = NULL;
-    applet_private = (VCardAppletPrivate *)g_malloc(sizeof(VCardAppletPrivate));
+    applet_private = g_new(VCardAppletPrivate, 1);
 
     pki_applet_data = &(applet_private->u.pki_data);
     pki_applet_data->cert_buffer = NULL;
diff --git a/libcacard/card_7816.c b/libcacard/card_7816.c
index 9fd59d4..decaea3 100644
--- a/libcacard/card_7816.c
+++ b/libcacard/card_7816.c
@@ -51,7 +51,7 @@  vcard_response_new_data(unsigned char *buf, int len)
 {
     VCardResponse *new_response;
 
-    new_response = (VCardResponse *)g_malloc(sizeof(VCardResponse));
+    new_response = g_new(VCardResponse, 1);
     new_response->b_data = g_malloc(len + 2);
     memcpy(new_response->b_data, buf, len);
     new_response->b_total_len = len+2;
@@ -132,7 +132,7 @@  vcard_response_new_status(vcard_7816_status_t status)
 {
     VCardResponse *new_response;
 
-    new_response = (VCardResponse *)g_malloc(sizeof(VCardResponse));
+    new_response = g_new(VCardResponse, 1);
     new_response->b_data = &new_response->b_sw1;
     new_response->b_len = 0;
     new_response->b_total_len = 2;
@@ -149,7 +149,7 @@  vcard_response_new_status_bytes(unsigned char sw1, unsigned char sw2)
 {
     VCardResponse *new_response;
 
-    new_response = (VCardResponse *)g_malloc(sizeof(VCardResponse));
+    new_response = g_new(VCardResponse, 1);
     new_response->b_data = &new_response->b_sw1;
     new_response->b_len = 0;
     new_response->b_total_len = 2;
@@ -336,7 +336,7 @@  vcard_apdu_new(unsigned char *raw_apdu, int len, vcard_7816_status_t *status)
         return NULL;
     }
 
-    new_apdu = (VCardAPDU *)g_malloc(sizeof(VCardAPDU));
+    new_apdu = g_new(VCardAPDU, 1);
     new_apdu->a_data = g_malloc(len);
     memcpy(new_apdu->a_data, raw_apdu, len);
     new_apdu->a_len = len;
diff --git a/libcacard/event.c b/libcacard/event.c
index 6192376..067fef8 100644
--- a/libcacard/event.c
+++ b/libcacard/event.c
@@ -17,7 +17,7 @@  vevent_new(VEventType type, VReader *reader, VCard *card)
 {
     VEvent *new_vevent;
 
-    new_vevent = (VEvent *)g_malloc(sizeof(VEvent));
+    new_vevent = g_new(VEvent, 1);
     new_vevent->next = NULL;
     new_vevent->type = type;
     new_vevent->reader = vreader_reference(reader);
diff --git a/libcacard/vcard.c b/libcacard/vcard.c
index b02556e..c0bdd13 100644
--- a/libcacard/vcard.c
+++ b/libcacard/vcard.c
@@ -37,7 +37,7 @@  vcard_buffer_response_new(unsigned char *buffer, int size)
 {
     VCardBufferResponse *new_buffer;
 
-    new_buffer = (VCardBufferResponse *)g_malloc(sizeof(VCardBufferResponse));
+    new_buffer = g_new(VCardBufferResponse, 1);
     new_buffer->buffer = (unsigned char *)g_malloc(size);
     memcpy(new_buffer->buffer, buffer, size);
     new_buffer->buffer_len = size;
@@ -102,7 +102,7 @@  vcard_new_applet(VCardProcessAPDU applet_process_function,
 {
     VCardApplet *applet;
 
-    applet = (VCardApplet *)g_malloc(sizeof(VCardApplet));
+    applet = g_new(VCardApplet, 1);
     applet->next = NULL;
     applet->applet_private = NULL;
     applet->applet_private_free = NULL;
@@ -151,7 +151,7 @@  vcard_new(VCardEmul *private, VCardEmulFree private_free)
     VCard *new_card;
     int i;
 
-    new_card = (VCard *)g_malloc(sizeof(VCard));
+    new_card = g_new(VCard, 1);
     new_card->applet_list = NULL;
     for (i = 0; i < MAX_CHANNEL; i++) {
         new_card->current_applet[i] = NULL;
diff --git a/libcacard/vcard_emul_nss.c b/libcacard/vcard_emul_nss.c
index 397485c..8897bae 100644
--- a/libcacard/vcard_emul_nss.c
+++ b/libcacard/vcard_emul_nss.c
@@ -94,9 +94,9 @@  vcard_emul_alloc_arrays(unsigned char ***certsp, int **cert_lenp,
     *certsp = NULL;
     *cert_lenp = NULL;
     *keysp = NULL;
-    *certsp = (unsigned char **)g_malloc(sizeof(unsigned char *)*cert_count);
-    *cert_lenp = (int *)g_malloc(sizeof(int)*cert_count);
-    *keysp = (VCardKey **)g_malloc(sizeof(VCardKey *)*cert_count);
+    *certsp = g_new(unsigned char *, cert_count);
+    *cert_lenp = g_new(int, cert_count);
+    *keysp = g_new(VCardKey *, cert_count);
     return PR_TRUE;
 }
 
@@ -140,7 +140,7 @@  vcard_emul_make_key(PK11SlotInfo *slot, CERTCertificate *cert)
 {
     VCardKey *key;
 
-    key = (VCardKey *)g_malloc(sizeof(VCardKey));
+    key = g_new(VCardKey, 1);
     key->slot = PK11_ReferenceSlot(slot);
     key->cert = CERT_DupCertificate(cert);
     /* NOTE: if we aren't logged into the token, this could return NULL */
@@ -452,7 +452,7 @@  vreader_emul_new(PK11SlotInfo *slot, VCardEmulType type, const char *params)
 {
     VReaderEmul *new_reader_emul;
 
-    new_reader_emul = (VReaderEmul *)g_malloc(sizeof(VReaderEmul));
+    new_reader_emul = g_new(VReaderEmul, 1);
 
     new_reader_emul->slot = PK11_ReferenceSlot(slot);
     new_reader_emul->default_type = type;
diff --git a/libcacard/vreader.c b/libcacard/vreader.c
index ec126df..a91e0a0 100644
--- a/libcacard/vreader.c
+++ b/libcacard/vreader.c
@@ -46,7 +46,7 @@  vreader_new(const char *name, VReaderEmul *private,
 {
     VReader *reader;
 
-    reader = (VReader *)g_malloc(sizeof(VReader));
+    reader = g_new(VReader, 1);
     qemu_mutex_init(&reader->lock);
     reader->reference_count = 1;
     reader->name = name ? strdup(name) : NULL;
@@ -236,8 +236,7 @@  vreader_list_entry_new(VReader *reader)
 {
     VReaderListEntry *new_reader_list_entry;
 
-    new_reader_list_entry = (VReaderListEntry *)
-                               g_malloc(sizeof(VReaderListEntry));
+    new_reader_list_entry = g_new(VReaderListEntry, 1);
     new_reader_list_entry->next = NULL;
     new_reader_list_entry->prev = NULL;
     new_reader_list_entry->reader = vreader_reference(reader);
@@ -260,7 +259,7 @@  vreader_list_new(void)
 {
     VReaderList *new_reader_list;
 
-    new_reader_list = (VReaderList *)g_malloc(sizeof(VReaderList));
+    new_reader_list = g_new(VReaderList, 1);
     new_reader_list->head = NULL;
     new_reader_list->tail = NULL;
     return new_reader_list;
diff --git a/ui/curses.c b/ui/curses.c
index c2be2c6..0d7fa5a 100644
--- a/ui/curses.c
+++ b/ui/curses.c
@@ -354,7 +354,7 @@  void curses_display_init(DisplayState *ds, int full_screen)
 #endif
 #endif
 
-    dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
+    dcl = g_new0(DisplayChangeListener, 1);
     dcl->dpy_update = curses_update;
     dcl->dpy_resize = curses_resize;
     dcl->dpy_refresh = curses_refresh;
diff --git a/ui/sdl.c b/ui/sdl.c
index 8cafc44..298bacd 100644
--- a/ui/sdl.c
+++ b/ui/sdl.c
@@ -166,7 +166,7 @@  static PixelFormat sdl_to_qemu_pixelformat(SDL_PixelFormat *sdl_pf)
 
 static DisplaySurface* sdl_create_displaysurface(int width, int height)
 {
-    DisplaySurface *surface = (DisplaySurface*) g_malloc0(sizeof(DisplaySurface));
+    DisplaySurface *surface = g_new0(DisplaySurface, 1);
     if (surface == NULL) {
         fprintf(stderr, "sdl_create_displaysurface: malloc failed\n");
         exit(1);
diff --git a/usb-redir.c b/usb-redir.c
index c74b156..a764cc8 100644
--- a/usb-redir.c
+++ b/usb-redir.c
@@ -234,7 +234,7 @@  static int usbredir_write(void *priv, uint8_t *data, int count)
 
 static AsyncURB *async_alloc(USBRedirDevice *dev, USBPacket *p)
 {
-    AsyncURB *aurb = (AsyncURB *) g_malloc0(sizeof(AsyncURB));
+    AsyncURB *aurb = g_new0(AsyncURB, 1);
     aurb->dev = dev;
     aurb->packet = p;
     aurb->packet_id = dev->packet_id;
diff --git a/xen-mapcache.c b/xen-mapcache.c
index 7bcb86e..8162b69 100644
--- a/xen-mapcache.c
+++ b/xen-mapcache.c
@@ -173,8 +173,8 @@  static void xen_remap_bucket(MapCacheEntry *entry,
     entry->vaddr_base = vaddr_base;
     entry->paddr_index = address_index;
     entry->size = size;
-    entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
-            BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
+    entry->valid_mapping = g_new0(unsigned long,
+                                  BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
 
     bitmap_zero(entry->valid_mapping, nb_pfn);
     for (i = 0; i < nb_pfn; i++) {