1 /*
   2  * Copyright (c) 2001, 2014, 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 "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
  27 #include "gc_implementation/g1/heapRegion.hpp"
  28 #include "memory/space.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/java.hpp"
  31 #include "services/memTracker.hpp"
  32 
  33 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  34 
  35 //////////////////////////////////////////////////////////////////////
  36 // G1BlockOffsetSharedArray
  37 //////////////////////////////////////////////////////////////////////
  38 
  39 G1BlockOffsetSharedArray::G1BlockOffsetSharedArray(MemRegion reserved,
  40                                                    size_t init_word_size) :
  41   _reserved(reserved), _end(NULL)
  42 {
  43   size_t size = compute_size(reserved.word_size());
  44   ReservedSpace rs(ReservedSpace::allocation_align_size_up(size));
  45   if (!rs.is_reserved()) {
  46     vm_exit_during_initialization("Could not reserve enough space for heap offset array");
  47   }
  48   if (!_vs.initialize(rs, 0)) {
  49     vm_exit_during_initialization("Could not reserve enough space for heap offset array");
  50   }
  51 
  52   MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);
  53 
  54   _offset_array = (u_char*)_vs.low_boundary();
  55   resize(init_word_size);
  56   if (TraceBlockOffsetTable) {
  57     gclog_or_tty->print_cr("G1BlockOffsetSharedArray::G1BlockOffsetSharedArray: ");
  58     gclog_or_tty->print_cr("  "
  59                   "  rs.base(): " INTPTR_FORMAT
  60                   "  rs.size(): " INTPTR_FORMAT
  61                   "  rs end(): " INTPTR_FORMAT,
  62                   rs.base(), rs.size(), rs.base() + rs.size());
  63     gclog_or_tty->print_cr("  "
  64                   "  _vs.low_boundary(): " INTPTR_FORMAT
  65                   "  _vs.high_boundary(): " INTPTR_FORMAT,
  66                   _vs.low_boundary(),
  67                   _vs.high_boundary());
  68   }
  69 }
  70 
  71 void G1BlockOffsetSharedArray::resize(size_t new_word_size) {
  72   assert(new_word_size <= _reserved.word_size(), "Resize larger than reserved");
  73   size_t new_size = compute_size(new_word_size);
  74   size_t old_size = _vs.committed_size();
  75   size_t delta;
  76   char* high = _vs.high();
  77   _end = _reserved.start() + new_word_size;
  78   if (new_size > old_size) {
  79     delta = ReservedSpace::page_align_size_up(new_size - old_size);
  80     assert(delta > 0, "just checking");
  81     if (!_vs.expand_by(delta)) {
  82       // Do better than this for Merlin
  83       vm_exit_out_of_memory(delta, OOM_MMAP_ERROR, "offset table expansion");
  84     }
  85     assert(_vs.high() == high + delta, "invalid expansion");
  86     // Initialization of the contents is left to the
  87     // G1BlockOffsetArray that uses it.
  88   } else {
  89     delta = ReservedSpace::page_align_size_down(old_size - new_size);
  90     if (delta == 0) return;
  91     _vs.shrink_by(delta);
  92     assert(_vs.high() == high - delta, "invalid expansion");
  93   }
  94 }
  95 
  96 bool G1BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {
  97   assert(p >= _reserved.start(), "just checking");
  98   size_t delta = pointer_delta(p, _reserved.start());
  99   return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;
 100 }
 101 
 102 void G1BlockOffsetSharedArray::set_offset_array(HeapWord* left, HeapWord* right, u_char offset) {
 103   check_index(index_for(right - 1), "right address out of range");
 104   assert(left  < right, "Heap addresses out of order");
 105   size_t num_cards = pointer_delta(right, left) >> LogN_words;
 106   if (UseMemSetInBOT) {
 107     memset(&_offset_array[index_for(left)], offset, num_cards);
 108   } else {
 109     size_t i = index_for(left);
 110     const size_t end = i + num_cards;
 111     for (; i < end; i++) {
 112       _offset_array[i] = offset;
 113     }
 114   }
 115 }
 116 
 117 
 118 //////////////////////////////////////////////////////////////////////
 119 // G1BlockOffsetArray
 120 //////////////////////////////////////////////////////////////////////
 121 
 122 G1BlockOffsetArray::G1BlockOffsetArray(G1BlockOffsetSharedArray* array,
 123                                        MemRegion mr, bool init_to_zero) :
 124   G1BlockOffsetTable(mr.start(), mr.end()),
 125   _unallocated_block(_bottom),
 126   _array(array), _gsp(NULL),
 127   _init_to_zero(init_to_zero) {
 128   assert(_bottom <= _end, "arguments out of order");
 129   if (!_init_to_zero) {
 130     // initialize cards to point back to mr.start()
 131     set_remainder_to_point_to_start(mr.start() + N_words, mr.end());
 132     _array->set_offset_array(0, 0);  // set first card to 0
 133   }
 134 }
 135 
 136 void G1BlockOffsetArray::set_space(G1OffsetTableContigSpace* sp) {
 137   _gsp = sp;
 138 }
 139 
 140 // The arguments follow the normal convention of denoting
 141 // a right-open interval: [start, end)
 142 void
 143 G1BlockOffsetArray:: set_remainder_to_point_to_start(HeapWord* start, HeapWord* end) {
 144 
 145   if (start >= end) {
 146     // The start address is equal to the end address (or to
 147     // the right of the end address) so there are not cards
 148     // that need to be updated..
 149     return;
 150   }
 151 
 152   // Write the backskip value for each region.
 153   //
 154   //    offset
 155   //    card             2nd                       3rd
 156   //     | +- 1st        |                         |
 157   //     v v             v                         v
 158   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+-+-+-
 159   //    |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...
 160   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+-+-+-
 161   //    11              19                        75
 162   //      12
 163   //
 164   //    offset card is the card that points to the start of an object
 165   //      x - offset value of offset card
 166   //    1st - start of first logarithmic region
 167   //      0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1
 168   //    2nd - start of second logarithmic region
 169   //      1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
 170   //    3rd - start of third logarithmic region
 171   //      2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
 172   //
 173   //    integer below the block offset entry is an example of
 174   //    the index of the entry
 175   //
 176   //    Given an address,
 177   //      Find the index for the address
 178   //      Find the block offset table entry
 179   //      Convert the entry to a back slide
 180   //        (e.g., with today's, offset = 0x81 =>
 181   //          back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
 182   //      Move back N (e.g., 8) entries and repeat with the
 183   //        value of the new entry
 184   //
 185   size_t start_card = _array->index_for(start);
 186   size_t end_card = _array->index_for(end-1);
 187   assert(start ==_array->address_for_index(start_card), "Precondition");
 188   assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");
 189   set_remainder_to_point_to_start_incl(start_card, end_card); // closed interval
 190 }
 191 
 192 // Unlike the normal convention in this code, the argument here denotes
 193 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
 194 // above.
 195 void
 196 G1BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card) {
 197   if (start_card > end_card) {
 198     return;
 199   }
 200   assert(start_card > _array->index_for(_bottom), "Cannot be first card");
 201   assert(_array->offset_array(start_card-1) <= N_words,
 202          "Offset card has an unexpected value");
 203   size_t start_card_for_region = start_card;
 204   u_char offset = max_jubyte;
 205   for (int i = 0; i < BlockOffsetArray::N_powers; i++) {
 206     // -1 so that the the card with the actual offset is counted.  Another -1
 207     // so that the reach ends in this region and not at the start
 208     // of the next.
 209     size_t reach = start_card - 1 + (BlockOffsetArray::power_to_cards_back(i+1) - 1);
 210     offset = N_words + i;
 211     if (reach >= end_card) {
 212       _array->set_offset_array(start_card_for_region, end_card, offset);
 213       start_card_for_region = reach + 1;
 214       break;
 215     }
 216     _array->set_offset_array(start_card_for_region, reach, offset);
 217     start_card_for_region = reach + 1;
 218   }
 219   assert(start_card_for_region > end_card, "Sanity check");
 220   DEBUG_ONLY(check_all_cards(start_card, end_card);)
 221 }
 222 
 223 // The block [blk_start, blk_end) has been allocated;
 224 // adjust the block offset table to represent this information;
 225 // right-open interval: [blk_start, blk_end)
 226 void
 227 G1BlockOffsetArray::alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
 228   mark_block(blk_start, blk_end);
 229   allocated(blk_start, blk_end);
 230 }
 231 
 232 // Adjust BOT to show that a previously whole block has been split
 233 // into two.
 234 void G1BlockOffsetArray::split_block(HeapWord* blk, size_t blk_size,
 235                                      size_t left_blk_size) {
 236   // Verify that the BOT shows [blk, blk + blk_size) to be one block.
 237   verify_single_block(blk, blk_size);
 238   // Update the BOT to indicate that [blk + left_blk_size, blk + blk_size)
 239   // is one single block.
 240   mark_block(blk + left_blk_size, blk + blk_size);
 241 }
 242 
 243 
 244 // Action_mark - update the BOT for the block [blk_start, blk_end).
 245 //               Current typical use is for splitting a block.
 246 // Action_single - update the BOT for an allocation.
 247 // Action_verify - BOT verification.
 248 void G1BlockOffsetArray::do_block_internal(HeapWord* blk_start,
 249                                            HeapWord* blk_end,
 250                                            Action action) {
 251   assert(Universe::heap()->is_in_reserved(blk_start),
 252          "reference must be into the heap");
 253   assert(Universe::heap()->is_in_reserved(blk_end-1),
 254          "limit must be within the heap");
 255   // This is optimized to make the test fast, assuming we only rarely
 256   // cross boundaries.
 257   uintptr_t end_ui = (uintptr_t)(blk_end - 1);
 258   uintptr_t start_ui = (uintptr_t)blk_start;
 259   // Calculate the last card boundary preceding end of blk
 260   intptr_t boundary_before_end = (intptr_t)end_ui;
 261   clear_bits(boundary_before_end, right_n_bits(LogN));
 262   if (start_ui <= (uintptr_t)boundary_before_end) {
 263     // blk starts at or crosses a boundary
 264     // Calculate index of card on which blk begins
 265     size_t    start_index = _array->index_for(blk_start);
 266     // Index of card on which blk ends
 267     size_t    end_index   = _array->index_for(blk_end - 1);
 268     // Start address of card on which blk begins
 269     HeapWord* boundary    = _array->address_for_index(start_index);
 270     assert(boundary <= blk_start, "blk should start at or after boundary");
 271     if (blk_start != boundary) {
 272       // blk starts strictly after boundary
 273       // adjust card boundary and start_index forward to next card
 274       boundary += N_words;
 275       start_index++;
 276     }
 277     assert(start_index <= end_index, "monotonicity of index_for()");
 278     assert(boundary <= (HeapWord*)boundary_before_end, "tautology");
 279     switch (action) {
 280       case Action_mark: {
 281         if (init_to_zero()) {
 282           _array->set_offset_array(start_index, boundary, blk_start);
 283           break;
 284         } // Else fall through to the next case
 285       }
 286       case Action_single: {
 287         _array->set_offset_array(start_index, boundary, blk_start);
 288         // We have finished marking the "offset card". We need to now
 289         // mark the subsequent cards that this blk spans.
 290         if (start_index < end_index) {
 291           HeapWord* rem_st = _array->address_for_index(start_index) + N_words;
 292           HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
 293           set_remainder_to_point_to_start(rem_st, rem_end);
 294         }
 295         break;
 296       }
 297       case Action_check: {
 298         _array->check_offset_array(start_index, boundary, blk_start);
 299         // We have finished checking the "offset card". We need to now
 300         // check the subsequent cards that this blk spans.
 301         check_all_cards(start_index + 1, end_index);
 302         break;
 303       }
 304       default:
 305         ShouldNotReachHere();
 306     }
 307   }
 308 }
 309 
 310 // The card-interval [start_card, end_card] is a closed interval; this
 311 // is an expensive check -- use with care and only under protection of
 312 // suitable flag.
 313 void G1BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {
 314 
 315   if (end_card < start_card) {
 316     return;
 317   }
 318   guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");
 319   for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
 320     u_char entry = _array->offset_array(c);
 321     if (c - start_card > BlockOffsetArray::power_to_cards_back(1)) {
 322       guarantee(entry > N_words,
 323                 err_msg("Should be in logarithmic region - "
 324                         "entry: %u, "
 325                         "_array->offset_array(c): %u, "
 326                         "N_words: %u",
 327                         (uint)entry, (uint)_array->offset_array(c), (uint)N_words));
 328     }
 329     size_t backskip = BlockOffsetArray::entry_to_cards_back(entry);
 330     size_t landing_card = c - backskip;
 331     guarantee(landing_card >= (start_card - 1), "Inv");
 332     if (landing_card >= start_card) {
 333       guarantee(_array->offset_array(landing_card) <= entry,
 334                 err_msg("Monotonicity - landing_card offset: %u, "
 335                         "entry: %u",
 336                         (uint)_array->offset_array(landing_card), (uint)entry));
 337     } else {
 338       guarantee(landing_card == start_card - 1, "Tautology");
 339       // Note that N_words is the maximum offset value
 340       guarantee(_array->offset_array(landing_card) <= N_words,
 341                 err_msg("landing card offset: %u, "
 342                         "N_words: %u",
 343                         (uint)_array->offset_array(landing_card), (uint)N_words));
 344     }
 345   }
 346 }
 347 
 348 // The range [blk_start, blk_end) represents a single contiguous block
 349 // of storage; modify the block offset table to represent this
 350 // information; Right-open interval: [blk_start, blk_end)
 351 // NOTE: this method does _not_ adjust _unallocated_block.
 352 void
 353 G1BlockOffsetArray::single_block(HeapWord* blk_start, HeapWord* blk_end) {
 354   do_block_internal(blk_start, blk_end, Action_single);
 355 }
 356 
 357 // Mark the BOT such that if [blk_start, blk_end) straddles a card
 358 // boundary, the card following the first such boundary is marked
 359 // with the appropriate offset.
 360 // NOTE: this method does _not_ adjust _unallocated_block or
 361 // any cards subsequent to the first one.
 362 void
 363 G1BlockOffsetArray::mark_block(HeapWord* blk_start, HeapWord* blk_end) {
 364   do_block_internal(blk_start, blk_end, Action_mark);
 365 }
 366 
 367 HeapWord* G1BlockOffsetArray::block_start_unsafe(const void* addr) {
 368   assert(_bottom <= addr && addr < _end,
 369          "addr must be covered by this Array");
 370   // Must read this exactly once because it can be modified by parallel
 371   // allocation.
 372   HeapWord* ub = _unallocated_block;
 373   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
 374     assert(ub < _end, "tautology (see above)");
 375     return ub;
 376   }
 377   // Otherwise, find the block start using the table.
 378   HeapWord* q = block_at_or_preceding(addr, false, 0);
 379   return forward_to_block_containing_addr(q, addr);
 380 }
 381 
 382 // This duplicates a little code from the above: unavoidable.
 383 HeapWord*
 384 G1BlockOffsetArray::block_start_unsafe_const(const void* addr) const {
 385   assert(_bottom <= addr && addr < _end,
 386          "addr must be covered by this Array");
 387   // Must read this exactly once because it can be modified by parallel
 388   // allocation.
 389   HeapWord* ub = _unallocated_block;
 390   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
 391     assert(ub < _end, "tautology (see above)");
 392     return ub;
 393   }
 394   // Otherwise, find the block start using the table.
 395   HeapWord* q = block_at_or_preceding(addr, false, 0);
 396   HeapWord* n = q + block_size(q);
 397   return forward_to_block_containing_addr_const(q, n, addr);
 398 }
 399 
 400 
 401 HeapWord*
 402 G1BlockOffsetArray::forward_to_block_containing_addr_slow(HeapWord* q,
 403                                                           HeapWord* n,
 404                                                           const void* addr) {
 405   // We're not in the normal case.  We need to handle an important subcase
 406   // here: LAB allocation.  An allocation previously recorded in the
 407   // offset table was actually a lab allocation, and was divided into
 408   // several objects subsequently.  Fix this situation as we answer the
 409   // query, by updating entries as we cross them.
 410 
 411   // If the fist object's end q is at the card boundary. Start refining
 412   // with the corresponding card (the value of the entry will be basically
 413   // set to 0). If the object crosses the boundary -- start from the next card.
 414   size_t n_index = _array->index_for(n);
 415   size_t next_index = _array->index_for(n) + !_array->is_card_boundary(n);
 416   // Calculate a consistent next boundary.  If "n" is not at the boundary
 417   // already, step to the boundary.
 418   HeapWord* next_boundary = _array->address_for_index(n_index) +
 419                             (n_index == next_index ? 0 : N_words);
 420   assert(next_boundary <= _array->_end,
 421          err_msg("next_boundary is beyond the end of the covered region "
 422                  " next_boundary " PTR_FORMAT " _array->_end " PTR_FORMAT,
 423                  next_boundary, _array->_end));
 424   if (addr >= gsp()->top()) return gsp()->top();
 425   while (next_boundary < addr) {
 426     while (n <= next_boundary) {
 427       q = n;
 428       oop obj = oop(q);
 429       if (obj->klass_or_null() == NULL) return q;
 430       n += block_size(q);
 431     }
 432     assert(q <= next_boundary && n > next_boundary, "Consequence of loop");
 433     // [q, n) is the block that crosses the boundary.
 434     alloc_block_work2(&next_boundary, &next_index, q, n);
 435   }
 436   return forward_to_block_containing_addr_const(q, n, addr);
 437 }
 438 
 439 HeapWord* G1BlockOffsetArray::block_start_careful(const void* addr) const {
 440   assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
 441 
 442   assert(_bottom <= addr && addr < _end,
 443          "addr must be covered by this Array");
 444   // Must read this exactly once because it can be modified by parallel
 445   // allocation.
 446   HeapWord* ub = _unallocated_block;
 447   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
 448     assert(ub < _end, "tautology (see above)");
 449     return ub;
 450   }
 451 
 452   // Otherwise, find the block start using the table, but taking
 453   // care (cf block_start_unsafe() above) not to parse any objects/blocks
 454   // on the cards themselves.
 455   size_t index = _array->index_for(addr);
 456   assert(_array->address_for_index(index) == addr,
 457          "arg should be start of card");
 458 
 459   HeapWord* q = (HeapWord*)addr;
 460   uint offset;
 461   do {
 462     offset = _array->offset_array(index--);
 463     q -= offset;
 464   } while (offset == N_words);
 465   assert(q <= addr, "block start should be to left of arg");
 466   return q;
 467 }
 468 
 469 // Note that the committed size of the covered space may have changed,
 470 // so the table size might also wish to change.
 471 void G1BlockOffsetArray::resize(size_t new_word_size) {
 472   HeapWord* new_end = _bottom + new_word_size;
 473   if (_end < new_end && !init_to_zero()) {
 474     // verify that the old and new boundaries are also card boundaries
 475     assert(_array->is_card_boundary(_end),
 476            "_end not a card boundary");
 477     assert(_array->is_card_boundary(new_end),
 478            "new _end would not be a card boundary");
 479     // set all the newly added cards
 480     _array->set_offset_array(_end, new_end, N_words);
 481   }
 482   _end = new_end;  // update _end
 483 }
 484 
 485 void G1BlockOffsetArray::set_region(MemRegion mr) {
 486   _bottom = mr.start();
 487   _end = mr.end();
 488 }
 489 
 490 //
 491 //              threshold_
 492 //              |   _index_
 493 //              v   v
 494 //      +-------+-------+-------+-------+-------+
 495 //      | i-1   |   i   | i+1   | i+2   | i+3   |
 496 //      +-------+-------+-------+-------+-------+
 497 //       ( ^    ]
 498 //         block-start
 499 //
 500 void G1BlockOffsetArray::alloc_block_work2(HeapWord** threshold_, size_t* index_,
 501                                            HeapWord* blk_start, HeapWord* blk_end) {
 502   // For efficiency, do copy-in/copy-out.
 503   HeapWord* threshold = *threshold_;
 504   size_t    index = *index_;
 505 
 506   assert(blk_start != NULL && blk_end > blk_start,
 507          "phantom block");
 508   assert(blk_end > threshold, "should be past threshold");
 509   assert(blk_start <= threshold, "blk_start should be at or before threshold");
 510   assert(pointer_delta(threshold, blk_start) <= N_words,
 511          "offset should be <= BlockOffsetSharedArray::N");
 512   assert(Universe::heap()->is_in_reserved(blk_start),
 513          "reference must be into the heap");
 514   assert(Universe::heap()->is_in_reserved(blk_end-1),
 515          "limit must be within the heap");
 516   assert(threshold == _array->_reserved.start() + index*N_words,
 517          "index must agree with threshold");
 518 
 519   DEBUG_ONLY(size_t orig_index = index;)
 520 
 521   // Mark the card that holds the offset into the block.  Note
 522   // that _next_offset_index and _next_offset_threshold are not
 523   // updated until the end of this method.
 524   _array->set_offset_array(index, threshold, blk_start);
 525 
 526   // We need to now mark the subsequent cards that this blk spans.
 527 
 528   // Index of card on which blk ends.
 529   size_t end_index   = _array->index_for(blk_end - 1);
 530 
 531   // Are there more cards left to be updated?
 532   if (index + 1 <= end_index) {
 533     HeapWord* rem_st  = _array->address_for_index(index + 1);
 534     // Calculate rem_end this way because end_index
 535     // may be the last valid index in the covered region.
 536     HeapWord* rem_end = _array->address_for_index(end_index) +  N_words;
 537     set_remainder_to_point_to_start(rem_st, rem_end);
 538   }
 539 
 540   index = end_index + 1;
 541   // Calculate threshold_ this way because end_index
 542   // may be the last valid index in the covered region.
 543   threshold = _array->address_for_index(end_index) + N_words;
 544   assert(threshold >= blk_end, "Incorrect offset threshold");
 545 
 546   // index_ and threshold_ updated here.
 547   *threshold_ = threshold;
 548   *index_ = index;
 549 
 550 #ifdef ASSERT
 551   // The offset can be 0 if the block starts on a boundary.  That
 552   // is checked by an assertion above.
 553   size_t start_index = _array->index_for(blk_start);
 554   HeapWord* boundary = _array->address_for_index(start_index);
 555   assert((_array->offset_array(orig_index) == 0 &&
 556           blk_start == boundary) ||
 557           (_array->offset_array(orig_index) > 0 &&
 558          _array->offset_array(orig_index) <= N_words),
 559          err_msg("offset array should have been set - "
 560                   "orig_index offset: %u, "
 561                   "blk_start: " PTR_FORMAT ", "
 562                   "boundary: " PTR_FORMAT,
 563                   (uint)_array->offset_array(orig_index),
 564                   blk_start, boundary));
 565   for (size_t j = orig_index + 1; j <= end_index; j++) {
 566     assert(_array->offset_array(j) > 0 &&
 567            _array->offset_array(j) <=
 568              (u_char) (N_words+BlockOffsetArray::N_powers-1),
 569            err_msg("offset array should have been set - "
 570                    "%u not > 0 OR %u not <= %u",
 571                    (uint) _array->offset_array(j),
 572                    (uint) _array->offset_array(j),
 573                    (uint) (N_words+BlockOffsetArray::N_powers-1)));
 574   }
 575 #endif
 576 }
 577 
 578 bool
 579 G1BlockOffsetArray::verify_for_object(HeapWord* obj_start,
 580                                       size_t word_size) const {
 581   size_t first_card = _array->index_for(obj_start);
 582   size_t last_card = _array->index_for(obj_start + word_size - 1);
 583   if (!_array->is_card_boundary(obj_start)) {
 584     // If the object is not on a card boundary the BOT entry of the
 585     // first card should point to another object so we should not
 586     // check that one.
 587     first_card += 1;
 588   }
 589   for (size_t card = first_card; card <= last_card; card += 1) {
 590     HeapWord* card_addr = _array->address_for_index(card);
 591     HeapWord* block_start = block_start_const(card_addr);
 592     if (block_start != obj_start) {
 593       gclog_or_tty->print_cr("block start: "PTR_FORMAT" is incorrect - "
 594                              "card index: "SIZE_FORMAT" "
 595                              "card addr: "PTR_FORMAT" BOT entry: %u "
 596                              "obj: "PTR_FORMAT" word size: "SIZE_FORMAT" "
 597                              "cards: ["SIZE_FORMAT","SIZE_FORMAT"]",
 598                              block_start, card, card_addr,
 599                              _array->offset_array(card),
 600                              obj_start, word_size, first_card, last_card);
 601       return false;
 602     }
 603   }
 604   return true;
 605 }
 606 
 607 #ifndef PRODUCT
 608 void
 609 G1BlockOffsetArray::print_on(outputStream* out) {
 610   size_t from_index = _array->index_for(_bottom);
 611   size_t to_index = _array->index_for(_end);
 612   out->print_cr(">> BOT for area ["PTR_FORMAT","PTR_FORMAT") "
 613                 "cards ["SIZE_FORMAT","SIZE_FORMAT")",
 614                 _bottom, _end, from_index, to_index);
 615   for (size_t i = from_index; i < to_index; ++i) {
 616     out->print_cr("  entry "SIZE_FORMAT_W(8)" | "PTR_FORMAT" : %3u",
 617                   i, _array->address_for_index(i),
 618                   (uint) _array->offset_array(i));
 619   }
 620 }
 621 #endif // !PRODUCT
 622 
 623 //////////////////////////////////////////////////////////////////////
 624 // G1BlockOffsetArrayContigSpace
 625 //////////////////////////////////////////////////////////////////////
 626 
 627 HeapWord*
 628 G1BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) {
 629   assert(_bottom <= addr && addr < _end,
 630          "addr must be covered by this Array");
 631   HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);
 632   return forward_to_block_containing_addr(q, addr);
 633 }
 634 
 635 HeapWord*
 636 G1BlockOffsetArrayContigSpace::
 637 block_start_unsafe_const(const void* addr) const {
 638   assert(_bottom <= addr && addr < _end,
 639          "addr must be covered by this Array");
 640   HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);
 641   HeapWord* n = q + block_size(q);
 642   return forward_to_block_containing_addr_const(q, n, addr);
 643 }
 644 
 645 G1BlockOffsetArrayContigSpace::
 646 G1BlockOffsetArrayContigSpace(G1BlockOffsetSharedArray* array,
 647                               MemRegion mr) :
 648   G1BlockOffsetArray(array, mr, true)
 649 {
 650   _next_offset_threshold = NULL;
 651   _next_offset_index = 0;
 652 }
 653 
 654 HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold() {
 655   assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
 656          "just checking");
 657   _next_offset_index = _array->index_for(_bottom);
 658   _next_offset_index++;
 659   _next_offset_threshold =
 660     _array->address_for_index(_next_offset_index);
 661   return _next_offset_threshold;
 662 }
 663 
 664 void G1BlockOffsetArrayContigSpace::zero_bottom_entry() {
 665   assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
 666          "just checking");
 667   size_t bottom_index = _array->index_for(_bottom);
 668   assert(_array->address_for_index(bottom_index) == _bottom,
 669          "Precondition of call");
 670   _array->set_offset_array(bottom_index, 0);
 671 }
 672 
 673 void
 674 G1BlockOffsetArrayContigSpace::set_for_starts_humongous(HeapWord* new_top) {
 675   assert(new_top <= _end, "_end should have already been updated");
 676 
 677   // The first BOT entry should have offset 0.
 678   zero_bottom_entry();
 679   initialize_threshold();
 680   alloc_block(_bottom, new_top);
 681  }
 682 
 683 #ifndef PRODUCT
 684 void
 685 G1BlockOffsetArrayContigSpace::print_on(outputStream* out) {
 686   G1BlockOffsetArray::print_on(out);
 687   out->print_cr("  next offset threshold: "PTR_FORMAT, _next_offset_threshold);
 688   out->print_cr("  next offset index:     "SIZE_FORMAT, _next_offset_index);
 689 }
 690 #endif // !PRODUCT