aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorMax Reitz <mreitz@redhat.com>2016-11-15 23:57:45 +0100
committerFam Zheng <famz@redhat.com>2017-01-26 10:25:01 +0800
commit20a579de8484096d18e65751ebe63fee31551f04 (patch)
treeb18432ea3cce4bb67817676b72a8c1e2ee0c3bb3 /util
parentc7f1cf01b8245762ca5864e835d84f6677ae8b1f (diff)
hbitmap: Add hbitmap_is_serializable()
Bitmaps with a granularity of 58 or above can be neither serialized nor deserialized (see the comment in the function added in this series for an explanation). This patch adds a function so that we can check whether a bitmap actually can be (de-)serialized at all, thus avoiding failing the necessary assertion in hbitmap_serialization_granularity(). Signed-off-by: Max Reitz <mreitz@redhat.com> Message-Id: <20161115225746.3590-2-mreitz@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Fam Zheng <famz@redhat.com>
Diffstat (limited to 'util')
-rw-r--r--util/hbitmap.c22
1 files changed, 19 insertions, 3 deletions
diff --git a/util/hbitmap.c b/util/hbitmap.c
index 9f691b76bd..35088e19c4 100644
--- a/util/hbitmap.c
+++ b/util/hbitmap.c
@@ -387,6 +387,24 @@ void hbitmap_reset_all(HBitmap *hb)
hb->count = 0;
}
+bool hbitmap_is_serializable(const HBitmap *hb)
+{
+ /* Every serialized chunk must be aligned to 64 bits so that endianness
+ * requirements can be fulfilled on both 64 bit and 32 bit hosts.
+ * We have hbitmap_serialization_granularity() which converts this
+ * alignment requirement from bitmap bits to items covered (e.g. sectors).
+ * That value is:
+ * 64 << hb->granularity
+ * Since this value must not exceed UINT64_MAX, hb->granularity must be
+ * less than 58 (== 64 - 6, where 6 is ld(64), i.e. 1 << 6 == 64).
+ *
+ * In order for hbitmap_serialization_granularity() to always return a
+ * meaningful value, bitmaps that are to be serialized must have a
+ * granularity of less than 58. */
+
+ return hb->granularity < 58;
+}
+
bool hbitmap_get(const HBitmap *hb, uint64_t item)
{
/* Compute position and bit in the last layer. */
@@ -399,9 +417,7 @@ bool hbitmap_get(const HBitmap *hb, uint64_t item)
uint64_t hbitmap_serialization_granularity(const HBitmap *hb)
{
- /* Must hold true so that the shift below is defined
- * (ld(64) == 6, i.e. 1 << 6 == 64) */
- assert(hb->granularity < 64 - 6);
+ assert(hbitmap_is_serializable(hb));
/* Require at least 64 bit granularity to be safe on both 64 bit and 32 bit
* hosts. */