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