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

Eric Blake eblake at redhat.com
Mon May 17 20:05:58 UTC 2010


On 05/17/2010 12:09 PM, Jim Fehlig wrote:
> ---
>  src/util/util.c |   38 ++++++++++++++++++++++++++++++++++++++
>  src/util/util.h |   10 ++++++++++
>  2 files changed, 48 insertions(+), 0 deletions(-)
> 
> diff --git a/src/util/util.c b/src/util/util.c
> index 26ac6ba..e600bef 100644
> --- a/src/util/util.c
> +++ b/src/util/util.c
> @@ -2817,3 +2817,41 @@ int virBuildPathInternal(char **path, ...)
>  
>      return ret;
>  }
> +
> +/* would CHAR_BIT or such be better than explicit '8'? */
> +#define VIR_BITMAP_BITS_PER_UNIT  (sizeof(virBitmap) * 8)

In answer to the question, yes, #include <limits.h> and use CHAR_BIT
(although it is guaranteed by POSIX to be 8).

> +virBitmapPtr virBitmapAlloc(unsigned int size)

Why not size_t?

> +{
> +    virBitmapPtr bitmap;
> +    unsigned int sz = (size / VIR_BITMAP_BITS_PER_UNIT) +
> +          (size % VIR_BITMAP_BITS_PER_UNIT);

Stefan already pointed out the bug here.

> +
> +    if (VIR_ALLOC_N(bitmap, sz) < 0)
> +        return NULL;
> +    return bitmap;
> +}
> +
> +void virBitmapFree(virBitmapPtr bitmap)
> +{
> +    VIR_FREE(bitmap);
> +}

Another function to add to cfg.mk's list of free-like functions.

> +
> +void virBitmapSetBit(virBitmapPtr bitmap, unsigned int b)
> +{
> +    bitmap[VIR_BITMAP_UNIT_OFFSET(b)] |= (1 << VIR_BITMAP_BIT_OFFSET(b));
> +}
> +
> +void virBitmapClearBit(virBitmapPtr bitmap, unsigned int b)
> +{
> +    bitmap[VIR_BITMAP_UNIT_OFFSET(b)] &= ~(1 << VIR_BITMAP_BIT_OFFSET(b));
> +}
> +
> +int virBitmapGetBit(virBitmapPtr bitmap, unsigned int b)

bool instead of int

> +{
> +    virBitmap bit = bitmap[VIR_BITMAP_UNIT_OFFSET(b)] &
> +          (1 << VIR_BITMAP_BIT_OFFSET(b));

Should these interfaces add sanity checks that b is in range (that is,
smaller than the original allocated size)?  Do we want to support
dynamically-growing bitsets?

-- 
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/20100517/58fe889a/attachment-0001.sig>


More information about the libvir-list mailing list