]> Git Repo - qemu.git/blob - include/qemu/bitmap.h
Merge remote-tracking branch 'remotes/stefanha/tags/block-pull-request' into staging
[qemu.git] / include / qemu / bitmap.h
1 /*
2  * Bitmap Module
3  *
4  * Copyright (C) 2010 Corentin Chary <[email protected]>
5  *
6  * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
7  *
8  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9  * See the COPYING.LIB file in the top-level directory.
10  */
11
12 #ifndef BITMAP_H
13 #define BITMAP_H
14
15 #include "qemu-common.h"
16 #include "qemu/bitops.h"
17
18 /*
19  * The available bitmap operations and their rough meaning in the
20  * case that the bitmap is a single unsigned long are thus:
21  *
22  * Note that nbits should be always a compile time evaluable constant.
23  * Otherwise many inlines will generate horrible code.
24  *
25  * bitmap_zero(dst, nbits)                      *dst = 0UL
26  * bitmap_fill(dst, nbits)                      *dst = ~0UL
27  * bitmap_copy(dst, src, nbits)                 *dst = *src
28  * bitmap_and(dst, src1, src2, nbits)           *dst = *src1 & *src2
29  * bitmap_or(dst, src1, src2, nbits)            *dst = *src1 | *src2
30  * bitmap_xor(dst, src1, src2, nbits)           *dst = *src1 ^ *src2
31  * bitmap_andnot(dst, src1, src2, nbits)        *dst = *src1 & ~(*src2)
32  * bitmap_complement(dst, src, nbits)           *dst = ~(*src)
33  * bitmap_equal(src1, src2, nbits)              Are *src1 and *src2 equal?
34  * bitmap_intersects(src1, src2, nbits)         Do *src1 and *src2 overlap?
35  * bitmap_empty(src, nbits)                     Are all bits zero in *src?
36  * bitmap_full(src, nbits)                      Are all bits set in *src?
37  * bitmap_set(dst, pos, nbits)                  Set specified bit area
38  * bitmap_clear(dst, pos, nbits)                Clear specified bit area
39  * bitmap_find_next_zero_area(buf, len, pos, n, mask)   Find bit free area
40  */
41
42 /*
43  * Also the following operations apply to bitmaps.
44  *
45  * set_bit(bit, addr)                   *addr |= bit
46  * clear_bit(bit, addr)                 *addr &= ~bit
47  * change_bit(bit, addr)                *addr ^= bit
48  * test_bit(bit, addr)                  Is bit set in *addr?
49  * test_and_set_bit(bit, addr)          Set bit and return old value
50  * test_and_clear_bit(bit, addr)        Clear bit and return old value
51  * test_and_change_bit(bit, addr)       Change bit and return old value
52  * find_first_zero_bit(addr, nbits)     Position first zero bit in *addr
53  * find_first_bit(addr, nbits)          Position first set bit in *addr
54  * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
55  * find_next_bit(addr, nbits, bit)      Position next set bit in *addr >= bit
56  */
57
58 #define BITMAP_LAST_WORD_MASK(nbits)                                    \
59     (                                                                   \
60         ((nbits) % BITS_PER_LONG) ?                                     \
61         (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL                       \
62         )
63
64 #define DECLARE_BITMAP(name,bits)                  \
65         unsigned long name[BITS_TO_LONGS(bits)]
66
67 #define small_nbits(nbits)                      \
68         ((nbits) <= BITS_PER_LONG)
69
70 int slow_bitmap_empty(const unsigned long *bitmap, long bits);
71 int slow_bitmap_full(const unsigned long *bitmap, long bits);
72 int slow_bitmap_equal(const unsigned long *bitmap1,
73                       const unsigned long *bitmap2, long bits);
74 void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
75                             long bits);
76 void slow_bitmap_shift_right(unsigned long *dst,
77                              const unsigned long *src, int shift, long bits);
78 void slow_bitmap_shift_left(unsigned long *dst,
79                             const unsigned long *src, int shift, long bits);
80 int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
81                     const unsigned long *bitmap2, long bits);
82 void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
83                     const unsigned long *bitmap2, long bits);
84 void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
85                      const unsigned long *bitmap2, long bits);
86 int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
87                        const unsigned long *bitmap2, long bits);
88 int slow_bitmap_intersects(const unsigned long *bitmap1,
89                            const unsigned long *bitmap2, long bits);
90
91 static inline unsigned long *bitmap_try_new(long nbits)
92 {
93     long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
94     return g_try_malloc0(len);
95 }
96
97 static inline unsigned long *bitmap_new(long nbits)
98 {
99     unsigned long *ptr = bitmap_try_new(nbits);
100     if (ptr == NULL) {
101         abort();
102     }
103     return ptr;
104 }
105
106 static inline void bitmap_zero(unsigned long *dst, long nbits)
107 {
108     if (small_nbits(nbits)) {
109         *dst = 0UL;
110     } else {
111         long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
112         memset(dst, 0, len);
113     }
114 }
115
116 static inline void bitmap_fill(unsigned long *dst, long nbits)
117 {
118     size_t nlongs = BITS_TO_LONGS(nbits);
119     if (!small_nbits(nbits)) {
120         long len = (nlongs - 1) * sizeof(unsigned long);
121         memset(dst, 0xff,  len);
122     }
123     dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
124 }
125
126 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
127                                long nbits)
128 {
129     if (small_nbits(nbits)) {
130         *dst = *src;
131     } else {
132         long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
133         memcpy(dst, src, len);
134     }
135 }
136
137 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
138                              const unsigned long *src2, long nbits)
139 {
140     if (small_nbits(nbits)) {
141         return (*dst = *src1 & *src2) != 0;
142     }
143     return slow_bitmap_and(dst, src1, src2, nbits);
144 }
145
146 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
147                              const unsigned long *src2, long nbits)
148 {
149     if (small_nbits(nbits)) {
150         *dst = *src1 | *src2;
151     } else {
152         slow_bitmap_or(dst, src1, src2, nbits);
153     }
154 }
155
156 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
157                               const unsigned long *src2, long nbits)
158 {
159     if (small_nbits(nbits)) {
160         *dst = *src1 ^ *src2;
161     } else {
162         slow_bitmap_xor(dst, src1, src2, nbits);
163     }
164 }
165
166 static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
167                                 const unsigned long *src2, long nbits)
168 {
169     if (small_nbits(nbits)) {
170         return (*dst = *src1 & ~(*src2)) != 0;
171     }
172     return slow_bitmap_andnot(dst, src1, src2, nbits);
173 }
174
175 static inline void bitmap_complement(unsigned long *dst,
176                                      const unsigned long *src,
177                                      long nbits)
178 {
179     if (small_nbits(nbits)) {
180         *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
181     } else {
182         slow_bitmap_complement(dst, src, nbits);
183     }
184 }
185
186 static inline int bitmap_equal(const unsigned long *src1,
187                                const unsigned long *src2, long nbits)
188 {
189     if (small_nbits(nbits)) {
190         return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
191     } else {
192         return slow_bitmap_equal(src1, src2, nbits);
193     }
194 }
195
196 static inline int bitmap_empty(const unsigned long *src, long nbits)
197 {
198     if (small_nbits(nbits)) {
199         return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
200     } else {
201         return slow_bitmap_empty(src, nbits);
202     }
203 }
204
205 static inline int bitmap_full(const unsigned long *src, long nbits)
206 {
207     if (small_nbits(nbits)) {
208         return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
209     } else {
210         return slow_bitmap_full(src, nbits);
211     }
212 }
213
214 static inline int bitmap_intersects(const unsigned long *src1,
215                                     const unsigned long *src2, long nbits)
216 {
217     if (small_nbits(nbits)) {
218         return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
219     } else {
220         return slow_bitmap_intersects(src1, src2, nbits);
221     }
222 }
223
224 void bitmap_set(unsigned long *map, long i, long len);
225 void bitmap_clear(unsigned long *map, long start, long nr);
226 unsigned long bitmap_find_next_zero_area(unsigned long *map,
227                                          unsigned long size,
228                                          unsigned long start,
229                                          unsigned long nr,
230                                          unsigned long align_mask);
231
232 static inline unsigned long *bitmap_zero_extend(unsigned long *old,
233                                                 long old_nbits, long new_nbits)
234 {
235     long new_len = BITS_TO_LONGS(new_nbits) * sizeof(unsigned long);
236     unsigned long *new = g_realloc(old, new_len);
237     bitmap_clear(new, old_nbits, new_nbits - old_nbits);
238     return new;
239 }
240
241 #endif /* BITMAP_H */
This page took 0.0358 seconds and 4 git commands to generate.