< prev index next >

src/share/vm/gc/shared/blockOffsetTable.cpp

Print this page




  70   _end = _reserved.start() + new_word_size;
  71   if (new_size > old_size) {
  72     delta = ReservedSpace::page_align_size_up(new_size - old_size);
  73     assert(delta > 0, "just checking");
  74     if (!_vs.expand_by(delta)) {
  75       // Do better than this for Merlin
  76       vm_exit_out_of_memory(delta, OOM_MMAP_ERROR, "offset table expansion");
  77     }
  78     assert(_vs.high() == high + delta, "invalid expansion");
  79   } else {
  80     delta = ReservedSpace::page_align_size_down(old_size - new_size);
  81     if (delta == 0) return;
  82     _vs.shrink_by(delta);
  83     assert(_vs.high() == high - delta, "invalid expansion");
  84   }
  85 }
  86 
  87 bool BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {
  88   assert(p >= _reserved.start(), "just checking");
  89   size_t delta = pointer_delta(p, _reserved.start());
  90   return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;
  91 }
  92 
  93 
  94 //////////////////////////////////////////////////////////////////////
  95 // BlockOffsetArray
  96 //////////////////////////////////////////////////////////////////////
  97 
  98 BlockOffsetArray::BlockOffsetArray(BlockOffsetSharedArray* array,
  99                                    MemRegion mr, bool init_to_zero_) :
 100   BlockOffsetTable(mr.start(), mr.end()),
 101   _array(array)
 102 {
 103   assert(_bottom <= _end, "arguments out of order");
 104   set_init_to_zero(init_to_zero_);
 105   if (!init_to_zero_) {
 106     // initialize cards to point back to mr.start()
 107     set_remainder_to_point_to_start(mr.start() + N_words, mr.end());
 108     _array->set_offset_array(0, 0);  // set first card to 0
 109   }
 110 }
 111 
 112 
 113 // The arguments follow the normal convention of denoting
 114 // a right-open interval: [start, end)
 115 void
 116 BlockOffsetArray::
 117 set_remainder_to_point_to_start(HeapWord* start, HeapWord* end, bool reducing) {
 118 
 119   check_reducing_assertion(reducing);
 120   if (start >= end) {
 121     // The start address is equal to the end address (or to
 122     // the right of the end address) so there are not cards
 123     // that need to be updated..
 124     return;
 125   }
 126 
 127   // Write the backskip value for each region.


 143   //    2nd - start of second logarithmic region
 144   //      1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
 145   //    3rd - start of third logarithmic region
 146   //      2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
 147   //
 148   //    integer below the block offset entry is an example of
 149   //    the index of the entry
 150   //
 151   //    Given an address,
 152   //      Find the index for the address
 153   //      Find the block offset table entry
 154   //      Convert the entry to a back slide
 155   //        (e.g., with today's, offset = 0x81 =>
 156   //          back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
 157   //      Move back N (e.g., 8) entries and repeat with the
 158   //        value of the new entry
 159   //
 160   size_t start_card = _array->index_for(start);
 161   size_t end_card = _array->index_for(end-1);
 162   assert(start ==_array->address_for_index(start_card), "Precondition");
 163   assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");
 164   set_remainder_to_point_to_start_incl(start_card, end_card, reducing); // closed interval
 165 }
 166 
 167 
 168 // Unlike the normal convention in this code, the argument here denotes
 169 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
 170 // above.
 171 void
 172 BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card, bool reducing) {
 173 
 174   check_reducing_assertion(reducing);
 175   if (start_card > end_card) {
 176     return;
 177   }
 178   assert(start_card > _array->index_for(_bottom), "Cannot be first card");
 179   assert(_array->offset_array(start_card-1) <= N_words,
 180     "Offset card has an unexpected value");
 181   size_t start_card_for_region = start_card;
 182   u_char offset = max_jubyte;
 183   for (int i = 0; i < N_powers; i++) {
 184     // -1 so that the the card with the actual offset is counted.  Another -1
 185     // so that the reach ends in this region and not at the start
 186     // of the next.
 187     size_t reach = start_card - 1 + (power_to_cards_back(i+1) - 1);
 188     offset = N_words + i;
 189     if (reach >= end_card) {
 190       _array->set_offset_array(start_card_for_region, end_card, offset, reducing);
 191       start_card_for_region = reach + 1;
 192       break;
 193     }
 194     _array->set_offset_array(start_card_for_region, reach, offset, reducing);
 195     start_card_for_region = reach + 1;
 196   }
 197   assert(start_card_for_region > end_card, "Sanity check");
 198   DEBUG_ONLY(check_all_cards(start_card, end_card);)
 199 }
 200 
 201 // The card-interval [start_card, end_card] is a closed interval; this
 202 // is an expensive check -- use with care and only under protection of
 203 // suitable flag.
 204 void BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {
 205 
 206   if (end_card < start_card) {
 207     return;
 208   }
 209   guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");
 210   u_char last_entry = N_words;
 211   for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
 212     u_char entry = _array->offset_array(c);
 213     guarantee(entry >= last_entry, "Monotonicity");
 214     if (c - start_card > power_to_cards_back(1)) {
 215       guarantee(entry > N_words, "Should be in logarithmic region");
 216     }
 217     size_t backskip = entry_to_cards_back(entry);
 218     size_t landing_card = c - backskip;
 219     guarantee(landing_card >= (start_card - 1), "Inv");
 220     if (landing_card >= start_card) {
 221       guarantee(_array->offset_array(landing_card) <= entry, "Monotonicity");
 222     } else {
 223       guarantee(landing_card == (start_card - 1), "Tautology");
 224       // Note that N_words is the maximum offset value
 225       guarantee(_array->offset_array(landing_card) <= N_words, "Offset value");
 226     }
 227     last_entry = entry;  // remember for monotonicity test
 228   }
 229 }
 230 
 231 
 232 void
 233 BlockOffsetArray::alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
 234   assert(blk_start != NULL && blk_end > blk_start,
 235          "phantom block");
 236   single_block(blk_start, blk_end);
 237 }
 238 
 239 // Action_mark - update the BOT for the block [blk_start, blk_end).
 240 //               Current typical use is for splitting a block.
 241 // Action_single - udpate the BOT for an allocation.
 242 // Action_verify - BOT verification.
 243 void
 244 BlockOffsetArray::do_block_internal(HeapWord* blk_start,
 245                                     HeapWord* blk_end,
 246                                     Action action, bool reducing) {
 247   assert(Universe::heap()->is_in_reserved(blk_start),
 248          "reference must be into the heap");
 249   assert(Universe::heap()->is_in_reserved(blk_end-1),
 250          "limit must be within the heap");
 251   // This is optimized to make the test fast, assuming we only rarely
 252   // cross boundaries.
 253   uintptr_t end_ui = (uintptr_t)(blk_end - 1);
 254   uintptr_t start_ui = (uintptr_t)blk_start;
 255   // Calculate the last card boundary preceding end of blk
 256   intptr_t boundary_before_end = (intptr_t)end_ui;
 257   clear_bits(boundary_before_end, right_n_bits(LogN));
 258   if (start_ui <= (uintptr_t)boundary_before_end) {
 259     // blk starts at or crosses a boundary
 260     // Calculate index of card on which blk begins
 261     size_t    start_index = _array->index_for(blk_start);
 262     // Index of card on which blk ends
 263     size_t    end_index   = _array->index_for(blk_end - 1);
 264     // Start address of card on which blk begins
 265     HeapWord* boundary    = _array->address_for_index(start_index);
 266     assert(boundary <= blk_start, "blk should start at or after boundary");
 267     if (blk_start != boundary) {
 268       // blk starts strictly after boundary
 269       // adjust card boundary and start_index forward to next card
 270       boundary += N_words;
 271       start_index++;
 272     }
 273     assert(start_index <= end_index, "monotonicity of index_for()");
 274     assert(boundary <= (HeapWord*)boundary_before_end, "tautology");
 275     switch (action) {
 276       case Action_mark: {
 277         if (init_to_zero()) {
 278           _array->set_offset_array(start_index, boundary, blk_start, reducing);
 279           break;
 280         } // Else fall through to the next case
 281       }
 282       case Action_single: {
 283         _array->set_offset_array(start_index, boundary, blk_start, reducing);
 284         // We have finished marking the "offset card". We need to now
 285         // mark the subsequent cards that this blk spans.
 286         if (start_index < end_index) {
 287           HeapWord* rem_st = _array->address_for_index(start_index) + N_words;
 288           HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
 289           set_remainder_to_point_to_start(rem_st, rem_end, reducing);
 290         }
 291         break;
 292       }
 293       case Action_check: {
 294         _array->check_offset_array(start_index, boundary, blk_start);
 295         // We have finished checking the "offset card". We need to now
 296         // check the subsequent cards that this blk spans.
 297         check_all_cards(start_index + 1, end_index);
 298         break;
 299       }
 300       default:
 301         ShouldNotReachHere();
 302     }
 303   }
 304 }
 305 
 306 // The range [blk_start, blk_end) represents a single contiguous block
 307 // of storage; modify the block offset table to represent this
 308 // information; Right-open interval: [blk_start, blk_end)


 433     _array->set_offset_array(suff_index, boundary, suff_addr, true /* reducing */);
 434     // Change any further cards that need changing in the suffix
 435     if (num_pref_cards > 0) {
 436       if (num_pref_cards >= num_suff_cards) {
 437         // Unilaterally fix all of the suffix cards: closed card
 438         // index interval in args below.
 439         set_remainder_to_point_to_start_incl(suff_index + 1, end_index - 1, true /* reducing */);
 440       } else {
 441         // Unilaterally fix the first (num_pref_cards - 1) following
 442         // the "offset card" in the suffix block.
 443         const size_t right_most_fixed_index = suff_index + num_pref_cards - 1;
 444         set_remainder_to_point_to_start_incl(suff_index + 1,
 445           right_most_fixed_index, true /* reducing */);
 446         // Fix the appropriate cards in the remainder of the
 447         // suffix block -- these are the last num_pref_cards
 448         // cards in each power block of the "new" range plumbed
 449         // from suff_addr.
 450         bool more = true;
 451         uint i = 1;
 452         // Fix the first power block with  back_by > num_pref_cards.
 453         while (more && (i < N_powers)) {
 454           size_t back_by = power_to_cards_back(i);
 455           size_t right_index = suff_index + back_by - 1;
 456           size_t left_index  = right_index - num_pref_cards + 1;
 457           if (right_index >= end_index - 1) { // last iteration
 458             right_index = end_index - 1;
 459             more = false;
 460           }
 461           if (left_index <= right_most_fixed_index) {
 462                 left_index = right_most_fixed_index + 1;
 463           }
 464           if (back_by > num_pref_cards) {
 465             // Fill in the remainder of this "power block", if it
 466             // is non-null.
 467             if (left_index <= right_index) {
 468               _array->set_offset_array(left_index, right_index,
 469                                      N_words + i - 1, true /* reducing */);
 470             } else {
 471               more = false; // we are done
 472               assert((end_index - 1) == right_index, "Must be at the end.");
 473             }
 474             i++;
 475             break;
 476           }
 477           i++;
 478         }
 479         // Fix the rest of the power blocks.
 480         while (more && (i < N_powers)) {
 481           size_t back_by = power_to_cards_back(i);
 482           size_t right_index = suff_index + back_by - 1;
 483           size_t left_index  = right_index - num_pref_cards + 1;
 484           if (right_index >= end_index - 1) { // last iteration
 485             right_index = end_index - 1;
 486             if (left_index > right_index) {
 487               break;
 488             }
 489             more  = false;
 490           }
 491           assert(left_index <= right_index, "Error");
 492           _array->set_offset_array(left_index, right_index, N_words + i - 1, true /* reducing */);
 493           i++;
 494         }
 495       }
 496     } // else no more cards to fix in suffix
 497   } // else nothing needs to be done
 498   // Verify that we did the right thing
 499   verify_single_block(pref_addr, left_blk_size);
 500   verify_single_block(suff_addr, blk_size - left_blk_size);
 501 }
 502 
 503 
 504 // Mark the BOT such that if [blk_start, blk_end) straddles a card
 505 // boundary, the card following the first such boundary is marked
 506 // with the appropriate offset.
 507 // NOTE: this method does _not_ adjust _unallocated_block or
 508 // any cards subsequent to the first one.
 509 void
 510 BlockOffsetArrayNonContigSpace::mark_block(HeapWord* blk_start,
 511                                            HeapWord* blk_end, bool reducing) {
 512   do_block_internal(blk_start, blk_end, Action_mark, reducing);
 513 }
 514 
 515 HeapWord* BlockOffsetArrayNonContigSpace::block_start_unsafe(
 516   const void* addr) const {
 517   assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
 518   assert(_bottom <= addr && addr < _end,
 519          "addr must be covered by this Array");
 520   // Must read this exactly once because it can be modified by parallel
 521   // allocation.
 522   HeapWord* ub = _unallocated_block;
 523   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
 524     assert(ub < _end, "tautology (see above)");
 525     return ub;
 526   }
 527 
 528   // Otherwise, find the block start using the table.
 529   size_t index = _array->index_for(addr);
 530   HeapWord* q = _array->address_for_index(index);
 531 
 532   uint offset = _array->offset_array(index);    // Extend u_char to uint.
 533   while (offset >= N_words) {
 534     // The excess of the offset from N_words indicates a power of Base
 535     // to go back by.
 536     size_t n_cards_back = entry_to_cards_back(offset);
 537     q -= (N_words * n_cards_back);
 538     assert(q >= _sp->bottom(),
 539            "q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,
 540            p2i(q), p2i(_sp->bottom()));
 541     assert(q < _sp->end(),
 542            "q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,
 543            p2i(q), p2i(_sp->end()));
 544     index -= n_cards_back;
 545     offset = _array->offset_array(index);
 546   }
 547   assert(offset < N_words, "offset too large");
 548   index--;
 549   q -= offset;
 550   assert(q >= _sp->bottom(),
 551          "q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,
 552          p2i(q), p2i(_sp->bottom()));
 553   assert(q < _sp->end(),
 554          "q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,
 555          p2i(q), p2i(_sp->end()));
 556   HeapWord* n = q;
 557 
 558   while (n <= addr) {
 559     debug_only(HeapWord* last = q);   // for debugging
 560     q = n;
 561     n += _sp->block_size(n);
 562     assert(n > q,
 563            "Looping at n = " PTR_FORMAT " with last = " PTR_FORMAT ","
 564            " while querying blk_start(" PTR_FORMAT ")"
 565            " on _sp = [" PTR_FORMAT "," PTR_FORMAT ")",
 566            p2i(n), p2i(last), p2i(addr), p2i(_sp->bottom()), p2i(_sp->end()));
 567   }


 582          "addr must be covered by this Array");
 583   // Must read this exactly once because it can be modified by parallel
 584   // allocation.
 585   HeapWord* ub = _unallocated_block;
 586   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
 587     assert(ub < _end, "tautology (see above)");
 588     return ub;
 589   }
 590 
 591   // Otherwise, find the block start using the table, but taking
 592   // care (cf block_start_unsafe() above) not to parse any objects/blocks
 593   // on the cards themselves.
 594   size_t index = _array->index_for(addr);
 595   assert(_array->address_for_index(index) == addr,
 596          "arg should be start of card");
 597 
 598   HeapWord* q = (HeapWord*)addr;
 599   uint offset;
 600   do {
 601     offset = _array->offset_array(index);
 602     if (offset < N_words) {
 603       q -= offset;
 604     } else {
 605       size_t n_cards_back = entry_to_cards_back(offset);
 606       q -= (n_cards_back * N_words);
 607       index -= n_cards_back;
 608     }
 609   } while (offset >= N_words);
 610   assert(q <= addr, "block start should be to left of arg");
 611   return q;
 612 }
 613 
 614 #ifndef PRODUCT
 615 // Verification & debugging - ensure that the offset table reflects the fact
 616 // that the block [blk_start, blk_end) or [blk, blk + size) is a
 617 // single block of storage. NOTE: can't const this because of
 618 // call to non-const do_block_internal() below.
 619 void BlockOffsetArrayNonContigSpace::verify_single_block(
 620   HeapWord* blk_start, HeapWord* blk_end) {
 621   if (VerifyBlockOffsetArray) {
 622     do_block_internal(blk_start, blk_end, Action_check);
 623   }
 624 }
 625 
 626 void BlockOffsetArrayNonContigSpace::verify_single_block(
 627   HeapWord* blk, size_t size) {
 628   verify_single_block(blk, blk + size);
 629 }


 651   }
 652 }
 653 
 654 //////////////////////////////////////////////////////////////////////
 655 // BlockOffsetArrayContigSpace
 656 //////////////////////////////////////////////////////////////////////
 657 
 658 HeapWord* BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) const {
 659   assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
 660 
 661   // Otherwise, find the block start using the table.
 662   assert(_bottom <= addr && addr < _end,
 663          "addr must be covered by this Array");
 664   size_t index = _array->index_for(addr);
 665   // We must make sure that the offset table entry we use is valid.  If
 666   // "addr" is past the end, start at the last known one and go forward.
 667   index = MIN2(index, _next_offset_index-1);
 668   HeapWord* q = _array->address_for_index(index);
 669 
 670   uint offset = _array->offset_array(index);    // Extend u_char to uint.
 671   while (offset > N_words) {
 672     // The excess of the offset from N_words indicates a power of Base
 673     // to go back by.
 674     size_t n_cards_back = entry_to_cards_back(offset);
 675     q -= (N_words * n_cards_back);
 676     assert(q >= _sp->bottom(), "Went below bottom!");
 677     index -= n_cards_back;
 678     offset = _array->offset_array(index);
 679   }
 680   while (offset == N_words) {
 681     assert(q >= _sp->bottom(), "Went below bottom!");
 682     q -= N_words;
 683     index--;
 684     offset = _array->offset_array(index);
 685   }
 686   assert(offset < N_words, "offset too large");
 687   q -= offset;
 688   HeapWord* n = q;
 689 
 690   while (n <= addr) {
 691     debug_only(HeapWord* last = q);   // for debugging
 692     q = n;
 693     n += _sp->block_size(n);
 694   }
 695   assert(q <= addr, "wrong order for current and arg");
 696   assert(addr <= n, "wrong order for arg and next");
 697   return q;
 698 }
 699 
 700 //
 701 //              _next_offset_threshold
 702 //              |   _next_offset_index
 703 //              v   v
 704 //      +-------+-------+-------+-------+-------+
 705 //      | i-1   |   i   | i+1   | i+2   | i+3   |
 706 //      +-------+-------+-------+-------+-------+
 707 //       ( ^    ]
 708 //         block-start
 709 //
 710 
 711 void BlockOffsetArrayContigSpace::alloc_block_work(HeapWord* blk_start,
 712                                         HeapWord* blk_end) {
 713   assert(blk_start != NULL && blk_end > blk_start,
 714          "phantom block");
 715   assert(blk_end > _next_offset_threshold,
 716          "should be past threshold");
 717   assert(blk_start <= _next_offset_threshold,
 718          "blk_start should be at or before threshold");
 719   assert(pointer_delta(_next_offset_threshold, blk_start) <= N_words,
 720          "offset should be <= BlockOffsetSharedArray::N");
 721   assert(Universe::heap()->is_in_reserved(blk_start),
 722          "reference must be into the heap");
 723   assert(Universe::heap()->is_in_reserved(blk_end-1),
 724          "limit must be within the heap");
 725   assert(_next_offset_threshold ==
 726          _array->_reserved.start() + _next_offset_index*N_words,
 727          "index must agree with threshold");
 728 
 729   debug_only(size_t orig_next_offset_index = _next_offset_index;)
 730 
 731   // Mark the card that holds the offset into the block.  Note
 732   // that _next_offset_index and _next_offset_threshold are not
 733   // updated until the end of this method.
 734   _array->set_offset_array(_next_offset_index,
 735                            _next_offset_threshold,
 736                            blk_start);
 737 
 738   // We need to now mark the subsequent cards that this blk spans.
 739 
 740   // Index of card on which blk ends.
 741   size_t end_index   = _array->index_for(blk_end - 1);
 742 
 743   // Are there more cards left to be updated?
 744   if (_next_offset_index + 1 <= end_index) {
 745     HeapWord* rem_st  = _array->address_for_index(_next_offset_index + 1);
 746     // Calculate rem_end this way because end_index
 747     // may be the last valid index in the covered region.
 748     HeapWord* rem_end = _array->address_for_index(end_index) +  N_words;
 749     set_remainder_to_point_to_start(rem_st, rem_end);
 750   }
 751 
 752   // _next_offset_index and _next_offset_threshold updated here.
 753   _next_offset_index = end_index + 1;
 754   // Calculate _next_offset_threshold this way because end_index
 755   // may be the last valid index in the covered region.
 756   _next_offset_threshold = _array->address_for_index(end_index) + N_words;
 757   assert(_next_offset_threshold >= blk_end, "Incorrect offset threshold");
 758 
 759 #ifdef ASSERT
 760   // The offset can be 0 if the block starts on a boundary.  That
 761   // is checked by an assertion above.
 762   size_t start_index = _array->index_for(blk_start);
 763   HeapWord* boundary    = _array->address_for_index(start_index);
 764   assert((_array->offset_array(orig_next_offset_index) == 0 &&
 765           blk_start == boundary) ||
 766           (_array->offset_array(orig_next_offset_index) > 0 &&
 767          _array->offset_array(orig_next_offset_index) <= N_words),
 768          "offset array should have been set");
 769   for (size_t j = orig_next_offset_index + 1; j <= end_index; j++) {
 770     assert(_array->offset_array(j) > 0 &&
 771            _array->offset_array(j) <= (u_char) (N_words+N_powers-1),
 772            "offset array should have been set");
 773   }
 774 #endif
 775 }
 776 
 777 HeapWord* BlockOffsetArrayContigSpace::initialize_threshold() {
 778   assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
 779          "just checking");
 780   _next_offset_index = _array->index_for(_bottom);
 781   _next_offset_index++;
 782   _next_offset_threshold =
 783     _array->address_for_index(_next_offset_index);
 784   return _next_offset_threshold;
 785 }
 786 
 787 void BlockOffsetArrayContigSpace::zero_bottom_entry() {
 788   assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
 789          "just checking");
 790   size_t bottom_index = _array->index_for(_bottom);
 791   _array->set_offset_array(bottom_index, 0);


  70   _end = _reserved.start() + new_word_size;
  71   if (new_size > old_size) {
  72     delta = ReservedSpace::page_align_size_up(new_size - old_size);
  73     assert(delta > 0, "just checking");
  74     if (!_vs.expand_by(delta)) {
  75       // Do better than this for Merlin
  76       vm_exit_out_of_memory(delta, OOM_MMAP_ERROR, "offset table expansion");
  77     }
  78     assert(_vs.high() == high + delta, "invalid expansion");
  79   } else {
  80     delta = ReservedSpace::page_align_size_down(old_size - new_size);
  81     if (delta == 0) return;
  82     _vs.shrink_by(delta);
  83     assert(_vs.high() == high - delta, "invalid expansion");
  84   }
  85 }
  86 
  87 bool BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {
  88   assert(p >= _reserved.start(), "just checking");
  89   size_t delta = pointer_delta(p, _reserved.start());
  90   return (delta & right_n_bits((int)BOTConstants::LogN_words)) == (size_t)NoBits;
  91 }
  92 
  93 
  94 //////////////////////////////////////////////////////////////////////
  95 // BlockOffsetArray
  96 //////////////////////////////////////////////////////////////////////
  97 
  98 BlockOffsetArray::BlockOffsetArray(BlockOffsetSharedArray* array,
  99                                    MemRegion mr, bool init_to_zero_) :
 100   BlockOffsetTable(mr.start(), mr.end()),
 101   _array(array)
 102 {
 103   assert(_bottom <= _end, "arguments out of order");
 104   set_init_to_zero(init_to_zero_);
 105   if (!init_to_zero_) {
 106     // initialize cards to point back to mr.start()
 107     set_remainder_to_point_to_start(mr.start() + BOTConstants::N_words, mr.end());
 108     _array->set_offset_array(0, 0);  // set first card to 0
 109   }
 110 }
 111 
 112 
 113 // The arguments follow the normal convention of denoting
 114 // a right-open interval: [start, end)
 115 void
 116 BlockOffsetArray::
 117 set_remainder_to_point_to_start(HeapWord* start, HeapWord* end, bool reducing) {
 118 
 119   check_reducing_assertion(reducing);
 120   if (start >= end) {
 121     // The start address is equal to the end address (or to
 122     // the right of the end address) so there are not cards
 123     // that need to be updated..
 124     return;
 125   }
 126 
 127   // Write the backskip value for each region.


 143   //    2nd - start of second logarithmic region
 144   //      1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
 145   //    3rd - start of third logarithmic region
 146   //      2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
 147   //
 148   //    integer below the block offset entry is an example of
 149   //    the index of the entry
 150   //
 151   //    Given an address,
 152   //      Find the index for the address
 153   //      Find the block offset table entry
 154   //      Convert the entry to a back slide
 155   //        (e.g., with today's, offset = 0x81 =>
 156   //          back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
 157   //      Move back N (e.g., 8) entries and repeat with the
 158   //        value of the new entry
 159   //
 160   size_t start_card = _array->index_for(start);
 161   size_t end_card = _array->index_for(end-1);
 162   assert(start ==_array->address_for_index(start_card), "Precondition");
 163   assert(end ==_array->address_for_index(end_card)+BOTConstants::N_words, "Precondition");
 164   set_remainder_to_point_to_start_incl(start_card, end_card, reducing); // closed interval
 165 }
 166 
 167 
 168 // Unlike the normal convention in this code, the argument here denotes
 169 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
 170 // above.
 171 void
 172 BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card, bool reducing) {
 173 
 174   check_reducing_assertion(reducing);
 175   if (start_card > end_card) {
 176     return;
 177   }
 178   assert(start_card > _array->index_for(_bottom), "Cannot be first card");
 179   assert(_array->offset_array(start_card-1) <= BOTConstants::N_words,
 180     "Offset card has an unexpected value");
 181   size_t start_card_for_region = start_card;
 182   u_char offset = max_jubyte;
 183   for (uint i = 0; i < BOTConstants::N_powers; i++) {
 184     // -1 so that the the card with the actual offset is counted.  Another -1
 185     // so that the reach ends in this region and not at the start
 186     // of the next.
 187     size_t reach = start_card - 1 + (BOTConstants::power_to_cards_back(i+1) - 1);
 188     offset = BOTConstants::N_words + i;
 189     if (reach >= end_card) {
 190       _array->set_offset_array(start_card_for_region, end_card, offset, reducing);
 191       start_card_for_region = reach + 1;
 192       break;
 193     }
 194     _array->set_offset_array(start_card_for_region, reach, offset, reducing);
 195     start_card_for_region = reach + 1;
 196   }
 197   assert(start_card_for_region > end_card, "Sanity check");
 198   DEBUG_ONLY(check_all_cards(start_card, end_card);)
 199 }
 200 
 201 // The card-interval [start_card, end_card] is a closed interval; this
 202 // is an expensive check -- use with care and only under protection of
 203 // suitable flag.
 204 void BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {
 205 
 206   if (end_card < start_card) {
 207     return;
 208   }
 209   guarantee(_array->offset_array(start_card) == BOTConstants::N_words, "Wrong value in second card");
 210   u_char last_entry = BOTConstants::N_words;
 211   for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
 212     u_char entry = _array->offset_array(c);
 213     guarantee(entry >= last_entry, "Monotonicity");
 214     if (c - start_card > BOTConstants::power_to_cards_back(1)) {
 215       guarantee(entry > BOTConstants::N_words, "Should be in logarithmic region");
 216     }
 217     size_t backskip = BOTConstants::entry_to_cards_back(entry);
 218     size_t landing_card = c - backskip;
 219     guarantee(landing_card >= (start_card - 1), "Inv");
 220     if (landing_card >= start_card) {
 221       guarantee(_array->offset_array(landing_card) <= entry, "Monotonicity");
 222     } else {
 223       guarantee(landing_card == (start_card - 1), "Tautology");
 224       // Note that N_words is the maximum offset value
 225       guarantee(_array->offset_array(landing_card) <= BOTConstants::N_words, "Offset value");
 226     }
 227     last_entry = entry;  // remember for monotonicity test
 228   }
 229 }
 230 
 231 
 232 void
 233 BlockOffsetArray::alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
 234   assert(blk_start != NULL && blk_end > blk_start,
 235          "phantom block");
 236   single_block(blk_start, blk_end);
 237 }
 238 
 239 // Action_mark - update the BOT for the block [blk_start, blk_end).
 240 //               Current typical use is for splitting a block.
 241 // Action_single - udpate the BOT for an allocation.
 242 // Action_verify - BOT verification.
 243 void
 244 BlockOffsetArray::do_block_internal(HeapWord* blk_start,
 245                                     HeapWord* blk_end,
 246                                     Action action, bool reducing) {
 247   assert(Universe::heap()->is_in_reserved(blk_start),
 248          "reference must be into the heap");
 249   assert(Universe::heap()->is_in_reserved(blk_end-1),
 250          "limit must be within the heap");
 251   // This is optimized to make the test fast, assuming we only rarely
 252   // cross boundaries.
 253   uintptr_t end_ui = (uintptr_t)(blk_end - 1);
 254   uintptr_t start_ui = (uintptr_t)blk_start;
 255   // Calculate the last card boundary preceding end of blk
 256   intptr_t boundary_before_end = (intptr_t)end_ui;
 257   clear_bits(boundary_before_end, right_n_bits((int)BOTConstants::LogN));
 258   if (start_ui <= (uintptr_t)boundary_before_end) {
 259     // blk starts at or crosses a boundary
 260     // Calculate index of card on which blk begins
 261     size_t    start_index = _array->index_for(blk_start);
 262     // Index of card on which blk ends
 263     size_t    end_index   = _array->index_for(blk_end - 1);
 264     // Start address of card on which blk begins
 265     HeapWord* boundary    = _array->address_for_index(start_index);
 266     assert(boundary <= blk_start, "blk should start at or after boundary");
 267     if (blk_start != boundary) {
 268       // blk starts strictly after boundary
 269       // adjust card boundary and start_index forward to next card
 270       boundary += BOTConstants::N_words;
 271       start_index++;
 272     }
 273     assert(start_index <= end_index, "monotonicity of index_for()");
 274     assert(boundary <= (HeapWord*)boundary_before_end, "tautology");
 275     switch (action) {
 276       case Action_mark: {
 277         if (init_to_zero()) {
 278           _array->set_offset_array(start_index, boundary, blk_start, reducing);
 279           break;
 280         } // Else fall through to the next case
 281       }
 282       case Action_single: {
 283         _array->set_offset_array(start_index, boundary, blk_start, reducing);
 284         // We have finished marking the "offset card". We need to now
 285         // mark the subsequent cards that this blk spans.
 286         if (start_index < end_index) {
 287           HeapWord* rem_st = _array->address_for_index(start_index) + BOTConstants::N_words;
 288           HeapWord* rem_end = _array->address_for_index(end_index) + BOTConstants::N_words;
 289           set_remainder_to_point_to_start(rem_st, rem_end, reducing);
 290         }
 291         break;
 292       }
 293       case Action_check: {
 294         _array->check_offset_array(start_index, boundary, blk_start);
 295         // We have finished checking the "offset card". We need to now
 296         // check the subsequent cards that this blk spans.
 297         check_all_cards(start_index + 1, end_index);
 298         break;
 299       }
 300       default:
 301         ShouldNotReachHere();
 302     }
 303   }
 304 }
 305 
 306 // The range [blk_start, blk_end) represents a single contiguous block
 307 // of storage; modify the block offset table to represent this
 308 // information; Right-open interval: [blk_start, blk_end)


 433     _array->set_offset_array(suff_index, boundary, suff_addr, true /* reducing */);
 434     // Change any further cards that need changing in the suffix
 435     if (num_pref_cards > 0) {
 436       if (num_pref_cards >= num_suff_cards) {
 437         // Unilaterally fix all of the suffix cards: closed card
 438         // index interval in args below.
 439         set_remainder_to_point_to_start_incl(suff_index + 1, end_index - 1, true /* reducing */);
 440       } else {
 441         // Unilaterally fix the first (num_pref_cards - 1) following
 442         // the "offset card" in the suffix block.
 443         const size_t right_most_fixed_index = suff_index + num_pref_cards - 1;
 444         set_remainder_to_point_to_start_incl(suff_index + 1,
 445           right_most_fixed_index, true /* reducing */);
 446         // Fix the appropriate cards in the remainder of the
 447         // suffix block -- these are the last num_pref_cards
 448         // cards in each power block of the "new" range plumbed
 449         // from suff_addr.
 450         bool more = true;
 451         uint i = 1;
 452         // Fix the first power block with  back_by > num_pref_cards.
 453         while (more && (i < BOTConstants::N_powers)) {
 454           size_t back_by = BOTConstants::power_to_cards_back(i);
 455           size_t right_index = suff_index + back_by - 1;
 456           size_t left_index  = right_index - num_pref_cards + 1;
 457           if (right_index >= end_index - 1) { // last iteration
 458             right_index = end_index - 1;
 459             more = false;
 460           }
 461           if (left_index <= right_most_fixed_index) {
 462                 left_index = right_most_fixed_index + 1;
 463           }
 464           if (back_by > num_pref_cards) {
 465             // Fill in the remainder of this "power block", if it
 466             // is non-null.
 467             if (left_index <= right_index) {
 468               _array->set_offset_array(left_index, right_index,
 469                                        BOTConstants::N_words + i - 1, true /* reducing */);
 470             } else {
 471               more = false; // we are done
 472               assert((end_index - 1) == right_index, "Must be at the end.");
 473             }
 474             i++;
 475             break;
 476           }
 477           i++;
 478         }
 479         // Fix the rest of the power blocks.
 480         while (more && (i < BOTConstants::N_powers)) {
 481           size_t back_by = BOTConstants::power_to_cards_back(i);
 482           size_t right_index = suff_index + back_by - 1;
 483           size_t left_index  = right_index - num_pref_cards + 1;
 484           if (right_index >= end_index - 1) { // last iteration
 485             right_index = end_index - 1;
 486             if (left_index > right_index) {
 487               break;
 488             }
 489             more  = false;
 490           }
 491           assert(left_index <= right_index, "Error");
 492           _array->set_offset_array(left_index, right_index, BOTConstants::N_words + i - 1, true /* reducing */);
 493           i++;
 494         }
 495       }
 496     } // else no more cards to fix in suffix
 497   } // else nothing needs to be done
 498   // Verify that we did the right thing
 499   verify_single_block(pref_addr, left_blk_size);
 500   verify_single_block(suff_addr, blk_size - left_blk_size);
 501 }
 502 
 503 
 504 // Mark the BOT such that if [blk_start, blk_end) straddles a card
 505 // boundary, the card following the first such boundary is marked
 506 // with the appropriate offset.
 507 // NOTE: this method does _not_ adjust _unallocated_block or
 508 // any cards subsequent to the first one.
 509 void
 510 BlockOffsetArrayNonContigSpace::mark_block(HeapWord* blk_start,
 511                                            HeapWord* blk_end, bool reducing) {
 512   do_block_internal(blk_start, blk_end, Action_mark, reducing);
 513 }
 514 
 515 HeapWord* BlockOffsetArrayNonContigSpace::block_start_unsafe(
 516   const void* addr) const {
 517   assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
 518   assert(_bottom <= addr && addr < _end,
 519          "addr must be covered by this Array");
 520   // Must read this exactly once because it can be modified by parallel
 521   // allocation.
 522   HeapWord* ub = _unallocated_block;
 523   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
 524     assert(ub < _end, "tautology (see above)");
 525     return ub;
 526   }
 527 
 528   // Otherwise, find the block start using the table.
 529   size_t index = _array->index_for(addr);
 530   HeapWord* q = _array->address_for_index(index);
 531 
 532   uint offset = _array->offset_array(index);    // Extend u_char to uint.
 533   while (offset >= BOTConstants::N_words) {
 534     // The excess of the offset from N_words indicates a power of Base
 535     // to go back by.
 536     size_t n_cards_back = BOTConstants::entry_to_cards_back(offset);
 537     q -= (BOTConstants::N_words * n_cards_back);
 538     assert(q >= _sp->bottom(),
 539            "q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,
 540            p2i(q), p2i(_sp->bottom()));
 541     assert(q < _sp->end(),
 542            "q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,
 543            p2i(q), p2i(_sp->end()));
 544     index -= n_cards_back;
 545     offset = _array->offset_array(index);
 546   }
 547   assert(offset < BOTConstants::N_words, "offset too large");
 548   index--;
 549   q -= offset;
 550   assert(q >= _sp->bottom(),
 551          "q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,
 552          p2i(q), p2i(_sp->bottom()));
 553   assert(q < _sp->end(),
 554          "q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,
 555          p2i(q), p2i(_sp->end()));
 556   HeapWord* n = q;
 557 
 558   while (n <= addr) {
 559     debug_only(HeapWord* last = q);   // for debugging
 560     q = n;
 561     n += _sp->block_size(n);
 562     assert(n > q,
 563            "Looping at n = " PTR_FORMAT " with last = " PTR_FORMAT ","
 564            " while querying blk_start(" PTR_FORMAT ")"
 565            " on _sp = [" PTR_FORMAT "," PTR_FORMAT ")",
 566            p2i(n), p2i(last), p2i(addr), p2i(_sp->bottom()), p2i(_sp->end()));
 567   }


 582          "addr must be covered by this Array");
 583   // Must read this exactly once because it can be modified by parallel
 584   // allocation.
 585   HeapWord* ub = _unallocated_block;
 586   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
 587     assert(ub < _end, "tautology (see above)");
 588     return ub;
 589   }
 590 
 591   // Otherwise, find the block start using the table, but taking
 592   // care (cf block_start_unsafe() above) not to parse any objects/blocks
 593   // on the cards themselves.
 594   size_t index = _array->index_for(addr);
 595   assert(_array->address_for_index(index) == addr,
 596          "arg should be start of card");
 597 
 598   HeapWord* q = (HeapWord*)addr;
 599   uint offset;
 600   do {
 601     offset = _array->offset_array(index);
 602     if (offset < BOTConstants::N_words) {
 603       q -= offset;
 604     } else {
 605       size_t n_cards_back = BOTConstants::entry_to_cards_back(offset);
 606       q -= (n_cards_back * BOTConstants::N_words);
 607       index -= n_cards_back;
 608     }
 609   } while (offset >= BOTConstants::N_words);
 610   assert(q <= addr, "block start should be to left of arg");
 611   return q;
 612 }
 613 
 614 #ifndef PRODUCT
 615 // Verification & debugging - ensure that the offset table reflects the fact
 616 // that the block [blk_start, blk_end) or [blk, blk + size) is a
 617 // single block of storage. NOTE: can't const this because of
 618 // call to non-const do_block_internal() below.
 619 void BlockOffsetArrayNonContigSpace::verify_single_block(
 620   HeapWord* blk_start, HeapWord* blk_end) {
 621   if (VerifyBlockOffsetArray) {
 622     do_block_internal(blk_start, blk_end, Action_check);
 623   }
 624 }
 625 
 626 void BlockOffsetArrayNonContigSpace::verify_single_block(
 627   HeapWord* blk, size_t size) {
 628   verify_single_block(blk, blk + size);
 629 }


 651   }
 652 }
 653 
 654 //////////////////////////////////////////////////////////////////////
 655 // BlockOffsetArrayContigSpace
 656 //////////////////////////////////////////////////////////////////////
 657 
 658 HeapWord* BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) const {
 659   assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
 660 
 661   // Otherwise, find the block start using the table.
 662   assert(_bottom <= addr && addr < _end,
 663          "addr must be covered by this Array");
 664   size_t index = _array->index_for(addr);
 665   // We must make sure that the offset table entry we use is valid.  If
 666   // "addr" is past the end, start at the last known one and go forward.
 667   index = MIN2(index, _next_offset_index-1);
 668   HeapWord* q = _array->address_for_index(index);
 669 
 670   uint offset = _array->offset_array(index);    // Extend u_char to uint.
 671   while (offset > BOTConstants::N_words) {
 672     // The excess of the offset from N_words indicates a power of Base
 673     // to go back by.
 674     size_t n_cards_back = BOTConstants::entry_to_cards_back(offset);
 675     q -= (BOTConstants::N_words * n_cards_back);
 676     assert(q >= _sp->bottom(), "Went below bottom!");
 677     index -= n_cards_back;
 678     offset = _array->offset_array(index);
 679   }
 680   while (offset == BOTConstants::N_words) {
 681     assert(q >= _sp->bottom(), "Went below bottom!");
 682     q -= BOTConstants::N_words;
 683     index--;
 684     offset = _array->offset_array(index);
 685   }
 686   assert(offset < BOTConstants::N_words, "offset too large");
 687   q -= offset;
 688   HeapWord* n = q;
 689 
 690   while (n <= addr) {
 691     debug_only(HeapWord* last = q);   // for debugging
 692     q = n;
 693     n += _sp->block_size(n);
 694   }
 695   assert(q <= addr, "wrong order for current and arg");
 696   assert(addr <= n, "wrong order for arg and next");
 697   return q;
 698 }
 699 
 700 //
 701 //              _next_offset_threshold
 702 //              |   _next_offset_index
 703 //              v   v
 704 //      +-------+-------+-------+-------+-------+
 705 //      | i-1   |   i   | i+1   | i+2   | i+3   |
 706 //      +-------+-------+-------+-------+-------+
 707 //       ( ^    ]
 708 //         block-start
 709 //
 710 
 711 void BlockOffsetArrayContigSpace::alloc_block_work(HeapWord* blk_start,
 712                                         HeapWord* blk_end) {
 713   assert(blk_start != NULL && blk_end > blk_start,
 714          "phantom block");
 715   assert(blk_end > _next_offset_threshold,
 716          "should be past threshold");
 717   assert(blk_start <= _next_offset_threshold,
 718          "blk_start should be at or before threshold");
 719   assert(pointer_delta(_next_offset_threshold, blk_start) <= BOTConstants::N_words,
 720          "offset should be <= BlockOffsetSharedArray::N");
 721   assert(Universe::heap()->is_in_reserved(blk_start),
 722          "reference must be into the heap");
 723   assert(Universe::heap()->is_in_reserved(blk_end-1),
 724          "limit must be within the heap");
 725   assert(_next_offset_threshold ==
 726          _array->_reserved.start() + _next_offset_index*BOTConstants::N_words,
 727          "index must agree with threshold");
 728 
 729   debug_only(size_t orig_next_offset_index = _next_offset_index;)
 730 
 731   // Mark the card that holds the offset into the block.  Note
 732   // that _next_offset_index and _next_offset_threshold are not
 733   // updated until the end of this method.
 734   _array->set_offset_array(_next_offset_index,
 735                            _next_offset_threshold,
 736                            blk_start);
 737 
 738   // We need to now mark the subsequent cards that this blk spans.
 739 
 740   // Index of card on which blk ends.
 741   size_t end_index   = _array->index_for(blk_end - 1);
 742 
 743   // Are there more cards left to be updated?
 744   if (_next_offset_index + 1 <= end_index) {
 745     HeapWord* rem_st  = _array->address_for_index(_next_offset_index + 1);
 746     // Calculate rem_end this way because end_index
 747     // may be the last valid index in the covered region.
 748     HeapWord* rem_end = _array->address_for_index(end_index) +  BOTConstants::N_words;
 749     set_remainder_to_point_to_start(rem_st, rem_end);
 750   }
 751 
 752   // _next_offset_index and _next_offset_threshold updated here.
 753   _next_offset_index = end_index + 1;
 754   // Calculate _next_offset_threshold this way because end_index
 755   // may be the last valid index in the covered region.
 756   _next_offset_threshold = _array->address_for_index(end_index) + BOTConstants::N_words;
 757   assert(_next_offset_threshold >= blk_end, "Incorrect offset threshold");
 758 
 759 #ifdef ASSERT
 760   // The offset can be 0 if the block starts on a boundary.  That
 761   // is checked by an assertion above.
 762   size_t start_index = _array->index_for(blk_start);
 763   HeapWord* boundary    = _array->address_for_index(start_index);
 764   assert((_array->offset_array(orig_next_offset_index) == 0 &&
 765           blk_start == boundary) ||
 766           (_array->offset_array(orig_next_offset_index) > 0 &&
 767          _array->offset_array(orig_next_offset_index) <= BOTConstants::N_words),
 768          "offset array should have been set");
 769   for (size_t j = orig_next_offset_index + 1; j <= end_index; j++) {
 770     assert(_array->offset_array(j) > 0 &&
 771            _array->offset_array(j) <= (u_char) (BOTConstants::N_words+BOTConstants::N_powers-1),
 772            "offset array should have been set");
 773   }
 774 #endif
 775 }
 776 
 777 HeapWord* BlockOffsetArrayContigSpace::initialize_threshold() {
 778   assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
 779          "just checking");
 780   _next_offset_index = _array->index_for(_bottom);
 781   _next_offset_index++;
 782   _next_offset_threshold =
 783     _array->address_for_index(_next_offset_index);
 784   return _next_offset_threshold;
 785 }
 786 
 787 void BlockOffsetArrayContigSpace::zero_bottom_entry() {
 788   assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
 789          "just checking");
 790   size_t bottom_index = _array->index_for(_bottom);
 791   _array->set_offset_array(bottom_index, 0);
< prev index next >