]>
Commit | Line | Data |
---|---|---|
e0e53b2f CC |
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 | ||
d6aaddfe | 15 | |
1de7afc9 | 16 | #include "qemu/bitops.h" |
e0e53b2f CC |
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? | |
9c22687e | 34 | * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap? |
e0e53b2f CC |
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 | |
9f02cfc8 | 38 | * bitmap_set_atomic(dst, pos, nbits) Set specified bit area with atomic ops |
e0e53b2f | 39 | * bitmap_clear(dst, pos, nbits) Clear specified bit area |
36546e5b | 40 | * bitmap_test_and_clear_atomic(dst, pos, nbits) Test and clear area |
e0e53b2f CC |
41 | * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area |
42 | */ | |
43 | ||
44 | /* | |
45 | * Also the following operations apply to bitmaps. | |
46 | * | |
47 | * set_bit(bit, addr) *addr |= bit | |
48 | * clear_bit(bit, addr) *addr &= ~bit | |
49 | * change_bit(bit, addr) *addr ^= bit | |
50 | * test_bit(bit, addr) Is bit set in *addr? | |
51 | * test_and_set_bit(bit, addr) Set bit and return old value | |
52 | * test_and_clear_bit(bit, addr) Clear bit and return old value | |
53 | * test_and_change_bit(bit, addr) Change bit and return old value | |
54 | * find_first_zero_bit(addr, nbits) Position first zero bit in *addr | |
55 | * find_first_bit(addr, nbits) Position first set bit in *addr | |
56 | * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit | |
57 | * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit | |
58 | */ | |
59 | ||
60 | #define BITMAP_LAST_WORD_MASK(nbits) \ | |
61 | ( \ | |
62 | ((nbits) % BITS_PER_LONG) ? \ | |
63 | (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \ | |
64 | ) | |
65 | ||
66 | #define DECLARE_BITMAP(name,bits) \ | |
9c22687e | 67 | unsigned long name[BITS_TO_LONGS(bits)] |
e0e53b2f CC |
68 | |
69 | #define small_nbits(nbits) \ | |
9c22687e | 70 | ((nbits) <= BITS_PER_LONG) |
e0e53b2f | 71 | |
9c22687e JQ |
72 | int slow_bitmap_empty(const unsigned long *bitmap, long bits); |
73 | int slow_bitmap_full(const unsigned long *bitmap, long bits); | |
e0e53b2f | 74 | int slow_bitmap_equal(const unsigned long *bitmap1, |
9c22687e | 75 | const unsigned long *bitmap2, long bits); |
e0e53b2f | 76 | void slow_bitmap_complement(unsigned long *dst, const unsigned long *src, |
9c22687e | 77 | long bits); |
e0e53b2f | 78 | int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1, |
9c22687e | 79 | const unsigned long *bitmap2, long bits); |
e0e53b2f | 80 | void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1, |
9c22687e | 81 | const unsigned long *bitmap2, long bits); |
e0e53b2f | 82 | void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, |
9c22687e | 83 | const unsigned long *bitmap2, long bits); |
e0e53b2f | 84 | int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, |
9c22687e | 85 | const unsigned long *bitmap2, long bits); |
e0e53b2f | 86 | int slow_bitmap_intersects(const unsigned long *bitmap1, |
9c22687e | 87 | const unsigned long *bitmap2, long bits); |
e0e53b2f | 88 | |
be4d57c1 | 89 | static inline unsigned long *bitmap_try_new(long nbits) |
e0e53b2f | 90 | { |
9c22687e | 91 | long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); |
be4d57c1 PL |
92 | return g_try_malloc0(len); |
93 | } | |
94 | ||
95 | static inline unsigned long *bitmap_new(long nbits) | |
96 | { | |
97 | unsigned long *ptr = bitmap_try_new(nbits); | |
98 | if (ptr == NULL) { | |
99 | abort(); | |
100 | } | |
101 | return ptr; | |
e0e53b2f CC |
102 | } |
103 | ||
9c22687e | 104 | static inline void bitmap_zero(unsigned long *dst, long nbits) |
e0e53b2f CC |
105 | { |
106 | if (small_nbits(nbits)) { | |
107 | *dst = 0UL; | |
108 | } else { | |
9c22687e | 109 | long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); |
e0e53b2f CC |
110 | memset(dst, 0, len); |
111 | } | |
112 | } | |
113 | ||
9c22687e | 114 | static inline void bitmap_fill(unsigned long *dst, long nbits) |
e0e53b2f CC |
115 | { |
116 | size_t nlongs = BITS_TO_LONGS(nbits); | |
117 | if (!small_nbits(nbits)) { | |
9c22687e | 118 | long len = (nlongs - 1) * sizeof(unsigned long); |
e0e53b2f CC |
119 | memset(dst, 0xff, len); |
120 | } | |
121 | dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits); | |
122 | } | |
123 | ||
124 | static inline void bitmap_copy(unsigned long *dst, const unsigned long *src, | |
9c22687e | 125 | long nbits) |
e0e53b2f CC |
126 | { |
127 | if (small_nbits(nbits)) { | |
128 | *dst = *src; | |
129 | } else { | |
9c22687e | 130 | long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); |
e0e53b2f CC |
131 | memcpy(dst, src, len); |
132 | } | |
133 | } | |
134 | ||
135 | static inline int bitmap_and(unsigned long *dst, const unsigned long *src1, | |
9c22687e | 136 | const unsigned long *src2, long nbits) |
e0e53b2f CC |
137 | { |
138 | if (small_nbits(nbits)) { | |
139 | return (*dst = *src1 & *src2) != 0; | |
140 | } | |
141 | return slow_bitmap_and(dst, src1, src2, nbits); | |
142 | } | |
143 | ||
144 | static inline void bitmap_or(unsigned long *dst, const unsigned long *src1, | |
9c22687e | 145 | const unsigned long *src2, long nbits) |
e0e53b2f CC |
146 | { |
147 | if (small_nbits(nbits)) { | |
148 | *dst = *src1 | *src2; | |
149 | } else { | |
150 | slow_bitmap_or(dst, src1, src2, nbits); | |
151 | } | |
152 | } | |
153 | ||
154 | static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1, | |
9c22687e | 155 | const unsigned long *src2, long nbits) |
e0e53b2f CC |
156 | { |
157 | if (small_nbits(nbits)) { | |
158 | *dst = *src1 ^ *src2; | |
159 | } else { | |
160 | slow_bitmap_xor(dst, src1, src2, nbits); | |
161 | } | |
162 | } | |
163 | ||
164 | static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1, | |
9c22687e | 165 | const unsigned long *src2, long nbits) |
e0e53b2f CC |
166 | { |
167 | if (small_nbits(nbits)) { | |
168 | return (*dst = *src1 & ~(*src2)) != 0; | |
169 | } | |
170 | return slow_bitmap_andnot(dst, src1, src2, nbits); | |
171 | } | |
172 | ||
9c22687e JQ |
173 | static inline void bitmap_complement(unsigned long *dst, |
174 | const unsigned long *src, | |
175 | long nbits) | |
e0e53b2f CC |
176 | { |
177 | if (small_nbits(nbits)) { | |
178 | *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits); | |
179 | } else { | |
180 | slow_bitmap_complement(dst, src, nbits); | |
181 | } | |
182 | } | |
183 | ||
184 | static inline int bitmap_equal(const unsigned long *src1, | |
9c22687e | 185 | const unsigned long *src2, long nbits) |
e0e53b2f CC |
186 | { |
187 | if (small_nbits(nbits)) { | |
188 | return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits)); | |
189 | } else { | |
190 | return slow_bitmap_equal(src1, src2, nbits); | |
191 | } | |
192 | } | |
193 | ||
9c22687e | 194 | static inline int bitmap_empty(const unsigned long *src, long nbits) |
e0e53b2f CC |
195 | { |
196 | if (small_nbits(nbits)) { | |
197 | return ! (*src & BITMAP_LAST_WORD_MASK(nbits)); | |
198 | } else { | |
199 | return slow_bitmap_empty(src, nbits); | |
200 | } | |
201 | } | |
202 | ||
9c22687e | 203 | static inline int bitmap_full(const unsigned long *src, long nbits) |
e0e53b2f CC |
204 | { |
205 | if (small_nbits(nbits)) { | |
206 | return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits)); | |
207 | } else { | |
208 | return slow_bitmap_full(src, nbits); | |
209 | } | |
210 | } | |
211 | ||
212 | static inline int bitmap_intersects(const unsigned long *src1, | |
9c22687e | 213 | const unsigned long *src2, long nbits) |
e0e53b2f CC |
214 | { |
215 | if (small_nbits(nbits)) { | |
216 | return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0; | |
217 | } else { | |
218 | return slow_bitmap_intersects(src1, src2, nbits); | |
219 | } | |
220 | } | |
221 | ||
9c22687e | 222 | void bitmap_set(unsigned long *map, long i, long len); |
9f02cfc8 | 223 | void bitmap_set_atomic(unsigned long *map, long i, long len); |
9c22687e | 224 | void bitmap_clear(unsigned long *map, long start, long nr); |
36546e5b | 225 | bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr); |
e0e53b2f | 226 | unsigned long bitmap_find_next_zero_area(unsigned long *map, |
9c22687e JQ |
227 | unsigned long size, |
228 | unsigned long start, | |
229 | unsigned long nr, | |
230 | unsigned long align_mask); | |
e0e53b2f | 231 | |
164590a6 JQ |
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 | ||
e0e53b2f | 241 | #endif /* BITMAP_H */ |