1 /* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 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 #include "precompiled.hpp" 26 #include "memory/allocation.inline.hpp" 27 #include "memory/resourceArea.hpp" 28 #include "runtime/atomic.hpp" 29 #include "utilities/bitMap.inline.hpp" 30 #include "utilities/copy.hpp" 31 #include "utilities/debug.hpp" 32 33 STATIC_ASSERT(sizeof(BitMap::bm_word_t) == BytesPerWord); // "Implementation assumption." 34 35 typedef BitMap::bm_word_t bm_word_t; 36 typedef BitMap::idx_t idx_t; 37 38 class ResourceBitMapAllocator : StackObj { 39 public: 40 bm_word_t* allocate(idx_t size_in_words) const { 41 return NEW_RESOURCE_ARRAY(bm_word_t, size_in_words); 42 } 43 void free(bm_word_t* map, idx_t size_in_words) const { 44 // Don't free resource allocated arrays. 45 } 46 }; 47 48 class CHeapBitMapAllocator : StackObj { 49 public: 50 bm_word_t* allocate(size_t size_in_words) const { 51 return ArrayAllocator<bm_word_t, mtInternal>::allocate(size_in_words); 52 } 53 void free(bm_word_t* map, idx_t size_in_words) const { 54 ArrayAllocator<bm_word_t, mtInternal>::free(map, size_in_words); 55 } 56 }; 57 58 class ArenaBitMapAllocator : StackObj { 59 Arena* _arena; 60 61 public: 62 ArenaBitMapAllocator(Arena* arena) : _arena(arena) {} 63 bm_word_t* allocate(idx_t size_in_words) const { 64 return (bm_word_t*)_arena->Amalloc(size_in_words * BytesPerWord); 65 } 66 void free(bm_word_t* map, idx_t size_in_words) const { 67 // ArenaBitMaps currently don't free memory. 68 } 69 }; 70 71 template <class Allocator> 72 BitMap::bm_word_t* BitMap::reallocate(const Allocator& allocator, bm_word_t* old_map, idx_t old_size_in_bits, idx_t new_size_in_bits) { 73 size_t old_size_in_words = calc_size_in_words(old_size_in_bits); 74 size_t new_size_in_words = calc_size_in_words(new_size_in_bits); 75 76 bm_word_t* map = NULL; 77 78 if (new_size_in_words > 0) { 79 map = allocator.allocate(new_size_in_words); 80 81 Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) map, 82 MIN2(old_size_in_words, new_size_in_words)); 83 84 if (new_size_in_words > old_size_in_words) { 85 clear_range_of_words(map, old_size_in_words, new_size_in_words); 86 } 87 } 88 89 if (old_map != NULL) { 90 allocator.free(old_map, old_size_in_words); 91 } 92 93 return map; 94 } 95 96 template <class Allocator> 97 bm_word_t* BitMap::allocate(const Allocator& allocator, idx_t size_in_bits) { 98 // Reuse reallocate to ensure that the new memory is cleared. 99 return reallocate(allocator, NULL, 0, size_in_bits); 100 } 101 102 template <class Allocator> 103 void BitMap::free(const Allocator& allocator, bm_word_t* map, idx_t size_in_bits) { 104 bm_word_t* ret = reallocate(allocator, map, size_in_bits, 0); 105 assert(ret == NULL, "Reallocate shouldn't have allocated"); 106 } 107 108 template <class Allocator> 109 void BitMap::resize(const Allocator& allocator, idx_t new_size_in_bits) { 110 bm_word_t* new_map = reallocate(allocator, map(), size(), new_size_in_bits); 111 112 update(new_map, new_size_in_bits); 113 } 114 115 template <class Allocator> 116 void BitMap::initialize(const Allocator& allocator, idx_t size_in_bits) { 117 assert(map() == NULL, "precondition"); 118 assert(size() == 0, "precondition"); 119 120 resize(allocator, size_in_bits); 121 } 122 123 template <class Allocator> 124 void BitMap::reinitialize(const Allocator& allocator, idx_t new_size_in_bits) { 125 // Remove previous bits. 126 resize(allocator, 0); 127 128 initialize(allocator, new_size_in_bits); 129 } 130 131 ResourceBitMap::ResourceBitMap(idx_t size_in_bits) 132 : BitMap(allocate(ResourceBitMapAllocator(), size_in_bits), size_in_bits) { 133 } 134 135 void ResourceBitMap::resize(idx_t new_size_in_bits) { 136 BitMap::resize(ResourceBitMapAllocator(), new_size_in_bits); 137 } 138 139 void ResourceBitMap::initialize(idx_t size_in_bits) { 140 BitMap::initialize(ResourceBitMapAllocator(), size_in_bits); 141 } 142 143 void ResourceBitMap::reinitialize(idx_t size_in_bits) { 144 BitMap::reinitialize(ResourceBitMapAllocator(), size_in_bits); 145 } 146 147 ArenaBitMap::ArenaBitMap(Arena* arena, idx_t size_in_bits) 148 : BitMap(allocate(ArenaBitMapAllocator(arena), size_in_bits), size_in_bits) { 149 } 150 151 CHeapBitMap::CHeapBitMap(idx_t size_in_bits) 152 : BitMap(allocate(CHeapBitMapAllocator(), size_in_bits), size_in_bits) { 153 } 154 155 CHeapBitMap::~CHeapBitMap() { 156 free(CHeapBitMapAllocator(), map(), size()); 157 } 158 159 void CHeapBitMap::resize(idx_t new_size_in_bits) { 160 BitMap::resize(CHeapBitMapAllocator(), new_size_in_bits); 161 } 162 163 void CHeapBitMap::initialize(idx_t size_in_bits) { 164 BitMap::initialize(CHeapBitMapAllocator(), size_in_bits); 165 } 166 167 void CHeapBitMap::reinitialize(idx_t size_in_bits) { 168 BitMap::reinitialize(CHeapBitMapAllocator(), size_in_bits); 169 } 170 171 #ifdef ASSERT 172 void BitMap::verify_index(idx_t index) const { 173 assert(index < _size, "BitMap index out of bounds"); 174 } 175 176 void BitMap::verify_range(idx_t beg_index, idx_t end_index) const { 177 assert(beg_index <= end_index, "BitMap range error"); 178 // Note that [0,0) and [size,size) are both valid ranges. 179 if (end_index != _size) verify_index(end_index); 180 } 181 #endif // #ifdef ASSERT 182 183 void BitMap::pretouch() { 184 os::pretouch_memory(word_addr(0), word_addr(size())); 185 } 186 187 void BitMap::set_range_within_word(idx_t beg, idx_t end) { 188 // With a valid range (beg <= end), this test ensures that end != 0, as 189 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write. 190 if (beg != end) { 191 bm_word_t mask = inverted_bit_mask_for_range(beg, end); 192 *word_addr(beg) |= ~mask; 193 } 194 } 195 196 void BitMap::clear_range_within_word(idx_t beg, idx_t end) { 197 // With a valid range (beg <= end), this test ensures that end != 0, as 198 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write. 199 if (beg != end) { 200 bm_word_t mask = inverted_bit_mask_for_range(beg, end); 201 *word_addr(beg) &= mask; 202 } 203 } 204 205 void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) { 206 assert(value == 0 || value == 1, "0 for clear, 1 for set"); 207 // With a valid range (beg <= end), this test ensures that end != 0, as 208 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write. 209 if (beg != end) { 210 intptr_t* pw = (intptr_t*)word_addr(beg); 211 intptr_t w = *pw; 212 intptr_t mr = (intptr_t)inverted_bit_mask_for_range(beg, end); 213 intptr_t nw = value ? (w | ~mr) : (w & mr); 214 while (true) { 215 intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w); 216 if (res == w) break; 217 w = res; 218 nw = value ? (w | ~mr) : (w & mr); 219 } 220 } 221 } 222 223 void BitMap::set_range(idx_t beg, idx_t end) { 224 verify_range(beg, end); 225 226 idx_t beg_full_word = word_index_round_up(beg); 227 idx_t end_full_word = word_index(end); 228 229 if (beg_full_word < end_full_word) { 230 // The range includes at least one full word. 231 set_range_within_word(beg, bit_index(beg_full_word)); 232 set_range_of_words(beg_full_word, end_full_word); 233 set_range_within_word(bit_index(end_full_word), end); 234 } else { 235 // The range spans at most 2 partial words. 236 idx_t boundary = MIN2(bit_index(beg_full_word), end); 237 set_range_within_word(beg, boundary); 238 set_range_within_word(boundary, end); 239 } 240 } 241 242 void BitMap::clear_range(idx_t beg, idx_t end) { 243 verify_range(beg, end); 244 245 idx_t beg_full_word = word_index_round_up(beg); 246 idx_t end_full_word = word_index(end); 247 248 if (beg_full_word < end_full_word) { 249 // The range includes at least one full word. 250 clear_range_within_word(beg, bit_index(beg_full_word)); 251 clear_range_of_words(beg_full_word, end_full_word); 252 clear_range_within_word(bit_index(end_full_word), end); 253 } else { 254 // The range spans at most 2 partial words. 255 idx_t boundary = MIN2(bit_index(beg_full_word), end); 256 clear_range_within_word(beg, boundary); 257 clear_range_within_word(boundary, end); 258 } 259 } 260 261 void BitMap::set_large_range(idx_t beg, idx_t end) { 262 verify_range(beg, end); 263 264 idx_t beg_full_word = word_index_round_up(beg); 265 idx_t end_full_word = word_index(end); 266 267 assert(end_full_word - beg_full_word >= 32, 268 "the range must include at least 32 bytes"); 269 270 // The range includes at least one full word. 271 set_range_within_word(beg, bit_index(beg_full_word)); 272 set_large_range_of_words(beg_full_word, end_full_word); 273 set_range_within_word(bit_index(end_full_word), end); 274 } 275 276 void BitMap::clear_large_range(idx_t beg, idx_t end) { 277 verify_range(beg, end); 278 279 idx_t beg_full_word = word_index_round_up(beg); 280 idx_t end_full_word = word_index(end); 281 282 if (end_full_word - beg_full_word < 32) { 283 clear_range(beg, end); 284 return; 285 } 286 287 // The range includes at least one full word. 288 clear_range_within_word(beg, bit_index(beg_full_word)); 289 clear_large_range_of_words(beg_full_word, end_full_word); 290 clear_range_within_word(bit_index(end_full_word), end); 291 } 292 293 void BitMap::at_put(idx_t offset, bool value) { 294 if (value) { 295 set_bit(offset); 296 } else { 297 clear_bit(offset); 298 } 299 } 300 301 // Return true to indicate that this thread changed 302 // the bit, false to indicate that someone else did. 303 // In either case, the requested bit is in the 304 // requested state some time during the period that 305 // this thread is executing this call. More importantly, 306 // if no other thread is executing an action to 307 // change the requested bit to a state other than 308 // the one that this thread is trying to set it to, 309 // then the the bit is in the expected state 310 // at exit from this method. However, rather than 311 // make such a strong assertion here, based on 312 // assuming such constrained use (which though true 313 // today, could change in the future to service some 314 // funky parallel algorithm), we encourage callers 315 // to do such verification, as and when appropriate. 316 bool BitMap::par_at_put(idx_t bit, bool value) { 317 return value ? par_set_bit(bit) : par_clear_bit(bit); 318 } 319 320 void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) { 321 if (value) { 322 set_range(start_offset, end_offset); 323 } else { 324 clear_range(start_offset, end_offset); 325 } 326 } 327 328 void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) { 329 verify_range(beg, end); 330 331 idx_t beg_full_word = word_index_round_up(beg); 332 idx_t end_full_word = word_index(end); 333 334 if (beg_full_word < end_full_word) { 335 // The range includes at least one full word. 336 par_put_range_within_word(beg, bit_index(beg_full_word), value); 337 if (value) { 338 set_range_of_words(beg_full_word, end_full_word); 339 } else { 340 clear_range_of_words(beg_full_word, end_full_word); 341 } 342 par_put_range_within_word(bit_index(end_full_word), end, value); 343 } else { 344 // The range spans at most 2 partial words. 345 idx_t boundary = MIN2(bit_index(beg_full_word), end); 346 par_put_range_within_word(beg, boundary, value); 347 par_put_range_within_word(boundary, end, value); 348 } 349 350 } 351 352 void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) { 353 if (value) { 354 set_large_range(beg, end); 355 } else { 356 clear_large_range(beg, end); 357 } 358 } 359 360 void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) { 361 verify_range(beg, end); 362 363 idx_t beg_full_word = word_index_round_up(beg); 364 idx_t end_full_word = word_index(end); 365 366 assert(end_full_word - beg_full_word >= 32, 367 "the range must include at least 32 bytes"); 368 369 // The range includes at least one full word. 370 par_put_range_within_word(beg, bit_index(beg_full_word), value); 371 if (value) { 372 set_large_range_of_words(beg_full_word, end_full_word); 373 } else { 374 clear_large_range_of_words(beg_full_word, end_full_word); 375 } 376 par_put_range_within_word(bit_index(end_full_word), end, value); 377 } 378 379 inline bm_word_t tail_mask(idx_t tail_bits) { 380 assert(tail_bits != 0, "precondition"); // Works, but shouldn't be called. 381 assert(tail_bits < (idx_t)BitsPerWord, "precondition"); 382 return (bm_word_t(1) << tail_bits) - 1; 383 } 384 385 // Get the low tail_bits of value, which is the last partial word of a map. 386 inline bm_word_t tail_of_map(bm_word_t value, idx_t tail_bits) { 387 return value & tail_mask(tail_bits); 388 } 389 390 // Compute the new last word of a map with a non-aligned length. 391 // new_value has the new trailing bits of the map in the low tail_bits. 392 // old_value is the last word of the map, including bits beyond the end. 393 // Returns old_value with the low tail_bits replaced by the corresponding 394 // bits in new_value. 395 inline bm_word_t merge_tail_of_map(bm_word_t new_value, 396 bm_word_t old_value, 397 idx_t tail_bits) { 398 bm_word_t mask = tail_mask(tail_bits); 399 return (new_value & mask) | (old_value & ~mask); 400 } 401 402 bool BitMap::contains(const BitMap& other) const { 403 assert(size() == other.size(), "must have same size"); 404 const bm_word_t* dest_map = map(); 405 const bm_word_t* other_map = other.map(); 406 idx_t limit = word_index(size()); 407 for (idx_t index = 0; index < limit; ++index) { 408 // false if other bitmap has bits set which are clear in this bitmap. 409 if ((~dest_map[index] & other_map[index]) != 0) return false; 410 } 411 idx_t rest = bit_in_word(size()); 412 // true unless there is a partial-word tail in which the other 413 // bitmap has bits set which are clear in this bitmap. 414 return (rest == 0) || tail_of_map(~dest_map[limit] & other_map[limit], rest) == 0; 415 } 416 417 bool BitMap::intersects(const BitMap& other) const { 418 assert(size() == other.size(), "must have same size"); 419 const bm_word_t* dest_map = map(); 420 const bm_word_t* other_map = other.map(); 421 idx_t limit = word_index(size()); 422 for (idx_t index = 0; index < limit; ++index) { 423 if ((dest_map[index] & other_map[index]) != 0) return true; 424 } 425 idx_t rest = bit_in_word(size()); 426 // false unless there is a partial-word tail with non-empty intersection. 427 return (rest > 0) && tail_of_map(dest_map[limit] & other_map[limit], rest) != 0; 428 } 429 430 void BitMap::set_union(const BitMap& other) { 431 assert(size() == other.size(), "must have same size"); 432 bm_word_t* dest_map = map(); 433 const bm_word_t* other_map = other.map(); 434 idx_t limit = word_index(size()); 435 for (idx_t index = 0; index < limit; ++index) { 436 dest_map[index] |= other_map[index]; 437 } 438 idx_t rest = bit_in_word(size()); 439 if (rest > 0) { 440 bm_word_t orig = dest_map[limit]; 441 dest_map[limit] = merge_tail_of_map(orig | other_map[limit], orig, rest); 442 } 443 } 444 445 void BitMap::set_difference(const BitMap& other) { 446 assert(size() == other.size(), "must have same size"); 447 bm_word_t* dest_map = map(); 448 const bm_word_t* other_map = other.map(); 449 idx_t limit = word_index(size()); 450 for (idx_t index = 0; index < limit; ++index) { 451 dest_map[index] &= ~other_map[index]; 452 } 453 idx_t rest = bit_in_word(size()); 454 if (rest > 0) { 455 bm_word_t orig = dest_map[limit]; 456 dest_map[limit] = merge_tail_of_map(orig & ~other_map[limit], orig, rest); 457 } 458 } 459 460 void BitMap::set_intersection(const BitMap& other) { 461 assert(size() == other.size(), "must have same size"); 462 bm_word_t* dest_map = map(); 463 const bm_word_t* other_map = other.map(); 464 idx_t limit = word_index(size()); 465 for (idx_t index = 0; index < limit; ++index) { 466 dest_map[index] &= other_map[index]; 467 } 468 idx_t rest = bit_in_word(size()); 469 if (rest > 0) { 470 bm_word_t orig = dest_map[limit]; 471 dest_map[limit] = merge_tail_of_map(orig & other_map[limit], orig, rest); 472 } 473 } 474 475 bool BitMap::set_union_with_result(const BitMap& other) { 476 assert(size() == other.size(), "must have same size"); 477 bool changed = false; 478 bm_word_t* dest_map = map(); 479 const bm_word_t* other_map = other.map(); 480 idx_t limit = word_index(size()); 481 for (idx_t index = 0; index < limit; ++index) { 482 bm_word_t orig = dest_map[index]; 483 bm_word_t temp = orig | other_map[index]; 484 changed = changed || (temp != orig); 485 dest_map[index] = temp; 486 } 487 idx_t rest = bit_in_word(size()); 488 if (rest > 0) { 489 bm_word_t orig = dest_map[limit]; 490 bm_word_t temp = merge_tail_of_map(orig | other_map[limit], orig, rest); 491 changed = changed || (temp != orig); 492 dest_map[limit] = temp; 493 } 494 return changed; 495 } 496 497 bool BitMap::set_difference_with_result(const BitMap& other) { 498 assert(size() == other.size(), "must have same size"); 499 bool changed = false; 500 bm_word_t* dest_map = map(); 501 const bm_word_t* other_map = other.map(); 502 idx_t limit = word_index(size()); 503 for (idx_t index = 0; index < limit; ++index) { 504 bm_word_t orig = dest_map[index]; 505 bm_word_t temp = orig & ~other_map[index]; 506 changed = changed || (temp != orig); 507 dest_map[index] = temp; 508 } 509 idx_t rest = bit_in_word(size()); 510 if (rest > 0) { 511 bm_word_t orig = dest_map[limit]; 512 bm_word_t temp = merge_tail_of_map(orig & ~other_map[limit], orig, rest); 513 changed = changed || (temp != orig); 514 dest_map[limit] = temp; 515 } 516 return changed; 517 } 518 519 bool BitMap::set_intersection_with_result(const BitMap& other) { 520 assert(size() == other.size(), "must have same size"); 521 bool changed = false; 522 bm_word_t* dest_map = map(); 523 const bm_word_t* other_map = other.map(); 524 idx_t limit = word_index(size()); 525 for (idx_t index = 0; index < limit; ++index) { 526 bm_word_t orig = dest_map[index]; 527 bm_word_t temp = orig & other_map[index]; 528 changed = changed || (temp != orig); 529 dest_map[index] = temp; 530 } 531 idx_t rest = bit_in_word(size()); 532 if (rest > 0) { 533 bm_word_t orig = dest_map[limit]; 534 bm_word_t temp = merge_tail_of_map(orig & other_map[limit], orig, rest); 535 changed = changed || (temp != orig); 536 dest_map[limit] = temp; 537 } 538 return changed; 539 } 540 541 void BitMap::set_from(const BitMap& other) { 542 assert(size() == other.size(), "must have same size"); 543 bm_word_t* dest_map = map(); 544 const bm_word_t* other_map = other.map(); 545 idx_t copy_words = word_index(size()); 546 Copy::disjoint_words((HeapWord*)other_map, (HeapWord*)dest_map, copy_words); 547 idx_t rest = bit_in_word(size()); 548 if (rest > 0) { 549 dest_map[copy_words] = merge_tail_of_map(other_map[copy_words], 550 dest_map[copy_words], 551 rest); 552 } 553 } 554 555 bool BitMap::is_same(const BitMap& other) const { 556 assert(size() == other.size(), "must have same size"); 557 const bm_word_t* dest_map = map(); 558 const bm_word_t* other_map = other.map(); 559 idx_t limit = word_index(size()); 560 for (idx_t index = 0; index < limit; ++index) { 561 if (dest_map[index] != other_map[index]) return false; 562 } 563 idx_t rest = bit_in_word(size()); 564 return (rest == 0) || (tail_of_map(dest_map[limit] ^ other_map[limit], rest) == 0); 565 } 566 567 bool BitMap::is_full() const { 568 const bm_word_t* words = map(); 569 idx_t limit = word_index(size()); 570 for (idx_t index = 0; index < limit; ++index) { 571 if (~words[index] != 0) return false; 572 } 573 idx_t rest = bit_in_word(size()); 574 return (rest == 0) || (tail_of_map(~words[limit], rest) == 0); 575 } 576 577 bool BitMap::is_empty() const { 578 const bm_word_t* words = map(); 579 idx_t limit = word_index(size()); 580 for (idx_t index = 0; index < limit; ++index) { 581 if (words[index] != 0) return false; 582 } 583 idx_t rest = bit_in_word(size()); 584 return (rest == 0) || (tail_of_map(words[limit], rest) == 0); 585 } 586 587 void BitMap::clear_large() { 588 clear_large_range_of_words(0, size_in_words()); 589 } 590 591 // Note that if the closure itself modifies the bitmap 592 // then modifications in and to the left of the _bit_ being 593 // currently sampled will not be seen. Note also that the 594 // interval [leftOffset, rightOffset) is right open. 595 bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) { 596 verify_range(leftOffset, rightOffset); 597 598 idx_t startIndex = word_index(leftOffset); 599 idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words()); 600 for (idx_t index = startIndex, offset = leftOffset; 601 offset < rightOffset && index < endIndex; 602 offset = (++index) << LogBitsPerWord) { 603 idx_t rest = map(index) >> (offset & (BitsPerWord - 1)); 604 for (; offset < rightOffset && rest != 0; offset++) { 605 if (rest & 1) { 606 if (!blk->do_bit(offset)) return false; 607 // resample at each closure application 608 // (see, for instance, CMS bug 4525989) 609 rest = map(index) >> (offset & (BitsPerWord -1)); 610 } 611 rest = rest >> 1; 612 } 613 } 614 return true; 615 } 616 617 BitMap::idx_t* BitMap::_pop_count_table = NULL; 618 619 void BitMap::init_pop_count_table() { 620 if (_pop_count_table == NULL) { 621 BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256, mtInternal); 622 for (uint i = 0; i < 256; i++) { 623 table[i] = num_set_bits(i); 624 } 625 626 intptr_t res = Atomic::cmpxchg_ptr((intptr_t) table, 627 (intptr_t*) &_pop_count_table, 628 (intptr_t) NULL_WORD); 629 if (res != NULL_WORD) { 630 guarantee( _pop_count_table == (void*) res, "invariant" ); 631 FREE_C_HEAP_ARRAY(idx_t, table); 632 } 633 } 634 } 635 636 BitMap::idx_t BitMap::num_set_bits(bm_word_t w) { 637 idx_t bits = 0; 638 639 while (w != 0) { 640 while ((w & 1) == 0) { 641 w >>= 1; 642 } 643 bits++; 644 w >>= 1; 645 } 646 return bits; 647 } 648 649 BitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) { 650 assert(_pop_count_table != NULL, "precondition"); 651 return _pop_count_table[c]; 652 } 653 654 BitMap::idx_t BitMap::count_one_bits() const { 655 init_pop_count_table(); // If necessary. 656 idx_t sum = 0; 657 typedef unsigned char uchar; 658 for (idx_t i = 0; i < size_in_words(); i++) { 659 bm_word_t w = map()[i]; 660 for (size_t j = 0; j < sizeof(bm_word_t); j++) { 661 sum += num_set_bits_from_table(uchar(w & 255)); 662 w >>= 8; 663 } 664 } 665 return sum; 666 } 667 668 void BitMap::print_on_error(outputStream* st, const char* prefix) const { 669 st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")", 670 prefix, p2i(map()), p2i((char*)map() + (size() >> LogBitsPerByte))); 671 } 672 673 #ifndef PRODUCT 674 675 void BitMap::print_on(outputStream* st) const { 676 tty->print("Bitmap(" SIZE_FORMAT "):", size()); 677 for (idx_t index = 0; index < size(); index++) { 678 tty->print("%c", at(index) ? '1' : '0'); 679 } 680 tty->cr(); 681 } 682 683 #endif