[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v2 2/7] util: Add API for converting virBitmap into printable string



---
Notes:
    Version 2:
    - modified for unsigned long base type

 src/libvirt_private.syms |    1 +
 src/util/bitmap.c        |   33 +++++++++++++++++++++++++++++++++
 src/util/bitmap.h        |    3 +++
 3 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 797a670..66917ca 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -16,6 +16,7 @@ virBitmapClearBit;
 virBitmapFree;
 virBitmapGetBit;
 virBitmapSetBit;
+virBitmapString;
 
 
 # buf.h
diff --git a/src/util/bitmap.c b/src/util/bitmap.c
index 978ec9b..2edb2db 100644
--- a/src/util/bitmap.c
+++ b/src/util/bitmap.c
@@ -32,6 +32,7 @@
 
 #include "bitmap.h"
 #include "memory.h"
+#include "buf.h"
 
 
 struct _virBitmap {
@@ -147,3 +148,35 @@ int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
     *result = !!(bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] & VIR_BITMAP_BIT(b));
     return 0;
 }
+
+/**
+ * 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--) {
+        virBufferVSprintf(&buf, "%0*lx",
+                          VIR_BITMAP_BITS_PER_UNIT / 4,
+                          bitmap->map[sz]);
+    }
+
+    if (virBufferError(&buf)) {
+        virBufferFreeAndReset(&buf);
+        return NULL;
+    }
+
+    return virBufferContentAndReset(&buf);
+}
diff --git a/src/util/bitmap.h b/src/util/bitmap.h
index 08515d1..db297b6 100644
--- a/src/util/bitmap.h
+++ b/src/util/bitmap.h
@@ -60,4 +60,7 @@ int virBitmapClearBit(virBitmapPtr bitmap, size_t b)
 int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
 
+char *virBitmapString(virBitmapPtr bitmap)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
+
 #endif
-- 
1.7.4.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]