* [Qemu-devel] [PATCH] audio: Use g_new() & friends where that makes obvious sense
@ 2015-09-14 11:03 Markus Armbruster
2015-09-14 15:25 ` Eric Blake
2015-09-24 7:02 ` Markus Armbruster
0 siblings, 2 replies; 3+ messages in thread
From: Markus Armbruster @ 2015-09-14 11:03 UTC (permalink / raw)
To: qemu-devel; +Cc: kraxel
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 patch as in commit b45c03f.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
audio/alsaaudio.c | 2 +-
audio/coreaudio.c | 2 +-
audio/dsoundaudio.c | 2 +-
audio/ossaudio.c | 2 +-
audio/paaudio.c | 2 +-
audio/wavaudio.c | 2 +-
hw/audio/intel-hda.c | 2 +-
7 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/audio/alsaaudio.c b/audio/alsaaudio.c
index 6315b2d..76813f4 100644
--- a/audio/alsaaudio.c
+++ b/audio/alsaaudio.c
@@ -1125,7 +1125,7 @@ static ALSAConf glob_conf = {
static void *alsa_audio_init (void)
{
- ALSAConf *conf = g_malloc(sizeof(ALSAConf));
+ ALSAConf *conf = g_new(ALSAConf, 1);
*conf = glob_conf;
return conf;
}
diff --git a/audio/coreaudio.c b/audio/coreaudio.c
index 6dfd63e..6390ba4 100644
--- a/audio/coreaudio.c
+++ b/audio/coreaudio.c
@@ -504,7 +504,7 @@ static CoreaudioConf glob_conf = {
static void *coreaudio_audio_init (void)
{
- CoreaudioConf *conf = g_malloc(sizeof(CoreaudioConf));
+ CoreaudioConf *conf = g_new(CoreaudioConf, 1);
*conf = glob_conf;
atexit(coreaudio_atexit);
diff --git a/audio/dsoundaudio.c b/audio/dsoundaudio.c
index e9472c1..3949fa4 100644
--- a/audio/dsoundaudio.c
+++ b/audio/dsoundaudio.c
@@ -786,7 +786,7 @@ static void *dsound_audio_init (void)
{
int err;
HRESULT hr;
- dsound *s = g_malloc0(sizeof(dsound));
+ dsound *s = g_new0(dsound, 1);
s->conf = glob_conf;
hr = CoInitialize (NULL);
diff --git a/audio/ossaudio.c b/audio/ossaudio.c
index 7dbe333..2614c62 100644
--- a/audio/ossaudio.c
+++ b/audio/ossaudio.c
@@ -848,7 +848,7 @@ static OSSConf glob_conf = {
static void *oss_audio_init (void)
{
- OSSConf *conf = g_malloc(sizeof(OSSConf));
+ OSSConf *conf = g_new(OSSConf, 1);
*conf = glob_conf;
if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
diff --git a/audio/paaudio.c b/audio/paaudio.c
index fea6071..9c498cf 100644
--- a/audio/paaudio.c
+++ b/audio/paaudio.c
@@ -814,7 +814,7 @@ static PAConf glob_conf = {
static void *qpa_audio_init (void)
{
- paaudio *g = g_malloc(sizeof(paaudio));
+ paaudio *g = g_new(paaudio, 1);
g->conf = glob_conf;
g->mainloop = NULL;
g->context = NULL;
diff --git a/audio/wavaudio.c b/audio/wavaudio.c
index c586020..f706fae 100644
--- a/audio/wavaudio.c
+++ b/audio/wavaudio.c
@@ -230,7 +230,7 @@ static WAVConf glob_conf = {
static void *wav_audio_init (void)
{
- WAVConf *conf = g_malloc(sizeof(WAVConf));
+ WAVConf *conf = g_new(WAVConf, 1);
*conf = glob_conf;
return conf;
}
diff --git a/hw/audio/intel-hda.c b/hw/audio/intel-hda.c
index 433463e..6074c2e 100644
--- a/hw/audio/intel-hda.c
+++ b/hw/audio/intel-hda.c
@@ -467,7 +467,7 @@ static void intel_hda_parse_bdl(IntelHDAState *d, IntelHDAStream *st)
addr = intel_hda_addr(st->bdlp_lbase, st->bdlp_ubase);
st->bentries = st->lvi +1;
g_free(st->bpl);
- st->bpl = g_malloc(sizeof(bpl) * st->bentries);
+ st->bpl = g_new(bpl, st->bentries);
for (i = 0; i < st->bentries; i++, addr += 16) {
pci_dma_read(&d->pci, addr, buf, 16);
st->bpl[i].addr = le64_to_cpu(*(uint64_t *)buf);
--
2.4.3
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] audio: Use g_new() & friends where that makes obvious sense
2015-09-14 11:03 [Qemu-devel] [PATCH] audio: Use g_new() & friends where that makes obvious sense Markus Armbruster
@ 2015-09-14 15:25 ` Eric Blake
2015-09-24 7:02 ` Markus Armbruster
1 sibling, 0 replies; 3+ messages in thread
From: Eric Blake @ 2015-09-14 15:25 UTC (permalink / raw)
To: Markus Armbruster, qemu-devel; +Cc: kraxel
[-- Attachment #1: Type: text/plain, Size: 658 bytes --]
On 09/14/2015 05:03 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 patch as in commit b45c03f.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
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] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] audio: Use g_new() & friends where that makes obvious sense
2015-09-14 11:03 [Qemu-devel] [PATCH] audio: Use g_new() & friends where that makes obvious sense Markus Armbruster
2015-09-14 15:25 ` Eric Blake
@ 2015-09-24 7:02 ` Markus Armbruster
1 sibling, 0 replies; 3+ messages in thread
From: Markus Armbruster @ 2015-09-24 7:02 UTC (permalink / raw)
To: qemu-devel; +Cc: kraxel
Ping? (also ui, usb, qxl)
Markus Armbruster <armbru@redhat.com> writes:
> 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 patch as in commit b45c03f.
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
> audio/alsaaudio.c | 2 +-
> audio/coreaudio.c | 2 +-
> audio/dsoundaudio.c | 2 +-
> audio/ossaudio.c | 2 +-
> audio/paaudio.c | 2 +-
> audio/wavaudio.c | 2 +-
> hw/audio/intel-hda.c | 2 +-
> 7 files changed, 7 insertions(+), 7 deletions(-)
>
> diff --git a/audio/alsaaudio.c b/audio/alsaaudio.c
> index 6315b2d..76813f4 100644
> --- a/audio/alsaaudio.c
> +++ b/audio/alsaaudio.c
> @@ -1125,7 +1125,7 @@ static ALSAConf glob_conf = {
>
> static void *alsa_audio_init (void)
> {
> - ALSAConf *conf = g_malloc(sizeof(ALSAConf));
> + ALSAConf *conf = g_new(ALSAConf, 1);
> *conf = glob_conf;
> return conf;
> }
> diff --git a/audio/coreaudio.c b/audio/coreaudio.c
> index 6dfd63e..6390ba4 100644
> --- a/audio/coreaudio.c
> +++ b/audio/coreaudio.c
> @@ -504,7 +504,7 @@ static CoreaudioConf glob_conf = {
>
> static void *coreaudio_audio_init (void)
> {
> - CoreaudioConf *conf = g_malloc(sizeof(CoreaudioConf));
> + CoreaudioConf *conf = g_new(CoreaudioConf, 1);
> *conf = glob_conf;
>
> atexit(coreaudio_atexit);
> diff --git a/audio/dsoundaudio.c b/audio/dsoundaudio.c
> index e9472c1..3949fa4 100644
> --- a/audio/dsoundaudio.c
> +++ b/audio/dsoundaudio.c
> @@ -786,7 +786,7 @@ static void *dsound_audio_init (void)
> {
> int err;
> HRESULT hr;
> - dsound *s = g_malloc0(sizeof(dsound));
> + dsound *s = g_new0(dsound, 1);
>
> s->conf = glob_conf;
> hr = CoInitialize (NULL);
> diff --git a/audio/ossaudio.c b/audio/ossaudio.c
> index 7dbe333..2614c62 100644
> --- a/audio/ossaudio.c
> +++ b/audio/ossaudio.c
> @@ -848,7 +848,7 @@ static OSSConf glob_conf = {
>
> static void *oss_audio_init (void)
> {
> - OSSConf *conf = g_malloc(sizeof(OSSConf));
> + OSSConf *conf = g_new(OSSConf, 1);
> *conf = glob_conf;
>
> if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
> diff --git a/audio/paaudio.c b/audio/paaudio.c
> index fea6071..9c498cf 100644
> --- a/audio/paaudio.c
> +++ b/audio/paaudio.c
> @@ -814,7 +814,7 @@ static PAConf glob_conf = {
>
> static void *qpa_audio_init (void)
> {
> - paaudio *g = g_malloc(sizeof(paaudio));
> + paaudio *g = g_new(paaudio, 1);
> g->conf = glob_conf;
> g->mainloop = NULL;
> g->context = NULL;
> diff --git a/audio/wavaudio.c b/audio/wavaudio.c
> index c586020..f706fae 100644
> --- a/audio/wavaudio.c
> +++ b/audio/wavaudio.c
> @@ -230,7 +230,7 @@ static WAVConf glob_conf = {
>
> static void *wav_audio_init (void)
> {
> - WAVConf *conf = g_malloc(sizeof(WAVConf));
> + WAVConf *conf = g_new(WAVConf, 1);
> *conf = glob_conf;
> return conf;
> }
> diff --git a/hw/audio/intel-hda.c b/hw/audio/intel-hda.c
> index 433463e..6074c2e 100644
> --- a/hw/audio/intel-hda.c
> +++ b/hw/audio/intel-hda.c
> @@ -467,7 +467,7 @@ static void intel_hda_parse_bdl(IntelHDAState *d, IntelHDAStream *st)
> addr = intel_hda_addr(st->bdlp_lbase, st->bdlp_ubase);
> st->bentries = st->lvi +1;
> g_free(st->bpl);
> - st->bpl = g_malloc(sizeof(bpl) * st->bentries);
> + st->bpl = g_new(bpl, st->bentries);
> for (i = 0; i < st->bentries; i++, addr += 16) {
> pci_dma_read(&d->pci, addr, buf, 16);
> st->bpl[i].addr = le64_to_cpu(*(uint64_t *)buf);
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-09-24 7:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-14 11:03 [Qemu-devel] [PATCH] audio: Use g_new() & friends where that makes obvious sense Markus Armbruster
2015-09-14 15:25 ` Eric Blake
2015-09-24 7:02 ` Markus Armbruster
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).