src/share/vm/utilities/bitMap.cpp

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 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_bitMap.cpp.incl"
duke@435 27
duke@435 28
duke@435 29 BitMap::BitMap(idx_t* map, idx_t size_in_bits) {
duke@435 30 assert(size_in_bits >= 0, "just checking");
duke@435 31 _map = map;
duke@435 32 _size = size_in_bits;
duke@435 33 }
duke@435 34
duke@435 35
duke@435 36 BitMap::BitMap(idx_t size_in_bits) {
duke@435 37 assert(size_in_bits >= 0, "just checking");
duke@435 38 _size = size_in_bits;
duke@435 39 _map = NEW_RESOURCE_ARRAY(idx_t, size_in_words());
duke@435 40 }
duke@435 41
duke@435 42
duke@435 43 void BitMap::resize(idx_t size_in_bits) {
duke@435 44 assert(size_in_bits >= 0, "just checking");
duke@435 45 size_t old_size_in_words = size_in_words();
duke@435 46 uintptr_t* old_map = map();
duke@435 47 _size = size_in_bits;
duke@435 48 size_t new_size_in_words = size_in_words();
duke@435 49 _map = NEW_RESOURCE_ARRAY(idx_t, new_size_in_words);
duke@435 50 Copy::disjoint_words((HeapWord*) old_map, (HeapWord*) _map, MIN2(old_size_in_words, new_size_in_words));
duke@435 51 if (new_size_in_words > old_size_in_words) {
duke@435 52 clear_range_of_words(old_size_in_words, size_in_words());
duke@435 53 }
duke@435 54 }
duke@435 55
duke@435 56 // Returns a bit mask for a range of bits [beg, end) within a single word. Each
duke@435 57 // bit in the mask is 0 if the bit is in the range, 1 if not in the range. The
duke@435 58 // returned mask can be used directly to clear the range, or inverted to set the
duke@435 59 // range. Note: end must not be 0.
duke@435 60 inline BitMap::idx_t
duke@435 61 BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
duke@435 62 assert(end != 0, "does not work when end == 0");
duke@435 63 assert(beg == end || word_index(beg) == word_index(end - 1),
duke@435 64 "must be a single-word range");
duke@435 65 idx_t mask = bit_mask(beg) - 1; // low (right) bits
duke@435 66 if (bit_in_word(end) != 0) {
duke@435 67 mask |= ~(bit_mask(end) - 1); // high (left) bits
duke@435 68 }
duke@435 69 return mask;
duke@435 70 }
duke@435 71
duke@435 72 void BitMap::set_range_within_word(idx_t beg, idx_t end) {
duke@435 73 // With a valid range (beg <= end), this test ensures that end != 0, as
duke@435 74 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
duke@435 75 if (beg != end) {
duke@435 76 idx_t mask = inverted_bit_mask_for_range(beg, end);
duke@435 77 *word_addr(beg) |= ~mask;
duke@435 78 }
duke@435 79 }
duke@435 80
duke@435 81 void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
duke@435 82 // With a valid range (beg <= end), this test ensures that end != 0, as
duke@435 83 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
duke@435 84 if (beg != end) {
duke@435 85 idx_t mask = inverted_bit_mask_for_range(beg, end);
duke@435 86 *word_addr(beg) &= mask;
duke@435 87 }
duke@435 88 }
duke@435 89
duke@435 90 void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
duke@435 91 assert(value == 0 || value == 1, "0 for clear, 1 for set");
duke@435 92 // With a valid range (beg <= end), this test ensures that end != 0, as
duke@435 93 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
duke@435 94 if (beg != end) {
duke@435 95 intptr_t* pw = (intptr_t*)word_addr(beg);
duke@435 96 intptr_t w = *pw;
duke@435 97 intptr_t mr = (intptr_t)inverted_bit_mask_for_range(beg, end);
duke@435 98 intptr_t nw = value ? (w | ~mr) : (w & mr);
duke@435 99 while (true) {
duke@435 100 intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
duke@435 101 if (res == w) break;
duke@435 102 w = *pw;
duke@435 103 nw = value ? (w | ~mr) : (w & mr);
duke@435 104 }
duke@435 105 }
duke@435 106 }
duke@435 107
duke@435 108 inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
duke@435 109 memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t));
duke@435 110 }
duke@435 111
duke@435 112 inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
duke@435 113 memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t));
duke@435 114 }
duke@435 115
duke@435 116 inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {
duke@435 117 idx_t bit_rounded_up = bit + (BitsPerWord - 1);
duke@435 118 // Check for integer arithmetic overflow.
duke@435 119 return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();
duke@435 120 }
duke@435 121
duke@435 122 void BitMap::set_range(idx_t beg, idx_t end) {
duke@435 123 verify_range(beg, end);
duke@435 124
duke@435 125 idx_t beg_full_word = word_index_round_up(beg);
duke@435 126 idx_t end_full_word = word_index(end);
duke@435 127
duke@435 128 if (beg_full_word < end_full_word) {
duke@435 129 // The range includes at least one full word.
duke@435 130 set_range_within_word(beg, bit_index(beg_full_word));
duke@435 131 set_range_of_words(beg_full_word, end_full_word);
duke@435 132 set_range_within_word(bit_index(end_full_word), end);
duke@435 133 } else {
duke@435 134 // The range spans at most 2 partial words.
duke@435 135 idx_t boundary = MIN2(bit_index(beg_full_word), end);
duke@435 136 set_range_within_word(beg, boundary);
duke@435 137 set_range_within_word(boundary, end);
duke@435 138 }
duke@435 139 }
duke@435 140
duke@435 141 void BitMap::clear_range(idx_t beg, idx_t end) {
duke@435 142 verify_range(beg, end);
duke@435 143
duke@435 144 idx_t beg_full_word = word_index_round_up(beg);
duke@435 145 idx_t end_full_word = word_index(end);
duke@435 146
duke@435 147 if (beg_full_word < end_full_word) {
duke@435 148 // The range includes at least one full word.
duke@435 149 clear_range_within_word(beg, bit_index(beg_full_word));
duke@435 150 clear_range_of_words(beg_full_word, end_full_word);
duke@435 151 clear_range_within_word(bit_index(end_full_word), end);
duke@435 152 } else {
duke@435 153 // The range spans at most 2 partial words.
duke@435 154 idx_t boundary = MIN2(bit_index(beg_full_word), end);
duke@435 155 clear_range_within_word(beg, boundary);
duke@435 156 clear_range_within_word(boundary, end);
duke@435 157 }
duke@435 158 }
duke@435 159
duke@435 160 void BitMap::set_large_range(idx_t beg, idx_t end) {
duke@435 161 verify_range(beg, end);
duke@435 162
duke@435 163 idx_t beg_full_word = word_index_round_up(beg);
duke@435 164 idx_t end_full_word = word_index(end);
duke@435 165
duke@435 166 assert(end_full_word - beg_full_word >= 32,
duke@435 167 "the range must include at least 32 bytes");
duke@435 168
duke@435 169 // The range includes at least one full word.
duke@435 170 set_range_within_word(beg, bit_index(beg_full_word));
duke@435 171 set_large_range_of_words(beg_full_word, end_full_word);
duke@435 172 set_range_within_word(bit_index(end_full_word), end);
duke@435 173 }
duke@435 174
duke@435 175 void BitMap::clear_large_range(idx_t beg, idx_t end) {
duke@435 176 verify_range(beg, end);
duke@435 177
duke@435 178 idx_t beg_full_word = word_index_round_up(beg);
duke@435 179 idx_t end_full_word = word_index(end);
duke@435 180
duke@435 181 assert(end_full_word - beg_full_word >= 32,
duke@435 182 "the range must include at least 32 bytes");
duke@435 183
duke@435 184 // The range includes at least one full word.
duke@435 185 clear_range_within_word(beg, bit_index(beg_full_word));
duke@435 186 clear_large_range_of_words(beg_full_word, end_full_word);
duke@435 187 clear_range_within_word(bit_index(end_full_word), end);
duke@435 188 }
duke@435 189
duke@435 190 void BitMap::at_put(idx_t offset, bool value) {
duke@435 191 if (value) {
duke@435 192 set_bit(offset);
duke@435 193 } else {
duke@435 194 clear_bit(offset);
duke@435 195 }
duke@435 196 }
duke@435 197
duke@435 198 // Return true to indicate that this thread changed
duke@435 199 // the bit, false to indicate that someone else did.
duke@435 200 // In either case, the requested bit is in the
duke@435 201 // requested state some time during the period that
duke@435 202 // this thread is executing this call. More importantly,
duke@435 203 // if no other thread is executing an action to
duke@435 204 // change the requested bit to a state other than
duke@435 205 // the one that this thread is trying to set it to,
duke@435 206 // then the the bit is in the expected state
duke@435 207 // at exit from this method. However, rather than
duke@435 208 // make such a strong assertion here, based on
duke@435 209 // assuming such constrained use (which though true
duke@435 210 // today, could change in the future to service some
duke@435 211 // funky parallel algorithm), we encourage callers
duke@435 212 // to do such verification, as and when appropriate.
duke@435 213 bool BitMap::par_at_put(idx_t bit, bool value) {
duke@435 214 return value ? par_set_bit(bit) : par_clear_bit(bit);
duke@435 215 }
duke@435 216
duke@435 217 void BitMap::at_put_grow(idx_t offset, bool value) {
duke@435 218 if (offset >= size()) {
duke@435 219 resize(2 * MAX2(size(), offset));
duke@435 220 }
duke@435 221 at_put(offset, value);
duke@435 222 }
duke@435 223
duke@435 224 void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
duke@435 225 if (value) {
duke@435 226 set_range(start_offset, end_offset);
duke@435 227 } else {
duke@435 228 clear_range(start_offset, end_offset);
duke@435 229 }
duke@435 230 }
duke@435 231
duke@435 232 void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
duke@435 233 verify_range(beg, end);
duke@435 234
duke@435 235 idx_t beg_full_word = word_index_round_up(beg);
duke@435 236 idx_t end_full_word = word_index(end);
duke@435 237
duke@435 238 if (beg_full_word < end_full_word) {
duke@435 239 // The range includes at least one full word.
duke@435 240 par_put_range_within_word(beg, bit_index(beg_full_word), value);
duke@435 241 if (value) {
duke@435 242 set_range_of_words(beg_full_word, end_full_word);
duke@435 243 } else {
duke@435 244 clear_range_of_words(beg_full_word, end_full_word);
duke@435 245 }
duke@435 246 par_put_range_within_word(bit_index(end_full_word), end, value);
duke@435 247 } else {
duke@435 248 // The range spans at most 2 partial words.
duke@435 249 idx_t boundary = MIN2(bit_index(beg_full_word), end);
duke@435 250 par_put_range_within_word(beg, boundary, value);
duke@435 251 par_put_range_within_word(boundary, end, value);
duke@435 252 }
duke@435 253
duke@435 254 }
duke@435 255
duke@435 256 void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
duke@435 257 if (value) {
duke@435 258 set_large_range(beg, end);
duke@435 259 } else {
duke@435 260 clear_large_range(beg, end);
duke@435 261 }
duke@435 262 }
duke@435 263
duke@435 264 void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
duke@435 265 verify_range(beg, end);
duke@435 266
duke@435 267 idx_t beg_full_word = word_index_round_up(beg);
duke@435 268 idx_t end_full_word = word_index(end);
duke@435 269
duke@435 270 assert(end_full_word - beg_full_word >= 32,
duke@435 271 "the range must include at least 32 bytes");
duke@435 272
duke@435 273 // The range includes at least one full word.
duke@435 274 par_put_range_within_word(beg, bit_index(beg_full_word), value);
duke@435 275 if (value) {
duke@435 276 set_large_range_of_words(beg_full_word, end_full_word);
duke@435 277 } else {
duke@435 278 clear_large_range_of_words(beg_full_word, end_full_word);
duke@435 279 }
duke@435 280 par_put_range_within_word(bit_index(end_full_word), end, value);
duke@435 281 }
duke@435 282
duke@435 283 bool BitMap::contains(const BitMap other) const {
duke@435 284 assert(size() == other.size(), "must have same size");
duke@435 285 uintptr_t* dest_map = map();
duke@435 286 uintptr_t* other_map = other.map();
duke@435 287 idx_t size = size_in_words();
duke@435 288 for (idx_t index = 0; index < size_in_words(); index++) {
duke@435 289 uintptr_t word_union = dest_map[index] | other_map[index];
duke@435 290 // If this has more bits set than dest_map[index], then other is not a
duke@435 291 // subset.
duke@435 292 if (word_union != dest_map[index]) return false;
duke@435 293 }
duke@435 294 return true;
duke@435 295 }
duke@435 296
duke@435 297 bool BitMap::intersects(const BitMap other) const {
duke@435 298 assert(size() == other.size(), "must have same size");
duke@435 299 uintptr_t* dest_map = map();
duke@435 300 uintptr_t* other_map = other.map();
duke@435 301 idx_t size = size_in_words();
duke@435 302 for (idx_t index = 0; index < size_in_words(); index++) {
duke@435 303 if ((dest_map[index] & other_map[index]) != 0) return true;
duke@435 304 }
duke@435 305 // Otherwise, no intersection.
duke@435 306 return false;
duke@435 307 }
duke@435 308
duke@435 309 void BitMap::set_union(BitMap other) {
duke@435 310 assert(size() == other.size(), "must have same size");
duke@435 311 idx_t* dest_map = map();
duke@435 312 idx_t* other_map = other.map();
duke@435 313 idx_t size = size_in_words();
duke@435 314 for (idx_t index = 0; index < size_in_words(); index++) {
duke@435 315 dest_map[index] = dest_map[index] | other_map[index];
duke@435 316 }
duke@435 317 }
duke@435 318
duke@435 319
duke@435 320 void BitMap::set_difference(BitMap other) {
duke@435 321 assert(size() == other.size(), "must have same size");
duke@435 322 idx_t* dest_map = map();
duke@435 323 idx_t* other_map = other.map();
duke@435 324 idx_t size = size_in_words();
duke@435 325 for (idx_t index = 0; index < size_in_words(); index++) {
duke@435 326 dest_map[index] = dest_map[index] & ~(other_map[index]);
duke@435 327 }
duke@435 328 }
duke@435 329
duke@435 330
duke@435 331 void BitMap::set_intersection(BitMap other) {
duke@435 332 assert(size() == other.size(), "must have same size");
duke@435 333 idx_t* dest_map = map();
duke@435 334 idx_t* other_map = other.map();
duke@435 335 idx_t size = size_in_words();
duke@435 336 for (idx_t index = 0; index < size; index++) {
duke@435 337 dest_map[index] = dest_map[index] & other_map[index];
duke@435 338 }
duke@435 339 }
duke@435 340
duke@435 341
duke@435 342 bool BitMap::set_union_with_result(BitMap other) {
duke@435 343 assert(size() == other.size(), "must have same size");
duke@435 344 bool changed = false;
duke@435 345 idx_t* dest_map = map();
duke@435 346 idx_t* other_map = other.map();
duke@435 347 idx_t size = size_in_words();
duke@435 348 for (idx_t index = 0; index < size; index++) {
duke@435 349 idx_t temp = map(index) | other_map[index];
duke@435 350 changed = changed || (temp != map(index));
duke@435 351 map()[index] = temp;
duke@435 352 }
duke@435 353 return changed;
duke@435 354 }
duke@435 355
duke@435 356
duke@435 357 bool BitMap::set_difference_with_result(BitMap other) {
duke@435 358 assert(size() == other.size(), "must have same size");
duke@435 359 bool changed = false;
duke@435 360 idx_t* dest_map = map();
duke@435 361 idx_t* other_map = other.map();
duke@435 362 idx_t size = size_in_words();
duke@435 363 for (idx_t index = 0; index < size; index++) {
duke@435 364 idx_t temp = dest_map[index] & ~(other_map[index]);
duke@435 365 changed = changed || (temp != dest_map[index]);
duke@435 366 dest_map[index] = temp;
duke@435 367 }
duke@435 368 return changed;
duke@435 369 }
duke@435 370
duke@435 371
duke@435 372 bool BitMap::set_intersection_with_result(BitMap other) {
duke@435 373 assert(size() == other.size(), "must have same size");
duke@435 374 bool changed = false;
duke@435 375 idx_t* dest_map = map();
duke@435 376 idx_t* other_map = other.map();
duke@435 377 idx_t size = size_in_words();
duke@435 378 for (idx_t index = 0; index < size; index++) {
duke@435 379 idx_t orig = dest_map[index];
duke@435 380 idx_t temp = orig & other_map[index];
duke@435 381 changed = changed || (temp != orig);
duke@435 382 dest_map[index] = temp;
duke@435 383 }
duke@435 384 return changed;
duke@435 385 }
duke@435 386
duke@435 387
duke@435 388 void BitMap::set_from(BitMap other) {
duke@435 389 assert(size() == other.size(), "must have same size");
duke@435 390 idx_t* dest_map = map();
duke@435 391 idx_t* other_map = other.map();
duke@435 392 idx_t size = size_in_words();
duke@435 393 for (idx_t index = 0; index < size; index++) {
duke@435 394 dest_map[index] = other_map[index];
duke@435 395 }
duke@435 396 }
duke@435 397
duke@435 398
duke@435 399 bool BitMap::is_same(BitMap other) {
duke@435 400 assert(size() == other.size(), "must have same size");
duke@435 401 idx_t* dest_map = map();
duke@435 402 idx_t* other_map = other.map();
duke@435 403 idx_t size = size_in_words();
duke@435 404 for (idx_t index = 0; index < size; index++) {
duke@435 405 if (dest_map[index] != other_map[index]) return false;
duke@435 406 }
duke@435 407 return true;
duke@435 408 }
duke@435 409
duke@435 410 bool BitMap::is_full() const {
duke@435 411 uintptr_t* word = map();
duke@435 412 idx_t rest = size();
duke@435 413 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
duke@435 414 if (*word != (uintptr_t) AllBits) return false;
duke@435 415 word++;
duke@435 416 }
duke@435 417 return rest == 0 || (*word | ~right_n_bits((int)rest)) == (uintptr_t) AllBits;
duke@435 418 }
duke@435 419
duke@435 420
duke@435 421 bool BitMap::is_empty() const {
duke@435 422 uintptr_t* word = map();
duke@435 423 idx_t rest = size();
duke@435 424 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
duke@435 425 if (*word != (uintptr_t) NoBits) return false;
duke@435 426 word++;
duke@435 427 }
duke@435 428 return rest == 0 || (*word & right_n_bits((int)rest)) == (uintptr_t) NoBits;
duke@435 429 }
duke@435 430
duke@435 431 void BitMap::clear_large() {
duke@435 432 clear_large_range_of_words(0, size_in_words());
duke@435 433 }
duke@435 434
duke@435 435 // Note that if the closure itself modifies the bitmap
duke@435 436 // then modifications in and to the left of the _bit_ being
duke@435 437 // currently sampled will not be seen. Note also that the
duke@435 438 // interval [leftOffset, rightOffset) is right open.
duke@435 439 void BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
duke@435 440 verify_range(leftOffset, rightOffset);
duke@435 441
duke@435 442 idx_t startIndex = word_index(leftOffset);
duke@435 443 idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words());
duke@435 444 for (idx_t index = startIndex, offset = leftOffset;
duke@435 445 offset < rightOffset && index < endIndex;
duke@435 446 offset = (++index) << LogBitsPerWord) {
duke@435 447 idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
duke@435 448 for (; offset < rightOffset && rest != (uintptr_t)NoBits; offset++) {
duke@435 449 if (rest & 1) {
duke@435 450 blk->do_bit(offset);
duke@435 451 // resample at each closure application
duke@435 452 // (see, for instance, CMS bug 4525989)
duke@435 453 rest = map(index) >> (offset & (BitsPerWord -1));
duke@435 454 // XXX debugging: remove
duke@435 455 // The following assertion assumes that closure application
duke@435 456 // doesn't clear bits (may not be true in general, e.g. G1).
duke@435 457 assert(rest & 1,
duke@435 458 "incorrect shift or closure application can clear bits?");
duke@435 459 }
duke@435 460 rest = rest >> 1;
duke@435 461 }
duke@435 462 }
duke@435 463 }
duke@435 464
duke@435 465 BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset,
duke@435 466 idx_t r_offset) const {
duke@435 467 assert(l_offset <= size(), "BitMap index out of bounds");
duke@435 468 assert(r_offset <= size(), "BitMap index out of bounds");
duke@435 469 assert(l_offset <= r_offset, "l_offset > r_offset ?");
duke@435 470
duke@435 471 if (l_offset == r_offset) {
duke@435 472 return l_offset;
duke@435 473 }
duke@435 474 idx_t index = word_index(l_offset);
duke@435 475 idx_t r_index = word_index(r_offset-1) + 1;
duke@435 476 idx_t res_offset = l_offset;
duke@435 477
duke@435 478 // check bits including and to the _left_ of offset's position
duke@435 479 idx_t pos = bit_in_word(res_offset);
duke@435 480 idx_t res = map(index) >> pos;
duke@435 481 if (res != (uintptr_t)NoBits) {
duke@435 482 // find the position of the 1-bit
duke@435 483 for (; !(res & 1); res_offset++) {
duke@435 484 res = res >> 1;
duke@435 485 }
duke@435 486 assert(res_offset >= l_offset, "just checking");
duke@435 487 return MIN2(res_offset, r_offset);
duke@435 488 }
duke@435 489 // skip over all word length 0-bit runs
duke@435 490 for (index++; index < r_index; index++) {
duke@435 491 res = map(index);
duke@435 492 if (res != (uintptr_t)NoBits) {
duke@435 493 // found a 1, return the offset
duke@435 494 for (res_offset = index << LogBitsPerWord; !(res & 1);
duke@435 495 res_offset++) {
duke@435 496 res = res >> 1;
duke@435 497 }
duke@435 498 assert(res & 1, "tautology; see loop condition");
duke@435 499 assert(res_offset >= l_offset, "just checking");
duke@435 500 return MIN2(res_offset, r_offset);
duke@435 501 }
duke@435 502 }
duke@435 503 return r_offset;
duke@435 504 }
duke@435 505
duke@435 506 BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset,
duke@435 507 idx_t r_offset) const {
duke@435 508 assert(l_offset <= size(), "BitMap index out of bounds");
duke@435 509 assert(r_offset <= size(), "BitMap index out of bounds");
duke@435 510 assert(l_offset <= r_offset, "l_offset > r_offset ?");
duke@435 511
duke@435 512 if (l_offset == r_offset) {
duke@435 513 return l_offset;
duke@435 514 }
duke@435 515 idx_t index = word_index(l_offset);
duke@435 516 idx_t r_index = word_index(r_offset-1) + 1;
duke@435 517 idx_t res_offset = l_offset;
duke@435 518
duke@435 519 // check bits including and to the _left_ of offset's position
duke@435 520 idx_t pos = res_offset & (BitsPerWord - 1);
duke@435 521 idx_t res = (map(index) >> pos) | left_n_bits((int)pos);
duke@435 522
duke@435 523 if (res != (uintptr_t)AllBits) {
duke@435 524 // find the position of the 0-bit
duke@435 525 for (; res & 1; res_offset++) {
duke@435 526 res = res >> 1;
duke@435 527 }
duke@435 528 assert(res_offset >= l_offset, "just checking");
duke@435 529 return MIN2(res_offset, r_offset);
duke@435 530 }
duke@435 531 // skip over all word length 1-bit runs
duke@435 532 for (index++; index < r_index; index++) {
duke@435 533 res = map(index);
duke@435 534 if (res != (uintptr_t)AllBits) {
duke@435 535 // found a 0, return the offset
duke@435 536 for (res_offset = index << LogBitsPerWord; res & 1;
duke@435 537 res_offset++) {
duke@435 538 res = res >> 1;
duke@435 539 }
duke@435 540 assert(!(res & 1), "tautology; see loop condition");
duke@435 541 assert(res_offset >= l_offset, "just checking");
duke@435 542 return MIN2(res_offset, r_offset);
duke@435 543 }
duke@435 544 }
duke@435 545 return r_offset;
duke@435 546 }
duke@435 547
duke@435 548 #ifndef PRODUCT
duke@435 549
duke@435 550 void BitMap::print_on(outputStream* st) const {
duke@435 551 tty->print("Bitmap(%d):", size());
duke@435 552 for (idx_t index = 0; index < size(); index++) {
duke@435 553 tty->print("%c", at(index) ? '1' : '0');
duke@435 554 }
duke@435 555 tty->cr();
duke@435 556 }
duke@435 557
duke@435 558 #endif
duke@435 559
duke@435 560
duke@435 561 BitMap2D::BitMap2D(uintptr_t* map, idx_t size_in_slots, idx_t bits_per_slot)
duke@435 562 : _bits_per_slot(bits_per_slot)
duke@435 563 , _map(map, size_in_slots * bits_per_slot)
duke@435 564 {
duke@435 565 }
duke@435 566
duke@435 567
duke@435 568 BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
duke@435 569 : _bits_per_slot(bits_per_slot)
duke@435 570 , _map(size_in_slots * bits_per_slot)
duke@435 571 {
duke@435 572 }

mercurial