Calculating the number of element can come handy in multiple places,
extract it from virBitmapNew.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/util/virbitmap.c | 28 ++++++++++++++++++----------
1 file changed, 18 insertions(+), 10 deletions(-)
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) {
--
2.47.0