All of lore.kernel.org
 help / color / mirror / Atom feed
From: Riku Voipio <riku.voipio@iki.fi>
To: Harmandeep Kaur <write.harmandeep@gmail.com>
Cc: Stefan Hajnoczi <stefanha@gmail.com>, qemu-devel <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] [PATCH v6] linux-user/syscall.c: malloc()/calloc() to g_malloc()/g_try_malloc()/g_new0()
Date: Mon, 12 Oct 2015 16:06:53 +0300	[thread overview]
Message-ID: <b365012a-94f3-42d1-9f02-65ca9f3b2519@iki.fi> (raw)
In-Reply-To: <CAPtdW15Ld0jNT7gFNr-P_-4+7uyr=1r=Eqi4ohYSHy0tNgKEfQ@mail.gmail.com>

On tiistaina 6. lokakuuta 2015 19.17.12 EEST, Harmandeep Kaur wrote:
> Convert malloc()/ calloc() calls to g_malloc()/ g_try_malloc()/ g_new0()
>
> All heap memory allocation should go through glib so that we can take
> advantage of a single memory allocator and its debugging/tracing features.

Please use git send-email next time, I needed to manually fix the patch to 
apply.

Applied to linux-user, thanks. 

> Signed-off-by: Harmandeep Kaur <write.harmandeep@gmail.com>
> ---
> v1->v2  convert the free() call in host_to_target_semarray()
> to g_free() and calls g_try_malloc(count)  instead of 
> g_try_malloc(sizeof(count))
>
> v2->v3 used g_try_new() and friends to avoid overflow issues
>
> v3->v4 use g_free for unlock_iovec() and host_to_target_semarray().
>
> v4->v5 one missing malloc() is converted and one converted is fixed.
>
> v5->v6  new improved commit description.
>
> ---
>  linux-user/syscall.c | 28 ++++++++++++++--------------
>  1 file changed, 14 insertions(+), 14 deletions(-)
>
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 98b5766..267aaa8 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -1559,7 +1559,7 @@ set_timeout:
>                  }
>  
>                  fprog.len = tswap16(tfprog->len);
> -                filter = malloc(fprog.len * sizeof(*filter));
> +                filter = g_try_new(struct sock_filter, fprog.len);
>                  if (filter == NULL) {
>                      unlock_user_struct(tfilter, tfprog->filter, 1);
>                      unlock_user_struct(tfprog, optval_addr, 1);
> @@ -1575,7 +1575,7 @@ set_timeout:
>  
>                  ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
>                                  SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
> -                free(filter);
> +                g_free(filter);
>  
>                  unlock_user_struct(tfilter, tfprog->filter, 1);
>                  unlock_user_struct(tfprog, optval_addr, 1);
> @@ -1886,7 +1886,7 @@ static struct iovec *lock_iovec(int type, 
> abi_ulong target_addr,
>          return NULL;
>      }
>  
> -    vec = calloc(count, sizeof(struct iovec));
> +    vec = g_try_new0(struct iovec, count);
>      if (vec == NULL) {
>          errno = ENOMEM;
>          return NULL;
> @@ -1950,7 +1950,7 @@ static struct iovec *lock_iovec(int type, 
> abi_ulong target_addr,
>      }
>      unlock_user(target_vec, target_addr, 0);
>   fail2:
> -    free(vec);
> +    g_free(vec);
>      errno = err;
>      return NULL;
>  }
> @@ -1975,7 +1975,7 @@ static void unlock_iovec(struct iovec 
> *vec, abi_ulong target_addr,
>          unlock_user(target_vec, target_addr, 0);
>      }
>  
> -    free(vec);
> +    g_free(vec);
>  }
>  
>  static inline int target_to_host_sock_type(int *type)
> @@ -2677,14 +2677,14 @@ static inline abi_long 
> target_to_host_semarray(int semid, unsigned short **host_
>  
>      nsems = semid_ds.sem_nsems;
>  
> -    *host_array = malloc(nsems*sizeof(unsigned short));
> +    *host_array = g_try_new(unsigned short, nsems);
>      if (!*host_array) {
>          return -TARGET_ENOMEM;
>      }
>      array = lock_user(VERIFY_READ, target_addr,
>                        nsems*sizeof(unsigned short), 1);
>      if (!array) {
> -        free(*host_array);
> +        g_free(*host_array);
>          return -TARGET_EFAULT;
>      }
>  
> @@ -2721,7 +2721,7 @@ static inline abi_long 
> host_to_target_semarray(int semid, abi_ulong target_addr,
>      for(i=0; i<nsems; i++) {
>          __put_user((*host_array)[i], &array[i]);
>      }
> -    free(*host_array);
> +    g_free(*host_array);
>      unlock_user(array, target_addr, 1);
>  
>      return 0;
> @@ -2980,7 +2980,7 @@ static inline abi_long do_msgsnd(int 
> msqid, abi_long msgp,
>  
>      if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
>          return -TARGET_EFAULT;
> -    host_mb = malloc(msgsz+sizeof(long));
> +    host_mb = g_try_malloc(msgsz + sizeof(long));
>      if (!host_mb) {
>          unlock_user_struct(target_mb, msgp, 0);
>          return -TARGET_ENOMEM;
> @@ -2988,7 +2988,7 @@ static inline abi_long do_msgsnd(int 
> msqid, abi_long msgp,
>      host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
>      memcpy(host_mb->mtext, target_mb->mtext, msgsz);
>      ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
> -    free(host_mb);
> +    g_free(host_mb);
>      unlock_user_struct(target_mb, msgp, 0);
>  
>      return ret;
> @@ -3416,7 +3416,7 @@ static abi_long 
> do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
>          /* We can't fit all the extents into the fixed size buffer.
>           * Allocate one that is large enough and use it instead.
>           */
> -        fm = malloc(outbufsz);
> +        fm = g_try_malloc(outbufsz);
>          if (!fm) {
>              return -TARGET_ENOMEM;
>          }
> @@ -3451,7 +3451,7 @@ static abi_long 
> do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
>          }
>      }
>      if (free_fm) {
> -        free(fm);
> +        g_free(fm);
>      }
>      return ret;
>  }
> @@ -7723,7 +7723,7 @@ abi_long do_syscall(void *cpu_env, int 
> num, abi_long arg1,
>              struct linux_dirent *dirp;
>              abi_long count = arg3;
>  
> -        dirp = malloc(count);
> +        dirp = g_try_malloc(count);
>          if (!dirp) {
>                  ret = -TARGET_ENOMEM;
>                  goto fail;
> @@ -7760,7 +7760,7 @@ abi_long do_syscall(void *cpu_env, int 
> num, abi_long arg1,
>          ret = count1;
>                  unlock_user(target_dirp, arg2, ret);
>              }
> -        free(dirp);
> +        g_free(dirp);
>          }
>  #else
>          {

  parent reply	other threads:[~2015-10-12 13:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-06 16:17 [Qemu-devel] [PATCH v6] linux-user/syscall.c: malloc()/calloc() to g_malloc()/g_try_malloc()/g_new0() Harmandeep Kaur
2015-10-08  9:54 ` Stefan Hajnoczi
2015-10-12 13:06 ` Riku Voipio [this message]
2015-10-12 14:40   ` Harmandeep Kaur

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=b365012a-94f3-42d1-9f02-65ca9f3b2519@iki.fi \
    --to=riku.voipio@iki.fi \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    --cc=write.harmandeep@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.