/** * virBitmapSetBit: * @bitmap: Pointer to bitmap * @b: bit position to set * * Set bit position @b in @bitmap * * Returns 0 on if bit is successfully set, -1 on error. */ int virBitmapSetBit(virBitmapPtr bitmap, size_t b) { if (bitmap->max_bit <= b) return -1; bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] |= VIR_BITMAP_BIT(b); return 0; }
/** * virBitmapSetBitExpand: * @bitmap: Pointer to bitmap * @b: bit position to set * * Set bit position @b in @bitmap. Expands the bitmap as necessary so that @b is * included in the map. * * Returns 0 on if bit is successfully set, -1 on error. */ int virBitmapSetBitExpand(virBitmapPtr bitmap, size_t b) { if (bitmap->max_bit <= b && virBitmapExpand(bitmap, b) < 0) return -1; bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] |= VIR_BITMAP_BIT(b); return 0; }
/** * virBitmapLastSetBit: * @bitmap: the bitmap * * Search for the last set bit in bitmap @bitmap. * * Returns the position of the found bit, or -1 if no bit is set. */ ssize_t virBitmapLastSetBit(virBitmapPtr bitmap) { ssize_t i; int unusedBits; ssize_t sz; unsigned long bits; /* If bitmap is empty then there is no set bit */ if (bitmap->map_len == 0) return -1; unusedBits = bitmap->map_len * VIR_BITMAP_BITS_PER_UNIT - bitmap->nbits; sz = bitmap->map_len - 1; if (unusedBits > 0) { bits = bitmap->map[sz] & (VIR_BITMAP_BIT(VIR_BITMAP_BITS_PER_UNIT - unusedBits) - 1); if (bits != 0) goto found; sz--; } for (; sz >= 0; sz--) { bits = bitmap->map[sz]; if (bits != 0) goto found; } /* Only reached if no set bit was found */ return -1; found: for (i = VIR_BITMAP_BITS_PER_UNIT - 1; i >= 0; i--) { if (bits & 1UL << i) return i + sz * VIR_BITMAP_BITS_PER_UNIT; } return -1; }
/** * virBitmapLastSetBit: * @bitmap: the bitmap * * Search for the last set bit in bitmap @bitmap. * * Returns the position of the found bit, or -1 if no bit is set. */ ssize_t virBitmapLastSetBit(virBitmapPtr bitmap) { ssize_t i; int unusedBits; ssize_t sz; unsigned long bits = 0; unusedBits = bitmap->map_len * VIR_BITMAP_BITS_PER_UNIT - bitmap->max_bit; sz = bitmap->map_len - 1; if (unusedBits > 0) { bits = bitmap->map[sz] & (VIR_BITMAP_BIT(VIR_BITMAP_BITS_PER_UNIT - unusedBits) - 1); if (bits != 0) goto found; sz--; } for (; sz >= 0; sz--) { bits = bitmap->map[sz]; if (bits != 0) goto found; } if (bits == 0) return -1; found: for (i = VIR_BITMAP_BITS_PER_UNIT - 1; i >= 0; i--) { if (bits & 1UL << i) return i + sz * VIR_BITMAP_BITS_PER_UNIT; } return -1; }
/* Helper function. caller must ensure b < bitmap->max_bit */ static bool virBitmapIsSet(virBitmapPtr bitmap, size_t b) { return !!(bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] & VIR_BITMAP_BIT(b)); }