bitmap: Use g_try_new0/g_renew
PATCH 1/1 bitmap: Use g_try_new0/g_renew
Avoids an explicit use of sizeof(). The GLib allocation macros ensure that the multiplication by the size of the element uses the right type and does not overflow.
Cc Roman Kiryanov <[email protected]>
Cc Daniel Berrange <[email protected]>
Signed-off-by Paolo Bonzini <[email protected]>
--- include/qemu/bitmap.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
include/qemu/bitmap.h +6 -6
@@ -92,8 +92,8 @@ long slow_bitmap_count_one(const unsigned long *bitmap, long nbits);static inline unsigned long *bitmap_try_new (long nbits) { -long len =BITS_TO_LONGS (nbits) *sizeof (unsigned long ); -return g_try_malloc0 (len); +long nelem =BITS_TO_LONGS (nbits); + return g_try_new0(unsigned long, nelem); }static inline unsigned long *bitmap_new (long nbits)
@@ -265,10 +265,10 @@ unsigned long bitmap_find_next_zero_area(unsigned long *map,static inline unsigned long *bitmap_zero_extend (unsigned long *old,long old_nbits,long new_nbits) { -long new_len =BITS_TO_LONGS (new_nbits) *sizeof (unsigned long ); -unsigned long *new =g_realloc (old, new_len); -bitmap_clear (new, old_nbits, new_nbits - old_nbits); -return new; +long new_nelem =BITS_TO_LONGS (new_nbits); + unsigned long *ptr = g_renew(unsigned long, old, new_nelem); +bitmap_clear (ptr, old_nbits, new_nbits - old_nbits); +return ptr; } void bitmap_to_le(unsigned long *dst, const unsigned long *src,
Signature · 1 line
2.44.0
Quoted 17 lines · Paolo Bonzini
Avoids an explicit use of
sizeof(). The GLib allocation macros ensure that the multiplication by the size of the element uses the right type and does not overflow.Cc Roman Kiryanov <[email protected]>Cc Daniel Berrange <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- include/qemu/bitmap.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)include/qemu/bitmap.h +0
-0
include/qemu/bitmap.h long slow_bitmap_count_one(const unsigned long *bitmap, long nbits); · line 99
Quoted 10 lines
include/qemu/bitmap.h +2
-2@@ -92,8 +92,8 @@ long slow_bitmap_count_one(const unsigned long *bitmap, long nbits);static inline unsigned long *bitmap_try_new (long nbits) { -long len =BITS_TO_LONGS (nbits) *sizeof (unsigned long ); -return g_try_malloc0 (len); +long nelem =BITS_TO_LONGS (nbits); + return g_try_new0(unsigned long, nelem); }static inline unsigned long *bitmap_new (long nbits)
I'd suggest this method be changed to directly call g_new0 too. Its current impl of calling bitmap_try_new() followed by a plain abort() has worse diagnostics than g_new0, which uses g_error to report the actual allocation size that failed.
Quoted 18 lines
include/qemu/bitmap.h +4
-4@@ -265,10 +265,10 @@ unsigned long bitmap_find_next_zero_area(unsigned long *map,static inline unsigned long *bitmap_zero_extend (unsigned long *old,long old_nbits,long new_nbits) { -long new_len =BITS_TO_LONGS (new_nbits) *sizeof (unsigned long ); -unsigned long *new =g_realloc (old, new_len); -bitmap_clear (new, old_nbits, new_nbits - old_nbits); -return new; +long new_nelem =BITS_TO_LONGS (new_nbits); + unsigned long *ptr = g_renew(unsigned long, old, new_nelem); +bitmap_clear (ptr, old_nbits, new_nbits - old_nbits); +return ptr; } void bitmap_to_le(unsigned long *dst, const unsigned long *src,Signature · 1 line
2.44.0
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|