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