[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH 1/2] util: introduce bitmap_try_new
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] [PATCH 1/2] util: introduce bitmap_try_new |
Date: |
Mon, 25 Aug 2014 09:09:40 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.7.0 |
On 08/22/2014 03:26 AM, Peter Lieven 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 | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h
> index 1babd5d..51b430f 100644
> --- a/include/qemu/bitmap.h
> +++ b/include/qemu/bitmap.h
> @@ -94,6 +94,12 @@ static inline unsigned long *bitmap_new(long nbits)
> return g_malloc0(len);
> }
>
> +static inline unsigned long *bitmap_try_new(long nbits)
> +{
> + long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
> + return g_try_malloc0(len);
> +}
> +
What you have works, but I personally would have reimplemented
bitmap_new as the first caller of bitmap_try_new in this patch, where
bitmap_new handles a NULL bitmap_try_new return by abort()ing, so that
it has the same behavior. By routing the one function to use the other,
we are future-proofing: if initialization of a bitmap ever needs
modification, we only update bitmap_try_new, rather than copying the
updates to both functions.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
[Qemu-devel] [PATCH 1/2] util: introduce bitmap_try_new, Peter Lieven, 2014/08/22
- Re: [Qemu-devel] [PATCH 1/2] util: introduce bitmap_try_new,
Eric Blake <=