Skip to contentSkip to conversation

linux-user: Use g_new() & friends where that makes obvious sense

PATCH Applied Markus Armbruster 4 participants 7 messages Sep 14, 2015 – Oct 3, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Use g_new() & friends where that makes obvious sense

g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.

This commit only touches allocations with size arguments of the form sizeof(T). Same Coccinelle semantic patchas in commit b45c03f.

---
 linux-user/elfload.c | 2 +-
 linux-user/main.c    | 2 +-
 linux-user/syscall.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
linux-user/elfload.c +1 -1
index eca0c7f..6584f0b 100644
@@ -2848,7 +2848,7 @@ static int fill_note_info(struct elf_note_info *info,
     TaskState *ts = (TaskState *)cpu->opaque;
     int i;
 
-    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));
linux-user/main.c +1 -1
index 829ee64..1ff821d 100644
@@ -4014,7 +4014,7 @@ int main(int argc, char **argv, char **envp)
     }
     target_argv[target_argc] = NULL;
 
-    ts = g_malloc0 (sizeof(TaskState));
+    ts = g_new0(TaskState, 1);
     init_task_state(ts);
     /* build Task State */
     ts->info = info;
linux-user/syscall.c +1 -1
index c0fabf7..60c62b8 100644
@@ -4560,7 +4560,7 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
         new_thread_info info;
         pthread_attr_t attr;
 
-        ts = g_malloc0(sizeof(TaskState));
+        ts = g_new0(TaskState, 1);
         init_task_state(ts);
         /* we create a new CPU instance. */
         new_env = cpu_copy(env);
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Markus Armbruster

g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.

This commit only touches allocations with size arguments of the form sizeof(T). Same Coccinelle semantic patchas in commit b45c03f.

Quoted 6 lines
---
 linux-user/elfload.c | 2 +-
 linux-user/main.c    | 2 +-
 linux-user/syscall.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
Quoted 8 lines · Eric Blake
Quoted 7 lines · Markus Armbruster

g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.

This commit only touches allocations with size arguments of the form sizeof(T). Same Coccinelle semantic patchas in commit b45c03f.

s/patchas/patch as/

Quoted 10 lines
Quoted 7 lines
---
 linux-user/elfload.c | 2 +-
 linux-user/main.c    | 2 +-
 linux-user/syscall.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw
In reply to Markus Armbruster

Ping?

Quoted 54 lines · Markus Armbruster

g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.

This commit only touches allocations with size arguments of the form sizeof(T). Same Coccinelle semantic patchas in commit b45c03f.

---
 linux-user/elfload.c | 2 +-
 linux-user/main.c    | 2 +-
 linux-user/syscall.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
linux-user/elfload.c +1 -1
index eca0c7f..6584f0b 100644
@@ -2848,7 +2848,7 @@ static int fill_note_info(struct elf_note_info *info,
     TaskState *ts = (TaskState *)cpu->opaque;
     int i;
 
-    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));
linux-user/main.c +1 -1
index 829ee64..1ff821d 100644
@@ -4014,7 +4014,7 @@ int main(int argc, char **argv, char **envp)
     }
     target_argv[target_argc] = NULL;
 
-    ts = g_malloc0 (sizeof(TaskState));
+    ts = g_new0(TaskState, 1);
     init_task_state(ts);
     /* build Task State */
     ts->info = info;
linux-user/syscall.c +1 -1
index c0fabf7..60c62b8 100644
@@ -4560,7 +4560,7 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
         new_thread_info info;
         pthread_attr_t attr;
 
-        ts = g_malloc0(sizeof(TaskState));
+        ts = g_new0(TaskState, 1);
         init_task_state(ts);
         /* we create a new CPU instance. */
         new_env = cpu_copy(env);
Reviewed-by
#4 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Am 24.09.2015 um 09:06 schrieb Markus Armbruster:

Quoted 17 lines

Ping?

Quoted 13 lines · Markus Armbruster

g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.

This commit only touches allocations with size arguments of the form sizeof(T). Same Coccinelle semantic patchas in commit b45c03f.

linux-user/elfload.c | 2 +- linux-user/main.c | 2 +-
linux-user/syscall.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)

This patch is trivial enough for qemu-trivial.

As Eric has written, the commit message needs
a small correction: s/patchas/patch as/

Regards
Stefan

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCAAGBQJWA6OuAAoJEOCMIdVndFCt6Q8P/R6kKihtOvr17lL3Av117n1z YPv6Tfa9jy1fd3qaeIMhXgm63CNTWTGU4o0A9/k3thqiD9kYVpqeZkd7gt1gE6uU QR/eReEMZNr+5AnkYnCmZ8wr5upd4RWUjCvbGSUpFgb1ui+VtwZ08XzCVehQfY39 2S6EWxR/I7ETxzue+AFZak9w4QqVF7IQmw7CPkeTCD/himdCpyeGxL9dYQnyfEdW mQeXUR2eipaqwIt1b6JkwQCU78rY2D/OLVDipZK9M4BSmJQWS2BsLboc+VC59bN9 JkVrKgSbuciqBMXDudY71qv9XO/wyM8MwnDM5GY7V9N1PoRVcg2mvSCRKq9lP82K V0Sb6n1j9/AgM3ngnLZqkiVYkeFrnccIkiKCJvmDnDP1ClC8Cy5V1wkPzk7TJgTK 4xvuI3kfWPIzbphWzZtcCstJKYaO5ydrEGGNGko37A6s65oE+gIhyrv/KSIhix+H n7KMOILY4wuk7UYoIgK6cvKL6QsVyhw0lR+RIR8p33ofAj20HylQng38h6spr50F XTl3J38ApbyxNhw+EJHXXwj14YJFrZTwN8C0X49UHkSD4wqR4lH+2kdNUYIYJJHR Zx+HkJj/8hLQq14WjtgZlNQ7noAck/yh9Rvyu54v6yfx1RAhNkDbiysQKgqkWzNM U5ro2khoY2BrI7Bdq29H
=/Z+4
-----END PGP SIGNATURE-----

#5 lore raw
Quoted 14 lines · Stefan Weil

Am 24.09.2015 um 09:06 schrieb Markus Armbruster:

Quoted 13 lines

Ping?

Quoted 9 lines · Markus Armbruster

g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.

This commit only touches allocations with size arguments of the form sizeof(T). Same Coccinelle semantic patchas in commit b45c03f.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

linux-user/elfload.c | 2 +- linux-user/main.c | 2 +-
linux-user/syscall.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)

This patch is trivial enough for qemu-trivial.

I guess it is. For code with a maintainer, I tend to try the maintainer first.

Quoted 2 lines

As Eric has written, the commit message needs
a small correction: s/patchas/patch as/

Yes.

#6 lore raw
In reply to Stefan Weil
Quoted 9 lines · Stefan Weil

Am 24.09.2015 um 09:06 schrieb Markus Armbruster:

Quoted 8 lines

Ping?

Quoted 4 lines · Markus Armbruster

g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors.

Applied to -trivial, with a trivial commit message fix. Thanks!

/mjt