2010-05-21 04:23:48 +00:00
|
|
|
/*
|
|
|
|
* bitmap.h: Simple bitmap operations
|
|
|
|
*
|
2011-02-10 10:11:15 +00:00
|
|
|
* Copyright (C) 2010-2011 Red Hat, Inc.
|
2010-05-21 04:23:48 +00:00
|
|
|
* Copyright (C) 2010 Novell, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-07-21 10:06:23 +00:00
|
|
|
* License along with this library; If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
2010-05-21 04:23:48 +00:00
|
|
|
*
|
|
|
|
* Author: Jim Fehlig <jfehlig@novell.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "bitmap.h"
|
|
|
|
#include "memory.h"
|
2011-02-09 12:14:51 +00:00
|
|
|
#include "buf.h"
|
2010-05-21 04:23:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct _virBitmap {
|
|
|
|
size_t size;
|
2011-02-10 10:11:15 +00:00
|
|
|
unsigned long *map;
|
2010-05-21 04:23:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-10 10:11:15 +00:00
|
|
|
#define VIR_BITMAP_BITS_PER_UNIT ((int) sizeof(unsigned long) * CHAR_BIT)
|
2010-05-21 04:23:48 +00:00
|
|
|
#define VIR_BITMAP_UNIT_OFFSET(b) ((b) / VIR_BITMAP_BITS_PER_UNIT)
|
|
|
|
#define VIR_BITMAP_BIT_OFFSET(b) ((b) % VIR_BITMAP_BITS_PER_UNIT)
|
2011-02-10 10:11:15 +00:00
|
|
|
#define VIR_BITMAP_BIT(b) (1UL << VIR_BITMAP_BIT_OFFSET(b))
|
2010-05-21 04:23:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virBitmapAlloc:
|
|
|
|
* @size: number of bits
|
|
|
|
*
|
|
|
|
* Allocate a bitmap capable of containing @size bits.
|
|
|
|
*
|
|
|
|
* Returns a pointer to the allocated bitmap or NULL if
|
|
|
|
* memory cannot be allocated.
|
|
|
|
*/
|
|
|
|
virBitmapPtr virBitmapAlloc(size_t size)
|
|
|
|
{
|
|
|
|
virBitmapPtr bitmap;
|
|
|
|
size_t sz;
|
|
|
|
|
2010-06-02 15:03:57 +00:00
|
|
|
if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size || size == 0)
|
2010-05-21 04:23:48 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sz = (size + VIR_BITMAP_BITS_PER_UNIT - 1) /
|
|
|
|
VIR_BITMAP_BITS_PER_UNIT;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(bitmap) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(bitmap->map, sz) < 0) {
|
|
|
|
VIR_FREE(bitmap);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-22 18:15:32 +00:00
|
|
|
bitmap->size = size;
|
2010-05-21 04:23:48 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virBitmapFree:
|
|
|
|
* @bitmap: previously allocated bitmap
|
|
|
|
*
|
|
|
|
* Free @bitmap previously allocated by virBitmapAlloc.
|
|
|
|
*/
|
|
|
|
void virBitmapFree(virBitmapPtr bitmap)
|
|
|
|
{
|
|
|
|
if (bitmap) {
|
|
|
|
VIR_FREE(bitmap->map);
|
|
|
|
VIR_FREE(bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2010-06-18 15:56:04 +00:00
|
|
|
if (bitmap->size <= b)
|
2010-05-21 04:23:48 +00:00
|
|
|
return -1;
|
|
|
|
|
2011-02-10 10:11:15 +00:00
|
|
|
bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] |= VIR_BITMAP_BIT(b);
|
2010-05-21 04:23:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virBitmapClearBit:
|
|
|
|
* @bitmap: Pointer to bitmap
|
|
|
|
* @b: bit position to clear
|
|
|
|
*
|
|
|
|
* Clear bit position @b in @bitmap
|
|
|
|
*
|
|
|
|
* Returns 0 on if bit is successfully clear, -1 on error.
|
|
|
|
*/
|
|
|
|
int virBitmapClearBit(virBitmapPtr bitmap, size_t b)
|
|
|
|
{
|
2010-06-18 15:56:04 +00:00
|
|
|
if (bitmap->size <= b)
|
2010-05-21 04:23:48 +00:00
|
|
|
return -1;
|
|
|
|
|
2011-02-10 10:11:15 +00:00
|
|
|
bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] &= ~VIR_BITMAP_BIT(b);
|
2010-05-21 04:23:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virBitmapGetBit:
|
|
|
|
* @bitmap: Pointer to bitmap
|
|
|
|
* @b: bit position to get
|
|
|
|
* @result: bool pointer to receive bit setting
|
|
|
|
*
|
|
|
|
* Get setting of bit position @b in @bitmap and store in @result
|
|
|
|
*
|
|
|
|
* On success, @result will contain the setting of @b and 0 is
|
|
|
|
* returned. On failure, -1 is returned and @result is unchanged.
|
|
|
|
*/
|
|
|
|
int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
|
|
|
|
{
|
2010-06-18 15:56:04 +00:00
|
|
|
if (bitmap->size <= b)
|
2010-05-21 04:23:48 +00:00
|
|
|
return -1;
|
|
|
|
|
2011-02-10 10:11:15 +00:00
|
|
|
*result = !!(bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] & VIR_BITMAP_BIT(b));
|
2010-05-21 04:23:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-02-09 12:14:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virBitmapString:
|
|
|
|
* @bitmap: Pointer to bitmap
|
|
|
|
*
|
|
|
|
* Convert @bitmap to printable string.
|
|
|
|
*
|
|
|
|
* Returns pointer to the string or NULL on error.
|
|
|
|
*/
|
|
|
|
char *virBitmapString(virBitmapPtr bitmap)
|
|
|
|
{
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
virBufferAddLit(&buf, "0x");
|
|
|
|
|
|
|
|
sz = (bitmap->size + VIR_BITMAP_BITS_PER_UNIT - 1) /
|
|
|
|
VIR_BITMAP_BITS_PER_UNIT;
|
|
|
|
|
|
|
|
while (sz--) {
|
2011-04-30 16:34:49 +00:00
|
|
|
virBufferAsprintf(&buf, "%0*lx",
|
2011-02-09 12:14:51 +00:00
|
|
|
VIR_BITMAP_BITS_PER_UNIT / 4,
|
|
|
|
bitmap->map[sz]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virBufferError(&buf)) {
|
|
|
|
virBufferFreeAndReset(&buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virBufferContentAndReset(&buf);
|
|
|
|
}
|