src/share/vm/utilities/bitMap.hpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 777
37f87013dfd8
permissions
-rw-r--r--

Initial load

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

mercurial