xref: /openbmc/qemu/util/hbitmap.c (revision a3b52535e8a577d9b12887a34183fe9077ea24d8)
1e7c033c3SPaolo Bonzini /*
2e7c033c3SPaolo Bonzini  * Hierarchical Bitmap Data Type
3e7c033c3SPaolo Bonzini  *
4e7c033c3SPaolo Bonzini  * Copyright Red Hat, Inc., 2012
5e7c033c3SPaolo Bonzini  *
6e7c033c3SPaolo Bonzini  * Author: Paolo Bonzini <pbonzini@redhat.com>
7e7c033c3SPaolo Bonzini  *
8e7c033c3SPaolo Bonzini  * This work is licensed under the terms of the GNU GPL, version 2 or
9e7c033c3SPaolo Bonzini  * later.  See the COPYING file in the top-level directory.
10e7c033c3SPaolo Bonzini  */
11e7c033c3SPaolo Bonzini 
12e7c033c3SPaolo Bonzini #include "qemu/osdep.h"
13e7c033c3SPaolo Bonzini #include "qemu/hbitmap.h"
14e7c033c3SPaolo Bonzini #include "qemu/host-utils.h"
15e7c033c3SPaolo Bonzini #include "trace.h"
16*a3b52535SVladimir Sementsov-Ogievskiy #include "crypto/hash.h"
17e7c033c3SPaolo Bonzini 
18e7c033c3SPaolo Bonzini /* HBitmaps provides an array of bits.  The bits are stored as usual in an
19e7c033c3SPaolo Bonzini  * array of unsigned longs, but HBitmap is also optimized to provide fast
20e7c033c3SPaolo Bonzini  * iteration over set bits; going from one bit to the next is O(logB n)
21e7c033c3SPaolo Bonzini  * worst case, with B = sizeof(long) * CHAR_BIT: the result is low enough
22e7c033c3SPaolo Bonzini  * that the number of levels is in fact fixed.
23e7c033c3SPaolo Bonzini  *
24e7c033c3SPaolo Bonzini  * In order to do this, it stacks multiple bitmaps with progressively coarser
25e7c033c3SPaolo Bonzini  * granularity; in all levels except the last, bit N is set iff the N-th
26e7c033c3SPaolo Bonzini  * unsigned long is nonzero in the immediately next level.  When iteration
27e7c033c3SPaolo Bonzini  * completes on the last level it can examine the 2nd-last level to quickly
28e7c033c3SPaolo Bonzini  * skip entire words, and even do so recursively to skip blocks of 64 words or
29e7c033c3SPaolo Bonzini  * powers thereof (32 on 32-bit machines).
30e7c033c3SPaolo Bonzini  *
31e7c033c3SPaolo Bonzini  * Given an index in the bitmap, it can be split in group of bits like
32e7c033c3SPaolo Bonzini  * this (for the 64-bit case):
33e7c033c3SPaolo Bonzini  *
34e7c033c3SPaolo Bonzini  *   bits 0-57 => word in the last bitmap     | bits 58-63 => bit in the word
35e7c033c3SPaolo Bonzini  *   bits 0-51 => word in the 2nd-last bitmap | bits 52-57 => bit in the word
36e7c033c3SPaolo Bonzini  *   bits 0-45 => word in the 3rd-last bitmap | bits 46-51 => bit in the word
37e7c033c3SPaolo Bonzini  *
38e7c033c3SPaolo Bonzini  * So it is easy to move up simply by shifting the index right by
39e7c033c3SPaolo Bonzini  * log2(BITS_PER_LONG) bits.  To move down, you shift the index left
40e7c033c3SPaolo Bonzini  * similarly, and add the word index within the group.  Iteration uses
41e7c033c3SPaolo Bonzini  * ffs (find first set bit) to find the next word to examine; this
42e7c033c3SPaolo Bonzini  * operation can be done in constant time in most current architectures.
43e7c033c3SPaolo Bonzini  *
44e7c033c3SPaolo Bonzini  * Setting or clearing a range of m bits on all levels, the work to perform
45e7c033c3SPaolo Bonzini  * is O(m + m/W + m/W^2 + ...), which is O(m) like on a regular bitmap.
46e7c033c3SPaolo Bonzini  *
47e7c033c3SPaolo Bonzini  * When iterating on a bitmap, each bit (on any level) is only visited
48e7c033c3SPaolo Bonzini  * once.  Hence, The total cost of visiting a bitmap with m bits in it is
49e7c033c3SPaolo Bonzini  * the number of bits that are set in all bitmaps.  Unless the bitmap is
50e7c033c3SPaolo Bonzini  * extremely sparse, this is also O(m + m/W + m/W^2 + ...), so the amortized
51e7c033c3SPaolo Bonzini  * cost of advancing from one bit to the next is usually constant (worst case
52e7c033c3SPaolo Bonzini  * O(logB n) as in the non-amortized complexity).
53e7c033c3SPaolo Bonzini  */
54e7c033c3SPaolo Bonzini 
55e7c033c3SPaolo Bonzini struct HBitmap {
56e7c033c3SPaolo Bonzini     /* Number of total bits in the bottom level.  */
57e7c033c3SPaolo Bonzini     uint64_t size;
58e7c033c3SPaolo Bonzini 
59e7c033c3SPaolo Bonzini     /* Number of set bits in the bottom level.  */
60e7c033c3SPaolo Bonzini     uint64_t count;
61e7c033c3SPaolo Bonzini 
62e7c033c3SPaolo Bonzini     /* A scaling factor.  Given a granularity of G, each bit in the bitmap will
63e7c033c3SPaolo Bonzini      * will actually represent a group of 2^G elements.  Each operation on a
64e7c033c3SPaolo Bonzini      * range of bits first rounds the bits to determine which group they land
65e7c033c3SPaolo Bonzini      * in, and then affect the entire page; iteration will only visit the first
66e7c033c3SPaolo Bonzini      * bit of each group.  Here is an example of operations in a size-16,
67e7c033c3SPaolo Bonzini      * granularity-1 HBitmap:
68e7c033c3SPaolo Bonzini      *
69e7c033c3SPaolo Bonzini      *    initial state            00000000
70e7c033c3SPaolo Bonzini      *    set(start=0, count=9)    11111000 (iter: 0, 2, 4, 6, 8)
71e7c033c3SPaolo Bonzini      *    reset(start=1, count=3)  00111000 (iter: 4, 6, 8)
72e7c033c3SPaolo Bonzini      *    set(start=9, count=2)    00111100 (iter: 4, 6, 8, 10)
73e7c033c3SPaolo Bonzini      *    reset(start=5, count=5)  00000000
74e7c033c3SPaolo Bonzini      *
75e7c033c3SPaolo Bonzini      * From an implementation point of view, when setting or resetting bits,
76e7c033c3SPaolo Bonzini      * the bitmap will scale bit numbers right by this amount of bits.  When
77e7c033c3SPaolo Bonzini      * iterating, the bitmap will scale bit numbers left by this amount of
78e7c033c3SPaolo Bonzini      * bits.
79e7c033c3SPaolo Bonzini      */
80e7c033c3SPaolo Bonzini     int granularity;
81e7c033c3SPaolo Bonzini 
8207ac4cdbSFam Zheng     /* A meta dirty bitmap to track the dirtiness of bits in this HBitmap. */
8307ac4cdbSFam Zheng     HBitmap *meta;
8407ac4cdbSFam Zheng 
85e7c033c3SPaolo Bonzini     /* A number of progressively less coarse bitmaps (i.e. level 0 is the
86e7c033c3SPaolo Bonzini      * coarsest).  Each bit in level N represents a word in level N+1 that
87e7c033c3SPaolo Bonzini      * has a set bit, except the last level where each bit represents the
88e7c033c3SPaolo Bonzini      * actual bitmap.
89e7c033c3SPaolo Bonzini      *
90e7c033c3SPaolo Bonzini      * Note that all bitmaps have the same number of levels.  Even a 1-bit
91e7c033c3SPaolo Bonzini      * bitmap will still allocate HBITMAP_LEVELS arrays.
92e7c033c3SPaolo Bonzini      */
93e7c033c3SPaolo Bonzini     unsigned long *levels[HBITMAP_LEVELS];
948515efbeSJohn Snow 
958515efbeSJohn Snow     /* The length of each levels[] array. */
968515efbeSJohn Snow     uint64_t sizes[HBITMAP_LEVELS];
97e7c033c3SPaolo Bonzini };
98e7c033c3SPaolo Bonzini 
99e7c033c3SPaolo Bonzini /* Advance hbi to the next nonzero word and return it.  hbi->pos
100e7c033c3SPaolo Bonzini  * is updated.  Returns zero if we reach the end of the bitmap.
101e7c033c3SPaolo Bonzini  */
102e7c033c3SPaolo Bonzini unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi)
103e7c033c3SPaolo Bonzini {
104e7c033c3SPaolo Bonzini     size_t pos = hbi->pos;
105e7c033c3SPaolo Bonzini     const HBitmap *hb = hbi->hb;
106e7c033c3SPaolo Bonzini     unsigned i = HBITMAP_LEVELS - 1;
107e7c033c3SPaolo Bonzini 
108e7c033c3SPaolo Bonzini     unsigned long cur;
109e7c033c3SPaolo Bonzini     do {
110f63ea4e9SVladimir Sementsov-Ogievskiy         i--;
111e7c033c3SPaolo Bonzini         pos >>= BITS_PER_LEVEL;
112f63ea4e9SVladimir Sementsov-Ogievskiy         cur = hbi->cur[i] & hb->levels[i][pos];
113e7c033c3SPaolo Bonzini     } while (cur == 0);
114e7c033c3SPaolo Bonzini 
115e7c033c3SPaolo Bonzini     /* Check for end of iteration.  We always use fewer than BITS_PER_LONG
116e7c033c3SPaolo Bonzini      * bits in the level 0 bitmap; thus we can repurpose the most significant
117e7c033c3SPaolo Bonzini      * bit as a sentinel.  The sentinel is set in hbitmap_alloc and ensures
118e7c033c3SPaolo Bonzini      * that the above loop ends even without an explicit check on i.
119e7c033c3SPaolo Bonzini      */
120e7c033c3SPaolo Bonzini 
121e7c033c3SPaolo Bonzini     if (i == 0 && cur == (1UL << (BITS_PER_LONG - 1))) {
122e7c033c3SPaolo Bonzini         return 0;
123e7c033c3SPaolo Bonzini     }
124e7c033c3SPaolo Bonzini     for (; i < HBITMAP_LEVELS - 1; i++) {
125e7c033c3SPaolo Bonzini         /* Shift back pos to the left, matching the right shifts above.
126e7c033c3SPaolo Bonzini          * The index of this word's least significant set bit provides
127e7c033c3SPaolo Bonzini          * the low-order bits.
128e7c033c3SPaolo Bonzini          */
12918331e7cSRichard Henderson         assert(cur);
13018331e7cSRichard Henderson         pos = (pos << BITS_PER_LEVEL) + ctzl(cur);
131e7c033c3SPaolo Bonzini         hbi->cur[i] = cur & (cur - 1);
132e7c033c3SPaolo Bonzini 
133e7c033c3SPaolo Bonzini         /* Set up next level for iteration.  */
134e7c033c3SPaolo Bonzini         cur = hb->levels[i + 1][pos];
135e7c033c3SPaolo Bonzini     }
136e7c033c3SPaolo Bonzini 
137e7c033c3SPaolo Bonzini     hbi->pos = pos;
138e7c033c3SPaolo Bonzini     trace_hbitmap_iter_skip_words(hbi->hb, hbi, pos, cur);
139e7c033c3SPaolo Bonzini 
140e7c033c3SPaolo Bonzini     assert(cur);
141e7c033c3SPaolo Bonzini     return cur;
142e7c033c3SPaolo Bonzini }
143e7c033c3SPaolo Bonzini 
144f63ea4e9SVladimir Sementsov-Ogievskiy int64_t hbitmap_iter_next(HBitmapIter *hbi)
145f63ea4e9SVladimir Sementsov-Ogievskiy {
146f63ea4e9SVladimir Sementsov-Ogievskiy     unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1] &
147f63ea4e9SVladimir Sementsov-Ogievskiy             hbi->hb->levels[HBITMAP_LEVELS - 1][hbi->pos];
148f63ea4e9SVladimir Sementsov-Ogievskiy     int64_t item;
149f63ea4e9SVladimir Sementsov-Ogievskiy 
150f63ea4e9SVladimir Sementsov-Ogievskiy     if (cur == 0) {
151f63ea4e9SVladimir Sementsov-Ogievskiy         cur = hbitmap_iter_skip_words(hbi);
152f63ea4e9SVladimir Sementsov-Ogievskiy         if (cur == 0) {
153f63ea4e9SVladimir Sementsov-Ogievskiy             return -1;
154f63ea4e9SVladimir Sementsov-Ogievskiy         }
155f63ea4e9SVladimir Sementsov-Ogievskiy     }
156f63ea4e9SVladimir Sementsov-Ogievskiy 
157f63ea4e9SVladimir Sementsov-Ogievskiy     /* The next call will resume work from the next bit.  */
158f63ea4e9SVladimir Sementsov-Ogievskiy     hbi->cur[HBITMAP_LEVELS - 1] = cur & (cur - 1);
159f63ea4e9SVladimir Sementsov-Ogievskiy     item = ((uint64_t)hbi->pos << BITS_PER_LEVEL) + ctzl(cur);
160f63ea4e9SVladimir Sementsov-Ogievskiy 
161f63ea4e9SVladimir Sementsov-Ogievskiy     return item << hbi->granularity;
162f63ea4e9SVladimir Sementsov-Ogievskiy }
163f63ea4e9SVladimir Sementsov-Ogievskiy 
164e7c033c3SPaolo Bonzini void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first)
165e7c033c3SPaolo Bonzini {
166e7c033c3SPaolo Bonzini     unsigned i, bit;
167e7c033c3SPaolo Bonzini     uint64_t pos;
168e7c033c3SPaolo Bonzini 
169e7c033c3SPaolo Bonzini     hbi->hb = hb;
170e7c033c3SPaolo Bonzini     pos = first >> hb->granularity;
1711b095244SPaolo Bonzini     assert(pos < hb->size);
172e7c033c3SPaolo Bonzini     hbi->pos = pos >> BITS_PER_LEVEL;
173e7c033c3SPaolo Bonzini     hbi->granularity = hb->granularity;
174e7c033c3SPaolo Bonzini 
175e7c033c3SPaolo Bonzini     for (i = HBITMAP_LEVELS; i-- > 0; ) {
176e7c033c3SPaolo Bonzini         bit = pos & (BITS_PER_LONG - 1);
177e7c033c3SPaolo Bonzini         pos >>= BITS_PER_LEVEL;
178e7c033c3SPaolo Bonzini 
179e7c033c3SPaolo Bonzini         /* Drop bits representing items before first.  */
180e7c033c3SPaolo Bonzini         hbi->cur[i] = hb->levels[i][pos] & ~((1UL << bit) - 1);
181e7c033c3SPaolo Bonzini 
182e7c033c3SPaolo Bonzini         /* We have already added level i+1, so the lowest set bit has
183e7c033c3SPaolo Bonzini          * been processed.  Clear it.
184e7c033c3SPaolo Bonzini          */
185e7c033c3SPaolo Bonzini         if (i != HBITMAP_LEVELS - 1) {
186e7c033c3SPaolo Bonzini             hbi->cur[i] &= ~(1UL << bit);
187e7c033c3SPaolo Bonzini         }
188e7c033c3SPaolo Bonzini     }
189e7c033c3SPaolo Bonzini }
190e7c033c3SPaolo Bonzini 
191e7c033c3SPaolo Bonzini bool hbitmap_empty(const HBitmap *hb)
192e7c033c3SPaolo Bonzini {
193e7c033c3SPaolo Bonzini     return hb->count == 0;
194e7c033c3SPaolo Bonzini }
195e7c033c3SPaolo Bonzini 
196e7c033c3SPaolo Bonzini int hbitmap_granularity(const HBitmap *hb)
197e7c033c3SPaolo Bonzini {
198e7c033c3SPaolo Bonzini     return hb->granularity;
199e7c033c3SPaolo Bonzini }
200e7c033c3SPaolo Bonzini 
201e7c033c3SPaolo Bonzini uint64_t hbitmap_count(const HBitmap *hb)
202e7c033c3SPaolo Bonzini {
203e7c033c3SPaolo Bonzini     return hb->count << hb->granularity;
204e7c033c3SPaolo Bonzini }
205e7c033c3SPaolo Bonzini 
206e7c033c3SPaolo Bonzini /* Count the number of set bits between start and end, not accounting for
207e7c033c3SPaolo Bonzini  * the granularity.  Also an example of how to use hbitmap_iter_next_word.
208e7c033c3SPaolo Bonzini  */
209e7c033c3SPaolo Bonzini static uint64_t hb_count_between(HBitmap *hb, uint64_t start, uint64_t last)
210e7c033c3SPaolo Bonzini {
211e7c033c3SPaolo Bonzini     HBitmapIter hbi;
212e7c033c3SPaolo Bonzini     uint64_t count = 0;
213e7c033c3SPaolo Bonzini     uint64_t end = last + 1;
214e7c033c3SPaolo Bonzini     unsigned long cur;
215e7c033c3SPaolo Bonzini     size_t pos;
216e7c033c3SPaolo Bonzini 
217e7c033c3SPaolo Bonzini     hbitmap_iter_init(&hbi, hb, start << hb->granularity);
218e7c033c3SPaolo Bonzini     for (;;) {
219e7c033c3SPaolo Bonzini         pos = hbitmap_iter_next_word(&hbi, &cur);
220e7c033c3SPaolo Bonzini         if (pos >= (end >> BITS_PER_LEVEL)) {
221e7c033c3SPaolo Bonzini             break;
222e7c033c3SPaolo Bonzini         }
223591b320aSPeter Maydell         count += ctpopl(cur);
224e7c033c3SPaolo Bonzini     }
225e7c033c3SPaolo Bonzini 
226e7c033c3SPaolo Bonzini     if (pos == (end >> BITS_PER_LEVEL)) {
227e7c033c3SPaolo Bonzini         /* Drop bits representing the END-th and subsequent items.  */
228e7c033c3SPaolo Bonzini         int bit = end & (BITS_PER_LONG - 1);
229e7c033c3SPaolo Bonzini         cur &= (1UL << bit) - 1;
230591b320aSPeter Maydell         count += ctpopl(cur);
231e7c033c3SPaolo Bonzini     }
232e7c033c3SPaolo Bonzini 
233e7c033c3SPaolo Bonzini     return count;
234e7c033c3SPaolo Bonzini }
235e7c033c3SPaolo Bonzini 
236e7c033c3SPaolo Bonzini /* Setting starts at the last layer and propagates up if an element
23707ac4cdbSFam Zheng  * changes.
238e7c033c3SPaolo Bonzini  */
239e7c033c3SPaolo Bonzini static inline bool hb_set_elem(unsigned long *elem, uint64_t start, uint64_t last)
240e7c033c3SPaolo Bonzini {
241e7c033c3SPaolo Bonzini     unsigned long mask;
24207ac4cdbSFam Zheng     unsigned long old;
243e7c033c3SPaolo Bonzini 
244e7c033c3SPaolo Bonzini     assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL));
245e7c033c3SPaolo Bonzini     assert(start <= last);
246e7c033c3SPaolo Bonzini 
247e7c033c3SPaolo Bonzini     mask = 2UL << (last & (BITS_PER_LONG - 1));
248e7c033c3SPaolo Bonzini     mask -= 1UL << (start & (BITS_PER_LONG - 1));
24907ac4cdbSFam Zheng     old = *elem;
250e7c033c3SPaolo Bonzini     *elem |= mask;
25107ac4cdbSFam Zheng     return old != *elem;
252e7c033c3SPaolo Bonzini }
253e7c033c3SPaolo Bonzini 
25407ac4cdbSFam Zheng /* The recursive workhorse (the depth is limited to HBITMAP_LEVELS)...
25507ac4cdbSFam Zheng  * Returns true if at least one bit is changed. */
25607ac4cdbSFam Zheng static bool hb_set_between(HBitmap *hb, int level, uint64_t start,
25707ac4cdbSFam Zheng                            uint64_t last)
258e7c033c3SPaolo Bonzini {
259e7c033c3SPaolo Bonzini     size_t pos = start >> BITS_PER_LEVEL;
260e7c033c3SPaolo Bonzini     size_t lastpos = last >> BITS_PER_LEVEL;
261e7c033c3SPaolo Bonzini     bool changed = false;
262e7c033c3SPaolo Bonzini     size_t i;
263e7c033c3SPaolo Bonzini 
264e7c033c3SPaolo Bonzini     i = pos;
265e7c033c3SPaolo Bonzini     if (i < lastpos) {
266e7c033c3SPaolo Bonzini         uint64_t next = (start | (BITS_PER_LONG - 1)) + 1;
267e7c033c3SPaolo Bonzini         changed |= hb_set_elem(&hb->levels[level][i], start, next - 1);
268e7c033c3SPaolo Bonzini         for (;;) {
269e7c033c3SPaolo Bonzini             start = next;
270e7c033c3SPaolo Bonzini             next += BITS_PER_LONG;
271e7c033c3SPaolo Bonzini             if (++i == lastpos) {
272e7c033c3SPaolo Bonzini                 break;
273e7c033c3SPaolo Bonzini             }
274e7c033c3SPaolo Bonzini             changed |= (hb->levels[level][i] == 0);
275e7c033c3SPaolo Bonzini             hb->levels[level][i] = ~0UL;
276e7c033c3SPaolo Bonzini         }
277e7c033c3SPaolo Bonzini     }
278e7c033c3SPaolo Bonzini     changed |= hb_set_elem(&hb->levels[level][i], start, last);
279e7c033c3SPaolo Bonzini 
280e7c033c3SPaolo Bonzini     /* If there was any change in this layer, we may have to update
281e7c033c3SPaolo Bonzini      * the one above.
282e7c033c3SPaolo Bonzini      */
283e7c033c3SPaolo Bonzini     if (level > 0 && changed) {
284e7c033c3SPaolo Bonzini         hb_set_between(hb, level - 1, pos, lastpos);
285e7c033c3SPaolo Bonzini     }
28607ac4cdbSFam Zheng     return changed;
287e7c033c3SPaolo Bonzini }
288e7c033c3SPaolo Bonzini 
289e7c033c3SPaolo Bonzini void hbitmap_set(HBitmap *hb, uint64_t start, uint64_t count)
290e7c033c3SPaolo Bonzini {
291e7c033c3SPaolo Bonzini     /* Compute range in the last layer.  */
29207ac4cdbSFam Zheng     uint64_t first, n;
293e7c033c3SPaolo Bonzini     uint64_t last = start + count - 1;
294e7c033c3SPaolo Bonzini 
295e7c033c3SPaolo Bonzini     trace_hbitmap_set(hb, start, count,
296e7c033c3SPaolo Bonzini                       start >> hb->granularity, last >> hb->granularity);
297e7c033c3SPaolo Bonzini 
29807ac4cdbSFam Zheng     first = start >> hb->granularity;
299e7c033c3SPaolo Bonzini     last >>= hb->granularity;
3000e321191SVladimir Sementsov-Ogievskiy     assert(last < hb->size);
30107ac4cdbSFam Zheng     n = last - first + 1;
302e7c033c3SPaolo Bonzini 
30307ac4cdbSFam Zheng     hb->count += n - hb_count_between(hb, first, last);
30407ac4cdbSFam Zheng     if (hb_set_between(hb, HBITMAP_LEVELS - 1, first, last) &&
30507ac4cdbSFam Zheng         hb->meta) {
30607ac4cdbSFam Zheng         hbitmap_set(hb->meta, start, count);
30707ac4cdbSFam Zheng     }
308e7c033c3SPaolo Bonzini }
309e7c033c3SPaolo Bonzini 
310e7c033c3SPaolo Bonzini /* Resetting works the other way round: propagate up if the new
311e7c033c3SPaolo Bonzini  * value is zero.
312e7c033c3SPaolo Bonzini  */
313e7c033c3SPaolo Bonzini static inline bool hb_reset_elem(unsigned long *elem, uint64_t start, uint64_t last)
314e7c033c3SPaolo Bonzini {
315e7c033c3SPaolo Bonzini     unsigned long mask;
316e7c033c3SPaolo Bonzini     bool blanked;
317e7c033c3SPaolo Bonzini 
318e7c033c3SPaolo Bonzini     assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL));
319e7c033c3SPaolo Bonzini     assert(start <= last);
320e7c033c3SPaolo Bonzini 
321e7c033c3SPaolo Bonzini     mask = 2UL << (last & (BITS_PER_LONG - 1));
322e7c033c3SPaolo Bonzini     mask -= 1UL << (start & (BITS_PER_LONG - 1));
323e7c033c3SPaolo Bonzini     blanked = *elem != 0 && ((*elem & ~mask) == 0);
324e7c033c3SPaolo Bonzini     *elem &= ~mask;
325e7c033c3SPaolo Bonzini     return blanked;
326e7c033c3SPaolo Bonzini }
327e7c033c3SPaolo Bonzini 
32807ac4cdbSFam Zheng /* The recursive workhorse (the depth is limited to HBITMAP_LEVELS)...
32907ac4cdbSFam Zheng  * Returns true if at least one bit is changed. */
33007ac4cdbSFam Zheng static bool hb_reset_between(HBitmap *hb, int level, uint64_t start,
33107ac4cdbSFam Zheng                              uint64_t last)
332e7c033c3SPaolo Bonzini {
333e7c033c3SPaolo Bonzini     size_t pos = start >> BITS_PER_LEVEL;
334e7c033c3SPaolo Bonzini     size_t lastpos = last >> BITS_PER_LEVEL;
335e7c033c3SPaolo Bonzini     bool changed = false;
336e7c033c3SPaolo Bonzini     size_t i;
337e7c033c3SPaolo Bonzini 
338e7c033c3SPaolo Bonzini     i = pos;
339e7c033c3SPaolo Bonzini     if (i < lastpos) {
340e7c033c3SPaolo Bonzini         uint64_t next = (start | (BITS_PER_LONG - 1)) + 1;
341e7c033c3SPaolo Bonzini 
342e7c033c3SPaolo Bonzini         /* Here we need a more complex test than when setting bits.  Even if
343e7c033c3SPaolo Bonzini          * something was changed, we must not blank bits in the upper level
344e7c033c3SPaolo Bonzini          * unless the lower-level word became entirely zero.  So, remove pos
345e7c033c3SPaolo Bonzini          * from the upper-level range if bits remain set.
346e7c033c3SPaolo Bonzini          */
347e7c033c3SPaolo Bonzini         if (hb_reset_elem(&hb->levels[level][i], start, next - 1)) {
348e7c033c3SPaolo Bonzini             changed = true;
349e7c033c3SPaolo Bonzini         } else {
350e7c033c3SPaolo Bonzini             pos++;
351e7c033c3SPaolo Bonzini         }
352e7c033c3SPaolo Bonzini 
353e7c033c3SPaolo Bonzini         for (;;) {
354e7c033c3SPaolo Bonzini             start = next;
355e7c033c3SPaolo Bonzini             next += BITS_PER_LONG;
356e7c033c3SPaolo Bonzini             if (++i == lastpos) {
357e7c033c3SPaolo Bonzini                 break;
358e7c033c3SPaolo Bonzini             }
359e7c033c3SPaolo Bonzini             changed |= (hb->levels[level][i] != 0);
360e7c033c3SPaolo Bonzini             hb->levels[level][i] = 0UL;
361e7c033c3SPaolo Bonzini         }
362e7c033c3SPaolo Bonzini     }
363e7c033c3SPaolo Bonzini 
364e7c033c3SPaolo Bonzini     /* Same as above, this time for lastpos.  */
365e7c033c3SPaolo Bonzini     if (hb_reset_elem(&hb->levels[level][i], start, last)) {
366e7c033c3SPaolo Bonzini         changed = true;
367e7c033c3SPaolo Bonzini     } else {
368e7c033c3SPaolo Bonzini         lastpos--;
369e7c033c3SPaolo Bonzini     }
370e7c033c3SPaolo Bonzini 
371e7c033c3SPaolo Bonzini     if (level > 0 && changed) {
372e7c033c3SPaolo Bonzini         hb_reset_between(hb, level - 1, pos, lastpos);
373e7c033c3SPaolo Bonzini     }
37407ac4cdbSFam Zheng 
37507ac4cdbSFam Zheng     return changed;
37607ac4cdbSFam Zheng 
377e7c033c3SPaolo Bonzini }
378e7c033c3SPaolo Bonzini 
379e7c033c3SPaolo Bonzini void hbitmap_reset(HBitmap *hb, uint64_t start, uint64_t count)
380e7c033c3SPaolo Bonzini {
381e7c033c3SPaolo Bonzini     /* Compute range in the last layer.  */
38207ac4cdbSFam Zheng     uint64_t first;
383e7c033c3SPaolo Bonzini     uint64_t last = start + count - 1;
384e7c033c3SPaolo Bonzini 
385e7c033c3SPaolo Bonzini     trace_hbitmap_reset(hb, start, count,
386e7c033c3SPaolo Bonzini                         start >> hb->granularity, last >> hb->granularity);
387e7c033c3SPaolo Bonzini 
38807ac4cdbSFam Zheng     first = start >> hb->granularity;
389e7c033c3SPaolo Bonzini     last >>= hb->granularity;
3900e321191SVladimir Sementsov-Ogievskiy     assert(last < hb->size);
391e7c033c3SPaolo Bonzini 
39207ac4cdbSFam Zheng     hb->count -= hb_count_between(hb, first, last);
39307ac4cdbSFam Zheng     if (hb_reset_between(hb, HBITMAP_LEVELS - 1, first, last) &&
39407ac4cdbSFam Zheng         hb->meta) {
39507ac4cdbSFam Zheng         hbitmap_set(hb->meta, start, count);
39607ac4cdbSFam Zheng     }
397e7c033c3SPaolo Bonzini }
398e7c033c3SPaolo Bonzini 
399c6a8c328SWen Congyang void hbitmap_reset_all(HBitmap *hb)
400c6a8c328SWen Congyang {
401c6a8c328SWen Congyang     unsigned int i;
402c6a8c328SWen Congyang 
403c6a8c328SWen Congyang     /* Same as hbitmap_alloc() except for memset() instead of malloc() */
404c6a8c328SWen Congyang     for (i = HBITMAP_LEVELS; --i >= 1; ) {
405c6a8c328SWen Congyang         memset(hb->levels[i], 0, hb->sizes[i] * sizeof(unsigned long));
406c6a8c328SWen Congyang     }
407c6a8c328SWen Congyang 
408c6a8c328SWen Congyang     hb->levels[0][0] = 1UL << (BITS_PER_LONG - 1);
409c6a8c328SWen Congyang     hb->count = 0;
410c6a8c328SWen Congyang }
411c6a8c328SWen Congyang 
41220a579deSMax Reitz bool hbitmap_is_serializable(const HBitmap *hb)
41320a579deSMax Reitz {
41420a579deSMax Reitz     /* Every serialized chunk must be aligned to 64 bits so that endianness
41520a579deSMax Reitz      * requirements can be fulfilled on both 64 bit and 32 bit hosts.
41620a579deSMax Reitz      * We have hbitmap_serialization_granularity() which converts this
41720a579deSMax Reitz      * alignment requirement from bitmap bits to items covered (e.g. sectors).
41820a579deSMax Reitz      * That value is:
41920a579deSMax Reitz      *    64 << hb->granularity
42020a579deSMax Reitz      * Since this value must not exceed UINT64_MAX, hb->granularity must be
42120a579deSMax Reitz      * less than 58 (== 64 - 6, where 6 is ld(64), i.e. 1 << 6 == 64).
42220a579deSMax Reitz      *
42320a579deSMax Reitz      * In order for hbitmap_serialization_granularity() to always return a
42420a579deSMax Reitz      * meaningful value, bitmaps that are to be serialized must have a
42520a579deSMax Reitz      * granularity of less than 58. */
42620a579deSMax Reitz 
42720a579deSMax Reitz     return hb->granularity < 58;
42820a579deSMax Reitz }
42920a579deSMax Reitz 
430e7c033c3SPaolo Bonzini bool hbitmap_get(const HBitmap *hb, uint64_t item)
431e7c033c3SPaolo Bonzini {
432e7c033c3SPaolo Bonzini     /* Compute position and bit in the last layer.  */
433e7c033c3SPaolo Bonzini     uint64_t pos = item >> hb->granularity;
434e7c033c3SPaolo Bonzini     unsigned long bit = 1UL << (pos & (BITS_PER_LONG - 1));
4350e321191SVladimir Sementsov-Ogievskiy     assert(pos < hb->size);
436e7c033c3SPaolo Bonzini 
437e7c033c3SPaolo Bonzini     return (hb->levels[HBITMAP_LEVELS - 1][pos >> BITS_PER_LEVEL] & bit) != 0;
438e7c033c3SPaolo Bonzini }
439e7c033c3SPaolo Bonzini 
4408258888eSVladimir Sementsov-Ogievskiy uint64_t hbitmap_serialization_granularity(const HBitmap *hb)
4418258888eSVladimir Sementsov-Ogievskiy {
44220a579deSMax Reitz     assert(hbitmap_is_serializable(hb));
4436725f887SMax Reitz 
4448258888eSVladimir Sementsov-Ogievskiy     /* Require at least 64 bit granularity to be safe on both 64 bit and 32 bit
4458258888eSVladimir Sementsov-Ogievskiy      * hosts. */
4466725f887SMax Reitz     return UINT64_C(64) << hb->granularity;
4478258888eSVladimir Sementsov-Ogievskiy }
4488258888eSVladimir Sementsov-Ogievskiy 
4498258888eSVladimir Sementsov-Ogievskiy /* Start should be aligned to serialization granularity, chunk size should be
4508258888eSVladimir Sementsov-Ogievskiy  * aligned to serialization granularity too, except for last chunk.
4518258888eSVladimir Sementsov-Ogievskiy  */
4528258888eSVladimir Sementsov-Ogievskiy static void serialization_chunk(const HBitmap *hb,
4538258888eSVladimir Sementsov-Ogievskiy                                 uint64_t start, uint64_t count,
4548258888eSVladimir Sementsov-Ogievskiy                                 unsigned long **first_el, uint64_t *el_count)
4558258888eSVladimir Sementsov-Ogievskiy {
4568258888eSVladimir Sementsov-Ogievskiy     uint64_t last = start + count - 1;
4578258888eSVladimir Sementsov-Ogievskiy     uint64_t gran = hbitmap_serialization_granularity(hb);
4588258888eSVladimir Sementsov-Ogievskiy 
4598258888eSVladimir Sementsov-Ogievskiy     assert((start & (gran - 1)) == 0);
4608258888eSVladimir Sementsov-Ogievskiy     assert((last >> hb->granularity) < hb->size);
4618258888eSVladimir Sementsov-Ogievskiy     if ((last >> hb->granularity) != hb->size - 1) {
4628258888eSVladimir Sementsov-Ogievskiy         assert((count & (gran - 1)) == 0);
4638258888eSVladimir Sementsov-Ogievskiy     }
4648258888eSVladimir Sementsov-Ogievskiy 
4658258888eSVladimir Sementsov-Ogievskiy     start = (start >> hb->granularity) >> BITS_PER_LEVEL;
4668258888eSVladimir Sementsov-Ogievskiy     last = (last >> hb->granularity) >> BITS_PER_LEVEL;
4678258888eSVladimir Sementsov-Ogievskiy 
4688258888eSVladimir Sementsov-Ogievskiy     *first_el = &hb->levels[HBITMAP_LEVELS - 1][start];
4698258888eSVladimir Sementsov-Ogievskiy     *el_count = last - start + 1;
4708258888eSVladimir Sementsov-Ogievskiy }
4718258888eSVladimir Sementsov-Ogievskiy 
4728258888eSVladimir Sementsov-Ogievskiy uint64_t hbitmap_serialization_size(const HBitmap *hb,
4738258888eSVladimir Sementsov-Ogievskiy                                     uint64_t start, uint64_t count)
4748258888eSVladimir Sementsov-Ogievskiy {
4758258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
4768258888eSVladimir Sementsov-Ogievskiy     unsigned long *cur;
4778258888eSVladimir Sementsov-Ogievskiy 
4788258888eSVladimir Sementsov-Ogievskiy     if (!count) {
4798258888eSVladimir Sementsov-Ogievskiy         return 0;
4808258888eSVladimir Sementsov-Ogievskiy     }
4818258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &cur, &el_count);
4828258888eSVladimir Sementsov-Ogievskiy 
4838258888eSVladimir Sementsov-Ogievskiy     return el_count * sizeof(unsigned long);
4848258888eSVladimir Sementsov-Ogievskiy }
4858258888eSVladimir Sementsov-Ogievskiy 
4868258888eSVladimir Sementsov-Ogievskiy void hbitmap_serialize_part(const HBitmap *hb, uint8_t *buf,
4878258888eSVladimir Sementsov-Ogievskiy                             uint64_t start, uint64_t count)
4888258888eSVladimir Sementsov-Ogievskiy {
4898258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
4908258888eSVladimir Sementsov-Ogievskiy     unsigned long *cur, *end;
4918258888eSVladimir Sementsov-Ogievskiy 
4928258888eSVladimir Sementsov-Ogievskiy     if (!count) {
4938258888eSVladimir Sementsov-Ogievskiy         return;
4948258888eSVladimir Sementsov-Ogievskiy     }
4958258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &cur, &el_count);
4968258888eSVladimir Sementsov-Ogievskiy     end = cur + el_count;
4978258888eSVladimir Sementsov-Ogievskiy 
4988258888eSVladimir Sementsov-Ogievskiy     while (cur != end) {
4998258888eSVladimir Sementsov-Ogievskiy         unsigned long el =
5008258888eSVladimir Sementsov-Ogievskiy             (BITS_PER_LONG == 32 ? cpu_to_le32(*cur) : cpu_to_le64(*cur));
5018258888eSVladimir Sementsov-Ogievskiy 
5028258888eSVladimir Sementsov-Ogievskiy         memcpy(buf, &el, sizeof(el));
5038258888eSVladimir Sementsov-Ogievskiy         buf += sizeof(el);
5048258888eSVladimir Sementsov-Ogievskiy         cur++;
5058258888eSVladimir Sementsov-Ogievskiy     }
5068258888eSVladimir Sementsov-Ogievskiy }
5078258888eSVladimir Sementsov-Ogievskiy 
5088258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_part(HBitmap *hb, uint8_t *buf,
5098258888eSVladimir Sementsov-Ogievskiy                               uint64_t start, uint64_t count,
5108258888eSVladimir Sementsov-Ogievskiy                               bool finish)
5118258888eSVladimir Sementsov-Ogievskiy {
5128258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
5138258888eSVladimir Sementsov-Ogievskiy     unsigned long *cur, *end;
5148258888eSVladimir Sementsov-Ogievskiy 
5158258888eSVladimir Sementsov-Ogievskiy     if (!count) {
5168258888eSVladimir Sementsov-Ogievskiy         return;
5178258888eSVladimir Sementsov-Ogievskiy     }
5188258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &cur, &el_count);
5198258888eSVladimir Sementsov-Ogievskiy     end = cur + el_count;
5208258888eSVladimir Sementsov-Ogievskiy 
5218258888eSVladimir Sementsov-Ogievskiy     while (cur != end) {
5228258888eSVladimir Sementsov-Ogievskiy         memcpy(cur, buf, sizeof(*cur));
5238258888eSVladimir Sementsov-Ogievskiy 
5248258888eSVladimir Sementsov-Ogievskiy         if (BITS_PER_LONG == 32) {
5258258888eSVladimir Sementsov-Ogievskiy             le32_to_cpus((uint32_t *)cur);
5268258888eSVladimir Sementsov-Ogievskiy         } else {
5278258888eSVladimir Sementsov-Ogievskiy             le64_to_cpus((uint64_t *)cur);
5288258888eSVladimir Sementsov-Ogievskiy         }
5298258888eSVladimir Sementsov-Ogievskiy 
5308258888eSVladimir Sementsov-Ogievskiy         buf += sizeof(unsigned long);
5318258888eSVladimir Sementsov-Ogievskiy         cur++;
5328258888eSVladimir Sementsov-Ogievskiy     }
5338258888eSVladimir Sementsov-Ogievskiy     if (finish) {
5348258888eSVladimir Sementsov-Ogievskiy         hbitmap_deserialize_finish(hb);
5358258888eSVladimir Sementsov-Ogievskiy     }
5368258888eSVladimir Sementsov-Ogievskiy }
5378258888eSVladimir Sementsov-Ogievskiy 
5388258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_zeroes(HBitmap *hb, uint64_t start, uint64_t count,
5398258888eSVladimir Sementsov-Ogievskiy                                 bool finish)
5408258888eSVladimir Sementsov-Ogievskiy {
5418258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
5428258888eSVladimir Sementsov-Ogievskiy     unsigned long *first;
5438258888eSVladimir Sementsov-Ogievskiy 
5448258888eSVladimir Sementsov-Ogievskiy     if (!count) {
5458258888eSVladimir Sementsov-Ogievskiy         return;
5468258888eSVladimir Sementsov-Ogievskiy     }
5478258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &first, &el_count);
5488258888eSVladimir Sementsov-Ogievskiy 
5498258888eSVladimir Sementsov-Ogievskiy     memset(first, 0, el_count * sizeof(unsigned long));
5508258888eSVladimir Sementsov-Ogievskiy     if (finish) {
5518258888eSVladimir Sementsov-Ogievskiy         hbitmap_deserialize_finish(hb);
5528258888eSVladimir Sementsov-Ogievskiy     }
5538258888eSVladimir Sementsov-Ogievskiy }
5548258888eSVladimir Sementsov-Ogievskiy 
5556bdc8b71SVladimir Sementsov-Ogievskiy void hbitmap_deserialize_ones(HBitmap *hb, uint64_t start, uint64_t count,
5566bdc8b71SVladimir Sementsov-Ogievskiy                               bool finish)
5576bdc8b71SVladimir Sementsov-Ogievskiy {
5586bdc8b71SVladimir Sementsov-Ogievskiy     uint64_t el_count;
5596bdc8b71SVladimir Sementsov-Ogievskiy     unsigned long *first;
5606bdc8b71SVladimir Sementsov-Ogievskiy 
5616bdc8b71SVladimir Sementsov-Ogievskiy     if (!count) {
5626bdc8b71SVladimir Sementsov-Ogievskiy         return;
5636bdc8b71SVladimir Sementsov-Ogievskiy     }
5646bdc8b71SVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &first, &el_count);
5656bdc8b71SVladimir Sementsov-Ogievskiy 
5666bdc8b71SVladimir Sementsov-Ogievskiy     memset(first, 0xff, el_count * sizeof(unsigned long));
5676bdc8b71SVladimir Sementsov-Ogievskiy     if (finish) {
5686bdc8b71SVladimir Sementsov-Ogievskiy         hbitmap_deserialize_finish(hb);
5696bdc8b71SVladimir Sementsov-Ogievskiy     }
5706bdc8b71SVladimir Sementsov-Ogievskiy }
5716bdc8b71SVladimir Sementsov-Ogievskiy 
5728258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_finish(HBitmap *bitmap)
5738258888eSVladimir Sementsov-Ogievskiy {
5748258888eSVladimir Sementsov-Ogievskiy     int64_t i, size, prev_size;
5758258888eSVladimir Sementsov-Ogievskiy     int lev;
5768258888eSVladimir Sementsov-Ogievskiy 
5778258888eSVladimir Sementsov-Ogievskiy     /* restore levels starting from penultimate to zero level, assuming
5788258888eSVladimir Sementsov-Ogievskiy      * that the last level is ok */
5798258888eSVladimir Sementsov-Ogievskiy     size = MAX((bitmap->size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
5808258888eSVladimir Sementsov-Ogievskiy     for (lev = HBITMAP_LEVELS - 1; lev-- > 0; ) {
5818258888eSVladimir Sementsov-Ogievskiy         prev_size = size;
5828258888eSVladimir Sementsov-Ogievskiy         size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
5838258888eSVladimir Sementsov-Ogievskiy         memset(bitmap->levels[lev], 0, size * sizeof(unsigned long));
5848258888eSVladimir Sementsov-Ogievskiy 
5858258888eSVladimir Sementsov-Ogievskiy         for (i = 0; i < prev_size; ++i) {
5868258888eSVladimir Sementsov-Ogievskiy             if (bitmap->levels[lev + 1][i]) {
5878258888eSVladimir Sementsov-Ogievskiy                 bitmap->levels[lev][i >> BITS_PER_LEVEL] |=
5888258888eSVladimir Sementsov-Ogievskiy                     1UL << (i & (BITS_PER_LONG - 1));
5898258888eSVladimir Sementsov-Ogievskiy             }
5908258888eSVladimir Sementsov-Ogievskiy         }
5918258888eSVladimir Sementsov-Ogievskiy     }
5928258888eSVladimir Sementsov-Ogievskiy 
5938258888eSVladimir Sementsov-Ogievskiy     bitmap->levels[0][0] |= 1UL << (BITS_PER_LONG - 1);
5948258888eSVladimir Sementsov-Ogievskiy }
5958258888eSVladimir Sementsov-Ogievskiy 
596e7c033c3SPaolo Bonzini void hbitmap_free(HBitmap *hb)
597e7c033c3SPaolo Bonzini {
598e7c033c3SPaolo Bonzini     unsigned i;
59907ac4cdbSFam Zheng     assert(!hb->meta);
600e7c033c3SPaolo Bonzini     for (i = HBITMAP_LEVELS; i-- > 0; ) {
601e7c033c3SPaolo Bonzini         g_free(hb->levels[i]);
602e7c033c3SPaolo Bonzini     }
603e7c033c3SPaolo Bonzini     g_free(hb);
604e7c033c3SPaolo Bonzini }
605e7c033c3SPaolo Bonzini 
606e7c033c3SPaolo Bonzini HBitmap *hbitmap_alloc(uint64_t size, int granularity)
607e7c033c3SPaolo Bonzini {
608e1cf5582SMarkus Armbruster     HBitmap *hb = g_new0(struct HBitmap, 1);
609e7c033c3SPaolo Bonzini     unsigned i;
610e7c033c3SPaolo Bonzini 
611e7c033c3SPaolo Bonzini     assert(granularity >= 0 && granularity < 64);
612e7c033c3SPaolo Bonzini     size = (size + (1ULL << granularity) - 1) >> granularity;
613e7c033c3SPaolo Bonzini     assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
614e7c033c3SPaolo Bonzini 
615e7c033c3SPaolo Bonzini     hb->size = size;
616e7c033c3SPaolo Bonzini     hb->granularity = granularity;
617e7c033c3SPaolo Bonzini     for (i = HBITMAP_LEVELS; i-- > 0; ) {
618e7c033c3SPaolo Bonzini         size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
6198515efbeSJohn Snow         hb->sizes[i] = size;
620e1cf5582SMarkus Armbruster         hb->levels[i] = g_new0(unsigned long, size);
621e7c033c3SPaolo Bonzini     }
622e7c033c3SPaolo Bonzini 
623e7c033c3SPaolo Bonzini     /* We necessarily have free bits in level 0 due to the definition
624e7c033c3SPaolo Bonzini      * of HBITMAP_LEVELS, so use one for a sentinel.  This speeds up
625e7c033c3SPaolo Bonzini      * hbitmap_iter_skip_words.
626e7c033c3SPaolo Bonzini      */
627e7c033c3SPaolo Bonzini     assert(size == 1);
628e7c033c3SPaolo Bonzini     hb->levels[0][0] |= 1UL << (BITS_PER_LONG - 1);
629e7c033c3SPaolo Bonzini     return hb;
630e7c033c3SPaolo Bonzini }
631be58721dSJohn Snow 
632ce1ffea8SJohn Snow void hbitmap_truncate(HBitmap *hb, uint64_t size)
633ce1ffea8SJohn Snow {
634ce1ffea8SJohn Snow     bool shrink;
635ce1ffea8SJohn Snow     unsigned i;
636ce1ffea8SJohn Snow     uint64_t num_elements = size;
637ce1ffea8SJohn Snow     uint64_t old;
638ce1ffea8SJohn Snow 
639ce1ffea8SJohn Snow     /* Size comes in as logical elements, adjust for granularity. */
640ce1ffea8SJohn Snow     size = (size + (1ULL << hb->granularity) - 1) >> hb->granularity;
641ce1ffea8SJohn Snow     assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
642ce1ffea8SJohn Snow     shrink = size < hb->size;
643ce1ffea8SJohn Snow 
644ce1ffea8SJohn Snow     /* bit sizes are identical; nothing to do. */
645ce1ffea8SJohn Snow     if (size == hb->size) {
646ce1ffea8SJohn Snow         return;
647ce1ffea8SJohn Snow     }
648ce1ffea8SJohn Snow 
649ce1ffea8SJohn Snow     /* If we're losing bits, let's clear those bits before we invalidate all of
650ce1ffea8SJohn Snow      * our invariants. This helps keep the bitcount consistent, and will prevent
651ce1ffea8SJohn Snow      * us from carrying around garbage bits beyond the end of the map.
652ce1ffea8SJohn Snow      */
653ce1ffea8SJohn Snow     if (shrink) {
654ce1ffea8SJohn Snow         /* Don't clear partial granularity groups;
655ce1ffea8SJohn Snow          * start at the first full one. */
6566725f887SMax Reitz         uint64_t start = ROUND_UP(num_elements, UINT64_C(1) << hb->granularity);
657ce1ffea8SJohn Snow         uint64_t fix_count = (hb->size << hb->granularity) - start;
658ce1ffea8SJohn Snow 
659ce1ffea8SJohn Snow         assert(fix_count);
660ce1ffea8SJohn Snow         hbitmap_reset(hb, start, fix_count);
661ce1ffea8SJohn Snow     }
662ce1ffea8SJohn Snow 
663ce1ffea8SJohn Snow     hb->size = size;
664ce1ffea8SJohn Snow     for (i = HBITMAP_LEVELS; i-- > 0; ) {
665ce1ffea8SJohn Snow         size = MAX(BITS_TO_LONGS(size), 1);
666ce1ffea8SJohn Snow         if (hb->sizes[i] == size) {
667ce1ffea8SJohn Snow             break;
668ce1ffea8SJohn Snow         }
669ce1ffea8SJohn Snow         old = hb->sizes[i];
670ce1ffea8SJohn Snow         hb->sizes[i] = size;
671ce1ffea8SJohn Snow         hb->levels[i] = g_realloc(hb->levels[i], size * sizeof(unsigned long));
672ce1ffea8SJohn Snow         if (!shrink) {
673ce1ffea8SJohn Snow             memset(&hb->levels[i][old], 0x00,
674ce1ffea8SJohn Snow                    (size - old) * sizeof(*hb->levels[i]));
675ce1ffea8SJohn Snow         }
676ce1ffea8SJohn Snow     }
67707ac4cdbSFam Zheng     if (hb->meta) {
67807ac4cdbSFam Zheng         hbitmap_truncate(hb->meta, hb->size << hb->granularity);
67907ac4cdbSFam Zheng     }
680ce1ffea8SJohn Snow }
681ce1ffea8SJohn Snow 
682ce1ffea8SJohn Snow 
683be58721dSJohn Snow /**
684be58721dSJohn Snow  * Given HBitmaps A and B, let A := A (BITOR) B.
685be58721dSJohn Snow  * Bitmap B will not be modified.
686be58721dSJohn Snow  *
687be58721dSJohn Snow  * @return true if the merge was successful,
688be58721dSJohn Snow  *         false if it was not attempted.
689be58721dSJohn Snow  */
690be58721dSJohn Snow bool hbitmap_merge(HBitmap *a, const HBitmap *b)
691be58721dSJohn Snow {
692be58721dSJohn Snow     int i;
693be58721dSJohn Snow     uint64_t j;
694be58721dSJohn Snow 
695be58721dSJohn Snow     if ((a->size != b->size) || (a->granularity != b->granularity)) {
696be58721dSJohn Snow         return false;
697be58721dSJohn Snow     }
698be58721dSJohn Snow 
699be58721dSJohn Snow     if (hbitmap_count(b) == 0) {
700be58721dSJohn Snow         return true;
701be58721dSJohn Snow     }
702be58721dSJohn Snow 
703be58721dSJohn Snow     /* This merge is O(size), as BITS_PER_LONG and HBITMAP_LEVELS are constant.
704be58721dSJohn Snow      * It may be possible to improve running times for sparsely populated maps
705be58721dSJohn Snow      * by using hbitmap_iter_next, but this is suboptimal for dense maps.
706be58721dSJohn Snow      */
707be58721dSJohn Snow     for (i = HBITMAP_LEVELS - 1; i >= 0; i--) {
708be58721dSJohn Snow         for (j = 0; j < a->sizes[i]; j++) {
709be58721dSJohn Snow             a->levels[i][j] |= b->levels[i][j];
710be58721dSJohn Snow         }
711be58721dSJohn Snow     }
712be58721dSJohn Snow 
713be58721dSJohn Snow     return true;
714be58721dSJohn Snow }
71507ac4cdbSFam Zheng 
71607ac4cdbSFam Zheng HBitmap *hbitmap_create_meta(HBitmap *hb, int chunk_size)
71707ac4cdbSFam Zheng {
71807ac4cdbSFam Zheng     assert(!(chunk_size & (chunk_size - 1)));
71907ac4cdbSFam Zheng     assert(!hb->meta);
72007ac4cdbSFam Zheng     hb->meta = hbitmap_alloc(hb->size << hb->granularity,
72107ac4cdbSFam Zheng                              hb->granularity + ctz32(chunk_size));
72207ac4cdbSFam Zheng     return hb->meta;
72307ac4cdbSFam Zheng }
72407ac4cdbSFam Zheng 
72507ac4cdbSFam Zheng void hbitmap_free_meta(HBitmap *hb)
72607ac4cdbSFam Zheng {
72707ac4cdbSFam Zheng     assert(hb->meta);
72807ac4cdbSFam Zheng     hbitmap_free(hb->meta);
72907ac4cdbSFam Zheng     hb->meta = NULL;
73007ac4cdbSFam Zheng }
731*a3b52535SVladimir Sementsov-Ogievskiy 
732*a3b52535SVladimir Sementsov-Ogievskiy char *hbitmap_sha256(const HBitmap *bitmap, Error **errp)
733*a3b52535SVladimir Sementsov-Ogievskiy {
734*a3b52535SVladimir Sementsov-Ogievskiy     size_t size = bitmap->sizes[HBITMAP_LEVELS - 1] * sizeof(unsigned long);
735*a3b52535SVladimir Sementsov-Ogievskiy     char *data = (char *)bitmap->levels[HBITMAP_LEVELS - 1];
736*a3b52535SVladimir Sementsov-Ogievskiy     char *hash = NULL;
737*a3b52535SVladimir Sementsov-Ogievskiy     qcrypto_hash_digest(QCRYPTO_HASH_ALG_SHA256, data, size, &hash, errp);
738*a3b52535SVladimir Sementsov-Ogievskiy 
739*a3b52535SVladimir Sementsov-Ogievskiy     return hash;
740*a3b52535SVladimir Sementsov-Ogievskiy }
741