* [PATCH] audio: converts malloc to g_new and free to g_free
@ 2026-06-19 8:09 Christian S. Lima
2026-06-19 8:12 ` Daniel P. Berrangé
2026-06-19 9:22 ` Peter Maydell
0 siblings, 2 replies; 4+ messages in thread
From: Christian S. Lima @ 2026-06-19 8:09 UTC (permalink / raw)
To: qemu-devel, Gerd Hoffmann
Following the qemu coding style change malloc to g_new, the advantage
are that g_new can catch multiplication overflowing size_t and allow
catch more type errors because it returns the type itself.
Signed-off-by: Christian S. Lima <christianslima@proton.me>
---
hw/audio/fmopl.c | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/hw/audio/fmopl.c b/hw/audio/fmopl.c
index a63ad0f04d..19c4b388f3 100644
--- a/hw/audio/fmopl.c
+++ b/hw/audio/fmopl.c
@@ -607,24 +607,24 @@ static int OPLOpenTable( void )
double pom;
/* allocate dynamic tables */
- if( (TL_TABLE = malloc(TL_MAX*2*sizeof(int32_t))) == NULL)
+ if( (TL_TABLE = g_new(int32_t, TL_MAX * 2)) == NULL)
return 0;
- if( (SIN_TABLE = malloc(SIN_ENT*4 *sizeof(int32_t *))) == NULL)
+ if( (SIN_TABLE = g_new(int32_t *, SIN_ENT * 4)) == NULL)
{
- free(TL_TABLE);
+ g_free(TL_TABLE);
return 0;
}
- if( (AMS_TABLE = malloc(AMS_ENT*2 *sizeof(int32_t))) == NULL)
+ if( (AMS_TABLE = g_new(int32_t, AMS_ENT * 2)) == NULL)
{
- free(TL_TABLE);
- free(SIN_TABLE);
+ g_free(TL_TABLE);
+ g_free(SIN_TABLE);
return 0;
}
- if( (VIB_TABLE = malloc(VIB_ENT*2 *sizeof(int32_t))) == NULL)
+ if( (VIB_TABLE = g_new(int32_t, VIB_ENT * 2)) == NULL)
{
- free(TL_TABLE);
- free(SIN_TABLE);
- free(AMS_TABLE);
+ g_free(TL_TABLE);
+ g_free(SIN_TABLE);
+ g_free(AMS_TABLE);
return 0;
}
ENV_CURVE = g_new(int32_t, 2 * EG_ENT + 1);
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] audio: converts malloc to g_new and free to g_free
2026-06-19 8:09 [PATCH] audio: converts malloc to g_new and free to g_free Christian S. Lima
@ 2026-06-19 8:12 ` Daniel P. Berrangé
2026-06-19 9:22 ` Peter Maydell
1 sibling, 0 replies; 4+ messages in thread
From: Daniel P. Berrangé @ 2026-06-19 8:12 UTC (permalink / raw)
To: Christian S. Lima; +Cc: qemu-devel, Gerd Hoffmann
On Fri, Jun 19, 2026 at 08:09:42AM +0000, Christian S. Lima wrote:
> Following the qemu coding style change malloc to g_new, the advantage
> are that g_new can catch multiplication overflowing size_t and allow
> catch more type errors because it returns the type itself.
>
> Signed-off-by: Christian S. Lima <christianslima@proton.me>
> ---
> hw/audio/fmopl.c | 20 ++++++++++----------
> 1 file changed, 10 insertions(+), 10 deletions(-)
>
> diff --git a/hw/audio/fmopl.c b/hw/audio/fmopl.c
> index a63ad0f04d..19c4b388f3 100644
> --- a/hw/audio/fmopl.c
> +++ b/hw/audio/fmopl.c
> @@ -607,24 +607,24 @@ static int OPLOpenTable( void )
> double pom;
>
> /* allocate dynamic tables */
> - if( (TL_TABLE = malloc(TL_MAX*2*sizeof(int32_t))) == NULL)
> + if( (TL_TABLE = g_new(int32_t, TL_MAX * 2)) == NULL)
g_new cannot fail, so all checks for NULL must be removed too.
Unless this is a performance critical path, we'll also prefer
g_new0 over g_new, so that all memory is guaranteed zero-initialized
> return 0;
> - if( (SIN_TABLE = malloc(SIN_ENT*4 *sizeof(int32_t *))) == NULL)
> + if( (SIN_TABLE = g_new(int32_t *, SIN_ENT * 4)) == NULL)
> {
> - free(TL_TABLE);
> + g_free(TL_TABLE);
> return 0;
> }
> - if( (AMS_TABLE = malloc(AMS_ENT*2 *sizeof(int32_t))) == NULL)
> + if( (AMS_TABLE = g_new(int32_t, AMS_ENT * 2)) == NULL)
> {
> - free(TL_TABLE);
> - free(SIN_TABLE);
> + g_free(TL_TABLE);
> + g_free(SIN_TABLE);
> return 0;
> }
> - if( (VIB_TABLE = malloc(VIB_ENT*2 *sizeof(int32_t))) == NULL)
> + if( (VIB_TABLE = g_new(int32_t, VIB_ENT * 2)) == NULL)
> {
> - free(TL_TABLE);
> - free(SIN_TABLE);
> - free(AMS_TABLE);
> + g_free(TL_TABLE);
> + g_free(SIN_TABLE);
> + g_free(AMS_TABLE);
> return 0;
> }
> ENV_CURVE = g_new(int32_t, 2 * EG_ENT + 1);
> --
> 2.53.0
>
>
>
With regards,
Daniel
--
|: https://berrange.com ~~ https://hachyderm.io/@berrange :|
|: https://libvirt.org ~~ https://entangle-photo.org :|
|: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] audio: converts malloc to g_new and free to g_free
2026-06-19 8:09 [PATCH] audio: converts malloc to g_new and free to g_free Christian S. Lima
2026-06-19 8:12 ` Daniel P. Berrangé
@ 2026-06-19 9:22 ` Peter Maydell
2026-06-19 21:50 ` Christian
1 sibling, 1 reply; 4+ messages in thread
From: Peter Maydell @ 2026-06-19 9:22 UTC (permalink / raw)
To: Christian S. Lima; +Cc: qemu-devel, Gerd Hoffmann
On Fri, 19 Jun 2026 at 09:10, Christian S. Lima
<christianslima@proton.me> wrote:
>
> Following the qemu coding style change malloc to g_new, the advantage
> are that g_new can catch multiplication overflowing size_t and allow
> catch more type errors because it returns the type itself.
>
> Signed-off-by: Christian S. Lima <christianslima@proton.me>
> ---
> hw/audio/fmopl.c | 20 ++++++++++----------
> 1 file changed, 10 insertions(+), 10 deletions(-)
Hi; we already have somebody working on updating this
set of malloc/free calls:
https://lore.kernel.org/qemu-devel/20260428162048.2995097-1-luc.fast2004@gmail.com/
thanks
-- PMM
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-06-19 21:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-19 8:09 [PATCH] audio: converts malloc to g_new and free to g_free Christian S. Lima
2026-06-19 8:12 ` Daniel P. Berrangé
2026-06-19 9:22 ` Peter Maydell
2026-06-19 21:50 ` Christian
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.