Skip to contentSkip to conversation

audio: converts malloc to g_new and free to g_free

PATCH In review Christian S. Lima 3 participants 4 messages Jun 19

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 audio: converts malloc to g_new and free to g_free

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.

---
 hw/audio/fmopl.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
hw/audio/fmopl.c +10 -10
index a63ad0f04d..19c4b388f3 100644
@@ -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);
Signature · 1 line
2.53.0
#1 lore raw
Quoted 14 lines · Christian S. Lima

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.

---
 hw/audio/fmopl.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
hw/audio/fmopl.c +0 -0
index a63ad0f04d..19c4b388f3 100644

hw/audio/fmopl.c static int OPLOpenTable( void ) · line 610

Quoted 5 lines
hw/audio/fmopl.c +1 -1
@@ -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

Quoted 34 lines
hw/audio/fmopl.c +9 -9
 		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);
Signature · 1 line
2.53.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#2 lore raw
In reply to Christian S. Lima
Quoted 9 lines · Christian S. Lima

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.

---
 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/[email protected]/

thanks
-- PMM