qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCHv2 1/2] util: introduce bitmap_try_new


From: Dr. David Alan Gilbert
Subject: Re: [Qemu-devel] [PATCHv2 1/2] util: introduce bitmap_try_new
Date: Wed, 1 Oct 2014 17:32:34 +0100
User-agent: Mutt/1.5.23 (2014-03-12)

* Peter Lieven (address@hidden) 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 <address@hidden>
> ---
>  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 / address@hidden / Manchester, UK



reply via email to

[Prev in Thread] Current Thread [Next in Thread]