|
1 /* |
|
2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 // Closure for iterating over BitMaps |
|
26 |
|
27 class BitMapClosure VALUE_OBJ_CLASS_SPEC { |
|
28 public: |
|
29 // Callback when bit in map is set |
|
30 virtual void do_bit(size_t offset) = 0; |
|
31 }; |
|
32 |
|
33 |
|
34 // Operations for bitmaps represented as arrays of unsigned 32- or 64-bit |
|
35 // integers (uintptr_t). |
|
36 // |
|
37 // Bit offsets are numbered from 0 to size-1 |
|
38 |
|
39 class BitMap VALUE_OBJ_CLASS_SPEC { |
|
40 friend class BitMap2D; |
|
41 |
|
42 public: |
|
43 typedef size_t idx_t; // Type used for bit and word indices. |
|
44 |
|
45 // Hints for range sizes. |
|
46 typedef enum { |
|
47 unknown_range, small_range, large_range |
|
48 } RangeSizeHint; |
|
49 |
|
50 private: |
|
51 idx_t* _map; // First word in bitmap |
|
52 idx_t _size; // Size of bitmap (in bits) |
|
53 |
|
54 // Puts the given value at the given offset, using resize() to size |
|
55 // the bitmap appropriately if needed using factor-of-two expansion. |
|
56 void at_put_grow(idx_t index, bool value); |
|
57 |
|
58 protected: |
|
59 // Return the position of bit within the word that contains it (e.g., if |
|
60 // bitmap words are 32 bits, return a number 0 <= n <= 31). |
|
61 static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); } |
|
62 |
|
63 // Return a mask that will select the specified bit, when applied to the word |
|
64 // containing the bit. |
|
65 static idx_t bit_mask(idx_t bit) { return (idx_t)1 << bit_in_word(bit); } |
|
66 |
|
67 // Return the index of the word containing the specified bit. |
|
68 static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; } |
|
69 |
|
70 // Return the bit number of the first bit in the specified word. |
|
71 static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; } |
|
72 |
|
73 // Return the array of bitmap words, or a specific word from it. |
|
74 idx_t* map() const { return _map; } |
|
75 idx_t map(idx_t word) const { return _map[word]; } |
|
76 |
|
77 // Return a pointer to the word containing the specified bit. |
|
78 idx_t* word_addr(idx_t bit) const { return map() + word_index(bit); } |
|
79 |
|
80 // Set a word to a specified value or to all ones; clear a word. |
|
81 void set_word (idx_t word, idx_t val) { _map[word] = val; } |
|
82 void set_word (idx_t word) { set_word(word, ~(uintptr_t)0); } |
|
83 void clear_word(idx_t word) { _map[word] = 0; } |
|
84 |
|
85 // Utilities for ranges of bits. Ranges are half-open [beg, end). |
|
86 |
|
87 // Ranges within a single word. |
|
88 inline idx_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const; |
|
89 inline void set_range_within_word (idx_t beg, idx_t end); |
|
90 inline void clear_range_within_word (idx_t beg, idx_t end); |
|
91 inline void par_put_range_within_word (idx_t beg, idx_t end, bool value); |
|
92 |
|
93 // Ranges spanning entire words. |
|
94 inline void set_range_of_words (idx_t beg, idx_t end); |
|
95 inline void clear_range_of_words (idx_t beg, idx_t end); |
|
96 inline void set_large_range_of_words (idx_t beg, idx_t end); |
|
97 inline void clear_large_range_of_words (idx_t beg, idx_t end); |
|
98 |
|
99 // The index of the first full word in a range. |
|
100 inline idx_t word_index_round_up(idx_t bit) const; |
|
101 |
|
102 // Verification, statistics. |
|
103 void verify_index(idx_t index) const { |
|
104 assert(index < _size, "BitMap index out of bounds"); |
|
105 } |
|
106 |
|
107 void verify_range(idx_t beg_index, idx_t end_index) const { |
|
108 #ifdef ASSERT |
|
109 assert(beg_index <= end_index, "BitMap range error"); |
|
110 // Note that [0,0) and [size,size) are both valid ranges. |
|
111 if (end_index != _size) verify_index(end_index); |
|
112 #endif |
|
113 } |
|
114 |
|
115 public: |
|
116 |
|
117 // Constructs a bitmap with no map, and size 0. |
|
118 BitMap() : _map(NULL), _size(0) {} |
|
119 |
|
120 // Construction |
|
121 BitMap(idx_t* map, idx_t size_in_bits); |
|
122 |
|
123 // Allocates necessary data structure in resource area |
|
124 BitMap(idx_t size_in_bits); |
|
125 |
|
126 void set_map(idx_t* map) { _map = map; } |
|
127 void set_size(idx_t size_in_bits) { _size = size_in_bits; } |
|
128 |
|
129 // Allocates necessary data structure in resource area. |
|
130 // Preserves state currently in bit map by copying data. |
|
131 // Zeros any newly-addressable bits. |
|
132 // Does not perform any frees (i.e., of current _map). |
|
133 void resize(idx_t size_in_bits); |
|
134 |
|
135 // Accessing |
|
136 idx_t size() const { return _size; } |
|
137 idx_t size_in_words() const { |
|
138 return word_index(size() + BitsPerWord - 1); |
|
139 } |
|
140 |
|
141 bool at(idx_t index) const { |
|
142 verify_index(index); |
|
143 return (*word_addr(index) & bit_mask(index)) != 0; |
|
144 } |
|
145 |
|
146 // Align bit index up or down to the next bitmap word boundary, or check |
|
147 // alignment. |
|
148 static idx_t word_align_up(idx_t bit) { |
|
149 return align_size_up(bit, BitsPerWord); |
|
150 } |
|
151 static idx_t word_align_down(idx_t bit) { |
|
152 return align_size_down(bit, BitsPerWord); |
|
153 } |
|
154 static bool is_word_aligned(idx_t bit) { |
|
155 return word_align_up(bit) == bit; |
|
156 } |
|
157 |
|
158 // Set or clear the specified bit. |
|
159 inline void set_bit(idx_t bit); |
|
160 inline void clear_bit(idx_t bit); |
|
161 |
|
162 // Atomically set or clear the specified bit. |
|
163 inline bool par_set_bit(idx_t bit); |
|
164 inline bool par_clear_bit(idx_t bit); |
|
165 |
|
166 // Put the given value at the given offset. The parallel version |
|
167 // will CAS the value into the bitmap and is quite a bit slower. |
|
168 // The parallel version also returns a value indicating if the |
|
169 // calling thread was the one that changed the value of the bit. |
|
170 void at_put(idx_t index, bool value); |
|
171 bool par_at_put(idx_t index, bool value); |
|
172 |
|
173 // Update a range of bits. Ranges are half-open [beg, end). |
|
174 void set_range (idx_t beg, idx_t end); |
|
175 void clear_range (idx_t beg, idx_t end); |
|
176 void set_large_range (idx_t beg, idx_t end); |
|
177 void clear_large_range (idx_t beg, idx_t end); |
|
178 void at_put_range(idx_t beg, idx_t end, bool value); |
|
179 void par_at_put_range(idx_t beg, idx_t end, bool value); |
|
180 void at_put_large_range(idx_t beg, idx_t end, bool value); |
|
181 void par_at_put_large_range(idx_t beg, idx_t end, bool value); |
|
182 |
|
183 // Update a range of bits, using a hint about the size. Currently only |
|
184 // inlines the predominant case of a 1-bit range. Works best when hint is a |
|
185 // compile-time constant. |
|
186 inline void set_range(idx_t beg, idx_t end, RangeSizeHint hint); |
|
187 inline void clear_range(idx_t beg, idx_t end, RangeSizeHint hint); |
|
188 inline void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint); |
|
189 inline void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint); |
|
190 |
|
191 // Clearing |
|
192 void clear(); |
|
193 void clear_large(); |
|
194 |
|
195 // Iteration support |
|
196 void iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex); |
|
197 inline void iterate(BitMapClosure* blk) { |
|
198 // call the version that takes an interval |
|
199 iterate(blk, 0, size()); |
|
200 } |
|
201 |
|
202 // Looking for 1's and 0's to the "right" |
|
203 idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const; |
|
204 idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const; |
|
205 |
|
206 idx_t get_next_one_offset(idx_t offset) const { |
|
207 return get_next_one_offset(offset, size()); |
|
208 } |
|
209 idx_t get_next_zero_offset(idx_t offset) const { |
|
210 return get_next_zero_offset(offset, size()); |
|
211 } |
|
212 |
|
213 |
|
214 |
|
215 // Find the next one bit in the range [beg_bit, end_bit), or return end_bit if |
|
216 // no one bit is found. Equivalent to get_next_one_offset(), but inline for |
|
217 // use in performance-critical code. |
|
218 inline idx_t find_next_one_bit(idx_t beg_bit, idx_t end_bit) const; |
|
219 |
|
220 // Set operations. |
|
221 void set_union(BitMap bits); |
|
222 void set_difference(BitMap bits); |
|
223 void set_intersection(BitMap bits); |
|
224 // Returns true iff "this" is a superset of "bits". |
|
225 bool contains(const BitMap bits) const; |
|
226 // Returns true iff "this and "bits" have a non-empty intersection. |
|
227 bool intersects(const BitMap bits) const; |
|
228 |
|
229 // Returns result of whether this map changed |
|
230 // during the operation |
|
231 bool set_union_with_result(BitMap bits); |
|
232 bool set_difference_with_result(BitMap bits); |
|
233 bool set_intersection_with_result(BitMap bits); |
|
234 |
|
235 void set_from(BitMap bits); |
|
236 |
|
237 bool is_same(BitMap bits); |
|
238 |
|
239 // Test if all bits are set or cleared |
|
240 bool is_full() const; |
|
241 bool is_empty() const; |
|
242 |
|
243 |
|
244 #ifndef PRODUCT |
|
245 public: |
|
246 // Printing |
|
247 void print_on(outputStream* st) const; |
|
248 #endif |
|
249 }; |
|
250 |
|
251 inline void BitMap::set_bit(idx_t bit) { |
|
252 verify_index(bit); |
|
253 *word_addr(bit) |= bit_mask(bit); |
|
254 } |
|
255 |
|
256 inline void BitMap::clear_bit(idx_t bit) { |
|
257 verify_index(bit); |
|
258 *word_addr(bit) &= ~bit_mask(bit); |
|
259 } |
|
260 |
|
261 inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) { |
|
262 if (hint == small_range && end - beg == 1) { |
|
263 set_bit(beg); |
|
264 } else { |
|
265 if (hint == large_range) { |
|
266 set_large_range(beg, end); |
|
267 } else { |
|
268 set_range(beg, end); |
|
269 } |
|
270 } |
|
271 } |
|
272 |
|
273 inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) { |
|
274 if (hint == small_range && end - beg == 1) { |
|
275 clear_bit(beg); |
|
276 } else { |
|
277 if (hint == large_range) { |
|
278 clear_large_range(beg, end); |
|
279 } else { |
|
280 clear_range(beg, end); |
|
281 } |
|
282 } |
|
283 } |
|
284 |
|
285 inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) { |
|
286 if (hint == small_range && end - beg == 1) { |
|
287 par_at_put(beg, true); |
|
288 } else { |
|
289 if (hint == large_range) { |
|
290 par_at_put_large_range(beg, end, true); |
|
291 } else { |
|
292 par_at_put_range(beg, end, true); |
|
293 } |
|
294 } |
|
295 } |
|
296 |
|
297 |
|
298 // Convenience class wrapping BitMap which provides multiple bits per slot. |
|
299 class BitMap2D VALUE_OBJ_CLASS_SPEC { |
|
300 public: |
|
301 typedef size_t idx_t; // Type used for bit and word indices. |
|
302 |
|
303 private: |
|
304 BitMap _map; |
|
305 idx_t _bits_per_slot; |
|
306 |
|
307 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const { |
|
308 return slot_index * _bits_per_slot + bit_within_slot_index; |
|
309 } |
|
310 |
|
311 void verify_bit_within_slot_index(idx_t index) const { |
|
312 assert(index < _bits_per_slot, "bit_within_slot index out of bounds"); |
|
313 } |
|
314 |
|
315 public: |
|
316 // Construction. bits_per_slot must be greater than 0. |
|
317 BitMap2D(uintptr_t* map, idx_t size_in_slots, idx_t bits_per_slot); |
|
318 |
|
319 // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0. |
|
320 BitMap2D(idx_t size_in_slots, idx_t bits_per_slot); |
|
321 |
|
322 idx_t size_in_bits() { |
|
323 return _map.size(); |
|
324 } |
|
325 |
|
326 // Returns number of full slots that have been allocated |
|
327 idx_t size_in_slots() { |
|
328 // Round down |
|
329 return _map.size() / _bits_per_slot; |
|
330 } |
|
331 |
|
332 bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) { |
|
333 verify_bit_within_slot_index(bit_within_slot_index); |
|
334 return (bit_index(slot_index, bit_within_slot_index) < size_in_bits()); |
|
335 } |
|
336 |
|
337 bool at(idx_t slot_index, idx_t bit_within_slot_index) const { |
|
338 verify_bit_within_slot_index(bit_within_slot_index); |
|
339 return _map.at(bit_index(slot_index, bit_within_slot_index)); |
|
340 } |
|
341 |
|
342 void set_bit(idx_t slot_index, idx_t bit_within_slot_index) { |
|
343 verify_bit_within_slot_index(bit_within_slot_index); |
|
344 _map.set_bit(bit_index(slot_index, bit_within_slot_index)); |
|
345 } |
|
346 |
|
347 void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) { |
|
348 verify_bit_within_slot_index(bit_within_slot_index); |
|
349 _map.clear_bit(bit_index(slot_index, bit_within_slot_index)); |
|
350 } |
|
351 |
|
352 void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) { |
|
353 verify_bit_within_slot_index(bit_within_slot_index); |
|
354 _map.at_put(bit_index(slot_index, bit_within_slot_index), value); |
|
355 } |
|
356 |
|
357 void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) { |
|
358 verify_bit_within_slot_index(bit_within_slot_index); |
|
359 _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value); |
|
360 } |
|
361 |
|
362 void clear() { |
|
363 _map.clear(); |
|
364 } |
|
365 }; |
|
366 |
|
367 |
|
368 |
|
369 inline void BitMap::set_range_of_words(idx_t beg, idx_t end) { |
|
370 uintptr_t* map = _map; |
|
371 for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0; |
|
372 } |
|
373 |
|
374 |
|
375 inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) { |
|
376 uintptr_t* map = _map; |
|
377 for (idx_t i = beg; i < end; ++i) map[i] = 0; |
|
378 } |
|
379 |
|
380 |
|
381 inline void BitMap::clear() { |
|
382 clear_range_of_words(0, size_in_words()); |
|
383 } |
|
384 |
|
385 |
|
386 inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) { |
|
387 if (hint == small_range && end - beg == 1) { |
|
388 par_at_put(beg, false); |
|
389 } else { |
|
390 if (hint == large_range) { |
|
391 par_at_put_large_range(beg, end, false); |
|
392 } else { |
|
393 par_at_put_range(beg, end, false); |
|
394 } |
|
395 } |
|
396 } |