* [Qemu-devel] [PATCH] qemu-char: Use g_new() & friends where that makes obvious sense
@ 2015-09-14 11:54 Markus Armbruster
2015-09-14 15:02 ` Paolo Bonzini
2015-09-14 15:56 ` Eric Blake
0 siblings, 2 replies; 4+ messages in thread
From: Markus Armbruster @ 2015-09-14 11:54 UTC (permalink / raw)
To: qemu-devel; +Cc: pbonzini
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.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
backends/testdev.c | 4 ++--
qemu-char.c | 22 +++++++++++-----------
2 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/backends/testdev.c b/backends/testdev.c
index eba396a..1429152 100644
--- a/backends/testdev.c
+++ b/backends/testdev.c
@@ -113,8 +113,8 @@ CharDriverState *chr_testdev_init(void)
TestdevCharState *testdev;
CharDriverState *chr;
- testdev = g_malloc0(sizeof(TestdevCharState));
- testdev->chr = chr = g_malloc0(sizeof(CharDriverState));
+ testdev = g_new0(TestdevCharState, 1);
+ testdev->chr = chr = g_new0(CharDriverState, 1);
chr->opaque = testdev;
chr->chr_write = testdev_write;
diff --git a/qemu-char.c b/qemu-char.c
index dd83203..653ea10 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -685,7 +685,7 @@ static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
MuxDriver *d;
chr = qemu_chr_alloc();
- d = g_malloc0(sizeof(MuxDriver));
+ d = g_new0(MuxDriver, 1);
chr->opaque = d;
d->drv = drv;
@@ -1064,7 +1064,7 @@ static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
FDCharDriver *s;
chr = qemu_chr_alloc();
- s = g_malloc0(sizeof(FDCharDriver));
+ s = g_new0(FDCharDriver, 1);
s->fd_in = io_channel_from_fd(fd_in);
s->fd_out = io_channel_from_fd(fd_out);
qemu_set_nonblock(fd_out);
@@ -1413,7 +1413,7 @@ static CharDriverState *qemu_chr_open_pty(const char *id,
fprintf(stderr, "char device redirected to %s (label %s)\n",
pty_name, id);
- s = g_malloc0(sizeof(PtyCharDriver));
+ s = g_new0(PtyCharDriver, 1);
chr->opaque = s;
chr->chr_write = pty_chr_write;
chr->chr_update_read_handler = pty_chr_update_read_handler;
@@ -1762,7 +1762,7 @@ static CharDriverState *qemu_chr_open_pp_fd(int fd)
return NULL;
}
- drv = g_malloc0(sizeof(ParallelCharDriver));
+ drv = g_new0(ParallelCharDriver, 1);
drv->fd = fd;
drv->mode = IEEE1284_MODE_COMPAT;
@@ -2050,7 +2050,7 @@ static CharDriverState *qemu_chr_open_win_path(const char *filename)
WinCharState *s;
chr = qemu_chr_alloc();
- s = g_malloc0(sizeof(WinCharState));
+ s = g_new0(WinCharState, 1);
chr->opaque = s;
chr->chr_write = win_chr_write;
chr->chr_close = win_chr_close;
@@ -2149,7 +2149,7 @@ static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
WinCharState *s;
chr = qemu_chr_alloc();
- s = g_malloc0(sizeof(WinCharState));
+ s = g_new0(WinCharState, 1);
chr->opaque = s;
chr->chr_write = win_chr_write;
chr->chr_close = win_chr_close;
@@ -2168,7 +2168,7 @@ static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
WinCharState *s;
chr = qemu_chr_alloc();
- s = g_malloc0(sizeof(WinCharState));
+ s = g_new0(WinCharState, 1);
s->hcom = fd_out;
chr->opaque = s;
chr->chr_write = win_chr_write;
@@ -2324,7 +2324,7 @@ static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
int is_console = 0;
chr = qemu_chr_alloc();
- stdio = g_malloc0(sizeof(WinStdioCharState));
+ stdio = g_new0(WinStdioCharState, 1);
stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
@@ -2487,7 +2487,7 @@ static CharDriverState *qemu_chr_open_udp_fd(int fd)
NetCharDriver *s = NULL;
chr = qemu_chr_alloc();
- s = g_malloc0(sizeof(NetCharDriver));
+ s = g_new0(NetCharDriver, 1);
s->fd = fd;
s->chan = io_channel_from_socket(s->fd);
@@ -2713,7 +2713,7 @@ static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
g_free(s->write_msgfds);
if (num) {
- s->write_msgfds = g_malloc(num * sizeof(int));
+ s->write_msgfds = g_new(int, num);
memcpy(s->write_msgfds, fds, num * sizeof(int));
}
@@ -4144,7 +4144,7 @@ static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
chr = qemu_chr_alloc();
- s = g_malloc0(sizeof(TCPCharDriver));
+ s = g_new0(TCPCharDriver, 1);
s->fd = -1;
s->listen_fd = -1;
--
2.4.3
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] qemu-char: Use g_new() & friends where that makes obvious sense
2015-09-14 11:54 [Qemu-devel] [PATCH] qemu-char: Use g_new() & friends where that makes obvious sense Markus Armbruster
@ 2015-09-14 15:02 ` Paolo Bonzini
2015-09-14 15:56 ` Eric Blake
1 sibling, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2015-09-14 15:02 UTC (permalink / raw)
To: Markus Armbruster, qemu-devel
On 14/09/2015 13:54, Markus Armbruster wrote:
> 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.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
> backends/testdev.c | 4 ++--
> qemu-char.c | 22 +++++++++++-----------
> 2 files changed, 13 insertions(+), 13 deletions(-)
>
> diff --git a/backends/testdev.c b/backends/testdev.c
> index eba396a..1429152 100644
> --- a/backends/testdev.c
> +++ b/backends/testdev.c
> @@ -113,8 +113,8 @@ CharDriverState *chr_testdev_init(void)
> TestdevCharState *testdev;
> CharDriverState *chr;
>
> - testdev = g_malloc0(sizeof(TestdevCharState));
> - testdev->chr = chr = g_malloc0(sizeof(CharDriverState));
> + testdev = g_new0(TestdevCharState, 1);
> + testdev->chr = chr = g_new0(CharDriverState, 1);
>
> chr->opaque = testdev;
> chr->chr_write = testdev_write;
> diff --git a/qemu-char.c b/qemu-char.c
> index dd83203..653ea10 100644
> --- a/qemu-char.c
> +++ b/qemu-char.c
> @@ -685,7 +685,7 @@ static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
> MuxDriver *d;
>
> chr = qemu_chr_alloc();
> - d = g_malloc0(sizeof(MuxDriver));
> + d = g_new0(MuxDriver, 1);
>
> chr->opaque = d;
> d->drv = drv;
> @@ -1064,7 +1064,7 @@ static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
> FDCharDriver *s;
>
> chr = qemu_chr_alloc();
> - s = g_malloc0(sizeof(FDCharDriver));
> + s = g_new0(FDCharDriver, 1);
> s->fd_in = io_channel_from_fd(fd_in);
> s->fd_out = io_channel_from_fd(fd_out);
> qemu_set_nonblock(fd_out);
> @@ -1413,7 +1413,7 @@ static CharDriverState *qemu_chr_open_pty(const char *id,
> fprintf(stderr, "char device redirected to %s (label %s)\n",
> pty_name, id);
>
> - s = g_malloc0(sizeof(PtyCharDriver));
> + s = g_new0(PtyCharDriver, 1);
> chr->opaque = s;
> chr->chr_write = pty_chr_write;
> chr->chr_update_read_handler = pty_chr_update_read_handler;
> @@ -1762,7 +1762,7 @@ static CharDriverState *qemu_chr_open_pp_fd(int fd)
> return NULL;
> }
>
> - drv = g_malloc0(sizeof(ParallelCharDriver));
> + drv = g_new0(ParallelCharDriver, 1);
> drv->fd = fd;
> drv->mode = IEEE1284_MODE_COMPAT;
>
> @@ -2050,7 +2050,7 @@ static CharDriverState *qemu_chr_open_win_path(const char *filename)
> WinCharState *s;
>
> chr = qemu_chr_alloc();
> - s = g_malloc0(sizeof(WinCharState));
> + s = g_new0(WinCharState, 1);
> chr->opaque = s;
> chr->chr_write = win_chr_write;
> chr->chr_close = win_chr_close;
> @@ -2149,7 +2149,7 @@ static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
> WinCharState *s;
>
> chr = qemu_chr_alloc();
> - s = g_malloc0(sizeof(WinCharState));
> + s = g_new0(WinCharState, 1);
> chr->opaque = s;
> chr->chr_write = win_chr_write;
> chr->chr_close = win_chr_close;
> @@ -2168,7 +2168,7 @@ static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
> WinCharState *s;
>
> chr = qemu_chr_alloc();
> - s = g_malloc0(sizeof(WinCharState));
> + s = g_new0(WinCharState, 1);
> s->hcom = fd_out;
> chr->opaque = s;
> chr->chr_write = win_chr_write;
> @@ -2324,7 +2324,7 @@ static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
> int is_console = 0;
>
> chr = qemu_chr_alloc();
> - stdio = g_malloc0(sizeof(WinStdioCharState));
> + stdio = g_new0(WinStdioCharState, 1);
>
> stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
> if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
> @@ -2487,7 +2487,7 @@ static CharDriverState *qemu_chr_open_udp_fd(int fd)
> NetCharDriver *s = NULL;
>
> chr = qemu_chr_alloc();
> - s = g_malloc0(sizeof(NetCharDriver));
> + s = g_new0(NetCharDriver, 1);
>
> s->fd = fd;
> s->chan = io_channel_from_socket(s->fd);
> @@ -2713,7 +2713,7 @@ static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num)
> g_free(s->write_msgfds);
>
> if (num) {
> - s->write_msgfds = g_malloc(num * sizeof(int));
> + s->write_msgfds = g_new(int, num);
> memcpy(s->write_msgfds, fds, num * sizeof(int));
> }
>
> @@ -4144,7 +4144,7 @@ static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
> int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
>
> chr = qemu_chr_alloc();
> - s = g_malloc0(sizeof(TCPCharDriver));
> + s = g_new0(TCPCharDriver, 1);
>
> s->fd = -1;
> s->listen_fd = -1;
>
Thanks, applied locally (pull request will come later this week).
Paolo
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] qemu-char: Use g_new() & friends where that makes obvious sense
2015-09-14 11:54 [Qemu-devel] [PATCH] qemu-char: Use g_new() & friends where that makes obvious sense Markus Armbruster
2015-09-14 15:02 ` Paolo Bonzini
@ 2015-09-14 15:56 ` Eric Blake
2015-09-14 16:09 ` Eric Blake
1 sibling, 1 reply; 4+ messages in thread
From: Eric Blake @ 2015-09-14 15:56 UTC (permalink / raw)
To: Markus Armbruster, qemu-devel; +Cc: pbonzini
[-- Attachment #1: Type: text/plain, Size: 796 bytes --]
On 09/14/2015 05:54 AM, Markus Armbruster wrote:
> 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.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
> backends/testdev.c | 4 ++--
> qemu-char.c | 22 +++++++++++-----------
> 2 files changed, 13 insertions(+), 13 deletions(-)
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH] qemu-char: Use g_new() & friends where that makes obvious sense
2015-09-14 15:56 ` Eric Blake
@ 2015-09-14 16:09 ` Eric Blake
0 siblings, 0 replies; 4+ messages in thread
From: Eric Blake @ 2015-09-14 16:09 UTC (permalink / raw)
To: Markus Armbruster, qemu-devel; +Cc: pbonzini
[-- Attachment #1: Type: text/plain, Size: 885 bytes --]
On 09/14/2015 09:56 AM, Eric Blake wrote:
> On 09/14/2015 05:54 AM, Markus Armbruster wrote:
>> 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/
>>
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>> backends/testdev.c | 4 ++--
>> qemu-char.c | 22 +++++++++++-----------
>> 2 files changed, 13 insertions(+), 13 deletions(-)
>
> Reviewed-by: Eric Blake <eblake@redhat.com>
>
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-09-14 16:09 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-14 11:54 [Qemu-devel] [PATCH] qemu-char: Use g_new() & friends where that makes obvious sense Markus Armbruster
2015-09-14 15:02 ` Paolo Bonzini
2015-09-14 15:56 ` Eric Blake
2015-09-14 16:09 ` Eric Blake
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).