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