annotate src/share/vm/utilities/bitMap.hpp @ 0:a61af66fc99e

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