[RFC PATCH 1/6] util: Add API for converting virBitmap into printable string

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

 



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

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index b9e3efe..335d12f 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 9741668..61280b4 100644
--- a/src/util/bitmap.c
+++ b/src/util/bitmap.c
@@ -1,7 +1,7 @@
 /*
  * bitmap.h: Simple bitmap operations
  *
- * Copyright (C) 2010 Red Hat, Inc.
+ * Copyright (C) 2010-2011 Red Hat, Inc.
  * Copyright (C) 2010 Novell, Inc.
  *
  * This library is free software; you can redistribute it and/or
@@ -32,6 +32,7 @@
 
 #include "bitmap.h"
 #include "memory.h"
+#include "buf.h"
 
 
 struct _virBitmap {
@@ -151,3 +152,32 @@ int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
     *result = bit != 0;
     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;
+
+    for ( ; sz > 0; sz--)
+        virBufferVSprintf(&buf, "%08x", bitmap->map[sz - 1]);
+
+    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

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]