[libvirt] [PATCH 1/3] Add simple bitmap operations to utils

Eric Blake eblake at redhat.com
Thu May 20 23:14:46 UTC 2010


On 05/20/2010 02:45 PM, Jim Fehlig wrote:
> +virBitmapPtr virBitmapAlloc(size_t size)
> +{
> +    virBitmapPtr bitmap;
> +    size_t sz = (size + VIR_BITMAP_BITS_PER_UNIT - 1) /
> +            VIR_BITMAP_BITS_PER_UNIT;

Check for overflow:
if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size)
    return NULL;

> +
> +    if (VIR_ALLOC_N(bitmap, sizeof(virBitmap)) < 0)

Use VIR_ALLOC(bitmap) - you want to allocate one bitmap object, not an
array of bitmaps of length 'sizeof(virBitmap)'.

But VIR_ALLOC_N is correct for bitmap->map.

> +
> +/**
> + * 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, unsigned int b)

size_t, not unsigned int, to avoid type mismatch when comparing against
_virBitmap.size.


> +
> +/*
> + * Set bit position @b in @bitmap
> + */
> +int virBitmapSetBit(virBitmapPtr bitmap, unsigned int b);

Add ATTRIBUTE_NONNULL(1) here, and on all subsequent prototypes that
take virBitmapPtr, and you can get rid of the bitmap==NULL check in the
implementations (but keep the virBitmapFree semantics of allowing NULL,
as it simplifies cleanup paths).  s/unsigned int/size_t/.

> +
> +/*
> + * Clear bit position @b in @bitmap
> + */
> +int virBitmapClearBit(virBitmapPtr bitmap, unsigned int b);

s/unsigned int/size_t/

> +
> +/*
> + * Get setting of bit position @b in @bitmap and store in @result
> + */
> +int virBitmapGetBit(virBitmapPtr bitmap, unsigned int b, bool *result);

s/unsigned int/size_t/.  ATTRIBUTE_NONNULL(3) as well.

-- 
Eric Blake   eblake at redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20100520/ef71d167/attachment-0001.sig>


More information about the libvir-list mailing list