From: Peter Krempa Date: Thu, 17 Oct 2024 11:32:39 +0000 (+0200) Subject: virbitmap: Extract and reuse buffer size calculation into a function X-Git-Tag: v10.9.0-rc1~29 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e572150ebe6eafefcde51a50986270899e8c3629;p=thirdparty%2Flibvirt.git virbitmap: Extract and reuse buffer size calculation into a function Calculating the number of element can come handy in multiple places, extract it from virBitmapNew. Signed-off-by: Peter Krempa Reviewed-by: Jiri Denemark --- diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index a1a8c5d126..7dc63da6db 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -49,6 +49,22 @@ struct _virBitmap { #define VIR_BITMAP_BIT(b) (1UL << VIR_BITMAP_BIT_OFFSET(b)) +/** + * Calculates and returns the number of elements in the bitmap buffer to fit @bits. + */ +static size_t +virBitmapBuffsize(size_t nbits) +{ + if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < nbits) { + /* VIR_DIV_UP would overflow, let's overallocate by 1 entry instead of + * the potential overflow */ + return (nbits / VIR_BITMAP_BITS_PER_UNIT) + 1; + } + + return VIR_DIV_UP(nbits, VIR_BITMAP_BITS_PER_UNIT); +} + + /** * virBitmapNew: * @size: number of bits @@ -61,15 +77,7 @@ virBitmap * virBitmapNew(size_t size) { virBitmap *bitmap; - size_t sz; - - if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size) { - /* VIR_DIV_UP would overflow, let's overallocate by 1 entry instead of - * the potential overflow */ - sz = (size / VIR_BITMAP_BITS_PER_UNIT) + 1; - } else { - sz = VIR_DIV_UP(size, VIR_BITMAP_BITS_PER_UNIT); - } + size_t sz = virBitmapBuffsize(size); bitmap = g_new0(virBitmap, 1); @@ -133,7 +141,7 @@ static void virBitmapExpand(virBitmap *map, size_t b) { - size_t new_len = VIR_DIV_UP(b + 1, VIR_BITMAP_BITS_PER_UNIT); + size_t new_len = virBitmapBuffsize(b + 1); /* resize the memory if necessary */ if (map->map_len < new_len) {