diff mbox

[15/29] hw/mips/mips_jazz.c: Fix memory leak spotted by valgrind

Message ID 1432814932-12608-16-git-send-email-zhaoshenglong@huawei.com
State New
Headers show

Commit Message

Shannon Zhao May 28, 2015, 12:08 p.m. UTC
From: Shannon Zhao <shannon.zhao@linaro.org>

valgrind complains about:
==28487== 1,568 (128 direct, 1,440 indirect) bytes in 1 blocks are definitely lost in loss record 2,160 of 2,212
==28487==    at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==28487==    by 0x2F5B97: malloc_and_trace (vl.c:2556)
==28487==    by 0x64C770E: g_malloc (in /usr/lib64/libglib-2.0.so.0.3600.3)
==28487==    by 0x384303: qemu_extend_irqs (irq.c:55)
==28487==    by 0x38438F: qemu_allocate_irqs (irq.c:64)
==28487==    by 0x3B81DD: rc4030_init (rc4030.c:806)
==28487==    by 0x240151: mips_jazz_init (mips_jazz.c:221)
==28487==    by 0x240898: mips_magnum_init (mips_jazz.c:341)
==28487==    by 0x2F9F62: main (vl.c:4249)

==28487== 32 bytes in 1 blocks are definitely lost in loss record 1,359 of 2,212
==28487==    at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==28487==    by 0x2F5B97: malloc_and_trace (vl.c:2556)
==28487==    by 0x64C71F5: ??? (in /usr/lib64/libglib-2.0.so.0.3600.3)
==28487==    by 0x64C7766: g_malloc0 (in /usr/lib64/libglib-2.0.so.0.3600.3)
==28487==    by 0x3B8132: rc4030_allocate_dmas (rc4030.c:787)
==28487==    by 0x3B81F5: rc4030_init (rc4030.c:807)
==28487==    by 0x240151: mips_jazz_init (mips_jazz.c:221)
==28487==    by 0x240898: mips_magnum_init (mips_jazz.c:341)
==28487==    by 0x2F9F62: main (vl.c:4249)

Signed-off-by: Shannon Zhao <zhaoshenglong@huawei.com>
Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org>
---
 hw/dma/rc4030.c     |  2 +-
 hw/mips/mips_jazz.c | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)
diff mbox

Patch

diff --git a/hw/dma/rc4030.c b/hw/dma/rc4030.c
index af26632..9b42aab 100644
--- a/hw/dma/rc4030.c
+++ b/hw/dma/rc4030.c
@@ -803,7 +803,7 @@  void *rc4030_init(qemu_irq timer, qemu_irq jazz_bus,
 
     s = g_malloc0(sizeof(rc4030State));
 
-    *irqs = qemu_allocate_irqs(rc4030_irq_jazz_request, s, 16);
+    *irqs = qemu_allocate_irqs(rc4030_irq_jazz_request, s, 10);
     *dmas = rc4030_allocate_dmas(s, 4);
 
     s->periodic_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, rc4030_periodic_timer, s);
diff --git a/hw/mips/mips_jazz.c b/hw/mips/mips_jazz.c
index 2c153e0..cd26f60 100644
--- a/hw/mips/mips_jazz.c
+++ b/hw/mips/mips_jazz.c
@@ -261,8 +261,10 @@  static void mips_jazz_init(MachineState *machine,
         break;
     case JAZZ_PICA61:
         isa_vga_mm_init(0x40000000, 0x60000000, 0, get_system_memory());
+        qemu_free_irq(rc4030[3]);
         break;
     default:
+        qemu_free_irq(rc4030[3]);
         break;
     }
 
@@ -288,6 +290,7 @@  static void mips_jazz_init(MachineState *machine,
     esp_init(0x80002000, 0,
              rc4030_dma_read, rc4030_dma_write, dmas[0],
              rc4030[5], &esp_reset, &dma_enable);
+    g_free(dmas);
 
     /* Floppy */
     if (drive_get_max_bus(IF_FLOPPY) >= MAX_FD) {
@@ -312,18 +315,28 @@  static void mips_jazz_init(MachineState *machine,
     if (serial_hds[0]) {
         serial_mm_init(address_space, 0x80006000, 0, rc4030[8], 8000000/16,
                        serial_hds[0], DEVICE_NATIVE_ENDIAN);
+    } else {
+        qemu_free_irq(rc4030[8]);
     }
+
     if (serial_hds[1]) {
         serial_mm_init(address_space, 0x80007000, 0, rc4030[9], 8000000/16,
                        serial_hds[1], DEVICE_NATIVE_ENDIAN);
+    } else {
+        qemu_free_irq(rc4030[9]);
     }
 
     /* Parallel port */
-    if (parallel_hds[0])
+    if (parallel_hds[0]) {
         parallel_mm_init(address_space, 0x80008000, 0, rc4030[0],
                          parallel_hds[0]);
+    } else {
+        qemu_free_irq(rc4030[0]);
+    }
 
     /* FIXME: missing Jazz sound at 0x8000c000, rc4030[2] */
+    qemu_free_irq(rc4030[2]);
+    g_free(rc4030);
 
     /* NVRAM */
     dev = qdev_create(NULL, "ds1225y");