Message ID | 1412060952-19407-2-git-send-email-pl@kamp.de |
---|---|
State | New |
Headers | show |
* 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
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; } static inline void bitmap_zero(unsigned long *dst, long nbits)
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(-)