*
* Convert a bitmap to a chunk of data containing bits information.
* Data consists of sequential bytes, with lower bytes containing
- * lower bits.
+ * lower bits. This function allocates @data.
*
* Returns 0 on success, -1 otherwise.
*/
int virBitmapToData(virBitmapPtr bitmap, unsigned char **data, int *dataLen)
{
int len;
- unsigned long *l;
- size_t i, j;
- unsigned char *bytes;
len = (bitmap->max_bit + CHAR_BIT - 1) / CHAR_BIT;
if (VIR_ALLOC_N(*data, len) < 0)
return -1;
- bytes = *data;
*dataLen = len;
+ virBitmapToDataBuf(bitmap, *data, *dataLen);
+
+ return 0;
+}
+
+/**
+ * virBitmapToDataBuf:
+ * @bytes: pointer to memory to fill
+ * @len: len of @bytes in byte
+ *
+ * Convert a bitmap to a chunk of data containing bits information.
+ * Data consists of sequential bytes, with lower bytes containing
+ * lower bits.
+ */
+void virBitmapToDataBuf(virBitmapPtr bitmap,
+ unsigned char *bytes,
+ size_t len)
+{
+ unsigned long *l;
+ size_t i, j;
+
+ memset(bytes, 0, len);
+
/* htole64 is not provided by gnulib, so we do the conversion by hand */
l = bitmap->map;
for (i = j = 0; i < len; i++, j++) {
}
bytes[i] = *l >> (j * CHAR_BIT);
}
-
- return 0;
}
/**
virBitmapPtr virBitmapNewData(void *data, int len) ATTRIBUTE_NONNULL(1);
int virBitmapToData(virBitmapPtr bitmap, unsigned char **data, int *dataLen)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
+
+void virBitmapToDataBuf(virBitmapPtr bitmap, unsigned char *data, size_t len)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
bool virBitmapEqual(virBitmapPtr b1, virBitmapPtr b2);