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