qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
To: Peter Lieven <pl@kamp.de>
Cc: kwolf@redhat.com, pbonzini@redhat.com, ronniesahlberg@gmail.com,
	qemu-devel@nongnu.org, stefanha@redhat.com
Subject: Re: [Qemu-devel] [PATCHv2 1/2] util: introduce bitmap_try_new
Date: Wed, 1 Oct 2014 17:32:34 +0100	[thread overview]
Message-ID: <20141001163233.GS2606@work-vm> (raw)
In-Reply-To: <1412060952-19407-2-git-send-email-pl@kamp.de>

* Peter Lieven (pl@kamp.de) wrote:
> regular bitmap_new simply aborts if the memory allocation fails.
> bitmap_try_new returns NULL on failure and allows for proper
> error handling.
> 
> Signed-off-by: Peter Lieven <pl@kamp.de>
> ---
>  include/qemu/bitmap.h |   13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h
> index 1babd5d..edf4f17 100644
> --- a/include/qemu/bitmap.h
> +++ b/include/qemu/bitmap.h
> @@ -88,10 +88,19 @@ int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
>  int slow_bitmap_intersects(const unsigned long *bitmap1,
>                             const unsigned long *bitmap2, long bits);
>  
> -static inline unsigned long *bitmap_new(long nbits)
> +static inline unsigned long *bitmap_try_new(long nbits)
>  {
>      long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
> -    return g_malloc0(len);
> +    return g_try_malloc0(len);
> +}
> +
> +static inline unsigned long *bitmap_new(long nbits)
> +{
> +    unsigned long *ptr = bitmap_try_new(nbits);
> +    if (ptr == NULL) {
> +        abort();
> +    }
> +    return ptr;
>  }

This seems like a good idea; it's probably a good idea to deprecate
use of bitmap_new and get rid of the other uses in the longterm
(there aren't that many).

size_t would be a nice type for the nbits; could you make your bitmap_try_new
use that and then fix up those users as we convert them to use the try?

Dave

>  
>  static inline void bitmap_zero(unsigned long *dst, long nbits)
> -- 
> 1.7.9.5
> 
> 
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK

  reply	other threads:[~2014-10-01 16:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-30  7:09 [Qemu-devel] [PATCHv2 0/2] introduce bitmap_try_new Peter Lieven
2014-09-30  7:09 ` [Qemu-devel] [PATCHv2 1/2] util: " Peter Lieven
2014-10-01 16:32   ` Dr. David Alan Gilbert [this message]
2014-09-30  7:09 ` [Qemu-devel] [PATCHv2 2/2] block/iscsi: handle failure on malloc of the allocationmap Peter Lieven
2014-09-30 10:12 ` [Qemu-devel] [PATCHv2 0/2] introduce bitmap_try_new Paolo Bonzini

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=20141001163233.GS2606@work-vm \
    --to=dgilbert@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=pl@kamp.de \
    --cc=qemu-devel@nongnu.org \
    --cc=ronniesahlberg@gmail.com \
    --cc=stefanha@redhat.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 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).