mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-23 13:05:27 +00:00
util: Add API for converting virBitmap into printable string
This commit is contained in:
parent
533bee8249
commit
40641f2a63
@ -16,6 +16,7 @@ virBitmapClearBit;
|
||||
virBitmapFree;
|
||||
virBitmapGetBit;
|
||||
virBitmapSetBit;
|
||||
virBitmapString;
|
||||
|
||||
|
||||
# buf.h
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user