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

Stefan Berger stefanb at linux.vnet.ibm.com
Mon May 17 18:58:32 UTC 2010


On Mon, 2010-05-17 at 12:09 -0600, 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)
> +#define VIR_BITMAP_UNIT_OFFSET(b) ((b) / VIR_BITMAP_BITS_PER_UNIT)
> +#define VIR_BITMAP_BIT_OFFSET(b)  ((b) % VIR_BITMAP_BITS_PER_UNIT)
> +
> +virBitmapPtr virBitmapAlloc(unsigned int size)
> +{
> +    virBitmapPtr bitmap;
> +    unsigned int sz = (size / VIR_BITMAP_BITS_PER_UNIT) +
> +          (size % VIR_BITMAP_BITS_PER_UNIT);

I think this should be calculated as

(size + VIR_BITMAP_BITS_PER_UNIT - 1) / VIR_BITMAP_BITS_PER_UNIT

mine    2 bit:  ( 2 + 32 - 1 ) / 32 =  33 / 32 = 1

yours   2 bit:  ( 2 / 32 ) + (2 % 32 ) = 0 + 2

> +
> +    if (VIR_ALLOC_N(bitmap, sz) < 0)
> +        return NULL;
> +    return bitmap;
> +}
> +
> +void virBitmapFree(virBitmapPtr bitmap)
> +{
> +    VIR_FREE(bitmap);
> +}
> +
> +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)
> +{
> +    virBitmap bit = bitmap[VIR_BITMAP_UNIT_OFFSET(b)] &
> +          (1 << VIR_BITMAP_BIT_OFFSET(b));
> +    return bit != 0;
> +}
> diff --git a/src/util/util.h b/src/util/util.h
> index 6bf6bcc..077e9c9 100644
> --- a/src/util/util.h
> +++ b/src/util/util.h
> @@ -31,6 +31,7 @@
>  # include <unistd.h>
>  # include <sys/select.h>
>  # include <sys/types.h>
> +# include <stdint.h>
> 
>  # ifndef MIN
>  #  define MIN(a, b) ((a) < (b) ? (a) : (b))
> @@ -274,4 +275,13 @@ void virFileWaitForDevices(void);
>  # define virBuildPath(path, ...) virBuildPathInternal(path, __VA_ARGS__, NULL)
>  int virBuildPathInternal(char **path, ...) ATTRIBUTE_SENTINEL;
> 
> +typedef uint32_t virBitmap;
> +typedef virBitmap *virBitmapPtr;
> +
> +virBitmapPtr virBitmapAlloc(unsigned int size);
> +void virBitmapFree(virBitmapPtr bitmap);
> +void virBitmapSetBit(virBitmap *bitmap, unsigned int b);
> +void virBitmapClearBit(virBitmap *bitmap, unsigned int b);
> +int virBitmapGetBit(virBitmap *bitmap, unsigned int b);
> +
>  #endif /* __VIR_UTIL_H__ */





More information about the libvir-list mailing list