:p
atchew
Login
In accordance with QEMU's coding style guidelines, replace raw memory allocation functions (malloc/free) with their GLib equivalents (g_new/g_malloc0/g_free). Also removes the old generic error-handling code from OPLOpenTable(), since the Glib functions abort the program automatically if memory is exhausted. Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/1798 Signed-off-by: Lucas Cardoso <luc.fast2004@gmail.com> --- hw/audio/fmopl.c | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/hw/audio/fmopl.c b/hw/audio/fmopl.c index XXXXXXX..XXXXXXX 100644 --- a/hw/audio/fmopl.c +++ b/hw/audio/fmopl.c @@ -XXX,XX +XXX,XX @@ static int OPLOpenTable( void ) double pom; /* allocate dynamic tables */ - if( (TL_TABLE = malloc(TL_MAX*2*sizeof(int32_t))) == NULL) - return 0; - if( (SIN_TABLE = malloc(SIN_ENT*4 *sizeof(int32_t *))) == NULL) - { - free(TL_TABLE); - return 0; - } - if( (AMS_TABLE = malloc(AMS_ENT*2 *sizeof(int32_t))) == NULL) - { - free(TL_TABLE); - free(SIN_TABLE); - return 0; - } - if( (VIB_TABLE = malloc(VIB_ENT*2 *sizeof(int32_t))) == NULL) - { - free(TL_TABLE); - free(SIN_TABLE); - free(AMS_TABLE); - return 0; - } + TL_TABLE = g_new(int32_t, TL_MAX * 2); + SIN_TABLE = g_new(int32_t *, SIN_ENT * 4); + AMS_TABLE = g_new(int32_t, AMS_ENT * 2); + VIB_TABLE = g_new(int32_t, VIB_ENT * 2); ENV_CURVE = g_new(int32_t, 2 * EG_ENT + 1); /* make total level table */ for (t = 0;t < EG_ENT-1 ;t++){ @@ -XXX,XX +XXX,XX @@ 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 */ @@ -XXX,XX +XXX,XX @@ FM_OPL *OPLCreate(int clock, int rate) /* allocate OPL state space */ state_size = sizeof(FM_OPL); state_size += sizeof(OPL_CH)*max_ch; - /* allocate memory block */ - ptr = malloc(state_size); - if(ptr==NULL) return NULL; - /* clear */ - memset(ptr,0,state_size); + /* allocate memory block and zero-initialize */ + ptr = g_malloc0(state_size); OPL = (FM_OPL *)ptr; ptr+=sizeof(FM_OPL); OPL->P_CH = (OPL_CH *)ptr; ptr+=sizeof(OPL_CH)*max_ch; /* set channel state pointer */ @@ -XXX,XX +XXX,XX @@ void OPLDestroy(FM_OPL *OPL) } #endif OPL_UnLockTable(); - free(OPL); + g_free(OPL); } /* ---------- Option handlers ---------- */ -- 2.51.0
In accordance with QEMU's coding style guidelines, replace raw memory allocation functions (malloc/free) with their GLib equivalents (g_new/g_malloc0/g_free/g_clear_pointer). Also removes the old generic error-handling code from OPLOpenTable(), since the Glib functions abort the program automatically if memory is exhausted. Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/1798 Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: Lucas Cardoso <luc.fast2004@gmail.com> --- v2: - After some studying, I decided to follow Marc-André's feedback to use g_clear_pointer() instead of g_free(), on the OPLCloseTable function. hw/audio/fmopl.c | 43 ++++++++++++------------------------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/hw/audio/fmopl.c b/hw/audio/fmopl.c index XXXXXXX..XXXXXXX 100644 --- a/hw/audio/fmopl.c +++ b/hw/audio/fmopl.c @@ -XXX,XX +XXX,XX @@ static int OPLOpenTable( void ) double pom; /* allocate dynamic tables */ - if( (TL_TABLE = malloc(TL_MAX*2*sizeof(int32_t))) == NULL) - return 0; - if( (SIN_TABLE = malloc(SIN_ENT*4 *sizeof(int32_t *))) == NULL) - { - free(TL_TABLE); - return 0; - } - if( (AMS_TABLE = malloc(AMS_ENT*2 *sizeof(int32_t))) == NULL) - { - free(TL_TABLE); - free(SIN_TABLE); - return 0; - } - if( (VIB_TABLE = malloc(VIB_ENT*2 *sizeof(int32_t))) == NULL) - { - free(TL_TABLE); - free(SIN_TABLE); - free(AMS_TABLE); - return 0; - } + TL_TABLE = g_new(int32_t, TL_MAX * 2); + SIN_TABLE = g_new(int32_t *, SIN_ENT * 4); + AMS_TABLE = g_new(int32_t, AMS_ENT * 2); + VIB_TABLE = g_new(int32_t, VIB_ENT * 2); ENV_CURVE = g_new(int32_t, 2 * EG_ENT + 1); /* make total level table */ for (t = 0;t < EG_ENT-1 ;t++){ @@ -XXX,XX +XXX,XX @@ 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_clear_pointer(&ENV_CURVE, g_free); + g_clear_pointer(&TL_TABLE, g_free); + g_clear_pointer(&SIN_TABLE, g_free); + g_clear_pointer(&AMS_TABLE, g_free); + g_clear_pointer(&VIB_TABLE, g_free); } /* CSM Key Control */ @@ -XXX,XX +XXX,XX @@ FM_OPL *OPLCreate(int clock, int rate) /* allocate OPL state space */ state_size = sizeof(FM_OPL); state_size += sizeof(OPL_CH)*max_ch; - /* allocate memory block */ - ptr = malloc(state_size); - if(ptr==NULL) return NULL; - /* clear */ - memset(ptr,0,state_size); + /* allocate memory block and zero-initialize */ + ptr = g_malloc0(state_size); OPL = (FM_OPL *)ptr; ptr+=sizeof(FM_OPL); OPL->P_CH = (OPL_CH *)ptr; ptr+=sizeof(OPL_CH)*max_ch; /* set channel state pointer */ @@ -XXX,XX +XXX,XX @@ void OPLDestroy(FM_OPL *OPL) } #endif OPL_UnLockTable(); - free(OPL); + g_free(OPL); } /* ---------- Option handlers ---------- */ -- 2.51.0