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