virbitmap: Introduce virBitmapOverlaps

This internal API just checks if two bitmaps intersect or not.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Michal Privoznik 2014-07-23 17:37:19 +02:00 committed by Daniel P. Berrange
parent 725a211fc0
commit 49baed2b29
4 changed files with 50 additions and 0 deletions

View File

@ -1005,6 +1005,7 @@ virBitmapNewCopy;
virBitmapNewData; virBitmapNewData;
virBitmapNextClearBit; virBitmapNextClearBit;
virBitmapNextSetBit; virBitmapNextSetBit;
virBitmapOverlaps;
virBitmapParse; virBitmapParse;
virBitmapSetAll; virBitmapSetAll;
virBitmapSetBit; virBitmapSetBit;

View File

@ -732,3 +732,23 @@ virBitmapDataToString(void *data,
virBitmapFree(map); virBitmapFree(map);
return ret; return ret;
} }
bool
virBitmapOverlaps(virBitmapPtr b1,
virBitmapPtr b2)
{
size_t i;
if (b1->max_bit > b2->max_bit) {
virBitmapPtr tmp = b1;
b1 = b2;
b2 = tmp;
}
for (i = 0; i < b1->map_len; i++) {
if (b1->map[i] & b2->map[i])
return true;
}
return false;
}

View File

@ -114,5 +114,8 @@ size_t virBitmapCountBits(virBitmapPtr bitmap)
char *virBitmapDataToString(void *data, char *virBitmapDataToString(void *data,
int len) int len)
ATTRIBUTE_NONNULL(1); ATTRIBUTE_NONNULL(1);
bool virBitmapOverlaps(virBitmapPtr b1,
virBitmapPtr b2)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
#endif #endif

View File

@ -509,6 +509,30 @@ test9(const void *opaque ATTRIBUTE_UNUSED)
} }
static int
test10(const void *opaque ATTRIBUTE_UNUSED)
{
int ret = -1;
virBitmapPtr b1 = NULL, b2 = NULL, b3 = NULL;
if (virBitmapParse("0-3,5-8,11-15", 0, &b1, 20) < 0 ||
virBitmapParse("4,9,10,16-19", 0, &b2, 20) < 0 ||
virBitmapParse("15", 0, &b3, 20) < 0)
goto cleanup;
if (virBitmapOverlaps(b1, b2) ||
virBitmapOverlaps(b2, b3) ||
!virBitmapOverlaps(b1, b3))
goto cleanup;
ret = 0;
cleanup:
virBitmapFree(b1);
virBitmapFree(b2);
virBitmapFree(b3);
return ret;
}
static int static int
mymain(void) mymain(void)
{ {
@ -532,6 +556,8 @@ mymain(void)
ret = -1; ret = -1;
if (virtTestRun("test9", test9, NULL) < 0) if (virtTestRun("test9", test9, NULL) < 0)
ret = -1; ret = -1;
if (virtTestRun("test10", test10, NULL) < 0)
ret = -1;
return ret; return ret;
} }