1 /*
   2  * Copyright (c) 2001, 2018, 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/parallel/gcTaskManager.hpp"
  27 #include "gc/parallel/objectStartArray.inline.hpp"
  28 #include "gc/parallel/parallelScavengeHeap.inline.hpp"
  29 #include "gc/parallel/psCardTable.hpp"
  30 #include "gc/parallel/psPromotionManager.inline.hpp"
  31 #include "gc/parallel/psScavenge.hpp"
  32 #include "gc/parallel/psTasks.hpp"
  33 #include "gc/parallel/psYoungGen.hpp"
  34 #include "oops/access.inline.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "runtime/prefetch.inline.hpp"
  37 #include "utilities/align.hpp"
  38 
  39 // Checks an individual oop for missing precise marks. Mark
  40 // may be either dirty or newgen.
  41 class CheckForUnmarkedOops : public OopClosure {
  42  private:
  43   PSYoungGen*  _young_gen;
  44   PSCardTable* _card_table;
  45   HeapWord*    _unmarked_addr;
  46 
  47  protected:
  48   template <class T> void do_oop_work(T* p) {
  49     oop obj = RawAccess<>::oop_load(p);
  50     if (_young_gen->is_in_reserved(obj) &&
  51         !_card_table->addr_is_marked_imprecise(p)) {
  52       // Don't overwrite the first missing card mark
  53       if (_unmarked_addr == NULL) {
  54         _unmarked_addr = (HeapWord*)p;
  55       }
  56     }
  57   }
  58 
  59  public:
  60   CheckForUnmarkedOops(PSYoungGen* young_gen, PSCardTable* card_table) :
  61     _young_gen(young_gen), _card_table(card_table), _unmarked_addr(NULL) { }
  62 
  63   virtual void do_oop(oop* p)       { CheckForUnmarkedOops::do_oop_work(p); }
  64   virtual void do_oop(narrowOop* p) { CheckForUnmarkedOops::do_oop_work(p); }
  65 
  66   bool has_unmarked_oop() {
  67     return _unmarked_addr != NULL;
  68   }
  69 };
  70 
  71 // Checks all objects for the existence of some type of mark,
  72 // precise or imprecise, dirty or newgen.
  73 class CheckForUnmarkedObjects : public ObjectClosure {
  74  private:
  75   PSYoungGen*  _young_gen;
  76   PSCardTable* _card_table;
  77 
  78  public:
  79   CheckForUnmarkedObjects() {
  80     ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
  81     _young_gen = heap->young_gen();
  82     _card_table = heap->card_table();
  83   }
  84 
  85   // Card marks are not precise. The current system can leave us with
  86   // a mismatch of precise marks and beginning of object marks. This means
  87   // we test for missing precise marks first. If any are found, we don't
  88   // fail unless the object head is also unmarked.
  89   virtual void do_object(oop obj) {
  90     CheckForUnmarkedOops object_check(_young_gen, _card_table);
  91     obj->oop_iterate_no_header(&object_check);
  92     if (object_check.has_unmarked_oop()) {
  93       guarantee(_card_table->addr_is_marked_imprecise(obj), "Found unmarked young_gen object");
  94     }
  95   }
  96 };
  97 
  98 // Checks for precise marking of oops as newgen.
  99 class CheckForPreciseMarks : public OopClosure {
 100  private:
 101   PSYoungGen*  _young_gen;
 102   PSCardTable* _card_table;
 103 
 104  protected:
 105   template <class T> void do_oop_work(T* p) {
 106     oop obj = RawAccess<OOP_NOT_NULL>::oop_load(p);
 107     if (_young_gen->is_in_reserved(obj)) {
 108       assert(_card_table->addr_is_marked_precise(p), "Found unmarked precise oop");
 109       _card_table->set_card_newgen(p);
 110     }
 111   }
 112 
 113  public:
 114   CheckForPreciseMarks(PSYoungGen* young_gen, PSCardTable* card_table) :
 115     _young_gen(young_gen), _card_table(card_table) { }
 116 
 117   virtual void do_oop(oop* p)       { CheckForPreciseMarks::do_oop_work(p); }
 118   virtual void do_oop(narrowOop* p) { CheckForPreciseMarks::do_oop_work(p); }
 119 };
 120 
 121 // We get passed the space_top value to prevent us from traversing into
 122 // the old_gen promotion labs, which cannot be safely parsed.
 123 
 124 // Do not call this method if the space is empty.
 125 // It is a waste to start tasks and get here only to
 126 // do no work.  If this method needs to be called
 127 // when the space is empty, fix the calculation of
 128 // end_card to allow sp_top == sp->bottom().
 129 
 130 void PSCardTable::scavenge_contents_parallel(ObjectStartArray* start_array,
 131                                              MutableSpace* sp,
 132                                              HeapWord* space_top,
 133                                              PSPromotionManager* pm,
 134                                              uint stripe_number,
 135                                              uint stripe_total) {
 136   int ssize = 128; // Naked constant!  Work unit = 64k.
 137   int dirty_card_count = 0;
 138 
 139   // It is a waste to get here if empty.
 140   assert(sp->bottom() < sp->top(), "Should not be called if empty");
 141   oop* sp_top = (oop*)space_top;
 142   jbyte* start_card = byte_for(sp->bottom());
 143   jbyte* end_card   = byte_for(sp_top - 1) + 1;
 144   oop* last_scanned = NULL; // Prevent scanning objects more than once
 145   // The width of the stripe ssize*stripe_total must be
 146   // consistent with the number of stripes so that the complete slice
 147   // is covered.
 148   size_t slice_width = ssize * stripe_total;
 149   for (jbyte* slice = start_card; slice < end_card; slice += slice_width) {
 150     jbyte* worker_start_card = slice + stripe_number * ssize;
 151     if (worker_start_card >= end_card)
 152       return; // We're done.
 153 
 154     jbyte* worker_end_card = worker_start_card + ssize;
 155     if (worker_end_card > end_card)
 156       worker_end_card = end_card;
 157 
 158     // We do not want to scan objects more than once. In order to accomplish
 159     // this, we assert that any object with an object head inside our 'slice'
 160     // belongs to us. We may need to extend the range of scanned cards if the
 161     // last object continues into the next 'slice'.
 162     //
 163     // Note! ending cards are exclusive!
 164     HeapWord* slice_start = addr_for(worker_start_card);
 165     HeapWord* slice_end = MIN2((HeapWord*) sp_top, addr_for(worker_end_card));
 166 
 167 #ifdef ASSERT
 168     if (GCWorkerDelayMillis > 0) {
 169       // Delay 1 worker so that it proceeds after all the work
 170       // has been completed.
 171       if (stripe_number < 2) {
 172         os::sleep(Thread::current(), GCWorkerDelayMillis, false);
 173       }
 174     }
 175 #endif
 176 
 177     // If there are not objects starting within the chunk, skip it.
 178     if (!start_array->object_starts_in_range(slice_start, slice_end)) {
 179       continue;
 180     }
 181     // Update our beginning addr
 182     HeapWord* first_object = start_array->object_start(slice_start);
 183     debug_only(oop* first_object_within_slice = (oop*) first_object;)
 184     if (first_object < slice_start) {
 185       last_scanned = (oop*)(first_object + oop(first_object)->size());
 186       debug_only(first_object_within_slice = last_scanned;)
 187       worker_start_card = byte_for(last_scanned);
 188     }
 189 
 190     // Update the ending addr
 191     if (slice_end < (HeapWord*)sp_top) {
 192       // The subtraction is important! An object may start precisely at slice_end.
 193       HeapWord* last_object = start_array->object_start(slice_end - 1);
 194       slice_end = last_object + oop(last_object)->size();
 195       // worker_end_card is exclusive, so bump it one past the end of last_object's
 196       // covered span.
 197       worker_end_card = byte_for(slice_end) + 1;
 198 
 199       if (worker_end_card > end_card)
 200         worker_end_card = end_card;
 201     }
 202 
 203     assert(slice_end <= (HeapWord*)sp_top, "Last object in slice crosses space boundary");
 204     assert(is_valid_card_address(worker_start_card), "Invalid worker start card");
 205     assert(is_valid_card_address(worker_end_card), "Invalid worker end card");
 206     // Note that worker_start_card >= worker_end_card is legal, and happens when
 207     // an object spans an entire slice.
 208     assert(worker_start_card <= end_card, "worker start card beyond end card");
 209     assert(worker_end_card <= end_card, "worker end card beyond end card");
 210 
 211     jbyte* current_card = worker_start_card;
 212     while (current_card < worker_end_card) {
 213       // Find an unclean card.
 214       while (current_card < worker_end_card && card_is_clean(*current_card)) {
 215         current_card++;
 216       }
 217       jbyte* first_unclean_card = current_card;
 218 
 219       // Find the end of a run of contiguous unclean cards
 220       while (current_card < worker_end_card && !card_is_clean(*current_card)) {
 221         while (current_card < worker_end_card && !card_is_clean(*current_card)) {
 222           current_card++;
 223         }
 224 
 225         if (current_card < worker_end_card) {
 226           // Some objects may be large enough to span several cards. If such
 227           // an object has more than one dirty card, separated by a clean card,
 228           // we will attempt to scan it twice. The test against "last_scanned"
 229           // prevents the redundant object scan, but it does not prevent newly
 230           // marked cards from being cleaned.
 231           HeapWord* last_object_in_dirty_region = start_array->object_start(addr_for(current_card)-1);
 232           size_t size_of_last_object = oop(last_object_in_dirty_region)->size();
 233           HeapWord* end_of_last_object = last_object_in_dirty_region + size_of_last_object;
 234           jbyte* ending_card_of_last_object = byte_for(end_of_last_object);
 235           assert(ending_card_of_last_object <= worker_end_card, "ending_card_of_last_object is greater than worker_end_card");
 236           if (ending_card_of_last_object > current_card) {
 237             // This means the object spans the next complete card.
 238             // We need to bump the current_card to ending_card_of_last_object
 239             current_card = ending_card_of_last_object;
 240           }
 241         }
 242       }
 243       jbyte* following_clean_card = current_card;
 244 
 245       if (first_unclean_card < worker_end_card) {
 246         oop* p = (oop*) start_array->object_start(addr_for(first_unclean_card));
 247         assert((HeapWord*)p <= addr_for(first_unclean_card), "checking");
 248         // "p" should always be >= "last_scanned" because newly GC dirtied
 249         // cards are no longer scanned again (see comment at end
 250         // of loop on the increment of "current_card").  Test that
 251         // hypothesis before removing this code.
 252         // If this code is removed, deal with the first time through
 253         // the loop when the last_scanned is the object starting in
 254         // the previous slice.
 255         assert((p >= last_scanned) ||
 256                (last_scanned == first_object_within_slice),
 257                "Should no longer be possible");
 258         if (p < last_scanned) {
 259           // Avoid scanning more than once; this can happen because
 260           // newgen cards set by GC may a different set than the
 261           // originally dirty set
 262           p = last_scanned;
 263         }
 264         oop* to = (oop*)addr_for(following_clean_card);
 265 
 266         // Test slice_end first!
 267         if ((HeapWord*)to > slice_end) {
 268           to = (oop*)slice_end;
 269         } else if (to > sp_top) {
 270           to = sp_top;
 271         }
 272 
 273         // we know which cards to scan, now clear them
 274         if (first_unclean_card <= worker_start_card+1)
 275           first_unclean_card = worker_start_card+1;
 276         if (following_clean_card >= worker_end_card-1)
 277           following_clean_card = worker_end_card-1;
 278 
 279         while (first_unclean_card < following_clean_card) {
 280           *first_unclean_card++ = clean_card;
 281         }
 282 
 283         const int interval = PrefetchScanIntervalInBytes;
 284         // scan all objects in the range
 285         if (interval != 0) {
 286           while (p < to) {
 287             Prefetch::write(p, interval);
 288             oop m = oop(p);
 289             assert(oopDesc::is_oop_or_null(m), "Expected an oop or NULL for header field at " PTR_FORMAT, p2i(m));
 290             pm->push_contents(m);
 291             p += m->size();
 292           }
 293           pm->drain_stacks_cond_depth();
 294         } else {
 295           while (p < to) {
 296             oop m = oop(p);
 297             assert(oopDesc::is_oop_or_null(m), "Expected an oop or NULL for header field at " PTR_FORMAT, p2i(m));
 298             pm->push_contents(m);
 299             p += m->size();
 300           }
 301           pm->drain_stacks_cond_depth();
 302         }
 303         last_scanned = p;
 304       }
 305       // "current_card" is still the "following_clean_card" or
 306       // the current_card is >= the worker_end_card so the
 307       // loop will not execute again.
 308       assert((current_card == following_clean_card) ||
 309              (current_card >= worker_end_card),
 310         "current_card should only be incremented if it still equals "
 311         "following_clean_card");
 312       // Increment current_card so that it is not processed again.
 313       // It may now be dirty because a old-to-young pointer was
 314       // found on it an updated.  If it is now dirty, it cannot be
 315       // be safely cleaned in the next iteration.
 316       current_card++;
 317     }
 318   }
 319 }
 320 
 321 // This should be called before a scavenge.
 322 void PSCardTable::verify_all_young_refs_imprecise() {
 323   CheckForUnmarkedObjects check;
 324 
 325   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 326   PSOldGen* old_gen = heap->old_gen();
 327 
 328   old_gen->object_iterate(&check);
 329 }
 330 
 331 // This should be called immediately after a scavenge, before mutators resume.
 332 void PSCardTable::verify_all_young_refs_precise() {
 333   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 334   PSOldGen* old_gen = heap->old_gen();
 335 
 336   CheckForPreciseMarks check(heap->young_gen(), this);
 337 
 338   old_gen->oop_iterate_no_header(&check);
 339 
 340   verify_all_young_refs_precise_helper(old_gen->object_space()->used_region());
 341 }
 342 
 343 void PSCardTable::verify_all_young_refs_precise_helper(MemRegion mr) {
 344   jbyte* bot = byte_for(mr.start());
 345   jbyte* top = byte_for(mr.end());
 346   while (bot <= top) {
 347     assert(*bot == clean_card || *bot == verify_card, "Found unwanted or unknown card mark");
 348     if (*bot == verify_card)
 349       *bot = youngergen_card;
 350     bot++;
 351   }
 352 }
 353 
 354 bool PSCardTable::addr_is_marked_imprecise(void *addr) {
 355   jbyte* p = byte_for(addr);
 356   jbyte val = *p;
 357 
 358   if (card_is_dirty(val))
 359     return true;
 360 
 361   if (card_is_newgen(val))
 362     return true;
 363 
 364   if (card_is_clean(val))
 365     return false;
 366 
 367   assert(false, "Found unhandled card mark type");
 368 
 369   return false;
 370 }
 371 
 372 // Also includes verify_card
 373 bool PSCardTable::addr_is_marked_precise(void *addr) {
 374   jbyte* p = byte_for(addr);
 375   jbyte val = *p;
 376 
 377   if (card_is_newgen(val))
 378     return true;
 379 
 380   if (card_is_verify(val))
 381     return true;
 382 
 383   if (card_is_clean(val))
 384     return false;
 385 
 386   if (card_is_dirty(val))
 387     return false;
 388 
 389   assert(false, "Found unhandled card mark type");
 390 
 391   return false;
 392 }
 393 
 394 // Assumes that only the base or the end changes.  This allows indentification
 395 // of the region that is being resized.  The
 396 // CardTable::resize_covered_region() is used for the normal case
 397 // where the covered regions are growing or shrinking at the high end.
 398 // The method resize_covered_region_by_end() is analogous to
 399 // CardTable::resize_covered_region() but
 400 // for regions that grow or shrink at the low end.
 401 void PSCardTable::resize_covered_region(MemRegion new_region) {
 402   for (int i = 0; i < _cur_covered_regions; i++) {
 403     if (_covered[i].start() == new_region.start()) {
 404       // Found a covered region with the same start as the
 405       // new region.  The region is growing or shrinking
 406       // from the start of the region.
 407       resize_covered_region_by_start(new_region);
 408       return;
 409     }
 410     if (_covered[i].start() > new_region.start()) {
 411       break;
 412     }
 413   }
 414 
 415   int changed_region = -1;
 416   for (int j = 0; j < _cur_covered_regions; j++) {
 417     if (_covered[j].end() == new_region.end()) {
 418       changed_region = j;
 419       // This is a case where the covered region is growing or shrinking
 420       // at the start of the region.
 421       assert(changed_region != -1, "Don't expect to add a covered region");
 422       assert(_covered[changed_region].byte_size() != new_region.byte_size(),
 423         "The sizes should be different here");
 424       resize_covered_region_by_end(changed_region, new_region);
 425       return;
 426     }
 427   }
 428   // This should only be a new covered region (where no existing
 429   // covered region matches at the start or the end).
 430   assert(_cur_covered_regions < _max_covered_regions,
 431     "An existing region should have been found");
 432   resize_covered_region_by_start(new_region);
 433 }
 434 
 435 void PSCardTable::resize_covered_region_by_start(MemRegion new_region) {
 436   CardTable::resize_covered_region(new_region);
 437   debug_only(verify_guard();)
 438 }
 439 
 440 void PSCardTable::resize_covered_region_by_end(int changed_region,
 441                                                MemRegion new_region) {
 442   assert(SafepointSynchronize::is_at_safepoint(),
 443     "Only expect an expansion at the low end at a GC");
 444   debug_only(verify_guard();)
 445 #ifdef ASSERT
 446   for (int k = 0; k < _cur_covered_regions; k++) {
 447     if (_covered[k].end() == new_region.end()) {
 448       assert(changed_region == k, "Changed region is incorrect");
 449       break;
 450     }
 451   }
 452 #endif
 453 
 454   // Commit new or uncommit old pages, if necessary.
 455   if (resize_commit_uncommit(changed_region, new_region)) {
 456     // Set the new start of the committed region
 457     resize_update_committed_table(changed_region, new_region);
 458   }
 459 
 460   // Update card table entries
 461   resize_update_card_table_entries(changed_region, new_region);
 462 
 463   // Update the covered region
 464   resize_update_covered_table(changed_region, new_region);
 465 
 466   int ind = changed_region;
 467   log_trace(gc, barrier)("CardTable::resize_covered_region: ");
 468   log_trace(gc, barrier)("    _covered[%d].start(): " INTPTR_FORMAT "  _covered[%d].last(): " INTPTR_FORMAT,
 469                 ind, p2i(_covered[ind].start()), ind, p2i(_covered[ind].last()));
 470   log_trace(gc, barrier)("    _committed[%d].start(): " INTPTR_FORMAT "  _committed[%d].last(): " INTPTR_FORMAT,
 471                 ind, p2i(_committed[ind].start()), ind, p2i(_committed[ind].last()));
 472   log_trace(gc, barrier)("    byte_for(start): " INTPTR_FORMAT "  byte_for(last): " INTPTR_FORMAT,
 473                 p2i(byte_for(_covered[ind].start())),  p2i(byte_for(_covered[ind].last())));
 474   log_trace(gc, barrier)("    addr_for(start): " INTPTR_FORMAT "  addr_for(last): " INTPTR_FORMAT,
 475                 p2i(addr_for((jbyte*) _committed[ind].start())), p2i(addr_for((jbyte*) _committed[ind].last())));
 476 
 477   debug_only(verify_guard();)
 478 }
 479 
 480 bool PSCardTable::resize_commit_uncommit(int changed_region,
 481                                          MemRegion new_region) {
 482   bool result = false;
 483   // Commit new or uncommit old pages, if necessary.
 484   MemRegion cur_committed = _committed[changed_region];
 485   assert(_covered[changed_region].end() == new_region.end(),
 486     "The ends of the regions are expected to match");
 487   // Extend the start of this _committed region to
 488   // to cover the start of any previous _committed region.
 489   // This forms overlapping regions, but never interior regions.
 490   HeapWord* min_prev_start = lowest_prev_committed_start(changed_region);
 491   if (min_prev_start < cur_committed.start()) {
 492     // Only really need to set start of "cur_committed" to
 493     // the new start (min_prev_start) but assertion checking code
 494     // below use cur_committed.end() so make it correct.
 495     MemRegion new_committed =
 496         MemRegion(min_prev_start, cur_committed.end());
 497     cur_committed = new_committed;
 498   }
 499 #ifdef ASSERT
 500   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 501   assert(cur_committed.start() == align_up(cur_committed.start(), os::vm_page_size()),
 502          "Starts should have proper alignment");
 503 #endif
 504 
 505   jbyte* new_start = byte_for(new_region.start());
 506   // Round down because this is for the start address
 507   HeapWord* new_start_aligned = align_down((HeapWord*)new_start, os::vm_page_size());
 508   // The guard page is always committed and should not be committed over.
 509   // This method is used in cases where the generation is growing toward
 510   // lower addresses but the guard region is still at the end of the
 511   // card table.  That still makes sense when looking for writes
 512   // off the end of the card table.
 513   if (new_start_aligned < cur_committed.start()) {
 514     // Expand the committed region
 515     //
 516     // Case A
 517     //                                          |+ guard +|
 518     //                          |+ cur committed +++++++++|
 519     //                  |+ new committed +++++++++++++++++|
 520     //
 521     // Case B
 522     //                                          |+ guard +|
 523     //                        |+ cur committed +|
 524     //                  |+ new committed +++++++|
 525     //
 526     // These are not expected because the calculation of the
 527     // cur committed region and the new committed region
 528     // share the same end for the covered region.
 529     // Case C
 530     //                                          |+ guard +|
 531     //                        |+ cur committed +|
 532     //                  |+ new committed +++++++++++++++++|
 533     // Case D
 534     //                                          |+ guard +|
 535     //                        |+ cur committed +++++++++++|
 536     //                  |+ new committed +++++++|
 537 
 538     HeapWord* new_end_for_commit =
 539       MIN2(cur_committed.end(), _guard_region.start());
 540     if(new_start_aligned < new_end_for_commit) {
 541       MemRegion new_committed =
 542         MemRegion(new_start_aligned, new_end_for_commit);
 543       os::commit_memory_or_exit((char*)new_committed.start(),
 544                                 new_committed.byte_size(), !ExecMem,
 545                                 "card table expansion");
 546     }
 547     result = true;
 548   } else if (new_start_aligned > cur_committed.start()) {
 549     // Shrink the committed region
 550 #if 0 // uncommitting space is currently unsafe because of the interactions
 551       // of growing and shrinking regions.  One region A can uncommit space
 552       // that it owns but which is being used by another region B (maybe).
 553       // Region B has not committed the space because it was already
 554       // committed by region A.
 555     MemRegion uncommit_region = committed_unique_to_self(changed_region,
 556       MemRegion(cur_committed.start(), new_start_aligned));
 557     if (!uncommit_region.is_empty()) {
 558       if (!os::uncommit_memory((char*)uncommit_region.start(),
 559                                uncommit_region.byte_size())) {
 560         // If the uncommit fails, ignore it.  Let the
 561         // committed table resizing go even though the committed
 562         // table will over state the committed space.
 563       }
 564     }
 565 #else
 566     assert(!result, "Should be false with current workaround");
 567 #endif
 568   }
 569   assert(_committed[changed_region].end() == cur_committed.end(),
 570     "end should not change");
 571   return result;
 572 }
 573 
 574 void PSCardTable::resize_update_committed_table(int changed_region,
 575                                                 MemRegion new_region) {
 576 
 577   jbyte* new_start = byte_for(new_region.start());
 578   // Set the new start of the committed region
 579   HeapWord* new_start_aligned = align_down((HeapWord*)new_start, os::vm_page_size());
 580   MemRegion new_committed = MemRegion(new_start_aligned,
 581                                       _committed[changed_region].end());
 582   _committed[changed_region] = new_committed;
 583   _committed[changed_region].set_start(new_start_aligned);
 584 }
 585 
 586 void PSCardTable::resize_update_card_table_entries(int changed_region,
 587                                                    MemRegion new_region) {
 588   debug_only(verify_guard();)
 589   MemRegion original_covered = _covered[changed_region];
 590   // Initialize the card entries.  Only consider the
 591   // region covered by the card table (_whole_heap)
 592   jbyte* entry;
 593   if (new_region.start() < _whole_heap.start()) {
 594     entry = byte_for(_whole_heap.start());
 595   } else {
 596     entry = byte_for(new_region.start());
 597   }
 598   jbyte* end = byte_for(original_covered.start());
 599   // If _whole_heap starts at the original covered regions start,
 600   // this loop will not execute.
 601   while (entry < end) { *entry++ = clean_card; }
 602 }
 603 
 604 void PSCardTable::resize_update_covered_table(int changed_region,
 605                                               MemRegion new_region) {
 606   // Update the covered region
 607   _covered[changed_region].set_start(new_region.start());
 608   _covered[changed_region].set_word_size(new_region.word_size());
 609 
 610   // reorder regions.  There should only be at most 1 out
 611   // of order.
 612   for (int i = _cur_covered_regions-1 ; i > 0; i--) {
 613     if (_covered[i].start() < _covered[i-1].start()) {
 614         MemRegion covered_mr = _covered[i-1];
 615         _covered[i-1] = _covered[i];
 616         _covered[i] = covered_mr;
 617         MemRegion committed_mr = _committed[i-1];
 618       _committed[i-1] = _committed[i];
 619       _committed[i] = committed_mr;
 620       break;
 621     }
 622   }
 623 #ifdef ASSERT
 624   for (int m = 0; m < _cur_covered_regions-1; m++) {
 625     assert(_covered[m].start() <= _covered[m+1].start(),
 626       "Covered regions out of order");
 627     assert(_committed[m].start() <= _committed[m+1].start(),
 628       "Committed regions out of order");
 629   }
 630 #endif
 631 }
 632 
 633 // Returns the start of any committed region that is lower than
 634 // the target committed region (index ind) and that intersects the
 635 // target region.  If none, return start of target region.
 636 //
 637 //      -------------
 638 //      |           |
 639 //      -------------
 640 //              ------------
 641 //              | target   |
 642 //              ------------
 643 //                               -------------
 644 //                               |           |
 645 //                               -------------
 646 //      ^ returns this
 647 //
 648 //      -------------
 649 //      |           |
 650 //      -------------
 651 //                      ------------
 652 //                      | target   |
 653 //                      ------------
 654 //                               -------------
 655 //                               |           |
 656 //                               -------------
 657 //                      ^ returns this
 658 
 659 HeapWord* PSCardTable::lowest_prev_committed_start(int ind) const {
 660   assert(_cur_covered_regions >= 0, "Expecting at least on region");
 661   HeapWord* min_start = _committed[ind].start();
 662   for (int j = 0; j < ind; j++) {
 663     HeapWord* this_start = _committed[j].start();
 664     if ((this_start < min_start) &&
 665         !(_committed[j].intersection(_committed[ind])).is_empty()) {
 666        min_start = this_start;
 667     }
 668   }
 669   return min_start;
 670 }
 671 
 672 bool PSCardTable::is_in_young(oop obj) const {
 673   return ParallelScavengeHeap::heap()->is_in_young(obj);
 674 }