[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 3/9] hw/audio/fmopl.c: Replaced calls to malloc with GLib's varia
From: |
Mahmoud Mandour |
Subject: |
[PATCH 3/9] hw/audio/fmopl.c: Replaced calls to malloc with GLib's variants |
Date: |
Sat, 13 Mar 2021 18:36:47 +0200 |
Replaced calls to malloc(), and free() to their equivalent
allocation functions from GLib.
Also added checking for null after ENV_CURVE allocation
following the same pattern of checking on preceeding
table allocations.
Signed-off-by: Mahmoud Mandour <ma.mandourr@gmail.com>
---
hw/audio/fmopl.c | 41 ++++++++++++++++++++++++-----------------
1 file changed, 24 insertions(+), 17 deletions(-)
diff --git a/hw/audio/fmopl.c b/hw/audio/fmopl.c
index 45f15c53b3..6cecf2b963 100644
--- a/hw/audio/fmopl.c
+++ b/hw/audio/fmopl.c
@@ -607,27 +607,34 @@ static int OPLOpenTable( void )
double pom;
/* allocate dynamic tables */
- if((TL_TABLE = malloc(TL_MAX * 2 * sizeof(int32_t))) == NULL)
+ if((TL_TABLE = g_try_new(int32_t, TL_MAX * 2)) == NULL)
return 0;
- if((SIN_TABLE = malloc(SIN_ENT * 4 *sizeof(int32_t *))) == NULL)
+ if((SIN_TABLE = g_try_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_try_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_try_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;
+ }
+ if((ENV_CURVE = g_try_new(int32_t, 2 * EG_ENT + 1)) == NULL)
+ {
+ g_free(TL_TABLE);
+ g_free(SIN_TABLE);
+ g_free(AMS_TABLE);
+ g_free(VIB_TABLE);
return 0;
}
- ENV_CURVE = g_new(int32_t, 2 * EG_ENT + 1);
/* make total level table */
for (t = 0;t < EG_ENT-1 ;t++){
rate = ((1<<TL_BITS)-1)/pow(10,EG_STEP*t/20); /* dB ->
voltage */
@@ -696,10 +703,10 @@ static int OPLOpenTable( void )
static void OPLCloseTable( void )
{
g_free(ENV_CURVE);
- free(TL_TABLE);
- free(SIN_TABLE);
- free(AMS_TABLE);
- free(VIB_TABLE);
+ g_free(TL_TABLE);
+ g_free(SIN_TABLE);
+ g_free(AMS_TABLE);
+ g_free(VIB_TABLE);
}
/* CSM Key Control */
@@ -1082,7 +1089,7 @@ FM_OPL *OPLCreate(int clock, int rate)
state_size = sizeof(FM_OPL);
state_size += sizeof(OPL_CH)*max_ch;
/* allocate memory block */
- ptr = malloc(state_size);
+ ptr = g_try_malloc(state_size);
if(ptr==NULL) return NULL;
/* clear */
memset(ptr,0,state_size);
@@ -1128,7 +1135,7 @@ void OPLDestroy(FM_OPL *OPL)
}
#endif
OPL_UnLockTable();
- free(OPL);
+ g_free(OPL);
}
/* ---------- Option handlers ---------- */
--
2.25.1
- [PATCH 0/9] Replacing malloc and the like with GLib's variants, Mahmoud Mandour, 2021/03/13
- [PATCH 1/9] bsd-user/elfload.c: Replaced calls to malloc/free with GLib variants, Mahmoud Mandour, 2021/03/13
- [PATCH 2/9] hw/audio/fmopl.c: Fixing some style errors., Mahmoud Mandour, 2021/03/13
- [PATCH 8/9] tools/virtiofsd/fuse_opt.c: Replaced a malloc with GLib's g_try_malloc, Mahmoud Mandour, 2021/03/13
- [PATCH 6/9] util/compatfd.c: Replaced a malloc with GLib's g_try_malloc, Mahmoud Mandour, 2021/03/13
- [PATCH 5/9] target/xtensa: Replaced malloc/free with GLib's variants., Mahmoud Mandour, 2021/03/13
- [PATCH 3/9] hw/audio/fmopl.c: Replaced calls to malloc with GLib's variants,
Mahmoud Mandour <=
- [PATCH 7/9] tools/virtiofsd/buffer.c: replaced a calloc call with GLib's g_try_new0, Mahmoud Mandour, 2021/03/13
- [PATCH 4/9] pc-bios/s390-ccw/netmain.c: Changed a malloc/free to GLib's variants, Mahmoud Mandour, 2021/03/13
- [PATCH 9/9] tools/virtiofsd: Replacing malloc-like calls with GLib's variants, Mahmoud Mandour, 2021/03/13
- Re: [PATCH 0/9] Replacing malloc and the like with GLib's variants, no-reply, 2021/03/13