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: qemu-devel@nongnu.org
Subject: Re: [PATCH 1/8] bsd-user/elfload.c: Replaced calls to malloc/free with GLib variants
Date: Mon, 15 Mar 2021 16:07:37 +0000	[thread overview]
Message-ID: <871rcg9z2q.fsf@linaro.org> (raw)
In-Reply-To: <20210314032324.45142-2-ma.mandourr@gmail.com>


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

> Replaced the calls to malloc(), realloc(), and free() to their
> equivalents in GLib's allocation functions in various places.
>
> Signed-off-by: Mahmoud Mandour <ma.mandourr@gmail.com>
> ---
>  bsd-user/elfload.c | 74 +++++++++++++++++++++++-----------------------
>  1 file changed, 37 insertions(+), 37 deletions(-)
>
> diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c
> index 5f4d824d78..7b0793693b 100644
> --- a/bsd-user/elfload.c
> +++ b/bsd-user/elfload.c
> @@ -867,8 +867,7 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
>          if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE)
>              return ~(abi_ulong)0UL;
>  
> -        elf_phdata =  (struct elf_phdr *)
> -                malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
> +        elf_phdata = g_try_new(struct elf_phdr,
> interp_elf_ex->ephnum)

Given this is start-up code I think you could use g_new instead of
g_try_new. As it will abort on no memory you can avoid the early return
check bellow. Also is elf_phdata never persists beyond this function you
could use g_autofree (and use g_steal_pointer on the one case when it is
returned if you need it)

>  
>          if (!elf_phdata)
>            return ~((abi_ulong)0UL);
> @@ -878,7 +877,7 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
>           * we will be doing the wrong thing.
>           */
>          if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
> -            free(elf_phdata);
> +            g_free(elf_phdata);
>              return ~((abi_ulong)0UL);
>          }
>  
> @@ -891,7 +890,7 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
>          if (retval < 0) {
>                  perror("load_elf_interp");
>                  exit(-1);
> -                free (elf_phdata);
> +                g_free(elf_phdata);
>                  return retval;
>          }
>  #ifdef BSWAP_NEEDED
> @@ -940,7 +939,7 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
>              if (error == -1) {
>                /* Real error */
>                close(interpreter_fd);
> -              free(elf_phdata);
> +              g_free(elf_phdata);
>                return ~((abi_ulong)0UL);
>              }
>  
> @@ -983,7 +982,7 @@ static abi_ulong load_elf_interp(struct elfhdr * interp_elf_ex,
>                          PROT_READ|PROT_WRITE|PROT_EXEC,
>                          MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0);
>          }
> -        free(elf_phdata);
> +        g_free(elf_phdata);
>

That would allow you to get rid of a lot of free/g_frees

I would also split this patch, one for each function you convert.

>          *interp_load_addr = load_addr;
>          return ((abi_ulong) interp_elf_ex->e_entry) + load_addr;
> @@ -1064,24 +1063,24 @@ static void load_symbols(struct elfhdr *hdr, int fd)
>  
>   found:
>      /* Now know where the strtab and symtab are.  Snarf them. */
> -    s = malloc(sizeof(*s));
> -    syms = malloc(symtab.sh_size);
> +    s = g_try_malloc(sizeof(*s));
> +    syms = g_try_malloc(symtab.sh_size);
>      if (!syms) {
> -        free(s);
> +        g_free(s);
>          return;
>      }
> -    s->disas_strtab = strings = malloc(strtab.sh_size);
> +    s->disas_strtab = strings = g_malloc(strtab.sh_size);
>      if (!s->disas_strtab) {
> -        free(s);
> -        free(syms);
> +        g_free(s);
> +        g_free(syms);
>          return;
>      }
>  
>      lseek(fd, symtab.sh_offset, SEEK_SET);
>      if (read(fd, syms, symtab.sh_size) != symtab.sh_size) {
> -        free(s);
> -        free(syms);
> -        free(strings);
> +        g_free(s);
> +        g_free(syms);
> +        g_free(strings);
>          return;
>      }
>  
> @@ -1113,11 +1112,11 @@ static void load_symbols(struct elfhdr *hdr, int fd)
>          that we threw away.  Whether or not this has any effect on the
>          memory allocation depends on the malloc implementation and how
>          many symbols we managed to discard. */
> -    new_syms = realloc(syms, nsyms * sizeof(*syms));
> +    new_syms = g_try_realloc(syms, nsyms * sizeof(*syms));
>      if (new_syms == NULL) {
> -        free(s);
> -        free(syms);
> -        free(strings);
> +        g_free(s);
> +        g_free(syms);
> +        g_free(strings);
>          return;
>      }
>      syms = new_syms;
> @@ -1126,9 +1125,9 @@ static void load_symbols(struct elfhdr *hdr, int fd)
>  
>      lseek(fd, strtab.sh_offset, SEEK_SET);
>      if (read(fd, strings, strtab.sh_size) != strtab.sh_size) {
> -        free(s);
> -        free(syms);
> -        free(strings);
> +        g_free(s);
> +        g_free(syms);
> +        g_free(strings);
>          return;
>      }
>      s->disas_num_syms = nsyms;
> @@ -1190,7 +1189,8 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
>      }
>  
>      /* Now read in all of the header information */
> -    elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
> +    elf_phdata =
> +        (struct elf_phdr *)g_try_malloc(elf_ex.e_phentsizei * elf_ex.e_phnum);
>      if (elf_phdata == NULL) {
>          return -ENOMEM;
>      }
> @@ -1204,7 +1204,7 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
>      if (retval < 0) {
>          perror("load_elf_binary");
>          exit(-1);
> -        free (elf_phdata);
> +        g_free(elf_phdata);
>          return -errno;
>      }
>  
> @@ -1231,8 +1231,8 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
>          if (elf_ppnt->p_type == PT_INTERP) {
>              if ( elf_interpreter != NULL )
>              {
> -                free (elf_phdata);
> -                free(elf_interpreter);
> +                g_free(elf_phdata);
> +                g_free(elf_interpreter);
>                  close(bprm->fd);
>                  return -EINVAL;
>              }
> @@ -1242,10 +1242,10 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
>               * is an a.out format binary
>               */
>  
> -            elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
> +            elf_interpreter = (char *)g_try_malloc(elf_ppnt->p_filesz);
>  
>              if (elf_interpreter == NULL) {
> -                free (elf_phdata);
> +                g_free(elf_phdata);
>                  close(bprm->fd);
>                  return -ENOMEM;
>              }
> @@ -1298,8 +1298,8 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
>              if (retval < 0) {
>                  perror("load_elf_binary3");
>                  exit(-1);
> -                free (elf_phdata);
> -                free(elf_interpreter);
> +                g_free(elf_phdata);
> +                g_free(elf_interpreter);
>                  close(bprm->fd);
>                  return retval;
>              }
> @@ -1323,8 +1323,8 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
>          }
>  
>          if (!interpreter_type) {
> -            free(elf_interpreter);
> -            free(elf_phdata);
> +            g_free(elf_interpreter);
> +            g_free(elf_phdata);
>              close(bprm->fd);
>              return -ELIBBAD;
>          }
> @@ -1346,8 +1346,8 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
>              }
>          }
>          if (!bprm->p) {
> -            free(elf_interpreter);
> -            free (elf_phdata);
> +            g_free(elf_interpreter);
> +            g_free(elf_phdata);
>              close(bprm->fd);
>              return -E2BIG;
>          }
> @@ -1486,17 +1486,17 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
>          reloc_func_desc = interp_load_addr;
>  
>          close(interpreter_fd);
> -        free(elf_interpreter);
> +        g_free(elf_interpreter);
>  
>          if (elf_entry == ~((abi_ulong)0UL)) {
>              printf("Unable to load interpreter\n");
> -            free(elf_phdata);
> +            g_free(elf_phdata);
>              exit(-1);
>              return 0;
>          }
>      }
>  
> -    free(elf_phdata);
> +    g_free(elf_phdata);
>  
>      if (qemu_log_enabled())
>          load_symbols(&elf_ex, bprm->fd);


-- 
Alex Bennée


  reply	other threads:[~2021-03-15 16:13 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 [this message]
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
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=871rcg9z2q.fsf@linaro.org \
    --to=alex.bennee@linaro.org \
    --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).