Already introduced in the past with 9479642fd3c5, but then renamed to virBitmapIntersect by a908e9e45eb2. This time we'll really use it. Signed-off-by: Martin Kletzander <mkletzan@xxxxxxxxxx> --- src/libvirt_private.syms | 1 + src/util/virbitmap.c | 22 ++++++++++++++++++++++ src/util/virbitmap.h | 3 +++ tests/virbitmaptest.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 57df411602b9..0b78a0681c5e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1370,6 +1370,7 @@ virBitmapSetAll; virBitmapSetBit; virBitmapSetBitExpand; virBitmapSize; +virBitmapSubtract; virBitmapToData; virBitmapToDataBuf; virBitmapToString; diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index 02d1f264d859..ac6ff4f6d26d 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -1169,3 +1169,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]; +} diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h index e964a3edc9cb..7b2bea8b534c 100644 --- a/src/util/virbitmap.h +++ b/src/util/virbitmap.h @@ -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 diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c index 3ea63e1295af..120c074d3def 100644 --- a/tests/virbitmaptest.c +++ b/tests/virbitmaptest.c @@ -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; } -- 2.15.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list