src/share/vm/utilities/bitMap.cpp

Mon, 09 Aug 2010 17:51:56 -0700

author
never
date
Mon, 09 Aug 2010 17:51:56 -0700
changeset 2044
f4f596978298
parent 1907
c18cbe5936b8
child 2314
f95d63e2154a
permissions
-rw-r--r--

Merge

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

mercurial