[libvirt] [PATCH 04/35] util: bitmap: Add virBitmapToDataBuf that does not allocate the buffer

Martin Kletzander mkletzan at redhat.com
Mon Jun 1 12:32:23 UTC 2015


On Fri, May 29, 2015 at 03:33:25PM +0200, Peter Krempa wrote:
>Since some functions can be optimized by reusing the buffers that they
>already have instead of allocating and copying new ones, lets split
>virBitmapToData to two functions where one only converts the data and
>the second one is a wrapper that allocates the buffer if necessary.
>---
> src/libvirt_private.syms |  1 +
> src/util/virbitmap.c     | 33 ++++++++++++++++++++++++++-------
> src/util/virbitmap.h     |  3 +++
> 3 files changed, 30 insertions(+), 7 deletions(-)
>
>diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
>index a90a1b7..7b502aa 100644
>--- a/src/libvirt_private.syms
>+++ b/src/libvirt_private.syms
>@@ -1120,6 +1120,7 @@ virBitmapSetBit;
> virBitmapSize;
> virBitmapString;
> virBitmapToData;
>+virBitmapToDataBuf;
>
>
> # util/virbuffer.h
>diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
>index bf905ab..c6a6b51 100644
>--- a/src/util/virbitmap.c
>+++ b/src/util/virbitmap.c
>@@ -492,25 +492,45 @@ virBitmapPtr virBitmapNewData(void *data, int len)
>  *
>  * 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;
>+}
>+
>+

After this change this is the only function in the file that has two
empty lines around it.  Put just one here ...

>+/**
>+ * 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++) {
>@@ -520,10 +540,9 @@ int virBitmapToData(virBitmapPtr bitmap, unsigned char **data, int *dataLen)
>         }
>         bytes[i] = *l >> (j * CHAR_BIT);
>     }
>-
>-    return 0;
> }
>
>+

... and don't add this one and it's perfect ;) ACK with that bit fixed.

> /**
>  * virBitmapEqual:
>  * @b1: bitmap 1
>diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h
>index d326c6a..47488de 100644
>--- a/src/util/virbitmap.h
>+++ b/src/util/virbitmap.h
>@@ -88,6 +88,9 @@ virBitmapPtr virBitmapNewCopy(virBitmapPtr src) ATTRIBUTE_NONNULL(1);
> 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);
>--
>2.4.1
>
>--
>libvir-list mailing list
>libvir-list at redhat.com
>https://www.redhat.com/mailman/listinfo/libvir-list
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20150601/4c92c7e4/attachment-0001.sig>


More information about the libvir-list mailing list