]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
util: bitmap: Add virBitmapToDataBuf that does not allocate the buffer
authorPeter Krempa <pkrempa@redhat.com>
Thu, 21 May 2015 16:25:36 +0000 (18:25 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 3 Jun 2015 07:42:07 +0000 (09:42 +0200)
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
src/util/virbitmap.c
src/util/virbitmap.h

index a90a1b76870c10d0a5bc134c35d1bae8338316fa..7b502aa31d699bd1cbfa20673d1e66b8d4adf096 100644 (file)
@@ -1120,6 +1120,7 @@ virBitmapSetBit;
 virBitmapSize;
 virBitmapString;
 virBitmapToData;
+virBitmapToDataBuf;
 
 
 # util/virbuffer.h
index bf905aba4d9dab5aeb03a5540c5b18345227440c..9abc8073dcca91b77eade1dee66b91b3ff2c2523 100644 (file)
@@ -492,25 +492,44 @@ 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;
+}
+
+/**
+ * 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,8 +539,6 @@ int virBitmapToData(virBitmapPtr bitmap, unsigned char **data, int *dataLen)
         }
         bytes[i] = *l >> (j * CHAR_BIT);
     }
-
-    return 0;
 }
 
 /**
index d326c6a6b079d43f464612afae629d60b60bb1f7..47488de47c176e294e8142403fc5f1ce71717dd3 100644 (file)
@@ -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);