1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

util: bitmap: Rename 'max_bit' to 'nbits'

'max_bit' is misleading as the value is set to the first invalid bit
as it's used as the number of bits in the bitmap. Rename it to a more
descriptive name.
This commit is contained in:
Peter Krempa 2018-02-05 11:24:05 +01:00
parent 3cac2a6c93
commit eacc9312e4

View File

@ -42,7 +42,7 @@
#define VIR_FROM_THIS VIR_FROM_NONE #define VIR_FROM_THIS VIR_FROM_NONE
struct _virBitmap { struct _virBitmap {
size_t max_bit; size_t nbits;
size_t map_len; size_t map_len;
size_t map_alloc; size_t map_alloc;
unsigned long *map; unsigned long *map;
@ -83,7 +83,7 @@ virBitmapNewQuiet(size_t size)
return NULL; return NULL;
} }
bitmap->max_bit = size; bitmap->nbits = size;
bitmap->map_len = sz; bitmap->map_len = sz;
bitmap->map_alloc = sz; bitmap->map_alloc = sz;
return bitmap; return bitmap;
@ -147,7 +147,7 @@ void virBitmapFree(virBitmapPtr bitmap)
int virBitmapCopy(virBitmapPtr dst, virBitmapPtr src) int virBitmapCopy(virBitmapPtr dst, virBitmapPtr src)
{ {
if (dst->max_bit != src->max_bit) { if (dst->nbits != src->nbits) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
@ -169,7 +169,7 @@ int virBitmapCopy(virBitmapPtr dst, virBitmapPtr src)
*/ */
int virBitmapSetBit(virBitmapPtr bitmap, size_t b) int virBitmapSetBit(virBitmapPtr bitmap, size_t b)
{ {
if (bitmap->max_bit <= b) if (bitmap->nbits <= b)
return -1; return -1;
bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] |= VIR_BITMAP_BIT(b); bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] |= VIR_BITMAP_BIT(b);
@ -197,7 +197,7 @@ static int virBitmapExpand(virBitmapPtr map, size_t b)
return -1; return -1;
} }
map->max_bit = b + 1; map->nbits = b + 1;
map->map_len = new_len; map->map_len = new_len;
return 0; return 0;
@ -216,7 +216,7 @@ static int virBitmapExpand(virBitmapPtr map, size_t b)
*/ */
int virBitmapSetBitExpand(virBitmapPtr bitmap, size_t b) int virBitmapSetBitExpand(virBitmapPtr bitmap, size_t b)
{ {
if (bitmap->max_bit <= b && virBitmapExpand(bitmap, b) < 0) if (bitmap->nbits <= b && virBitmapExpand(bitmap, b) < 0)
return -1; return -1;
bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] |= VIR_BITMAP_BIT(b); bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] |= VIR_BITMAP_BIT(b);
@ -235,7 +235,7 @@ int virBitmapSetBitExpand(virBitmapPtr bitmap, size_t b)
*/ */
int virBitmapClearBit(virBitmapPtr bitmap, size_t b) int virBitmapClearBit(virBitmapPtr bitmap, size_t b)
{ {
if (bitmap->max_bit <= b) if (bitmap->nbits <= b)
return -1; return -1;
bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] &= ~VIR_BITMAP_BIT(b); bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] &= ~VIR_BITMAP_BIT(b);
@ -255,7 +255,7 @@ int virBitmapClearBit(virBitmapPtr bitmap, size_t b)
*/ */
int virBitmapClearBitExpand(virBitmapPtr bitmap, size_t b) int virBitmapClearBitExpand(virBitmapPtr bitmap, size_t b)
{ {
if (bitmap->max_bit <= b) { if (bitmap->nbits <= b) {
if (virBitmapExpand(bitmap, b) < 0) if (virBitmapExpand(bitmap, b) < 0)
return -1; return -1;
} else { } else {
@ -266,7 +266,7 @@ int virBitmapClearBitExpand(virBitmapPtr bitmap, size_t b)
} }
/* Helper function. caller must ensure b < bitmap->max_bit */ /* Helper function. caller must ensure b < bitmap->nbits */
static bool virBitmapIsSet(virBitmapPtr bitmap, size_t b) static bool virBitmapIsSet(virBitmapPtr bitmap, size_t b)
{ {
return !!(bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] & VIR_BITMAP_BIT(b)); return !!(bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] & VIR_BITMAP_BIT(b));
@ -284,7 +284,7 @@ static bool virBitmapIsSet(virBitmapPtr bitmap, size_t b)
*/ */
bool virBitmapIsBitSet(virBitmapPtr bitmap, size_t b) bool virBitmapIsBitSet(virBitmapPtr bitmap, size_t b)
{ {
if (bitmap->max_bit <= b) if (bitmap->nbits <= b)
return false; return false;
return virBitmapIsSet(bitmap, b); return virBitmapIsSet(bitmap, b);
@ -303,7 +303,7 @@ bool virBitmapIsBitSet(virBitmapPtr bitmap, size_t b)
*/ */
int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result) int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
{ {
if (bitmap->max_bit <= b) if (bitmap->nbits <= b)
return -1; return -1;
*result = virBitmapIsSet(bitmap, b); *result = virBitmapIsSet(bitmap, b);
@ -350,14 +350,14 @@ virBitmapToString(virBitmapPtr bitmap,
if (!trim) if (!trim)
return ret; return ret;
if (bitmap->max_bit != bitmap->map_len * VIR_BITMAP_BITS_PER_UNIT) { if (bitmap->nbits != bitmap->map_len * VIR_BITMAP_BITS_PER_UNIT) {
char *tmp = ret; char *tmp = ret;
if (prefix) if (prefix)
tmp += 2; tmp += 2;
len = strlen(tmp); len = strlen(tmp);
sz = VIR_DIV_UP(bitmap->max_bit, 4); sz = VIR_DIV_UP(bitmap->nbits, 4);
diff = len - sz; diff = len - sz;
if (diff) if (diff)
@ -692,7 +692,7 @@ virBitmapPtr virBitmapNewCopy(virBitmapPtr src)
{ {
virBitmapPtr dst; virBitmapPtr dst;
if ((dst = virBitmapNew(src->max_bit)) == NULL) if ((dst = virBitmapNew(src->nbits)) == NULL)
return NULL; return NULL;
if (virBitmapCopy(dst, src) != 0) { if (virBitmapCopy(dst, src) != 0) {
@ -818,7 +818,7 @@ bool virBitmapEqual(virBitmapPtr b1, virBitmapPtr b2)
if (!b1 || !b2) if (!b1 || !b2)
return false; return false;
if (b1->max_bit > b2->max_bit) { if (b1->nbits > b2->nbits) {
tmp = b1; tmp = b1;
b1 = b2; b1 = b2;
b2 = tmp; b2 = tmp;
@ -841,7 +841,7 @@ bool virBitmapEqual(virBitmapPtr b1, virBitmapPtr b2)
size_t virBitmapSize(virBitmapPtr bitmap) size_t virBitmapSize(virBitmapPtr bitmap)
{ {
return bitmap->max_bit; return bitmap->nbits;
} }
/** /**
@ -852,7 +852,7 @@ size_t virBitmapSize(virBitmapPtr bitmap)
*/ */
void virBitmapSetAll(virBitmapPtr bitmap) void virBitmapSetAll(virBitmapPtr bitmap)
{ {
int tail = bitmap->max_bit % VIR_BITMAP_BITS_PER_UNIT; int tail = bitmap->nbits % VIR_BITMAP_BITS_PER_UNIT;
memset(bitmap->map, 0xff, memset(bitmap->map, 0xff,
bitmap->map_len * (VIR_BITMAP_BITS_PER_UNIT / CHAR_BIT)); bitmap->map_len * (VIR_BITMAP_BITS_PER_UNIT / CHAR_BIT));
@ -887,7 +887,7 @@ bool virBitmapIsAllSet(virBitmapPtr bitmap)
int unusedBits; int unusedBits;
size_t sz; size_t sz;
unusedBits = bitmap->map_len * VIR_BITMAP_BITS_PER_UNIT - bitmap->max_bit; unusedBits = bitmap->map_len * VIR_BITMAP_BITS_PER_UNIT - bitmap->nbits;
sz = bitmap->map_len; sz = bitmap->map_len;
if (unusedBits > 0) if (unusedBits > 0)
@ -946,7 +946,7 @@ virBitmapNextSetBit(virBitmapPtr bitmap, ssize_t pos)
pos++; pos++;
if (pos >= bitmap->max_bit) if (pos >= bitmap->nbits)
return -1; return -1;
nl = pos / VIR_BITMAP_BITS_PER_UNIT; nl = pos / VIR_BITMAP_BITS_PER_UNIT;
@ -983,7 +983,7 @@ virBitmapLastSetBit(virBitmapPtr bitmap)
if (bitmap->map_len == 0) if (bitmap->map_len == 0)
return -1; return -1;
unusedBits = bitmap->map_len * VIR_BITMAP_BITS_PER_UNIT - bitmap->max_bit; unusedBits = bitmap->map_len * VIR_BITMAP_BITS_PER_UNIT - bitmap->nbits;
sz = bitmap->map_len - 1; sz = bitmap->map_len - 1;
if (unusedBits > 0) { if (unusedBits > 0) {
@ -1035,7 +1035,7 @@ virBitmapNextClearBit(virBitmapPtr bitmap, ssize_t pos)
pos++; pos++;
if (pos >= bitmap->max_bit) if (pos >= bitmap->nbits)
return -1; return -1;
nl = pos / VIR_BITMAP_BITS_PER_UNIT; nl = pos / VIR_BITMAP_BITS_PER_UNIT;
@ -1048,7 +1048,7 @@ virBitmapNextClearBit(virBitmapPtr bitmap, ssize_t pos)
if (nl == bitmap->map_len - 1) { if (nl == bitmap->map_len - 1) {
/* Ensure tail bits are ignored. */ /* Ensure tail bits are ignored. */
int tail = bitmap->max_bit % VIR_BITMAP_BITS_PER_UNIT; int tail = bitmap->nbits % VIR_BITMAP_BITS_PER_UNIT;
if (tail) if (tail)
bits &= -1UL >> (VIR_BITMAP_BITS_PER_UNIT - tail); bits &= -1UL >> (VIR_BITMAP_BITS_PER_UNIT - tail);
@ -1140,7 +1140,7 @@ virBitmapOverlaps(virBitmapPtr b1,
{ {
size_t i; size_t i;
if (b1->max_bit > b2->max_bit) { if (b1->nbits > b2->nbits) {
virBitmapPtr tmp = b1; virBitmapPtr tmp = b1;
b1 = b2; b1 = b2;
b2 = tmp; b2 = tmp;
@ -1216,11 +1216,11 @@ virBitmapShrink(virBitmapPtr map,
if (!map) if (!map)
return 0; return 0;
if (map->max_bit >= b) if (map->nbits >= b)
map->max_bit = b; map->nbits = b;
nl = map->max_bit / VIR_BITMAP_BITS_PER_UNIT; nl = map->nbits / VIR_BITMAP_BITS_PER_UNIT;
nb = map->max_bit % VIR_BITMAP_BITS_PER_UNIT; nb = map->nbits % VIR_BITMAP_BITS_PER_UNIT;
map->map[nl] &= ((1UL << nb) - 1); map->map[nl] &= ((1UL << nb) - 1);
nl++; nl++;