19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_VM_UTILITIES_BITMAP_HPP
26 #define SHARE_VM_UTILITIES_BITMAP_HPP
27
28 #include "memory/allocation.hpp"
29 #include "utilities/top.hpp"
30
31 // Forward decl;
32 class BitMapClosure;
33
34 // Operations for bitmaps represented as arrays of unsigned integers.
35 // Bit offsets are numbered from 0 to size-1.
36
37 class BitMap VALUE_OBJ_CLASS_SPEC {
38 friend class BitMap2D;
39
40 public:
41 typedef size_t idx_t; // Type used for bit and word indices.
42 typedef uintptr_t bm_word_t; // Element type of array that represents
43 // the bitmap.
44
45 // Hints for range sizes.
46 typedef enum {
47 unknown_range, small_range, large_range
48 } RangeSizeHint;
49
50 private:
51 ArrayAllocator<bm_word_t, mtInternal> _map_allocator;
52 bm_word_t* _map; // First word in bitmap
53 idx_t _size; // Size of bitmap (in bits)
54
55 // Puts the given value at the given offset, using resize() to size
56 // the bitmap appropriately if needed using factor-of-two expansion.
57 void at_put_grow(idx_t index, bool value);
58
147 verify_index(index);
148 return (*word_addr(index) & bit_mask(index)) != 0;
149 }
150
151 // Align bit index up or down to the next bitmap word boundary, or check
152 // alignment.
153 static idx_t word_align_up(idx_t bit) {
154 return align_size_up(bit, BitsPerWord);
155 }
156 static idx_t word_align_down(idx_t bit) {
157 return align_size_down(bit, BitsPerWord);
158 }
159 static bool is_word_aligned(idx_t bit) {
160 return word_align_up(bit) == bit;
161 }
162
163 // Set or clear the specified bit.
164 inline void set_bit(idx_t bit);
165 inline void clear_bit(idx_t bit);
166
167 // Atomically set or clear the specified bit.
168 inline bool par_set_bit(idx_t bit);
169 inline bool par_clear_bit(idx_t bit);
170
171 // Put the given value at the given offset. The parallel version
172 // will CAS the value into the bitmap and is quite a bit slower.
173 // The parallel version also returns a value indicating if the
174 // calling thread was the one that changed the value of the bit.
175 void at_put(idx_t index, bool value);
176 bool par_at_put(idx_t index, bool value);
177
178 // Update a range of bits. Ranges are half-open [beg, end).
179 void set_range (idx_t beg, idx_t end);
180 void clear_range (idx_t beg, idx_t end);
181 void set_large_range (idx_t beg, idx_t end);
182 void clear_large_range (idx_t beg, idx_t end);
183 void at_put_range(idx_t beg, idx_t end, bool value);
184 void par_at_put_range(idx_t beg, idx_t end, bool value);
185 void at_put_large_range(idx_t beg, idx_t end, bool value);
186 void par_at_put_large_range(idx_t beg, idx_t end, bool value);
253 // (For expedience, currently requires the offset to be aligned to the
254 // bitsize of a uintptr_t. This should go away in the future though it
255 // will probably remain a good case to optimize.)
256 void set_intersection_at_offset(BitMap bits, idx_t offset);
257
258 void set_from(BitMap bits);
259
260 bool is_same(BitMap bits);
261
262 // Test if all bits are set or cleared
263 bool is_full() const;
264 bool is_empty() const;
265
266 void print_on_error(outputStream* st, const char* prefix) const;
267
268 #ifndef PRODUCT
269 public:
270 // Printing
271 void print_on(outputStream* st) const;
272 #endif
273 };
274
275 // Convenience class wrapping BitMap which provides multiple bits per slot.
276 class BitMap2D VALUE_OBJ_CLASS_SPEC {
277 public:
278 typedef BitMap::idx_t idx_t; // Type used for bit and word indices.
279 typedef BitMap::bm_word_t bm_word_t; // Element type of array that
280 // represents the bitmap.
281 private:
282 BitMap _map;
283 idx_t _bits_per_slot;
284
285 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
286 return slot_index * _bits_per_slot + bit_within_slot_index;
287 }
288
289 void verify_bit_within_slot_index(idx_t index) const {
290 assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
291 }
292
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_VM_UTILITIES_BITMAP_HPP
26 #define SHARE_VM_UTILITIES_BITMAP_HPP
27
28 #include "memory/allocation.hpp"
29 #include "utilities/top.hpp"
30
31 // Forward decl;
32 class BitMapClosure;
33
34 // Operations for bitmaps represented as arrays of unsigned integers.
35 // Bit offsets are numbered from 0 to size-1.
36
37 class BitMap VALUE_OBJ_CLASS_SPEC {
38 friend class BitMap2D;
39 friend class BitMapIterator;
40
41 public:
42 typedef size_t idx_t; // Type used for bit and word indices.
43 typedef uintptr_t bm_word_t; // Element type of array that represents
44 // the bitmap.
45
46 // Hints for range sizes.
47 typedef enum {
48 unknown_range, small_range, large_range
49 } RangeSizeHint;
50
51 private:
52 ArrayAllocator<bm_word_t, mtInternal> _map_allocator;
53 bm_word_t* _map; // First word in bitmap
54 idx_t _size; // Size of bitmap (in bits)
55
56 // Puts the given value at the given offset, using resize() to size
57 // the bitmap appropriately if needed using factor-of-two expansion.
58 void at_put_grow(idx_t index, bool value);
59
148 verify_index(index);
149 return (*word_addr(index) & bit_mask(index)) != 0;
150 }
151
152 // Align bit index up or down to the next bitmap word boundary, or check
153 // alignment.
154 static idx_t word_align_up(idx_t bit) {
155 return align_size_up(bit, BitsPerWord);
156 }
157 static idx_t word_align_down(idx_t bit) {
158 return align_size_down(bit, BitsPerWord);
159 }
160 static bool is_word_aligned(idx_t bit) {
161 return word_align_up(bit) == bit;
162 }
163
164 // Set or clear the specified bit.
165 inline void set_bit(idx_t bit);
166 inline void clear_bit(idx_t bit);
167
168 // Set or clear the specified bit with result
169 inline bool set_bit_with_result(idx_t bit);
170 inline bool clear_bit_with_result(idx_t bit);
171
172 // Atomically set or clear the specified bit.
173 inline bool par_set_bit(idx_t bit);
174 inline bool par_clear_bit(idx_t bit);
175
176 // Put the given value at the given offset. The parallel version
177 // will CAS the value into the bitmap and is quite a bit slower.
178 // The parallel version also returns a value indicating if the
179 // calling thread was the one that changed the value of the bit.
180 void at_put(idx_t index, bool value);
181 bool par_at_put(idx_t index, bool value);
182
183 // Update a range of bits. Ranges are half-open [beg, end).
184 void set_range (idx_t beg, idx_t end);
185 void clear_range (idx_t beg, idx_t end);
186 void set_large_range (idx_t beg, idx_t end);
187 void clear_large_range (idx_t beg, idx_t end);
188 void at_put_range(idx_t beg, idx_t end, bool value);
189 void par_at_put_range(idx_t beg, idx_t end, bool value);
190 void at_put_large_range(idx_t beg, idx_t end, bool value);
191 void par_at_put_large_range(idx_t beg, idx_t end, bool value);
258 // (For expedience, currently requires the offset to be aligned to the
259 // bitsize of a uintptr_t. This should go away in the future though it
260 // will probably remain a good case to optimize.)
261 void set_intersection_at_offset(BitMap bits, idx_t offset);
262
263 void set_from(BitMap bits);
264
265 bool is_same(BitMap bits);
266
267 // Test if all bits are set or cleared
268 bool is_full() const;
269 bool is_empty() const;
270
271 void print_on_error(outputStream* st, const char* prefix) const;
272
273 #ifndef PRODUCT
274 public:
275 // Printing
276 void print_on(outputStream* st) const;
277 #endif
278 };
279
280 class BitMapIterator VALUE_OBJ_CLASS_SPEC {
281 friend class BitMap;
282
283 typedef BitMap::idx_t idx_t; // Type used for bit and word indices.
284 typedef BitMap::bm_word_t bm_word_t; // Element type of array that
285 // represents the bitmap.
286
287 // We walk over the bits in a word in chunks of size window_size.
288 enum { window_size = 8,
289 window_mask = right_n_bits(window_size),
290 table_size = (1 << window_size) };
291
292 // For an integer of length window_size, what is the first set bit?
293 static const uint8_t _first_bit[table_size];
294
295 // For an integer of length window_size, what is the first set bit?
296 static const uint8_t _second_bit[table_size];
297
298
299 BitMap* _bs;
300 bm_word_t _current;
301 idx_t _value;
302 idx_t _last_word;
303 idx_t _max_word;
304
305 public:
306 BitMapIterator(BitMap* bs);
307
308 // Return the next element of the set. Return 0 when done.
309
310 uint next();
311 };
312
313 // Convenience class wrapping BitMap which provides multiple bits per slot.
314 class BitMap2D VALUE_OBJ_CLASS_SPEC {
315 public:
316 typedef BitMap::idx_t idx_t; // Type used for bit and word indices.
317 typedef BitMap::bm_word_t bm_word_t; // Element type of array that
318 // represents the bitmap.
319 private:
320 BitMap _map;
321 idx_t _bits_per_slot;
322
323 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
324 return slot_index * _bits_per_slot + bit_within_slot_index;
325 }
326
327 void verify_bit_within_slot_index(idx_t index) const {
328 assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
329 }
330
|