audio: Use g_new() & friends where that makes obvious sense
PATCH 1/1 audio: 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 patch as in commit b45c03f.
--- 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(-)
audio/alsaaudio.c +1 -1
@@ -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; }
audio/coreaudio.c +1 -1
@@ -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);
audio/dsoundaudio.c +1 -1
@@ -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 );
audio/ossaudio.c +1 -1
@@ -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 ||
audio/paaudio.c +1 -1
@@ -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 ;
audio/wavaudio.c +1 -1
@@ -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; }
hw/audio/intel-hda.c +1 -1
@@ -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);
Signature · 1 line
2.4.3
Quoted 10 lines · Markus Armbruster
g_new(T, n)is neater thang_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowingsize_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 <[email protected]>---
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Ping? (also ui, usb, qxl)
Quoted 110 lines · Markus Armbruster
g_new(T, n)is neater thang_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowingsize_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 <[email protected]>--- 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(-)audio/alsaaudio.c +1
-1@@ -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; }audio/coreaudio.c +1
-1@@ -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);audio/dsoundaudio.c +1
-1@@ -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 );audio/ossaudio.c +1
-1@@ -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 ||audio/paaudio.c +1
-1@@ -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 ;audio/wavaudio.c +1
-1@@ -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; }hw/audio/intel-hda.c +1
-1@@ -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);