Skip to contentSkip to conversation

Converted malloc calls to g_malloc and g_new

PATCH In review Mriyam Tamuli 3 participants 8 messages Mar 12, 2016 – Mar 13, 2016 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Converted malloc calls to g_malloc and g_new

---
 block/iscsi.c      |  2 +-
 bsd-user/elfload.c | 12 ++++++------
 bsd-user/qemu.h    |  2 +-
 configure          |  4 ++--
 disas/ia64.c       |  2 +-
 5 files changed, 11 insertions(+), 11 deletions(-)
block/iscsi.c +1 -1
index 128ea79..2d6e5b4 100644
@@ -840,7 +840,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
         return &acb->common;
     }
 
-    acb->task = malloc(sizeof(struct scsi_task));
+    acb->task = g_malloc(sizeof(struct scsi_task));
     if (acb->task == NULL) {
         error_report("iSCSI: Failed to allocate task for scsi command. %s",
                      iscsi_get_error(iscsi));
bsd-user/elfload.c +6 -6
index 0a6092b..74d7c79 100644
@@ -868,7 +868,7 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
             return ~(abi_ulong)0UL;
 
         elf_phdata =  (struct elf_phdr *)
-                malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
+                g_malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
 
         if (!elf_phdata)
           return ~((abi_ulong)0UL);
@@ -1064,13 +1064,13 @@ static void load_symbols(struct elfhdr *hdr, int fd)
 
  found:
     /* Now know where the strtab and symtab are.  Snarf them. */
-    s = malloc(sizeof(*s));
-    syms = malloc(symtab.sh_size);
+    s = g_malloc(sizeof(*s));
+    syms = g_malloc(symtab.sh_size);
     if (!syms) {
         free(s);
         return;
     }
-    s->disas_strtab = strings = malloc(strtab.sh_size);
+    s->disas_strtab = strings = g_malloc(strtab.sh_size);
     if (!s->disas_strtab) {
         free(s);
         free(syms);
@@ -1191,7 +1191,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
     }
 
     /* Now read in all of the header information */
-    elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
+    elf_phdata = g_new(elf_ex.e_phentsize * elf_ex.e_phnum);
     if (elf_phdata == NULL) {
         return -ENOMEM;
     }
@@ -1244,7 +1244,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
              * is an a.out format binary
              */
 
-            elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
+            elf_interpreter = g_new(elf_ppnt->p_filesz);
 
             if (elf_interpreter == NULL) {
                 free (elf_phdata);
bsd-user/qemu.h +1 -1
index 03b502a..ada4360 100644
@@ -357,7 +357,7 @@ static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy
 #ifdef DEBUG_REMAP
     {
         void *addr;
-        addr = malloc(len);
+        addr = g_malloc(len);
         if (copy)
             memcpy(addr, g2h(guest_addr), len);
         else
configure +2 -2
index 2b32876..5df672b 100755
@@ -3512,7 +3512,7 @@ fi
 if test "$tcmalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }
 EOF
 
   if compile_prog "" "-ltcmalloc" ; then
@@ -3528,7 +3528,7 @@ fi
 if test "$jemalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }
 EOF
 
   if compile_prog "" "-ljemalloc" ; then
disas/ia64.c +1 -1
index 140754c..b0733ed 100644
@@ -10268,7 +10268,7 @@ static struct ia64_opcode *
 make_ia64_opcode (ia64_insn opcode, const char *name, int place, int depind)
 {
   struct ia64_opcode *res =
-    (struct ia64_opcode *) malloc (sizeof (struct ia64_opcode));
+    g_new(sizeof(struct ia64_opcode));
   res->name = strdup (name);
   res->type = main_table[place].opcode_type;
   res->num_outputs = main_table[place].num_outputs;
Signature · 1 line
2.5.0
#1 lore raw

Can anyone please comment on this? Am I doing anything wrong here?

Quoted 122 lines · Mriyam Tamuli
---
 block/iscsi.c      |  2 +-
 bsd-user/elfload.c | 12 ++++++------
 bsd-user/qemu.h    |  2 +-
 configure          |  4 ++--
 disas/ia64.c       |  2 +-
 5 files changed, 11 insertions(+), 11 deletions(-)
block/iscsi.c +1 -1
index 128ea79..2d6e5b4 100644
@@ -840,7 +840,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
         return &acb->common;
     }
 
-    acb->task = malloc(sizeof(struct scsi_task));
+    acb->task = g_malloc(sizeof(struct scsi_task));
     if (acb->task == NULL) {
         error_report("iSCSI: Failed to allocate task for scsi command. %s",
                      iscsi_get_error(iscsi));
bsd-user/elfload.c +6 -6
index 0a6092b..74d7c79 100644
@@ -868,7 +868,7 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
             return ~(abi_ulong)0UL;
 
         elf_phdata =  (struct elf_phdr *)
-                malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
+                g_malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
 
         if (!elf_phdata)
           return ~((abi_ulong)0UL);
@@ -1064,13 +1064,13 @@ static void load_symbols(struct elfhdr *hdr, int fd)
 
  found:
     /* Now know where the strtab and symtab are.  Snarf them. */
-    s = malloc(sizeof(*s));
-    syms = malloc(symtab.sh_size);
+    s = g_malloc(sizeof(*s));
+    syms = g_malloc(symtab.sh_size);
     if (!syms) {
         free(s);
         return;
     }
-    s->disas_strtab = strings = malloc(strtab.sh_size);
+    s->disas_strtab = strings = g_malloc(strtab.sh_size);
     if (!s->disas_strtab) {
         free(s);
         free(syms);
@@ -1191,7 +1191,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
     }
 
     /* Now read in all of the header information */
-    elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
+    elf_phdata = g_new(elf_ex.e_phentsize * elf_ex.e_phnum);
     if (elf_phdata == NULL) {
         return -ENOMEM;
     }
@@ -1244,7 +1244,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
              * is an a.out format binary
              */
 
-            elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
+            elf_interpreter = g_new(elf_ppnt->p_filesz);
 
             if (elf_interpreter == NULL) {
                 free (elf_phdata);
bsd-user/qemu.h +1 -1
index 03b502a..ada4360 100644
@@ -357,7 +357,7 @@ static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy
 #ifdef DEBUG_REMAP
     {
         void *addr;
-        addr = malloc(len);
+        addr = g_malloc(len);
         if (copy)
             memcpy(addr, g2h(guest_addr), len);
         else
configure +2 -2
index 2b32876..5df672b 100755
@@ -3512,7 +3512,7 @@ fi
 if test "$tcmalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }
 EOF
 
   if compile_prog "" "-ltcmalloc" ; then
@@ -3528,7 +3528,7 @@ fi
 if test "$jemalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }
 EOF
 
   if compile_prog "" "-ljemalloc" ; then
disas/ia64.c +1 -2
index 140754c..b0733ed 100644
@@ -10268,7 +10268,7 @@ static struct ia64_opcode *
 make_ia64_opcode (ia64_insn opcode, const char *name, int place, int depind)
 {
   struct ia64_opcode *res =
-    (struct ia64_opcode *) malloc (sizeof (struct ia64_opcode));
+    g_new(sizeof(struct ia64_opcode));
   res->name = strdup (name);
   res->type = main_table[place].opcode_type;
   res->num_outputs = main_table[place].num_outputs;
--

2.5.0

#2 lore raw
Quoted 1 line · Mriyam Tamuli

Can anyone please comment on this? Am I doing anything wrong here?

It's best to wait a few days. Most QEMU hackers work 9-5 jobs so activity on the mailing list is fairly light over the weekend.

Quoted 116 lines · Mriyam Tamuli
Quoted 114 lines
---
 block/iscsi.c      |  2 +-
 bsd-user/elfload.c | 12 ++++++------
 bsd-user/qemu.h    |  2 +-
 configure          |  4 ++--
 disas/ia64.c       |  2 +-
 5 files changed, 11 insertions(+), 11 deletions(-)
block/iscsi.c +1 -1
index 128ea79..2d6e5b4 100644
@@ -840,7 +840,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
         return &acb->common;
     }
 
-    acb->task = malloc(sizeof(struct scsi_task));
+    acb->task = g_malloc(sizeof(struct scsi_task));
     if (acb->task == NULL) {
         error_report("iSCSI: Failed to allocate task for scsi command. %s",
                      iscsi_get_error(iscsi));
bsd-user/elfload.c +6 -6
index 0a6092b..74d7c79 100644
@@ -868,7 +868,7 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
             return ~(abi_ulong)0UL;
 
         elf_phdata =  (struct elf_phdr *)
-                malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
+                g_malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
 
         if (!elf_phdata)
           return ~((abi_ulong)0UL);
@@ -1064,13 +1064,13 @@ static void load_symbols(struct elfhdr *hdr, int fd)
 
  found:
     /* Now know where the strtab and symtab are.  Snarf them. */
-    s = malloc(sizeof(*s));
-    syms = malloc(symtab.sh_size);
+    s = g_malloc(sizeof(*s));
+    syms = g_malloc(symtab.sh_size);
     if (!syms) {
         free(s);
         return;
     }
-    s->disas_strtab = strings = malloc(strtab.sh_size);
+    s->disas_strtab = strings = g_malloc(strtab.sh_size);
     if (!s->disas_strtab) {
         free(s);
         free(syms);
@@ -1191,7 +1191,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
     }
 
     /* Now read in all of the header information */
-    elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
+    elf_phdata = g_new(elf_ex.e_phentsize * elf_ex.e_phnum);
     if (elf_phdata == NULL) {
         return -ENOMEM;
     }
@@ -1244,7 +1244,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
              * is an a.out format binary
              */
 
-            elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
+            elf_interpreter = g_new(elf_ppnt->p_filesz);
 
             if (elf_interpreter == NULL) {
                 free (elf_phdata);
bsd-user/qemu.h +1 -1
index 03b502a..ada4360 100644
@@ -357,7 +357,7 @@ static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy
 #ifdef DEBUG_REMAP
     {
         void *addr;
-        addr = malloc(len);
+        addr = g_malloc(len);
         if (copy)
             memcpy(addr, g2h(guest_addr), len);
         else
configure +2 -2
index 2b32876..5df672b 100755
@@ -3512,7 +3512,7 @@ fi
 if test "$tcmalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }
 EOF
 
   if compile_prog "" "-ltcmalloc" ; then
@@ -3528,7 +3528,7 @@ fi
 if test "$jemalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }
 EOF
 
   if compile_prog "" "-ljemalloc" ; then
disas/ia64.c +0 -0
index 140754c..b0733ed 100644
@@ -10268,7 +10268,7 @@ static struct ia64_opcode *
 make_ia64_opcode (ia64_insn opcode, const char *name, int place, int depind)
 {
   struct ia64_opcode *res =
Quoted 8 lines
Quoted 8 lines
disas/ia64.c +1 -2
-    (struct ia64_opcode *) malloc (sizeof (struct ia64_opcode));
+    g_new(sizeof(struct ia64_opcode));
   res->name = strdup (name);
   res->type = main_table[place].opcode_type;
   res->num_outputs = main_table[place].num_outputs;
--

2.5.0

--
Alex Bennée

#3 lore raw
In reply to Mriyam Tamuli

Am 12.03.2016 um 12:51 schrieb Mriyam Tamuli:

Quoted 14 lines
---
 block/iscsi.c      |  2 +-
 bsd-user/elfload.c | 12 ++++++------
 bsd-user/qemu.h    |  2 +-
 configure          |  4 ++--
 disas/ia64.c       |  2 +-
 5 files changed, 11 insertions(+), 11 deletions(-)
block/iscsi.c +0 -0
index 128ea79..2d6e5b4 100644

block/iscsi.c static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, · line 843

Quoted 5 lines
block/iscsi.c +1 -1
@@ -840,7 +840,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
         return &acb->common;
     }
 
-    acb->task = malloc(sizeof(struct scsi_task));
+    acb->task = g_malloc(sizeof(struct scsi_task));

I suggest using g_new(struct scsi_task, 1) here.
The following NULL check should be removed as it is not needed with g_malloc / g_new.

bsd-user/elfload.c static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex, · line 871

Quoted 13 lines
block/iscsi.c +0 -0
     if (acb->task == NULL) {
         error_report("iSCSI: Failed to allocate task for scsi command. %s",
                      iscsi_get_error(iscsi));
bsd-user/elfload.c +1 -1
index 0a6092b..74d7c79 100644
@@ -868,7 +868,7 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
             return ~(abi_ulong)0UL;
 
         elf_phdata =  (struct elf_phdr *)
-                malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
+                g_malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);

Use g_new and remove type cast and following NULL check.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1067

Quoted 10 lines
bsd-user/elfload.c +1 -2
         if (!elf_phdata)
           return ~((abi_ulong)0UL);
@@ -1064,13 +1064,13 @@ static void load_symbols(struct elfhdr *hdr, int fd)
 
  found:
     /* Now know where the strtab and symtab are.  Snarf them. */
-    s = malloc(sizeof(*s));
-    syms = malloc(symtab.sh_size);
+    s = g_malloc(sizeof(*s));

Maybe g_new here.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1068

Quoted 1 line
bsd-user/elfload.c +1 -0
+    syms = g_malloc(symtab.sh_size);

No NULL check needed.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1073

Quoted 6 lines
bsd-user/elfload.c +1 -1
     if (!syms) {
         free(s);
         return;
     }
-    s->disas_strtab = strings = malloc(strtab.sh_size);
+    s->disas_strtab = strings = g_malloc(strtab.sh_size);

No NULL check needed.

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, · line 1194

Quoted 9 lines
bsd-user/elfload.c +1 -1
     if (!s->disas_strtab) {
         free(s);
         free(syms);
@@ -1191,7 +1191,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
     }
 
     /* Now read in all of the header information */
-    elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
+    elf_phdata = g_new(elf_ex.e_phentsize * elf_ex.e_phnum);

g_new(elf_ex.e_phentsize, elf_ex.e_phnum)

No NULL check needed.

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, · line 1247

Quoted 9 lines
bsd-user/elfload.c +1 -1
     if (elf_phdata == NULL) {
         return -ENOMEM;
     }
@@ -1244,7 +1244,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
              * is an a.out format binary
              */
 
-            elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
+            elf_interpreter = g_new(elf_ppnt->p_filesz);

Wrong number of parameters.

No NULL check needed.

Quoted 21 lines
bsd-user/elfload.c +0 -0
             if (elf_interpreter == NULL) {
                 free (elf_phdata);
bsd-user/qemu.h +1 -1
index 03b502a..ada4360 100644
@@ -357,7 +357,7 @@ static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy
 #ifdef DEBUG_REMAP
     {
         void *addr;
-        addr = malloc(len);
+        addr = g_malloc(len);
         if (copy)
             memcpy(addr, g2h(guest_addr), len);
         else
configure +0 -0
index 2b32876..5df672b 100755

configure fi · line 3515

Quoted 5 lines
configure +1 -1
@@ -3512,7 +3512,7 @@ fi
 if test "$tcmalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }

I don't think that replacing malloc is correct here, because that test wants to test for libtcmalloc.

configure fi · line 3531

Quoted 9 lines
configure +1 -1
 EOF
 
   if compile_prog "" "-ltcmalloc" ; then
@@ -3528,7 +3528,7 @@ fi
 if test "$jemalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }

Similar case here, don't replace malloc for this test.

disas/ia64.c static struct ia64_opcode * · line 10271

Quoted 13 lines
configure +0 -0
 EOF
 
   if compile_prog "" "-ljemalloc" ; then
disas/ia64.c +1 -1
index 140754c..b0733ed 100644
@@ -10268,7 +10268,7 @@ static struct ia64_opcode *
 make_ia64_opcode (ia64_insn opcode, const char *name, int place, int depind)
 {
   struct ia64_opcode *res =
-    (struct ia64_opcode *) malloc (sizeof (struct ia64_opcode));
+    g_new(sizeof(struct ia64_opcode));

Wrong number (and value) of parameters for g_new.

disas/ia64.c static struct ia64_opcode * · line 10274

Quoted 3 lines
disas/ia64.c +0 -0
   res->name = strdup (name);
   res->type = main_table[place].opcode_type;
   res->num_outputs = main_table[place].num_outputs;

In general, for each replaced malloc there should normally be (at least) one replaced free (missing in your patch). Replacing malloc is a good thing in most cases (see file HACKING, section 3).

I suggest to split your patch in smaller parts which only replace malloc / free for one file and remove any type casts or NULL checks which then are no longer needed.

Those smaller parts can be sent fo qemu-trivial and will be accepted there after a successful review.

Kind regards,
Stefan W.

#4 lore raw

Thanks a lot for the feedback. I will do as you suggested. I have two more questions, though -

1. So after a patch is submitted, there will be a review in sometime by someone? Is that how I will come to know if my changes are correct?

2. If I split the patch into smaller parts and send to qemu-trivial, do I need to cc qemu-devel?

Warm regards,
Mriyam

Quoted 182 lines · Stefan Weil,

Am 12.03.2016 um 12:51 schrieb Mriyam Tamuli:

block/iscsi.c static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState

Quoted 14 lines
---
 block/iscsi.c      |  2 +-
 bsd-user/elfload.c | 12 ++++++------
 bsd-user/qemu.h    |  2 +-
 configure          |  4 ++--
 disas/ia64.c       |  2 +-
 5 files changed, 11 insertions(+), 11 deletions(-)
block/iscsi.c +0 -0
index 128ea79..2d6e5b4 100644
@@ -840,7 +840,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState

*bs,

block/iscsi.c static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, · line 843

Quoted 5 lines
block/iscsi.c +1 -1
         return &acb->common;
     }
 
-    acb->task = malloc(sizeof(struct scsi_task));
+    acb->task = g_malloc(sizeof(struct scsi_task));

I suggest using g_new(struct scsi_task, 1) here.
The following NULL check should be removed as it is not needed with g_malloc / g_new.

block/iscsi.c static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState · line 845

Quoted 2 lines
block/iscsi.c +0 -0
     if (acb->task == NULL) {
         error_report("iSCSI: Failed to allocate task for scsi command.

%s",

bsd-user/elfload.c static abi_ulong load_elf_interp(struct elfhdr *

Quoted 6 lines
block/iscsi.c +0 -0
                      iscsi_get_error(iscsi));
bsd-user/elfload.c +0 -0
index 0a6092b..74d7c79 100644
@@ -868,7 +868,7 @@ static abi_ulong load_elf_interp(struct elfhdr *

interp_elf_ex,

bsd-user/elfload.c static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,

Quoted 4 lines
bsd-user/elfload.c +0 -1
             return ~(abi_ulong)0UL;
 
         elf_phdata =  (struct elf_phdr *)
-                malloc(sizeof(struct elf_phdr) *

interp_elf_ex->e_phnum);

bsd-user/elfload.c static abi_ulong load_elf_interp(struct elfhdr * · line 871

Quoted 1 line
bsd-user/elfload.c +1 -0
+                g_malloc(sizeof(struct elf_phdr) *

interp_elf_ex->e_phnum);

Use g_new and remove type cast and following NULL check.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int

Quoted 4 lines
bsd-user/elfload.c +0 -0
         if (!elf_phdata)
           return ~((abi_ulong)0UL);
@@ -1064,13 +1064,13 @@ static void load_symbols(struct elfhdr *hdr, int

fd)

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1067

Quoted 6 lines
bsd-user/elfload.c +1 -2
  found:
     /* Now know where the strtab and symtab are.  Snarf them. */
-    s = malloc(sizeof(*s));
-    syms = malloc(symtab.sh_size);
+    s = g_malloc(sizeof(*s));

Maybe g_new here.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1068

Quoted 1 line
bsd-user/elfload.c +1 -0
+    syms = g_malloc(symtab.sh_size);

No NULL check needed.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1073

Quoted 6 lines
bsd-user/elfload.c +1 -1
     if (!syms) {
         free(s);
         return;
     }
-    s->disas_strtab = strings = malloc(strtab.sh_size);
+    s->disas_strtab = strings = g_malloc(strtab.sh_size);

No NULL check needed.

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm,

Quoted 4 lines
bsd-user/elfload.c +0 -0
     if (!s->disas_strtab) {
         free(s);
         free(syms);
@@ -1191,7 +1191,7 @@ int load_elf_binary(struct linux_binprm * bprm,

struct target_pt_regs * regs,

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,

Quoted 4 lines
bsd-user/elfload.c +0 -1
     }
 
     /* Now read in all of the header information */
-    elf_phdata = (struct elf_phdr

*)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, · line 1194

Quoted 1 line
bsd-user/elfload.c +1 -0
+    elf_phdata = g_new(elf_ex.e_phentsize * elf_ex.e_phnum);

g_new(elf_ex.e_phentsize, elf_ex.e_phnum)

No NULL check needed.

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm,

Quoted 4 lines
bsd-user/elfload.c +0 -0
     if (elf_phdata == NULL) {
         return -ENOMEM;
     }
@@ -1244,7 +1244,7 @@ int load_elf_binary(struct linux_binprm * bprm,

struct target_pt_regs * regs,

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, · line 1247

Quoted 5 lines
bsd-user/elfload.c +1 -1
              * is an a.out format binary
              */
 
-            elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
+            elf_interpreter = g_new(elf_ppnt->p_filesz);

Wrong number of parameters.

No NULL check needed.

bsd-user/qemu.h static inline void *lock_user(int type, abi_ulong

Quoted 8 lines
bsd-user/elfload.c +0 -0
             if (elf_interpreter == NULL) {
                 free (elf_phdata);
bsd-user/qemu.h +0 -0
index 03b502a..ada4360 100644
@@ -357,7 +357,7 @@ static inline void *lock_user(int type, abi_ulong

guest_addr, long len, int copy

Quoted 13 lines
bsd-user/qemu.h +1 -1
 #ifdef DEBUG_REMAP
     {
         void *addr;
-        addr = malloc(len);
+        addr = g_malloc(len);
         if (copy)
             memcpy(addr, g2h(guest_addr), len);
         else
configure +0 -0
index 2b32876..5df672b 100755

configure fi · line 3515

Quoted 5 lines
configure +1 -1
@@ -3512,7 +3512,7 @@ fi
 if test "$tcmalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }

I don't think that replacing malloc is correct here, because that test wants to test for libtcmalloc.

configure fi · line 3531

Quoted 9 lines
configure +1 -1
 EOF
 
   if compile_prog "" "-ltcmalloc" ; then
@@ -3528,7 +3528,7 @@ fi
 if test "$jemalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }

Similar case here, don't replace malloc for this test.

disas/ia64.c static struct ia64_opcode * · line 10268

Quoted 9 lines
configure +0 -0
 EOF
 
   if compile_prog "" "-ljemalloc" ; then
disas/ia64.c +0 -0
index 140754c..b0733ed 100644
@@ -10268,7 +10268,7 @@ static struct ia64_opcode *
 make_ia64_opcode (ia64_insn opcode, const char *name, int place, int

depind)

disas/ia64.c static struct ia64_opcode * · line 10271

Quoted 4 lines
disas/ia64.c +1 -1
 {
   struct ia64_opcode *res =
-    (struct ia64_opcode *) malloc (sizeof (struct ia64_opcode));
+    g_new(sizeof(struct ia64_opcode));

Wrong number (and value) of parameters for g_new.

disas/ia64.c static struct ia64_opcode * · line 10274

Quoted 3 lines
disas/ia64.c +0 -0
   res->name = strdup (name);
   res->type = main_table[place].opcode_type;
   res->num_outputs = main_table[place].num_outputs;

In general, for each replaced malloc there should normally be (at least) one replaced free (missing in your patch). Replacing malloc is a good thing in most cases (see file HACKING, section 3).

I suggest to split your patch in smaller parts which only replace malloc / free for one file and remove any type casts or NULL checks which then are no longer needed.

Those smaller parts can be sent fo qemu-trivial and will be accepted there after a successful review.

Kind regards,
Stefan W.

#5 lore raw

Am 12.03.2016 um 18:53 schrieb Mriyam Tamuli:

Quoted 5 lines

Thanks a lot for the feedback. I will do as you suggested. I have two more questions, though -

1. So after a patch is submitted, there will be a review in sometime by someone? Is that how I will come to know if my changes are correct?

Yes, that's how it should work. But it can also happen that a patch does not get any attention. In that case you can send a polite reminder.

See http://wiki.qemu.org/Contribute/SubmitAPatch for more important information on submitting patches.

Quoted 3 lines

2. If I split the patch into smaller parts and send to qemu-trivial, do I need to cc qemu-devel?

Yes, trivial patches should be sent to qemu-trivial, qemu-devel and any maintainers of the modified code.

Stefan

#6 lore raw
In reply to Mriyam Tamuli
Quoted 5 lines · Mriyam Tamuli

Thanks a lot for the feedback. I will do as you suggested. I have two more questions, though -

1. So after a patch is submitted, there will be a review in sometime by someone? Is that how I will come to know if my changes are correct?

To answer the first part first. You are responsible for testing your changes. For simple things like this a compile check is usually enough (although make check is good practice).

For more involved changes you should either ensure the code is exercised by the unit test code or manually devise a test that exercises the code paths you have just changed. For some more involved things this can be quite hard to do - it is not unknown to add printfs as a temporary patch to check something gets exercised while booting a kernel and then removing the printfs for the submission ;-)

Quoted 2 lines

2. If I split the patch into smaller parts and send to qemu-trivial, do I need to cc qemu-devel?

Generally yes. qemu-devel is the master development mailing list. Other lists like qemu-trivial and qemu-$ARCH are convenience lists that help with sorting patches out for reviewers.

Quoted 188 lines

Warm regards,
Mriyam

Quoted 182 lines · Stefan Weil,

Am 12.03.2016 um 12:51 schrieb Mriyam Tamuli:

block/iscsi.c static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState

Quoted 14 lines
---
 block/iscsi.c      |  2 +-
 bsd-user/elfload.c | 12 ++++++------
 bsd-user/qemu.h    |  2 +-
 configure          |  4 ++--
 disas/ia64.c       |  2 +-
 5 files changed, 11 insertions(+), 11 deletions(-)
block/iscsi.c +0 -0
index 128ea79..2d6e5b4 100644
@@ -840,7 +840,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState

*bs,

block/iscsi.c static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, · line 843

Quoted 5 lines
block/iscsi.c +1 -1
         return &acb->common;
     }
 
-    acb->task = malloc(sizeof(struct scsi_task));
+    acb->task = g_malloc(sizeof(struct scsi_task));

I suggest using g_new(struct scsi_task, 1) here.
The following NULL check should be removed as it is not needed with g_malloc / g_new.

block/iscsi.c static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState · line 845

Quoted 2 lines
block/iscsi.c +0 -0
     if (acb->task == NULL) {
         error_report("iSCSI: Failed to allocate task for scsi command.

%s",

bsd-user/elfload.c static abi_ulong load_elf_interp(struct elfhdr *

Quoted 6 lines
block/iscsi.c +0 -0
                      iscsi_get_error(iscsi));
bsd-user/elfload.c +0 -0
index 0a6092b..74d7c79 100644
@@ -868,7 +868,7 @@ static abi_ulong load_elf_interp(struct elfhdr *

interp_elf_ex,

bsd-user/elfload.c static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,

Quoted 4 lines
bsd-user/elfload.c +0 -1
             return ~(abi_ulong)0UL;
 
         elf_phdata =  (struct elf_phdr *)
-                malloc(sizeof(struct elf_phdr) *

interp_elf_ex->e_phnum);

bsd-user/elfload.c static abi_ulong load_elf_interp(struct elfhdr * · line 871

Quoted 1 line
bsd-user/elfload.c +1 -0
+                g_malloc(sizeof(struct elf_phdr) *

interp_elf_ex->e_phnum);

Use g_new and remove type cast and following NULL check.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int

Quoted 4 lines
bsd-user/elfload.c +0 -0
         if (!elf_phdata)
           return ~((abi_ulong)0UL);
@@ -1064,13 +1064,13 @@ static void load_symbols(struct elfhdr *hdr, int

fd)

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1067

Quoted 6 lines
bsd-user/elfload.c +1 -2
  found:
     /* Now know where the strtab and symtab are.  Snarf them. */
-    s = malloc(sizeof(*s));
-    syms = malloc(symtab.sh_size);
+    s = g_malloc(sizeof(*s));

Maybe g_new here.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1068

Quoted 1 line
bsd-user/elfload.c +1 -0
+    syms = g_malloc(symtab.sh_size);

No NULL check needed.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1073

Quoted 6 lines
bsd-user/elfload.c +1 -1
     if (!syms) {
         free(s);
         return;
     }
-    s->disas_strtab = strings = malloc(strtab.sh_size);
+    s->disas_strtab = strings = g_malloc(strtab.sh_size);

No NULL check needed.

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm,

Quoted 4 lines
bsd-user/elfload.c +0 -0
     if (!s->disas_strtab) {
         free(s);
         free(syms);
@@ -1191,7 +1191,7 @@ int load_elf_binary(struct linux_binprm * bprm,

struct target_pt_regs * regs,

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,

Quoted 4 lines
bsd-user/elfload.c +0 -1
     }
 
     /* Now read in all of the header information */
-    elf_phdata = (struct elf_phdr

*)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, · line 1194

Quoted 1 line
bsd-user/elfload.c +1 -0
+    elf_phdata = g_new(elf_ex.e_phentsize * elf_ex.e_phnum);

g_new(elf_ex.e_phentsize, elf_ex.e_phnum)

No NULL check needed.

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm,

Quoted 4 lines
bsd-user/elfload.c +0 -0
     if (elf_phdata == NULL) {
         return -ENOMEM;
     }
@@ -1244,7 +1244,7 @@ int load_elf_binary(struct linux_binprm * bprm,

struct target_pt_regs * regs,

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, · line 1247

Quoted 5 lines
bsd-user/elfload.c +1 -1
              * is an a.out format binary
              */
 
-            elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
+            elf_interpreter = g_new(elf_ppnt->p_filesz);

Wrong number of parameters.

No NULL check needed.

bsd-user/qemu.h static inline void *lock_user(int type, abi_ulong

Quoted 8 lines
bsd-user/elfload.c +0 -0
             if (elf_interpreter == NULL) {
                 free (elf_phdata);
bsd-user/qemu.h +0 -0
index 03b502a..ada4360 100644
@@ -357,7 +357,7 @@ static inline void *lock_user(int type, abi_ulong

guest_addr, long len, int copy

Quoted 13 lines
bsd-user/qemu.h +1 -1
 #ifdef DEBUG_REMAP
     {
         void *addr;
-        addr = malloc(len);
+        addr = g_malloc(len);
         if (copy)
             memcpy(addr, g2h(guest_addr), len);
         else
configure +0 -0
index 2b32876..5df672b 100755

configure fi · line 3515

Quoted 5 lines
configure +1 -1
@@ -3512,7 +3512,7 @@ fi
 if test "$tcmalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }

I don't think that replacing malloc is correct here, because that test wants to test for libtcmalloc.

configure fi · line 3531

Quoted 9 lines
configure +1 -1
 EOF
 
   if compile_prog "" "-ltcmalloc" ; then
@@ -3528,7 +3528,7 @@ fi
 if test "$jemalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }

Similar case here, don't replace malloc for this test.

disas/ia64.c static struct ia64_opcode * · line 10268

Quoted 9 lines
configure +0 -0
 EOF
 
   if compile_prog "" "-ljemalloc" ; then
disas/ia64.c +0 -0
index 140754c..b0733ed 100644
@@ -10268,7 +10268,7 @@ static struct ia64_opcode *
 make_ia64_opcode (ia64_insn opcode, const char *name, int place, int

depind)

disas/ia64.c static struct ia64_opcode * · line 10271

Quoted 4 lines
disas/ia64.c +1 -1
 {
   struct ia64_opcode *res =
-    (struct ia64_opcode *) malloc (sizeof (struct ia64_opcode));
+    g_new(sizeof(struct ia64_opcode));

Wrong number (and value) of parameters for g_new.

disas/ia64.c static struct ia64_opcode * · line 10274

Quoted 3 lines
disas/ia64.c +0 -0
   res->name = strdup (name);
   res->type = main_table[place].opcode_type;
   res->num_outputs = main_table[place].num_outputs;

In general, for each replaced malloc there should normally be (at least) one replaced free (missing in your patch). Replacing malloc is a good thing in most cases (see file HACKING, section 3).

I suggest to split your patch in smaller parts which only replace malloc / free for one file and remove any type casts or NULL checks which then are no longer needed.

Those smaller parts can be sent fo qemu-trivial and will be accepted there after a successful review.

Kind regards,
Stefan W.

--
Alex Bennée

#7 lore raw
In reply to Mriyam Tamuli
Quoted 1 line · Mriyam Tamuli

While this is a good change you still need to ensure any corresponding free's are converted to g_free (and g_delete for g_new).

Quoted 13 lines
---
 block/iscsi.c      |  2 +-
 bsd-user/elfload.c | 12 ++++++------
 bsd-user/qemu.h    |  2 +-
 configure          |  4 ++--
 disas/ia64.c       |  2 +-
 5 files changed, 11 insertions(+), 11 deletions(-)
block/iscsi.c +0 -0
index 128ea79..2d6e5b4 100644

block/iscsi.c static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, · line 844

Quoted 6 lines
block/iscsi.c +1 -1
@@ -840,7 +840,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
         return &acb->common;
     }
 
-    acb->task = malloc(sizeof(struct scsi_task));
+    acb->task = g_malloc(sizeof(struct scsi_task));
     if (acb->task == NULL) {

g_malloc can't fail so the error leg can be removed.

Quoted 7 lines
block/iscsi.c +0 -0
         error_report("iSCSI: Failed to allocate task for scsi command. %s",
                      iscsi_get_error(iscsi));
bsd-user/elfload.c +0 -0
index 0a6092b..74d7c79 100644

bsd-user/elfload.c static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex, · line 874

Quoted 8 lines
bsd-user/elfload.c +1 -1
@@ -868,7 +868,7 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
             return ~(abi_ulong)0UL;
 
         elf_phdata =  (struct elf_phdr *)
-                malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
+                g_malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
 
         if (!elf_phdata)
           return ~((abi_ulong)0UL);

Again error checking becomes redundant.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1072

Quoted 12 lines
bsd-user/elfload.c +2 -2
@@ -1064,13 +1064,13 @@ static void load_symbols(struct elfhdr *hdr, int fd)
 
  found:
     /* Now know where the strtab and symtab are.  Snarf them. */
-    s = malloc(sizeof(*s));
-    syms = malloc(symtab.sh_size);
+    s = g_malloc(sizeof(*s));
+    syms = g_malloc(symtab.sh_size);
     if (!syms) {
         free(s);
         return;
     }

And here.

bsd-user/elfload.c static void load_symbols(struct elfhdr *hdr, int fd) · line 1076

Quoted 5 lines
bsd-user/elfload.c +1 -1
-    s->disas_strtab = strings = malloc(strtab.sh_size);
+    s->disas_strtab = strings = g_malloc(strtab.sh_size);
     if (!s->disas_strtab) {
         free(s);
         free(syms);

And here

Quoted 10 lines
bsd-user/elfload.c +1 -1
@@ -1191,7 +1191,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
     }
 
     /* Now read in all of the header information */
-    elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
+    elf_phdata = g_new(elf_ex.e_phentsize * elf_ex.e_phnum);
     if (elf_phdata == NULL) {
         return -ENOMEM;
     }

bsd-user/elfload.c int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, · line 1249

Quoted 7 lines
bsd-user/elfload.c +1 -1
@@ -1244,7 +1244,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
              * is an a.out format binary
              */
 
-            elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
+            elf_interpreter = g_new(elf_ppnt->p_filesz);
 
             if (elf_interpreter == NULL) {

And here.

Quoted 19 lines
bsd-user/elfload.c +0 -0
                 free (elf_phdata);
bsd-user/qemu.h +1 -1
index 03b502a..ada4360 100644
@@ -357,7 +357,7 @@ static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy
 #ifdef DEBUG_REMAP
     {
         void *addr;
-        addr = malloc(len);
+        addr = g_malloc(len);
         if (copy)
             memcpy(addr, g2h(guest_addr), len);
         else
configure +0 -0
index 2b32876..5df672b 100755

configure fi · line 3515

Quoted 5 lines
configure +1 -1
@@ -3512,7 +3512,7 @@ fi
 if test "$tcmalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }

I wouldn't touch the configure cases. There are used for probing the library support and in this case I don't see how it woould work considering only stdlib.h was included.

Quoted 17 lines
configure +1 -1
 EOF
 
   if compile_prog "" "-ltcmalloc" ; then
@@ -3528,7 +3528,7 @@ fi
 if test "$jemalloc" = "yes" ; then
   cat > $TMPC << EOF
 #include <stdlib.h>
-int main(void) { malloc(1); return 0; }
+int main(void) { g_malloc(1); return 0; }
 EOF
 
   if compile_prog "" "-ljemalloc" ; then
disas/ia64.c +0 -0
index 140754c..b0733ed 100644

disas/ia64.c static struct ia64_opcode * · line 10274

Quoted 8 lines
disas/ia64.c +1 -1
@@ -10268,7 +10268,7 @@ static struct ia64_opcode *
 make_ia64_opcode (ia64_insn opcode, const char *name, int place, int depind)
 {
   struct ia64_opcode *res =
-    (struct ia64_opcode *) malloc (sizeof (struct ia64_opcode));
+    g_new(sizeof(struct ia64_opcode));
   res->name = strdup (name);
   res->type = main_table[place].opcode_type;
   res->num_outputs = main_table[place].num_outputs;

--
Alex Bennée