Skip to content

Commit

Permalink
xen/bitmap: don't open code DIV_ROUND_UP()
Browse files Browse the repository at this point in the history
Also, change bitmap_long_to_byte() and bitmap_byte_to_long() to take
'unsigned int' instead of 'int' number of bits, to match the type of
their callers.

Suggested-by: Andrew Cooper <[email protected]>
Signed-off-by: Jane Malalane <[email protected]>
Reviewed-by: Jan Beulich <[email protected]>
  • Loading branch information
janeyasmin authored and jbeulich committed Aug 12, 2021
1 parent b8848c0 commit 5a88d52
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions xen/common/bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <xen/bitops.h>
#include <xen/cpumask.h>
#include <xen/guest_access.h>
#include <xen/lib.h>
#include <asm/byteorder.h>

/*
Expand Down Expand Up @@ -338,7 +339,8 @@ EXPORT_SYMBOL(bitmap_allocate_region);

#ifdef __BIG_ENDIAN

static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits)
static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp,
unsigned int nbits)
{
unsigned long l;
int i, j, b;
Expand All @@ -354,7 +356,8 @@ static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits)
clamp_last_byte(bp, nbits);
}

static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, int nbits)
static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp,
unsigned int nbits)
{
unsigned long l;
int i, j, b;
Expand All @@ -371,18 +374,20 @@ static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, int nbits)

#elif defined(__LITTLE_ENDIAN)

static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits)
static void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp,
unsigned int nbits)
{
memcpy(bp, lp, (nbits+7)/8);
memcpy(bp, lp, DIV_ROUND_UP(nbits, BITS_PER_BYTE));
clamp_last_byte(bp, nbits);
}

static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, int nbits)
static void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp,
unsigned int nbits)
{
/* We may need to pad the final longword with zeroes. */
if (nbits & (BITS_PER_LONG-1))
lp[BITS_TO_LONGS(nbits)-1] = 0;
memcpy(lp, bp, (nbits+7)/8);
memcpy(lp, bp, DIV_ROUND_UP(nbits, BITS_PER_BYTE));
}

#endif
Expand All @@ -393,13 +398,14 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap,
unsigned int guest_bytes, copy_bytes, i;
uint8_t zero = 0;
int err = 0;
uint8_t *bytemap = xmalloc_array(uint8_t, (nbits + 7) / 8);
unsigned int xen_bytes = DIV_ROUND_UP(nbits, BITS_PER_BYTE);
uint8_t *bytemap = xmalloc_array(uint8_t, xen_bytes);

if ( !bytemap )
return -ENOMEM;

guest_bytes = (xenctl_bitmap->nr_bits + 7) / 8;
copy_bytes = min(guest_bytes, (nbits + 7) / 8);
guest_bytes = DIV_ROUND_UP(xenctl_bitmap->nr_bits, BITS_PER_BYTE);
copy_bytes = min(guest_bytes, xen_bytes);

bitmap_long_to_byte(bytemap, bitmap, nbits);

Expand All @@ -422,13 +428,14 @@ int xenctl_bitmap_to_bitmap(unsigned long *bitmap,
{
unsigned int guest_bytes, copy_bytes;
int err = 0;
uint8_t *bytemap = xzalloc_array(uint8_t, (nbits + 7) / 8);
unsigned int xen_bytes = DIV_ROUND_UP(nbits, BITS_PER_BYTE);
uint8_t *bytemap = xzalloc_array(uint8_t, xen_bytes);

if ( !bytemap )
return -ENOMEM;

guest_bytes = (xenctl_bitmap->nr_bits + 7) / 8;
copy_bytes = min(guest_bytes, (nbits + 7) / 8);
guest_bytes = DIV_ROUND_UP(xenctl_bitmap->nr_bits, BITS_PER_BYTE);
copy_bytes = min(guest_bytes, xen_bytes);

if ( copy_bytes )
{
Expand Down

0 comments on commit 5a88d52

Please sign in to comment.