util: Reintroduce virBitmapSubtract

Already introduced in the past with 9479642fd3, but then renamed to
virBitmapIntersect by a908e9e45e.  This time we'll really use it.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
Martin Kletzander 2017-10-05 15:09:30 +02:00
parent 2e5579a43b
commit 449442c34d
4 changed files with 68 additions and 0 deletions

View File

@ -1376,6 +1376,7 @@ virBitmapSetAll;
virBitmapSetBit;
virBitmapSetBitExpand;
virBitmapSize;
virBitmapSubtract;
virBitmapToData;
virBitmapToDataBuf;
virBitmapToString;

View File

@ -1174,3 +1174,25 @@ virBitmapIntersect(virBitmapPtr a,
for (i = 0; i < max; i++)
a->map[i] &= b->map[i];
}
/**
* virBitmapSubtract:
* @a: minuend/result
* @b: subtrahend
*
* Performs subtraction of two bitmaps: a = a - b
*/
void
virBitmapSubtract(virBitmapPtr a,
virBitmapPtr b)
{
size_t i;
size_t max = a->map_len;
if (max > b->map_len)
max = b->map_len;
for (i = 0; i < max; i++)
a->map[i] &= ~b->map[i];
}

View File

@ -150,4 +150,7 @@ bool virBitmapOverlaps(virBitmapPtr b1,
void virBitmapIntersect(virBitmapPtr a, virBitmapPtr b)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
void virBitmapSubtract(virBitmapPtr a, virBitmapPtr b)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
#endif

View File

@ -701,6 +701,39 @@ test13(const void *opaque ATTRIBUTE_UNUSED)
#undef TEST_MAP
static int
test14(const void *opaque)
{
const struct testBinaryOpData *data = opaque;
virBitmapPtr amap = NULL;
virBitmapPtr bmap = NULL;
virBitmapPtr resmap = NULL;
int ret = -1;
if (virBitmapParse(data->a, &amap, 256) < 0 ||
virBitmapParse(data->b, &bmap, 256) < 0 ||
virBitmapParse(data->res, &resmap, 256) < 0)
goto cleanup;
virBitmapSubtract(amap, bmap);
if (!virBitmapEqual(amap, resmap)) {
fprintf(stderr,
"\n bitmap subtraction failed: '%s' - '%s' != '%s'\n",
data->a, data->b, data->res);
goto cleanup;
}
ret = 0;
cleanup:
virBitmapFree(amap);
virBitmapFree(bmap);
virBitmapFree(resmap);
return ret;
}
#define TESTBINARYOP(A, B, RES, FUNC) \
testBinaryOpData.a = A; \
@ -750,6 +783,15 @@ mymain(void)
if (virTestRun("test13", test13, NULL) < 0)
ret = -1;
virTestCounterReset("test14-");
TESTBINARYOP("0", "0", "0,^0", test14);
TESTBINARYOP("0-3", "0", "1-3", test14);
TESTBINARYOP("0-3", "0,3", "1-2", test14);
TESTBINARYOP("0,^0", "0", "0,^0", test14);
TESTBINARYOP("0-3", "0-3", "0,^0", test14);
TESTBINARYOP("0-3", "0,^0", "0-3", test14);
TESTBINARYOP("0,2", "1,3", "0,2", test14);
return ret;
}