Re: [PATCH 1/3] Add simple bitmap operations to utils

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

 



Eric Blake wrote:
> On 05/20/2010 02:45 PM, Jim Fehlig wrote:
>   
>> +virBitmapPtr virBitmapAlloc(size_t size)
>> +{
>> +    virBitmapPtr bitmap;
>> +    size_t sz = (size + VIR_BITMAP_BITS_PER_UNIT - 1) /
>> +            VIR_BITMAP_BITS_PER_UNIT;
>>     
>
> Check for overflow:
> if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size)
>     return NULL;
>
>   
>> +
>> +    if (VIR_ALLOC_N(bitmap, sizeof(virBitmap)) < 0)
>>     
>
> Use VIR_ALLOC(bitmap) - you want to allocate one bitmap object, not an
> array of bitmaps of length 'sizeof(virBitmap)'.
>   

Opps.  Copy and paste from bitmap->map.

This and your other comments are addressed in V3.

Regards,
Jim

>From 5be6648c058a953514a01c03bc9ef6a6dadda1c4 Mon Sep 17 00:00:00 2001
Message-Id: <5be6648c058a953514a01c03bc9ef6a6dadda1c4.1274453106.git.jfehlig@xxxxxxxxxx>
In-Reply-To: <cover.1274453106.git.jfehlig@xxxxxxxxxx>
References: <cover.1274453106.git.jfehlig@xxxxxxxxxx>
From: Jim Fehlig <jfehlig@xxxxxxxxxx>
Date: Thu, 20 May 2010 22:23:48 -0600
Subject: [PATCH 1/3] Add simple bitmap operations to utils

V2:
  - Move bitmap impl to src/util/bitmap.[ch]
  - Use CHAR_BIT instead of explicit '8'
  - Use size_t instead of unsigned int
  - Fix calculation of bitmap size in virBitmapAlloc
  - Ensure bit is within range of map in the set, clear, and get
    operations
  - Use bool in virBitmapGetBit
  - Add virBitmapFree to free-like funcs in cfg.mk

V3:
  - Check for overflow in virBitmapAlloc
  - Fix copy and paste bug in virBitmapAlloc
  - Use size_t in prototypes
  - Add ATTRIBUTE_NONNULL in prototypes where appropriate
    and remove NULL check form impl
---
 cfg.mk                   |    1 +
 src/Makefile.am          |    1 +
 src/libvirt_private.syms |    8 +++
 src/util/bitmap.c        |  151 ++++++++++++++++++++++++++++++++++++++++++++++
 src/util/bitmap.h        |   61 +++++++++++++++++++
 5 files changed, 222 insertions(+), 0 deletions(-)
 create mode 100644 src/util/bitmap.c
 create mode 100644 src/util/bitmap.h

diff --git a/cfg.mk b/cfg.mk
index b024c75..bdf9ea9 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -67,6 +67,7 @@ local-checks-to-skip =			\
 useless_free_options =				\
   --name=VIR_FREE				\
   --name=sexpr_free				\
+  --name=virBitmapFree                          \
   --name=virCPUDefFree				\
   --name=virCapabilitiesFree			\
   --name=virCapabilitiesFreeGuest		\
diff --git a/src/Makefile.am b/src/Makefile.am
index 9f4e7a2..8413285 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -50,6 +50,7 @@ augeastest_DATA =
 # helper APIs for various purposes
 UTIL_SOURCES =							\
 		util/authhelper.c util/authhelper.h		\
+		util/bitmap.c util/bitmap.h			\
 		util/bridge.c util/bridge.h			\
 		util/buf.c util/buf.h				\
 		util/conf.c util/conf.h				\
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index bdeab0f..8149719 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -4,6 +4,14 @@
 #
 
 
+# bitmap.h
+virBitmapAlloc;
+virBitmapFree;
+virBitmapSetBit;
+virBitmapClearBit;
+virBitmapGetBit;
+
+
 # buf.h
 virBufferVSprintf;
 virBufferEscapeString;
diff --git a/src/util/bitmap.c b/src/util/bitmap.c
new file mode 100644
index 0000000..69094a5
--- /dev/null
+++ b/src/util/bitmap.c
@@ -0,0 +1,151 @@
+/*
+ * bitmap.h: Simple bitmap operations
+ *
+ * Copyright (C) 2010 Novell, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: Jim Fehlig <jfehlig@xxxxxxxxxx>
+ */
+
+#include <config.h>
+
+#include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+#include "bitmap.h"
+#include "memory.h"
+
+
+struct _virBitmap {
+    size_t size;
+    uint32_t *map;
+};
+
+
+#define VIR_BITMAP_BITS_PER_UNIT  (sizeof(uint32_t) * CHAR_BIT)
+#define VIR_BITMAP_UNIT_OFFSET(b) ((b) / VIR_BITMAP_BITS_PER_UNIT)
+#define VIR_BITMAP_BIT_OFFSET(b)  ((b) % VIR_BITMAP_BITS_PER_UNIT)
+
+
+/**
+ * virBitmapAlloc:
+ * @size: number of bits
+ *
+ * Allocate a bitmap capable of containing @size bits.
+ *
+ * Returns a pointer to the allocated bitmap or NULL if
+ * memory cannot be allocated.
+ */
+virBitmapPtr virBitmapAlloc(size_t size)
+{
+    virBitmapPtr bitmap;
+    size_t sz;
+
+    if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size)
+        return NULL;
+
+    sz = (size + VIR_BITMAP_BITS_PER_UNIT - 1) /
+          VIR_BITMAP_BITS_PER_UNIT;
+
+    if (VIR_ALLOC(bitmap) < 0)
+        return NULL;
+
+    if (VIR_ALLOC_N(bitmap->map, sz) < 0) {
+        VIR_FREE(bitmap);
+        return NULL;
+    }
+
+    return bitmap;
+}
+
+/**
+ * virBitmapFree:
+ * @bitmap: previously allocated bitmap
+ *
+ * Free @bitmap previously allocated by virBitmapAlloc.
+ */
+void virBitmapFree(virBitmapPtr bitmap)
+{
+    if (bitmap) {
+        VIR_FREE(bitmap->map);
+        VIR_FREE(bitmap);
+    }
+}
+
+/**
+ * virBitmapSetBit:
+ * @bitmap: Pointer to bitmap
+ * @b: bit position to set
+ *
+ * Set bit position @b in @bitmap
+ *
+ * Returns 0 on if bit is successfully set, -1 on error.
+ */
+int virBitmapSetBit(virBitmapPtr bitmap, size_t b)
+{
+    if (b > bitmap->size - 1)
+        return -1;
+
+    bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] |= (1 << VIR_BITMAP_BIT_OFFSET(b));
+    return 0;
+}
+
+/**
+ * virBitmapClearBit:
+ * @bitmap: Pointer to bitmap
+ * @b: bit position to clear
+ *
+ * Clear bit position @b in @bitmap
+ *
+ * Returns 0 on if bit is successfully clear, -1 on error.
+ */
+int virBitmapClearBit(virBitmapPtr bitmap, size_t b)
+{
+    if (b > bitmap->size - 1)
+        return -1;
+
+    bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] &= ~(1 << VIR_BITMAP_BIT_OFFSET(b));
+    return 0;
+}
+
+/**
+ * virBitmapGetBit:
+ * @bitmap: Pointer to bitmap
+ * @b: bit position to get
+ * @result: bool pointer to receive bit setting
+ *
+ * Get setting of bit position @b in @bitmap and store in @result
+ *
+ * On success, @result will contain the setting of @b and 0 is
+ * returned.  On failure, -1 is returned and @result is unchanged.
+ */
+int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
+{
+    uint32_t bit;
+
+    if (b > bitmap->size - 1)
+        return -1;
+
+    bit = bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] &
+            (1 << VIR_BITMAP_BIT_OFFSET(b));
+
+    *result = bit != 0;
+    return 0;
+}
diff --git a/src/util/bitmap.h b/src/util/bitmap.h
new file mode 100644
index 0000000..078e300
--- /dev/null
+++ b/src/util/bitmap.h
@@ -0,0 +1,61 @@
+/*
+ * bitmap.h: Simple bitmap operations
+ *
+ * Copyright (C) 2010 Novell, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: Jim Fehlig <jfehlig@xxxxxxxxxx>
+ */
+
+#ifndef __BITMAP_H__
+# define __BITMAP_H__
+
+#include "internal.h"
+
+#include <stdbool.h>
+#include <sys/types.h>
+
+
+typedef struct _virBitmap virBitmap;
+typedef virBitmap *virBitmapPtr;
+
+/*
+ * Allocate a bitmap capable of containing @size bits.
+ */
+virBitmapPtr virBitmapAlloc(size_t size);
+
+/*
+ * Free previously allocated bitmap
+ */
+void virBitmapFree(virBitmapPtr bitmap);
+
+/*
+ * Set bit position @b in @bitmap
+ */
+int virBitmapSetBit(virBitmapPtr bitmap, size_t b) ATTRIBUTE_NONNULL(1);
+
+/*
+ * Clear bit position @b in @bitmap
+ */
+int virBitmapClearBit(virBitmapPtr bitmap, size_t b) ATTRIBUTE_NONNULL(1);
+
+/*
+ * Get setting of bit position @b in @bitmap and store in @result
+ */
+int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
+
+#endif
-- 
1.6.0.2

--
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]