qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: Mahmoud Mandour <ma.mandourr@gmail.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>, qemu-devel@nongnu.org
Subject: Re: [PATCH 3/8] hw/audio/fmopl.c: Replaced calls to malloc with GLib's variants
Date: Mon, 15 Mar 2021 16:12:33 +0000	[thread overview]
Message-ID: <87y2eo8kd5.fsf@linaro.org> (raw)
In-Reply-To: <20210314032324.45142-4-ma.mandourr@gmail.com>


Mahmoud Mandour <ma.mandourr@gmail.com> writes:

> 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 | 42 +++++++++++++++++++++++++-----------------
>  1 file changed, 25 insertions(+), 17 deletions(-)
>
> diff --git a/hw/audio/fmopl.c b/hw/audio/fmopl.c
> index 51b773695a..795c7a23dc 100644
> --- a/hw/audio/fmopl.c
> +++ b/hw/audio/fmopl.c
> @@ -607,33 +607,41 @@ static int OPLOpenTable( void )
>  	double pom;
>  
>  	/* allocate dynamic tables */
> -    TL_TABLE = malloc(TL_MAX * 2 * sizeof(int32_t));
> +    TL_TABLE = g_try_new(int32_t, TL_MAX * 2);
>      if (TL_TABLE == NULL) {
>          return 0;
>      }

This is initialisation code I think so you should be able to use
g_malloc() and drop the NULL checks.

>  
> -    SIN_TABLE = malloc(SIN_ENT * 4 * sizeof(int32_t *));
> +    SIN_TABLE = g_try_new(int32_t *, SIN_ENT * 4);
>      if (SIN_TABLE == NULL) {
> -        free(TL_TABLE);
> +        g_free(TL_TABLE);
>          return 0;
>      }
>  
> -    AMS_TABLE = malloc(AMS_ENT * 2 * sizeof(int32_t));
> +    AMS_TABLE = g_try_new(int32_t, AMS_ENT * 2);
>      if (AMS_TABLE == NULL) {
> -        free(TL_TABLE);
> -        free(SIN_TABLE);
> +        g_free(TL_TABLE);
> +        g_free(SIN_TABLE);
>          return 0;
>      }
>  
> -    VIB_TABLE = malloc(VIB_ENT * 2 * sizeof(int32_t));
> +    VIB_TABLE = g_try_new(int32_t, VIB_ENT * 2);
>      if (VIB_TABLE == 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_try_new(int32_t, 2 * EG_ENT + 1);
> +    if (ENV_CURVE == NULL) {
> +        g_free(TL_TABLE);
> +        g_free(SIN_TABLE);
> +        g_free(AMS_TABLE);
> +        g_free(VIB_TABLE);
>          return 0;
>      }

Again g_autofree could be used, but if any of your static
initialisation fails won't the system fail to boot anyway?


>  
> -    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 */
> @@ -702,10 +710,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 */
> @@ -1088,7 +1096,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);
> @@ -1134,7 +1142,7 @@ void OPLDestroy(FM_OPL *OPL)
>  	}
>  #endif
>  	OPL_UnLockTable();
> -    free(OPL);
> +    g_free(OPL);
>  }
>  
>  /* ----------  Option handlers ----------       */


-- 
Alex Bennée


  reply	other threads:[~2021-03-15 16:26 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-14  3:23 [PATCH 0/8] Replacing malloc and the like with GLib's variants Mahmoud Mandour
2021-03-14  3:23 ` [PATCH 1/8] bsd-user/elfload.c: Replaced calls to malloc/free with GLib variants Mahmoud Mandour
2021-03-15 16:07   ` Alex Bennée
2021-03-15 16:22     ` Mahmoud Mandour
2021-03-15 17:56       ` Alex Bennée
2021-03-14  3:23 ` [PATCH 2/8] hw/audio/fmopl.c: Fixing some style errors Mahmoud Mandour
2021-03-14  3:23 ` [PATCH 3/8] hw/audio/fmopl.c: Replaced calls to malloc with GLib's variants Mahmoud Mandour
2021-03-15 16:12   ` Alex Bennée [this message]
2021-03-15 17:35     ` Mahmoud Mandour
2021-03-14  3:23 ` [PATCH 4/8] target/xtensa: Replaced malloc/free " Mahmoud Mandour
2021-03-14 15:38   ` Max Filippov
2021-03-14  3:23 ` [PATCH 5/8] util/compatfd.c: Replaced a malloc with GLib's variant Mahmoud Mandour
2021-03-15  6:10   ` Thomas Huth
2021-03-15  6:43     ` Mahmoud Mandour
2021-03-15  7:29       ` Thomas Huth
2021-03-15 16:15         ` Alex Bennée
2021-03-14  3:23 ` [PATCH 6/8] tools/virtiofsd/buffer.c: replaced a calloc call with GLib's g_try_new0 Mahmoud Mandour
2021-03-15  9:53   ` Stefan Hajnoczi
2021-05-25 19:01   ` Dr. David Alan Gilbert
2021-03-14  3:23 ` [PATCH 7/8] tools/virtiofsd/fuse_opt.c: Replaced a malloc with GLib's g_try_malloc Mahmoud Mandour
2021-03-15  9:54   ` Stefan Hajnoczi
2021-03-14  3:23 ` [PATCH 8/8] tools/virtiofsd: Replacing malloc-like calls with GLib's variants Mahmoud Mandour
2021-03-15 10:01   ` Stefan Hajnoczi
2021-03-15 10:46     ` Mahmoud Mandour
2021-03-16 17:23       ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y2eo8kd5.fsf@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=kraxel@redhat.com \
    --cc=ma.mandourr@gmail.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).