xref: /openbmc/qemu/util/hbitmap.c (revision f63ea4e92bad1dbad58b198f51ac48657c1facbd)
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"
16e7c033c3SPaolo Bonzini 
17e7c033c3SPaolo Bonzini /* HBitmaps provides an array of bits.  The bits are stored as usual in an
18e7c033c3SPaolo Bonzini  * array of unsigned longs, but HBitmap is also optimized to provide fast
19e7c033c3SPaolo Bonzini  * iteration over set bits; going from one bit to the next is O(logB n)
20e7c033c3SPaolo Bonzini  * worst case, with B = sizeof(long) * CHAR_BIT: the result is low enough
21e7c033c3SPaolo Bonzini  * that the number of levels is in fact fixed.
22e7c033c3SPaolo Bonzini  *
23e7c033c3SPaolo Bonzini  * In order to do this, it stacks multiple bitmaps with progressively coarser
24e7c033c3SPaolo Bonzini  * granularity; in all levels except the last, bit N is set iff the N-th
25e7c033c3SPaolo Bonzini  * unsigned long is nonzero in the immediately next level.  When iteration
26e7c033c3SPaolo Bonzini  * completes on the last level it can examine the 2nd-last level to quickly
27e7c033c3SPaolo Bonzini  * skip entire words, and even do so recursively to skip blocks of 64 words or
28e7c033c3SPaolo Bonzini  * powers thereof (32 on 32-bit machines).
29e7c033c3SPaolo Bonzini  *
30e7c033c3SPaolo Bonzini  * Given an index in the bitmap, it can be split in group of bits like
31e7c033c3SPaolo Bonzini  * this (for the 64-bit case):
32e7c033c3SPaolo Bonzini  *
33e7c033c3SPaolo Bonzini  *   bits 0-57 => word in the last bitmap     | bits 58-63 => bit in the word
34e7c033c3SPaolo Bonzini  *   bits 0-51 => word in the 2nd-last bitmap | bits 52-57 => bit in the word
35e7c033c3SPaolo Bonzini  *   bits 0-45 => word in the 3rd-last bitmap | bits 46-51 => bit in the word
36e7c033c3SPaolo Bonzini  *
37e7c033c3SPaolo Bonzini  * So it is easy to move up simply by shifting the index right by
38e7c033c3SPaolo Bonzini  * log2(BITS_PER_LONG) bits.  To move down, you shift the index left
39e7c033c3SPaolo Bonzini  * similarly, and add the word index within the group.  Iteration uses
40e7c033c3SPaolo Bonzini  * ffs (find first set bit) to find the next word to examine; this
41e7c033c3SPaolo Bonzini  * operation can be done in constant time in most current architectures.
42e7c033c3SPaolo Bonzini  *
43e7c033c3SPaolo Bonzini  * Setting or clearing a range of m bits on all levels, the work to perform
44e7c033c3SPaolo Bonzini  * is O(m + m/W + m/W^2 + ...), which is O(m) like on a regular bitmap.
45e7c033c3SPaolo Bonzini  *
46e7c033c3SPaolo Bonzini  * When iterating on a bitmap, each bit (on any level) is only visited
47e7c033c3SPaolo Bonzini  * once.  Hence, The total cost of visiting a bitmap with m bits in it is
48e7c033c3SPaolo Bonzini  * the number of bits that are set in all bitmaps.  Unless the bitmap is
49e7c033c3SPaolo Bonzini  * extremely sparse, this is also O(m + m/W + m/W^2 + ...), so the amortized
50e7c033c3SPaolo Bonzini  * cost of advancing from one bit to the next is usually constant (worst case
51e7c033c3SPaolo Bonzini  * O(logB n) as in the non-amortized complexity).
52e7c033c3SPaolo Bonzini  */
53e7c033c3SPaolo Bonzini 
54e7c033c3SPaolo Bonzini struct HBitmap {
55e7c033c3SPaolo Bonzini     /* Number of total bits in the bottom level.  */
56e7c033c3SPaolo Bonzini     uint64_t size;
57e7c033c3SPaolo Bonzini 
58e7c033c3SPaolo Bonzini     /* Number of set bits in the bottom level.  */
59e7c033c3SPaolo Bonzini     uint64_t count;
60e7c033c3SPaolo Bonzini 
61e7c033c3SPaolo Bonzini     /* A scaling factor.  Given a granularity of G, each bit in the bitmap will
62e7c033c3SPaolo Bonzini      * will actually represent a group of 2^G elements.  Each operation on a
63e7c033c3SPaolo Bonzini      * range of bits first rounds the bits to determine which group they land
64e7c033c3SPaolo Bonzini      * in, and then affect the entire page; iteration will only visit the first
65e7c033c3SPaolo Bonzini      * bit of each group.  Here is an example of operations in a size-16,
66e7c033c3SPaolo Bonzini      * granularity-1 HBitmap:
67e7c033c3SPaolo Bonzini      *
68e7c033c3SPaolo Bonzini      *    initial state            00000000
69e7c033c3SPaolo Bonzini      *    set(start=0, count=9)    11111000 (iter: 0, 2, 4, 6, 8)
70e7c033c3SPaolo Bonzini      *    reset(start=1, count=3)  00111000 (iter: 4, 6, 8)
71e7c033c3SPaolo Bonzini      *    set(start=9, count=2)    00111100 (iter: 4, 6, 8, 10)
72e7c033c3SPaolo Bonzini      *    reset(start=5, count=5)  00000000
73e7c033c3SPaolo Bonzini      *
74e7c033c3SPaolo Bonzini      * From an implementation point of view, when setting or resetting bits,
75e7c033c3SPaolo Bonzini      * the bitmap will scale bit numbers right by this amount of bits.  When
76e7c033c3SPaolo Bonzini      * iterating, the bitmap will scale bit numbers left by this amount of
77e7c033c3SPaolo Bonzini      * bits.
78e7c033c3SPaolo Bonzini      */
79e7c033c3SPaolo Bonzini     int granularity;
80e7c033c3SPaolo Bonzini 
8107ac4cdbSFam Zheng     /* A meta dirty bitmap to track the dirtiness of bits in this HBitmap. */
8207ac4cdbSFam Zheng     HBitmap *meta;
8307ac4cdbSFam Zheng 
84e7c033c3SPaolo Bonzini     /* A number of progressively less coarse bitmaps (i.e. level 0 is the
85e7c033c3SPaolo Bonzini      * coarsest).  Each bit in level N represents a word in level N+1 that
86e7c033c3SPaolo Bonzini      * has a set bit, except the last level where each bit represents the
87e7c033c3SPaolo Bonzini      * actual bitmap.
88e7c033c3SPaolo Bonzini      *
89e7c033c3SPaolo Bonzini      * Note that all bitmaps have the same number of levels.  Even a 1-bit
90e7c033c3SPaolo Bonzini      * bitmap will still allocate HBITMAP_LEVELS arrays.
91e7c033c3SPaolo Bonzini      */
92e7c033c3SPaolo Bonzini     unsigned long *levels[HBITMAP_LEVELS];
938515efbeSJohn Snow 
948515efbeSJohn Snow     /* The length of each levels[] array. */
958515efbeSJohn Snow     uint64_t sizes[HBITMAP_LEVELS];
96e7c033c3SPaolo Bonzini };
97e7c033c3SPaolo Bonzini 
98e7c033c3SPaolo Bonzini /* Advance hbi to the next nonzero word and return it.  hbi->pos
99e7c033c3SPaolo Bonzini  * is updated.  Returns zero if we reach the end of the bitmap.
100e7c033c3SPaolo Bonzini  */
101e7c033c3SPaolo Bonzini unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi)
102e7c033c3SPaolo Bonzini {
103e7c033c3SPaolo Bonzini     size_t pos = hbi->pos;
104e7c033c3SPaolo Bonzini     const HBitmap *hb = hbi->hb;
105e7c033c3SPaolo Bonzini     unsigned i = HBITMAP_LEVELS - 1;
106e7c033c3SPaolo Bonzini 
107e7c033c3SPaolo Bonzini     unsigned long cur;
108e7c033c3SPaolo Bonzini     do {
109*f63ea4e9SVladimir Sementsov-Ogievskiy         i--;
110e7c033c3SPaolo Bonzini         pos >>= BITS_PER_LEVEL;
111*f63ea4e9SVladimir Sementsov-Ogievskiy         cur = hbi->cur[i] & hb->levels[i][pos];
112e7c033c3SPaolo Bonzini     } while (cur == 0);
113e7c033c3SPaolo Bonzini 
114e7c033c3SPaolo Bonzini     /* Check for end of iteration.  We always use fewer than BITS_PER_LONG
115e7c033c3SPaolo Bonzini      * bits in the level 0 bitmap; thus we can repurpose the most significant
116e7c033c3SPaolo Bonzini      * bit as a sentinel.  The sentinel is set in hbitmap_alloc and ensures
117e7c033c3SPaolo Bonzini      * that the above loop ends even without an explicit check on i.
118e7c033c3SPaolo Bonzini      */
119e7c033c3SPaolo Bonzini 
120e7c033c3SPaolo Bonzini     if (i == 0 && cur == (1UL << (BITS_PER_LONG - 1))) {
121e7c033c3SPaolo Bonzini         return 0;
122e7c033c3SPaolo Bonzini     }
123e7c033c3SPaolo Bonzini     for (; i < HBITMAP_LEVELS - 1; i++) {
124e7c033c3SPaolo Bonzini         /* Shift back pos to the left, matching the right shifts above.
125e7c033c3SPaolo Bonzini          * The index of this word's least significant set bit provides
126e7c033c3SPaolo Bonzini          * the low-order bits.
127e7c033c3SPaolo Bonzini          */
12818331e7cSRichard Henderson         assert(cur);
12918331e7cSRichard Henderson         pos = (pos << BITS_PER_LEVEL) + ctzl(cur);
130e7c033c3SPaolo Bonzini         hbi->cur[i] = cur & (cur - 1);
131e7c033c3SPaolo Bonzini 
132e7c033c3SPaolo Bonzini         /* Set up next level for iteration.  */
133e7c033c3SPaolo Bonzini         cur = hb->levels[i + 1][pos];
134e7c033c3SPaolo Bonzini     }
135e7c033c3SPaolo Bonzini 
136e7c033c3SPaolo Bonzini     hbi->pos = pos;
137e7c033c3SPaolo Bonzini     trace_hbitmap_iter_skip_words(hbi->hb, hbi, pos, cur);
138e7c033c3SPaolo Bonzini 
139e7c033c3SPaolo Bonzini     assert(cur);
140e7c033c3SPaolo Bonzini     return cur;
141e7c033c3SPaolo Bonzini }
142e7c033c3SPaolo Bonzini 
143*f63ea4e9SVladimir Sementsov-Ogievskiy int64_t hbitmap_iter_next(HBitmapIter *hbi)
144*f63ea4e9SVladimir Sementsov-Ogievskiy {
145*f63ea4e9SVladimir Sementsov-Ogievskiy     unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1] &
146*f63ea4e9SVladimir Sementsov-Ogievskiy             hbi->hb->levels[HBITMAP_LEVELS - 1][hbi->pos];
147*f63ea4e9SVladimir Sementsov-Ogievskiy     int64_t item;
148*f63ea4e9SVladimir Sementsov-Ogievskiy 
149*f63ea4e9SVladimir Sementsov-Ogievskiy     if (cur == 0) {
150*f63ea4e9SVladimir Sementsov-Ogievskiy         cur = hbitmap_iter_skip_words(hbi);
151*f63ea4e9SVladimir Sementsov-Ogievskiy         if (cur == 0) {
152*f63ea4e9SVladimir Sementsov-Ogievskiy             return -1;
153*f63ea4e9SVladimir Sementsov-Ogievskiy         }
154*f63ea4e9SVladimir Sementsov-Ogievskiy     }
155*f63ea4e9SVladimir Sementsov-Ogievskiy 
156*f63ea4e9SVladimir Sementsov-Ogievskiy     /* The next call will resume work from the next bit.  */
157*f63ea4e9SVladimir Sementsov-Ogievskiy     hbi->cur[HBITMAP_LEVELS - 1] = cur & (cur - 1);
158*f63ea4e9SVladimir Sementsov-Ogievskiy     item = ((uint64_t)hbi->pos << BITS_PER_LEVEL) + ctzl(cur);
159*f63ea4e9SVladimir Sementsov-Ogievskiy 
160*f63ea4e9SVladimir Sementsov-Ogievskiy     return item << hbi->granularity;
161*f63ea4e9SVladimir Sementsov-Ogievskiy }
162*f63ea4e9SVladimir Sementsov-Ogievskiy 
163e7c033c3SPaolo Bonzini void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first)
164e7c033c3SPaolo Bonzini {
165e7c033c3SPaolo Bonzini     unsigned i, bit;
166e7c033c3SPaolo Bonzini     uint64_t pos;
167e7c033c3SPaolo Bonzini 
168e7c033c3SPaolo Bonzini     hbi->hb = hb;
169e7c033c3SPaolo Bonzini     pos = first >> hb->granularity;
1701b095244SPaolo Bonzini     assert(pos < hb->size);
171e7c033c3SPaolo Bonzini     hbi->pos = pos >> BITS_PER_LEVEL;
172e7c033c3SPaolo Bonzini     hbi->granularity = hb->granularity;
173e7c033c3SPaolo Bonzini 
174e7c033c3SPaolo Bonzini     for (i = HBITMAP_LEVELS; i-- > 0; ) {
175e7c033c3SPaolo Bonzini         bit = pos & (BITS_PER_LONG - 1);
176e7c033c3SPaolo Bonzini         pos >>= BITS_PER_LEVEL;
177e7c033c3SPaolo Bonzini 
178e7c033c3SPaolo Bonzini         /* Drop bits representing items before first.  */
179e7c033c3SPaolo Bonzini         hbi->cur[i] = hb->levels[i][pos] & ~((1UL << bit) - 1);
180e7c033c3SPaolo Bonzini 
181e7c033c3SPaolo Bonzini         /* We have already added level i+1, so the lowest set bit has
182e7c033c3SPaolo Bonzini          * been processed.  Clear it.
183e7c033c3SPaolo Bonzini          */
184e7c033c3SPaolo Bonzini         if (i != HBITMAP_LEVELS - 1) {
185e7c033c3SPaolo Bonzini             hbi->cur[i] &= ~(1UL << bit);
186e7c033c3SPaolo Bonzini         }
187e7c033c3SPaolo Bonzini     }
188e7c033c3SPaolo Bonzini }
189e7c033c3SPaolo Bonzini 
190e7c033c3SPaolo Bonzini bool hbitmap_empty(const HBitmap *hb)
191e7c033c3SPaolo Bonzini {
192e7c033c3SPaolo Bonzini     return hb->count == 0;
193e7c033c3SPaolo Bonzini }
194e7c033c3SPaolo Bonzini 
195e7c033c3SPaolo Bonzini int hbitmap_granularity(const HBitmap *hb)
196e7c033c3SPaolo Bonzini {
197e7c033c3SPaolo Bonzini     return hb->granularity;
198e7c033c3SPaolo Bonzini }
199e7c033c3SPaolo Bonzini 
200e7c033c3SPaolo Bonzini uint64_t hbitmap_count(const HBitmap *hb)
201e7c033c3SPaolo Bonzini {
202e7c033c3SPaolo Bonzini     return hb->count << hb->granularity;
203e7c033c3SPaolo Bonzini }
204e7c033c3SPaolo Bonzini 
205e7c033c3SPaolo Bonzini /* Count the number of set bits between start and end, not accounting for
206e7c033c3SPaolo Bonzini  * the granularity.  Also an example of how to use hbitmap_iter_next_word.
207e7c033c3SPaolo Bonzini  */
208e7c033c3SPaolo Bonzini static uint64_t hb_count_between(HBitmap *hb, uint64_t start, uint64_t last)
209e7c033c3SPaolo Bonzini {
210e7c033c3SPaolo Bonzini     HBitmapIter hbi;
211e7c033c3SPaolo Bonzini     uint64_t count = 0;
212e7c033c3SPaolo Bonzini     uint64_t end = last + 1;
213e7c033c3SPaolo Bonzini     unsigned long cur;
214e7c033c3SPaolo Bonzini     size_t pos;
215e7c033c3SPaolo Bonzini 
216e7c033c3SPaolo Bonzini     hbitmap_iter_init(&hbi, hb, start << hb->granularity);
217e7c033c3SPaolo Bonzini     for (;;) {
218e7c033c3SPaolo Bonzini         pos = hbitmap_iter_next_word(&hbi, &cur);
219e7c033c3SPaolo Bonzini         if (pos >= (end >> BITS_PER_LEVEL)) {
220e7c033c3SPaolo Bonzini             break;
221e7c033c3SPaolo Bonzini         }
222591b320aSPeter Maydell         count += ctpopl(cur);
223e7c033c3SPaolo Bonzini     }
224e7c033c3SPaolo Bonzini 
225e7c033c3SPaolo Bonzini     if (pos == (end >> BITS_PER_LEVEL)) {
226e7c033c3SPaolo Bonzini         /* Drop bits representing the END-th and subsequent items.  */
227e7c033c3SPaolo Bonzini         int bit = end & (BITS_PER_LONG - 1);
228e7c033c3SPaolo Bonzini         cur &= (1UL << bit) - 1;
229591b320aSPeter Maydell         count += ctpopl(cur);
230e7c033c3SPaolo Bonzini     }
231e7c033c3SPaolo Bonzini 
232e7c033c3SPaolo Bonzini     return count;
233e7c033c3SPaolo Bonzini }
234e7c033c3SPaolo Bonzini 
235e7c033c3SPaolo Bonzini /* Setting starts at the last layer and propagates up if an element
23607ac4cdbSFam Zheng  * changes.
237e7c033c3SPaolo Bonzini  */
238e7c033c3SPaolo Bonzini static inline bool hb_set_elem(unsigned long *elem, uint64_t start, uint64_t last)
239e7c033c3SPaolo Bonzini {
240e7c033c3SPaolo Bonzini     unsigned long mask;
24107ac4cdbSFam Zheng     unsigned long old;
242e7c033c3SPaolo Bonzini 
243e7c033c3SPaolo Bonzini     assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL));
244e7c033c3SPaolo Bonzini     assert(start <= last);
245e7c033c3SPaolo Bonzini 
246e7c033c3SPaolo Bonzini     mask = 2UL << (last & (BITS_PER_LONG - 1));
247e7c033c3SPaolo Bonzini     mask -= 1UL << (start & (BITS_PER_LONG - 1));
24807ac4cdbSFam Zheng     old = *elem;
249e7c033c3SPaolo Bonzini     *elem |= mask;
25007ac4cdbSFam Zheng     return old != *elem;
251e7c033c3SPaolo Bonzini }
252e7c033c3SPaolo Bonzini 
25307ac4cdbSFam Zheng /* The recursive workhorse (the depth is limited to HBITMAP_LEVELS)...
25407ac4cdbSFam Zheng  * Returns true if at least one bit is changed. */
25507ac4cdbSFam Zheng static bool hb_set_between(HBitmap *hb, int level, uint64_t start,
25607ac4cdbSFam Zheng                            uint64_t last)
257e7c033c3SPaolo Bonzini {
258e7c033c3SPaolo Bonzini     size_t pos = start >> BITS_PER_LEVEL;
259e7c033c3SPaolo Bonzini     size_t lastpos = last >> BITS_PER_LEVEL;
260e7c033c3SPaolo Bonzini     bool changed = false;
261e7c033c3SPaolo Bonzini     size_t i;
262e7c033c3SPaolo Bonzini 
263e7c033c3SPaolo Bonzini     i = pos;
264e7c033c3SPaolo Bonzini     if (i < lastpos) {
265e7c033c3SPaolo Bonzini         uint64_t next = (start | (BITS_PER_LONG - 1)) + 1;
266e7c033c3SPaolo Bonzini         changed |= hb_set_elem(&hb->levels[level][i], start, next - 1);
267e7c033c3SPaolo Bonzini         for (;;) {
268e7c033c3SPaolo Bonzini             start = next;
269e7c033c3SPaolo Bonzini             next += BITS_PER_LONG;
270e7c033c3SPaolo Bonzini             if (++i == lastpos) {
271e7c033c3SPaolo Bonzini                 break;
272e7c033c3SPaolo Bonzini             }
273e7c033c3SPaolo Bonzini             changed |= (hb->levels[level][i] == 0);
274e7c033c3SPaolo Bonzini             hb->levels[level][i] = ~0UL;
275e7c033c3SPaolo Bonzini         }
276e7c033c3SPaolo Bonzini     }
277e7c033c3SPaolo Bonzini     changed |= hb_set_elem(&hb->levels[level][i], start, last);
278e7c033c3SPaolo Bonzini 
279e7c033c3SPaolo Bonzini     /* If there was any change in this layer, we may have to update
280e7c033c3SPaolo Bonzini      * the one above.
281e7c033c3SPaolo Bonzini      */
282e7c033c3SPaolo Bonzini     if (level > 0 && changed) {
283e7c033c3SPaolo Bonzini         hb_set_between(hb, level - 1, pos, lastpos);
284e7c033c3SPaolo Bonzini     }
28507ac4cdbSFam Zheng     return changed;
286e7c033c3SPaolo Bonzini }
287e7c033c3SPaolo Bonzini 
288e7c033c3SPaolo Bonzini void hbitmap_set(HBitmap *hb, uint64_t start, uint64_t count)
289e7c033c3SPaolo Bonzini {
290e7c033c3SPaolo Bonzini     /* Compute range in the last layer.  */
29107ac4cdbSFam Zheng     uint64_t first, n;
292e7c033c3SPaolo Bonzini     uint64_t last = start + count - 1;
293e7c033c3SPaolo Bonzini 
294e7c033c3SPaolo Bonzini     trace_hbitmap_set(hb, start, count,
295e7c033c3SPaolo Bonzini                       start >> hb->granularity, last >> hb->granularity);
296e7c033c3SPaolo Bonzini 
29707ac4cdbSFam Zheng     first = start >> hb->granularity;
298e7c033c3SPaolo Bonzini     last >>= hb->granularity;
2990e321191SVladimir Sementsov-Ogievskiy     assert(last < hb->size);
30007ac4cdbSFam Zheng     n = last - first + 1;
301e7c033c3SPaolo Bonzini 
30207ac4cdbSFam Zheng     hb->count += n - hb_count_between(hb, first, last);
30307ac4cdbSFam Zheng     if (hb_set_between(hb, HBITMAP_LEVELS - 1, first, last) &&
30407ac4cdbSFam Zheng         hb->meta) {
30507ac4cdbSFam Zheng         hbitmap_set(hb->meta, start, count);
30607ac4cdbSFam Zheng     }
307e7c033c3SPaolo Bonzini }
308e7c033c3SPaolo Bonzini 
309e7c033c3SPaolo Bonzini /* Resetting works the other way round: propagate up if the new
310e7c033c3SPaolo Bonzini  * value is zero.
311e7c033c3SPaolo Bonzini  */
312e7c033c3SPaolo Bonzini static inline bool hb_reset_elem(unsigned long *elem, uint64_t start, uint64_t last)
313e7c033c3SPaolo Bonzini {
314e7c033c3SPaolo Bonzini     unsigned long mask;
315e7c033c3SPaolo Bonzini     bool blanked;
316e7c033c3SPaolo Bonzini 
317e7c033c3SPaolo Bonzini     assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL));
318e7c033c3SPaolo Bonzini     assert(start <= last);
319e7c033c3SPaolo Bonzini 
320e7c033c3SPaolo Bonzini     mask = 2UL << (last & (BITS_PER_LONG - 1));
321e7c033c3SPaolo Bonzini     mask -= 1UL << (start & (BITS_PER_LONG - 1));
322e7c033c3SPaolo Bonzini     blanked = *elem != 0 && ((*elem & ~mask) == 0);
323e7c033c3SPaolo Bonzini     *elem &= ~mask;
324e7c033c3SPaolo Bonzini     return blanked;
325e7c033c3SPaolo Bonzini }
326e7c033c3SPaolo Bonzini 
32707ac4cdbSFam Zheng /* The recursive workhorse (the depth is limited to HBITMAP_LEVELS)...
32807ac4cdbSFam Zheng  * Returns true if at least one bit is changed. */
32907ac4cdbSFam Zheng static bool hb_reset_between(HBitmap *hb, int level, uint64_t start,
33007ac4cdbSFam Zheng                              uint64_t last)
331e7c033c3SPaolo Bonzini {
332e7c033c3SPaolo Bonzini     size_t pos = start >> BITS_PER_LEVEL;
333e7c033c3SPaolo Bonzini     size_t lastpos = last >> BITS_PER_LEVEL;
334e7c033c3SPaolo Bonzini     bool changed = false;
335e7c033c3SPaolo Bonzini     size_t i;
336e7c033c3SPaolo Bonzini 
337e7c033c3SPaolo Bonzini     i = pos;
338e7c033c3SPaolo Bonzini     if (i < lastpos) {
339e7c033c3SPaolo Bonzini         uint64_t next = (start | (BITS_PER_LONG - 1)) + 1;
340e7c033c3SPaolo Bonzini 
341e7c033c3SPaolo Bonzini         /* Here we need a more complex test than when setting bits.  Even if
342e7c033c3SPaolo Bonzini          * something was changed, we must not blank bits in the upper level
343e7c033c3SPaolo Bonzini          * unless the lower-level word became entirely zero.  So, remove pos
344e7c033c3SPaolo Bonzini          * from the upper-level range if bits remain set.
345e7c033c3SPaolo Bonzini          */
346e7c033c3SPaolo Bonzini         if (hb_reset_elem(&hb->levels[level][i], start, next - 1)) {
347e7c033c3SPaolo Bonzini             changed = true;
348e7c033c3SPaolo Bonzini         } else {
349e7c033c3SPaolo Bonzini             pos++;
350e7c033c3SPaolo Bonzini         }
351e7c033c3SPaolo Bonzini 
352e7c033c3SPaolo Bonzini         for (;;) {
353e7c033c3SPaolo Bonzini             start = next;
354e7c033c3SPaolo Bonzini             next += BITS_PER_LONG;
355e7c033c3SPaolo Bonzini             if (++i == lastpos) {
356e7c033c3SPaolo Bonzini                 break;
357e7c033c3SPaolo Bonzini             }
358e7c033c3SPaolo Bonzini             changed |= (hb->levels[level][i] != 0);
359e7c033c3SPaolo Bonzini             hb->levels[level][i] = 0UL;
360e7c033c3SPaolo Bonzini         }
361e7c033c3SPaolo Bonzini     }
362e7c033c3SPaolo Bonzini 
363e7c033c3SPaolo Bonzini     /* Same as above, this time for lastpos.  */
364e7c033c3SPaolo Bonzini     if (hb_reset_elem(&hb->levels[level][i], start, last)) {
365e7c033c3SPaolo Bonzini         changed = true;
366e7c033c3SPaolo Bonzini     } else {
367e7c033c3SPaolo Bonzini         lastpos--;
368e7c033c3SPaolo Bonzini     }
369e7c033c3SPaolo Bonzini 
370e7c033c3SPaolo Bonzini     if (level > 0 && changed) {
371e7c033c3SPaolo Bonzini         hb_reset_between(hb, level - 1, pos, lastpos);
372e7c033c3SPaolo Bonzini     }
37307ac4cdbSFam Zheng 
37407ac4cdbSFam Zheng     return changed;
37507ac4cdbSFam Zheng 
376e7c033c3SPaolo Bonzini }
377e7c033c3SPaolo Bonzini 
378e7c033c3SPaolo Bonzini void hbitmap_reset(HBitmap *hb, uint64_t start, uint64_t count)
379e7c033c3SPaolo Bonzini {
380e7c033c3SPaolo Bonzini     /* Compute range in the last layer.  */
38107ac4cdbSFam Zheng     uint64_t first;
382e7c033c3SPaolo Bonzini     uint64_t last = start + count - 1;
383e7c033c3SPaolo Bonzini 
384e7c033c3SPaolo Bonzini     trace_hbitmap_reset(hb, start, count,
385e7c033c3SPaolo Bonzini                         start >> hb->granularity, last >> hb->granularity);
386e7c033c3SPaolo Bonzini 
38707ac4cdbSFam Zheng     first = start >> hb->granularity;
388e7c033c3SPaolo Bonzini     last >>= hb->granularity;
3890e321191SVladimir Sementsov-Ogievskiy     assert(last < hb->size);
390e7c033c3SPaolo Bonzini 
39107ac4cdbSFam Zheng     hb->count -= hb_count_between(hb, first, last);
39207ac4cdbSFam Zheng     if (hb_reset_between(hb, HBITMAP_LEVELS - 1, first, last) &&
39307ac4cdbSFam Zheng         hb->meta) {
39407ac4cdbSFam Zheng         hbitmap_set(hb->meta, start, count);
39507ac4cdbSFam Zheng     }
396e7c033c3SPaolo Bonzini }
397e7c033c3SPaolo Bonzini 
398c6a8c328SWen Congyang void hbitmap_reset_all(HBitmap *hb)
399c6a8c328SWen Congyang {
400c6a8c328SWen Congyang     unsigned int i;
401c6a8c328SWen Congyang 
402c6a8c328SWen Congyang     /* Same as hbitmap_alloc() except for memset() instead of malloc() */
403c6a8c328SWen Congyang     for (i = HBITMAP_LEVELS; --i >= 1; ) {
404c6a8c328SWen Congyang         memset(hb->levels[i], 0, hb->sizes[i] * sizeof(unsigned long));
405c6a8c328SWen Congyang     }
406c6a8c328SWen Congyang 
407c6a8c328SWen Congyang     hb->levels[0][0] = 1UL << (BITS_PER_LONG - 1);
408c6a8c328SWen Congyang     hb->count = 0;
409c6a8c328SWen Congyang }
410c6a8c328SWen Congyang 
41120a579deSMax Reitz bool hbitmap_is_serializable(const HBitmap *hb)
41220a579deSMax Reitz {
41320a579deSMax Reitz     /* Every serialized chunk must be aligned to 64 bits so that endianness
41420a579deSMax Reitz      * requirements can be fulfilled on both 64 bit and 32 bit hosts.
41520a579deSMax Reitz      * We have hbitmap_serialization_granularity() which converts this
41620a579deSMax Reitz      * alignment requirement from bitmap bits to items covered (e.g. sectors).
41720a579deSMax Reitz      * That value is:
41820a579deSMax Reitz      *    64 << hb->granularity
41920a579deSMax Reitz      * Since this value must not exceed UINT64_MAX, hb->granularity must be
42020a579deSMax Reitz      * less than 58 (== 64 - 6, where 6 is ld(64), i.e. 1 << 6 == 64).
42120a579deSMax Reitz      *
42220a579deSMax Reitz      * In order for hbitmap_serialization_granularity() to always return a
42320a579deSMax Reitz      * meaningful value, bitmaps that are to be serialized must have a
42420a579deSMax Reitz      * granularity of less than 58. */
42520a579deSMax Reitz 
42620a579deSMax Reitz     return hb->granularity < 58;
42720a579deSMax Reitz }
42820a579deSMax Reitz 
429e7c033c3SPaolo Bonzini bool hbitmap_get(const HBitmap *hb, uint64_t item)
430e7c033c3SPaolo Bonzini {
431e7c033c3SPaolo Bonzini     /* Compute position and bit in the last layer.  */
432e7c033c3SPaolo Bonzini     uint64_t pos = item >> hb->granularity;
433e7c033c3SPaolo Bonzini     unsigned long bit = 1UL << (pos & (BITS_PER_LONG - 1));
4340e321191SVladimir Sementsov-Ogievskiy     assert(pos < hb->size);
435e7c033c3SPaolo Bonzini 
436e7c033c3SPaolo Bonzini     return (hb->levels[HBITMAP_LEVELS - 1][pos >> BITS_PER_LEVEL] & bit) != 0;
437e7c033c3SPaolo Bonzini }
438e7c033c3SPaolo Bonzini 
4398258888eSVladimir Sementsov-Ogievskiy uint64_t hbitmap_serialization_granularity(const HBitmap *hb)
4408258888eSVladimir Sementsov-Ogievskiy {
44120a579deSMax Reitz     assert(hbitmap_is_serializable(hb));
4426725f887SMax Reitz 
4438258888eSVladimir Sementsov-Ogievskiy     /* Require at least 64 bit granularity to be safe on both 64 bit and 32 bit
4448258888eSVladimir Sementsov-Ogievskiy      * hosts. */
4456725f887SMax Reitz     return UINT64_C(64) << hb->granularity;
4468258888eSVladimir Sementsov-Ogievskiy }
4478258888eSVladimir Sementsov-Ogievskiy 
4488258888eSVladimir Sementsov-Ogievskiy /* Start should be aligned to serialization granularity, chunk size should be
4498258888eSVladimir Sementsov-Ogievskiy  * aligned to serialization granularity too, except for last chunk.
4508258888eSVladimir Sementsov-Ogievskiy  */
4518258888eSVladimir Sementsov-Ogievskiy static void serialization_chunk(const HBitmap *hb,
4528258888eSVladimir Sementsov-Ogievskiy                                 uint64_t start, uint64_t count,
4538258888eSVladimir Sementsov-Ogievskiy                                 unsigned long **first_el, uint64_t *el_count)
4548258888eSVladimir Sementsov-Ogievskiy {
4558258888eSVladimir Sementsov-Ogievskiy     uint64_t last = start + count - 1;
4568258888eSVladimir Sementsov-Ogievskiy     uint64_t gran = hbitmap_serialization_granularity(hb);
4578258888eSVladimir Sementsov-Ogievskiy 
4588258888eSVladimir Sementsov-Ogievskiy     assert((start & (gran - 1)) == 0);
4598258888eSVladimir Sementsov-Ogievskiy     assert((last >> hb->granularity) < hb->size);
4608258888eSVladimir Sementsov-Ogievskiy     if ((last >> hb->granularity) != hb->size - 1) {
4618258888eSVladimir Sementsov-Ogievskiy         assert((count & (gran - 1)) == 0);
4628258888eSVladimir Sementsov-Ogievskiy     }
4638258888eSVladimir Sementsov-Ogievskiy 
4648258888eSVladimir Sementsov-Ogievskiy     start = (start >> hb->granularity) >> BITS_PER_LEVEL;
4658258888eSVladimir Sementsov-Ogievskiy     last = (last >> hb->granularity) >> BITS_PER_LEVEL;
4668258888eSVladimir Sementsov-Ogievskiy 
4678258888eSVladimir Sementsov-Ogievskiy     *first_el = &hb->levels[HBITMAP_LEVELS - 1][start];
4688258888eSVladimir Sementsov-Ogievskiy     *el_count = last - start + 1;
4698258888eSVladimir Sementsov-Ogievskiy }
4708258888eSVladimir Sementsov-Ogievskiy 
4718258888eSVladimir Sementsov-Ogievskiy uint64_t hbitmap_serialization_size(const HBitmap *hb,
4728258888eSVladimir Sementsov-Ogievskiy                                     uint64_t start, uint64_t count)
4738258888eSVladimir Sementsov-Ogievskiy {
4748258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
4758258888eSVladimir Sementsov-Ogievskiy     unsigned long *cur;
4768258888eSVladimir Sementsov-Ogievskiy 
4778258888eSVladimir Sementsov-Ogievskiy     if (!count) {
4788258888eSVladimir Sementsov-Ogievskiy         return 0;
4798258888eSVladimir Sementsov-Ogievskiy     }
4808258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &cur, &el_count);
4818258888eSVladimir Sementsov-Ogievskiy 
4828258888eSVladimir Sementsov-Ogievskiy     return el_count * sizeof(unsigned long);
4838258888eSVladimir Sementsov-Ogievskiy }
4848258888eSVladimir Sementsov-Ogievskiy 
4858258888eSVladimir Sementsov-Ogievskiy void hbitmap_serialize_part(const HBitmap *hb, uint8_t *buf,
4868258888eSVladimir Sementsov-Ogievskiy                             uint64_t start, uint64_t count)
4878258888eSVladimir Sementsov-Ogievskiy {
4888258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
4898258888eSVladimir Sementsov-Ogievskiy     unsigned long *cur, *end;
4908258888eSVladimir Sementsov-Ogievskiy 
4918258888eSVladimir Sementsov-Ogievskiy     if (!count) {
4928258888eSVladimir Sementsov-Ogievskiy         return;
4938258888eSVladimir Sementsov-Ogievskiy     }
4948258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &cur, &el_count);
4958258888eSVladimir Sementsov-Ogievskiy     end = cur + el_count;
4968258888eSVladimir Sementsov-Ogievskiy 
4978258888eSVladimir Sementsov-Ogievskiy     while (cur != end) {
4988258888eSVladimir Sementsov-Ogievskiy         unsigned long el =
4998258888eSVladimir Sementsov-Ogievskiy             (BITS_PER_LONG == 32 ? cpu_to_le32(*cur) : cpu_to_le64(*cur));
5008258888eSVladimir Sementsov-Ogievskiy 
5018258888eSVladimir Sementsov-Ogievskiy         memcpy(buf, &el, sizeof(el));
5028258888eSVladimir Sementsov-Ogievskiy         buf += sizeof(el);
5038258888eSVladimir Sementsov-Ogievskiy         cur++;
5048258888eSVladimir Sementsov-Ogievskiy     }
5058258888eSVladimir Sementsov-Ogievskiy }
5068258888eSVladimir Sementsov-Ogievskiy 
5078258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_part(HBitmap *hb, uint8_t *buf,
5088258888eSVladimir Sementsov-Ogievskiy                               uint64_t start, uint64_t count,
5098258888eSVladimir Sementsov-Ogievskiy                               bool finish)
5108258888eSVladimir Sementsov-Ogievskiy {
5118258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
5128258888eSVladimir Sementsov-Ogievskiy     unsigned long *cur, *end;
5138258888eSVladimir Sementsov-Ogievskiy 
5148258888eSVladimir Sementsov-Ogievskiy     if (!count) {
5158258888eSVladimir Sementsov-Ogievskiy         return;
5168258888eSVladimir Sementsov-Ogievskiy     }
5178258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &cur, &el_count);
5188258888eSVladimir Sementsov-Ogievskiy     end = cur + el_count;
5198258888eSVladimir Sementsov-Ogievskiy 
5208258888eSVladimir Sementsov-Ogievskiy     while (cur != end) {
5218258888eSVladimir Sementsov-Ogievskiy         memcpy(cur, buf, sizeof(*cur));
5228258888eSVladimir Sementsov-Ogievskiy 
5238258888eSVladimir Sementsov-Ogievskiy         if (BITS_PER_LONG == 32) {
5248258888eSVladimir Sementsov-Ogievskiy             le32_to_cpus((uint32_t *)cur);
5258258888eSVladimir Sementsov-Ogievskiy         } else {
5268258888eSVladimir Sementsov-Ogievskiy             le64_to_cpus((uint64_t *)cur);
5278258888eSVladimir Sementsov-Ogievskiy         }
5288258888eSVladimir Sementsov-Ogievskiy 
5298258888eSVladimir Sementsov-Ogievskiy         buf += sizeof(unsigned long);
5308258888eSVladimir Sementsov-Ogievskiy         cur++;
5318258888eSVladimir Sementsov-Ogievskiy     }
5328258888eSVladimir Sementsov-Ogievskiy     if (finish) {
5338258888eSVladimir Sementsov-Ogievskiy         hbitmap_deserialize_finish(hb);
5348258888eSVladimir Sementsov-Ogievskiy     }
5358258888eSVladimir Sementsov-Ogievskiy }
5368258888eSVladimir Sementsov-Ogievskiy 
5378258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_zeroes(HBitmap *hb, uint64_t start, uint64_t count,
5388258888eSVladimir Sementsov-Ogievskiy                                 bool finish)
5398258888eSVladimir Sementsov-Ogievskiy {
5408258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
5418258888eSVladimir Sementsov-Ogievskiy     unsigned long *first;
5428258888eSVladimir Sementsov-Ogievskiy 
5438258888eSVladimir Sementsov-Ogievskiy     if (!count) {
5448258888eSVladimir Sementsov-Ogievskiy         return;
5458258888eSVladimir Sementsov-Ogievskiy     }
5468258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &first, &el_count);
5478258888eSVladimir Sementsov-Ogievskiy 
5488258888eSVladimir Sementsov-Ogievskiy     memset(first, 0, el_count * sizeof(unsigned long));
5498258888eSVladimir Sementsov-Ogievskiy     if (finish) {
5508258888eSVladimir Sementsov-Ogievskiy         hbitmap_deserialize_finish(hb);
5518258888eSVladimir Sementsov-Ogievskiy     }
5528258888eSVladimir Sementsov-Ogievskiy }
5538258888eSVladimir Sementsov-Ogievskiy 
5548258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_finish(HBitmap *bitmap)
5558258888eSVladimir Sementsov-Ogievskiy {
5568258888eSVladimir Sementsov-Ogievskiy     int64_t i, size, prev_size;
5578258888eSVladimir Sementsov-Ogievskiy     int lev;
5588258888eSVladimir Sementsov-Ogievskiy 
5598258888eSVladimir Sementsov-Ogievskiy     /* restore levels starting from penultimate to zero level, assuming
5608258888eSVladimir Sementsov-Ogievskiy      * that the last level is ok */
5618258888eSVladimir Sementsov-Ogievskiy     size = MAX((bitmap->size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
5628258888eSVladimir Sementsov-Ogievskiy     for (lev = HBITMAP_LEVELS - 1; lev-- > 0; ) {
5638258888eSVladimir Sementsov-Ogievskiy         prev_size = size;
5648258888eSVladimir Sementsov-Ogievskiy         size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
5658258888eSVladimir Sementsov-Ogievskiy         memset(bitmap->levels[lev], 0, size * sizeof(unsigned long));
5668258888eSVladimir Sementsov-Ogievskiy 
5678258888eSVladimir Sementsov-Ogievskiy         for (i = 0; i < prev_size; ++i) {
5688258888eSVladimir Sementsov-Ogievskiy             if (bitmap->levels[lev + 1][i]) {
5698258888eSVladimir Sementsov-Ogievskiy                 bitmap->levels[lev][i >> BITS_PER_LEVEL] |=
5708258888eSVladimir Sementsov-Ogievskiy                     1UL << (i & (BITS_PER_LONG - 1));
5718258888eSVladimir Sementsov-Ogievskiy             }
5728258888eSVladimir Sementsov-Ogievskiy         }
5738258888eSVladimir Sementsov-Ogievskiy     }
5748258888eSVladimir Sementsov-Ogievskiy 
5758258888eSVladimir Sementsov-Ogievskiy     bitmap->levels[0][0] |= 1UL << (BITS_PER_LONG - 1);
5768258888eSVladimir Sementsov-Ogievskiy }
5778258888eSVladimir Sementsov-Ogievskiy 
578e7c033c3SPaolo Bonzini void hbitmap_free(HBitmap *hb)
579e7c033c3SPaolo Bonzini {
580e7c033c3SPaolo Bonzini     unsigned i;
58107ac4cdbSFam Zheng     assert(!hb->meta);
582e7c033c3SPaolo Bonzini     for (i = HBITMAP_LEVELS; i-- > 0; ) {
583e7c033c3SPaolo Bonzini         g_free(hb->levels[i]);
584e7c033c3SPaolo Bonzini     }
585e7c033c3SPaolo Bonzini     g_free(hb);
586e7c033c3SPaolo Bonzini }
587e7c033c3SPaolo Bonzini 
588e7c033c3SPaolo Bonzini HBitmap *hbitmap_alloc(uint64_t size, int granularity)
589e7c033c3SPaolo Bonzini {
590e1cf5582SMarkus Armbruster     HBitmap *hb = g_new0(struct HBitmap, 1);
591e7c033c3SPaolo Bonzini     unsigned i;
592e7c033c3SPaolo Bonzini 
593e7c033c3SPaolo Bonzini     assert(granularity >= 0 && granularity < 64);
594e7c033c3SPaolo Bonzini     size = (size + (1ULL << granularity) - 1) >> granularity;
595e7c033c3SPaolo Bonzini     assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
596e7c033c3SPaolo Bonzini 
597e7c033c3SPaolo Bonzini     hb->size = size;
598e7c033c3SPaolo Bonzini     hb->granularity = granularity;
599e7c033c3SPaolo Bonzini     for (i = HBITMAP_LEVELS; i-- > 0; ) {
600e7c033c3SPaolo Bonzini         size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
6018515efbeSJohn Snow         hb->sizes[i] = size;
602e1cf5582SMarkus Armbruster         hb->levels[i] = g_new0(unsigned long, size);
603e7c033c3SPaolo Bonzini     }
604e7c033c3SPaolo Bonzini 
605e7c033c3SPaolo Bonzini     /* We necessarily have free bits in level 0 due to the definition
606e7c033c3SPaolo Bonzini      * of HBITMAP_LEVELS, so use one for a sentinel.  This speeds up
607e7c033c3SPaolo Bonzini      * hbitmap_iter_skip_words.
608e7c033c3SPaolo Bonzini      */
609e7c033c3SPaolo Bonzini     assert(size == 1);
610e7c033c3SPaolo Bonzini     hb->levels[0][0] |= 1UL << (BITS_PER_LONG - 1);
611e7c033c3SPaolo Bonzini     return hb;
612e7c033c3SPaolo Bonzini }
613be58721dSJohn Snow 
614ce1ffea8SJohn Snow void hbitmap_truncate(HBitmap *hb, uint64_t size)
615ce1ffea8SJohn Snow {
616ce1ffea8SJohn Snow     bool shrink;
617ce1ffea8SJohn Snow     unsigned i;
618ce1ffea8SJohn Snow     uint64_t num_elements = size;
619ce1ffea8SJohn Snow     uint64_t old;
620ce1ffea8SJohn Snow 
621ce1ffea8SJohn Snow     /* Size comes in as logical elements, adjust for granularity. */
622ce1ffea8SJohn Snow     size = (size + (1ULL << hb->granularity) - 1) >> hb->granularity;
623ce1ffea8SJohn Snow     assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
624ce1ffea8SJohn Snow     shrink = size < hb->size;
625ce1ffea8SJohn Snow 
626ce1ffea8SJohn Snow     /* bit sizes are identical; nothing to do. */
627ce1ffea8SJohn Snow     if (size == hb->size) {
628ce1ffea8SJohn Snow         return;
629ce1ffea8SJohn Snow     }
630ce1ffea8SJohn Snow 
631ce1ffea8SJohn Snow     /* If we're losing bits, let's clear those bits before we invalidate all of
632ce1ffea8SJohn Snow      * our invariants. This helps keep the bitcount consistent, and will prevent
633ce1ffea8SJohn Snow      * us from carrying around garbage bits beyond the end of the map.
634ce1ffea8SJohn Snow      */
635ce1ffea8SJohn Snow     if (shrink) {
636ce1ffea8SJohn Snow         /* Don't clear partial granularity groups;
637ce1ffea8SJohn Snow          * start at the first full one. */
6386725f887SMax Reitz         uint64_t start = ROUND_UP(num_elements, UINT64_C(1) << hb->granularity);
639ce1ffea8SJohn Snow         uint64_t fix_count = (hb->size << hb->granularity) - start;
640ce1ffea8SJohn Snow 
641ce1ffea8SJohn Snow         assert(fix_count);
642ce1ffea8SJohn Snow         hbitmap_reset(hb, start, fix_count);
643ce1ffea8SJohn Snow     }
644ce1ffea8SJohn Snow 
645ce1ffea8SJohn Snow     hb->size = size;
646ce1ffea8SJohn Snow     for (i = HBITMAP_LEVELS; i-- > 0; ) {
647ce1ffea8SJohn Snow         size = MAX(BITS_TO_LONGS(size), 1);
648ce1ffea8SJohn Snow         if (hb->sizes[i] == size) {
649ce1ffea8SJohn Snow             break;
650ce1ffea8SJohn Snow         }
651ce1ffea8SJohn Snow         old = hb->sizes[i];
652ce1ffea8SJohn Snow         hb->sizes[i] = size;
653ce1ffea8SJohn Snow         hb->levels[i] = g_realloc(hb->levels[i], size * sizeof(unsigned long));
654ce1ffea8SJohn Snow         if (!shrink) {
655ce1ffea8SJohn Snow             memset(&hb->levels[i][old], 0x00,
656ce1ffea8SJohn Snow                    (size - old) * sizeof(*hb->levels[i]));
657ce1ffea8SJohn Snow         }
658ce1ffea8SJohn Snow     }
65907ac4cdbSFam Zheng     if (hb->meta) {
66007ac4cdbSFam Zheng         hbitmap_truncate(hb->meta, hb->size << hb->granularity);
66107ac4cdbSFam Zheng     }
662ce1ffea8SJohn Snow }
663ce1ffea8SJohn Snow 
664ce1ffea8SJohn Snow 
665be58721dSJohn Snow /**
666be58721dSJohn Snow  * Given HBitmaps A and B, let A := A (BITOR) B.
667be58721dSJohn Snow  * Bitmap B will not be modified.
668be58721dSJohn Snow  *
669be58721dSJohn Snow  * @return true if the merge was successful,
670be58721dSJohn Snow  *         false if it was not attempted.
671be58721dSJohn Snow  */
672be58721dSJohn Snow bool hbitmap_merge(HBitmap *a, const HBitmap *b)
673be58721dSJohn Snow {
674be58721dSJohn Snow     int i;
675be58721dSJohn Snow     uint64_t j;
676be58721dSJohn Snow 
677be58721dSJohn Snow     if ((a->size != b->size) || (a->granularity != b->granularity)) {
678be58721dSJohn Snow         return false;
679be58721dSJohn Snow     }
680be58721dSJohn Snow 
681be58721dSJohn Snow     if (hbitmap_count(b) == 0) {
682be58721dSJohn Snow         return true;
683be58721dSJohn Snow     }
684be58721dSJohn Snow 
685be58721dSJohn Snow     /* This merge is O(size), as BITS_PER_LONG and HBITMAP_LEVELS are constant.
686be58721dSJohn Snow      * It may be possible to improve running times for sparsely populated maps
687be58721dSJohn Snow      * by using hbitmap_iter_next, but this is suboptimal for dense maps.
688be58721dSJohn Snow      */
689be58721dSJohn Snow     for (i = HBITMAP_LEVELS - 1; i >= 0; i--) {
690be58721dSJohn Snow         for (j = 0; j < a->sizes[i]; j++) {
691be58721dSJohn Snow             a->levels[i][j] |= b->levels[i][j];
692be58721dSJohn Snow         }
693be58721dSJohn Snow     }
694be58721dSJohn Snow 
695be58721dSJohn Snow     return true;
696be58721dSJohn Snow }
69707ac4cdbSFam Zheng 
69807ac4cdbSFam Zheng HBitmap *hbitmap_create_meta(HBitmap *hb, int chunk_size)
69907ac4cdbSFam Zheng {
70007ac4cdbSFam Zheng     assert(!(chunk_size & (chunk_size - 1)));
70107ac4cdbSFam Zheng     assert(!hb->meta);
70207ac4cdbSFam Zheng     hb->meta = hbitmap_alloc(hb->size << hb->granularity,
70307ac4cdbSFam Zheng                              hb->granularity + ctz32(chunk_size));
70407ac4cdbSFam Zheng     return hb->meta;
70507ac4cdbSFam Zheng }
70607ac4cdbSFam Zheng 
70707ac4cdbSFam Zheng void hbitmap_free_meta(HBitmap *hb)
70807ac4cdbSFam Zheng {
70907ac4cdbSFam Zheng     assert(hb->meta);
71007ac4cdbSFam Zheng     hbitmap_free(hb->meta);
71107ac4cdbSFam Zheng     hb->meta = NULL;
71207ac4cdbSFam Zheng }
713