1 /*
   2  * Copyright (c) 2015, 2017, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
  26 
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "gc/g1/suspendibleThreadSet.hpp"
  29 #include "gc/shared/markBitMap.inline.hpp"
  30 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
  31 #include "gc/shenandoah/brooksPointer.inline.hpp"
  32 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
  33 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  34 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
  35 #include "gc/shenandoah/shenandoahConnectionMatrix.inline.hpp"
  36 #include "gc/shenandoah/shenandoahHeap.hpp"
  37 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  38 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  39 #include "gc/shenandoah/shenandoahStringDedup.hpp"
  40 #include "gc/shenandoah/shenandoahUtils.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "runtime/atomic.hpp"
  43 #include "runtime/interfaceSupport.hpp"
  44 #include "runtime/prefetch.hpp"
  45 #include "runtime/prefetch.inline.hpp"
  46 #include "runtime/thread.hpp"
  47 #include "utilities/copy.hpp"
  48 
  49 template <class T>
  50 void ShenandoahUpdateRefsClosure::do_oop_work(T* p) {
  51   T o = oopDesc::load_heap_oop(p);
  52   if (! oopDesc::is_null(o)) {
  53     oop obj = oopDesc::decode_heap_oop_not_null(o);
  54     _heap->update_oop_ref_not_null(p, obj);
  55   }
  56 }
  57 
  58 void ShenandoahUpdateRefsClosure::do_oop(oop* p)       { do_oop_work(p); }
  59 void ShenandoahUpdateRefsClosure::do_oop(narrowOop* p) { do_oop_work(p); }
  60 
  61 /*
  62  * Marks the object. Returns true if the object has not been marked before and has
  63  * been marked by this thread. Returns false if the object has already been marked,
  64  * or if a competing thread succeeded in marking this object.
  65  */
  66 inline bool ShenandoahHeap::mark_next(oop obj) const {
  67 #ifdef ASSERT
  68   if (! oopDesc::unsafe_equals(obj, oopDesc::bs()->read_barrier(obj))) {
  69     tty->print_cr("heap region containing obj:");
  70     ShenandoahHeapRegion* obj_region = heap_region_containing(obj);
  71     obj_region->print();
  72     tty->print_cr("heap region containing forwardee:");
  73     ShenandoahHeapRegion* forward_region = heap_region_containing(oopDesc::bs()->read_barrier(obj));
  74     forward_region->print();
  75   }
  76 #endif
  77 
  78   assert(oopDesc::unsafe_equals(obj, oopDesc::bs()->read_barrier(obj)), "only mark forwarded copy of objects");
  79   return mark_next_no_checks(obj);
  80 }
  81 
  82 inline bool ShenandoahHeap::mark_next_no_checks(oop obj) const {
  83   HeapWord* addr = (HeapWord*) obj;
  84   return (! allocated_after_next_mark_start(addr)) && _next_mark_bit_map->parMark(addr);
  85 }
  86 
  87 inline bool ShenandoahHeap::is_marked_next(oop obj) const {
  88   HeapWord* addr = (HeapWord*) obj;
  89   return allocated_after_next_mark_start(addr) || _next_mark_bit_map->isMarked(addr);
  90 }
  91 
  92 inline bool ShenandoahHeap::is_marked_complete(oop obj) const {
  93   HeapWord* addr = (HeapWord*) obj;
  94   return allocated_after_complete_mark_start(addr) || _complete_mark_bit_map->isMarked(addr);
  95 }
  96 
  97 inline bool ShenandoahHeap::need_update_refs() const {
  98   return _need_update_refs;
  99 }
 100 
 101 inline size_t ShenandoahHeap::heap_region_index_containing(const void* addr) const {
 102   uintptr_t region_start = ((uintptr_t) addr);
 103   uintptr_t index = (region_start - (uintptr_t) base()) >> ShenandoahHeapRegion::region_size_bytes_shift();
 104 #ifdef ASSERT
 105   if (index >= num_regions()) {
 106     tty->print_cr("heap region does not contain address, heap base: "PTR_FORMAT \
 107                   ", real bottom of first region: "PTR_FORMAT", num_regions: "SIZE_FORMAT", region_size: "SIZE_FORMAT,
 108                   p2i(base()),
 109                   p2i(_ordered_regions->get(0)->bottom()),
 110                   num_regions(),
 111                   ShenandoahHeapRegion::region_size_bytes());
 112   }
 113 #endif
 114   assert(index < num_regions(), "heap region index must be in range");
 115   return index;
 116 }
 117 
 118 inline ShenandoahHeapRegion* ShenandoahHeap::heap_region_containing(const void* addr) const {
 119   size_t index = heap_region_index_containing(addr);
 120   ShenandoahHeapRegion* result = _ordered_regions->get(index);
 121 #ifdef ASSERT
 122   if (!(addr >= result->bottom() && addr < result->end())) {
 123     tty->print_cr("heap region does not contain address, heap base: "PTR_FORMAT \
 124                   ", real bottom of first region: "PTR_FORMAT", num_regions: "SIZE_FORMAT,
 125                   p2i(base()),
 126                   p2i(_ordered_regions->get(0)->bottom()),
 127                   num_regions());
 128   }
 129 #endif
 130   assert(addr >= result->bottom() && addr < result->end(), "address must be in found region");
 131   return result;
 132 }
 133 
 134 template <class T>
 135 inline oop ShenandoahHeap::update_oop_ref_not_null(T* p, oop obj) {
 136   if (in_collection_set(obj)) {
 137     oop forw = ShenandoahBarrierSet::resolve_oop_static_not_null(obj);
 138     assert(! oopDesc::unsafe_equals(forw, obj) || is_full_gc_in_progress() || cancelled_concgc(), "expect forwarded object");
 139     obj = forw;
 140     oopDesc::encode_store_heap_oop(p, obj);
 141   }
 142 #ifdef ASSERT
 143   else {
 144     assert(oopDesc::unsafe_equals(obj, ShenandoahBarrierSet::resolve_oop_static_not_null(obj)), "expect not forwarded");
 145   }
 146 #endif
 147   return obj;
 148 }
 149 
 150 template <class T>
 151 inline oop ShenandoahHeap::maybe_update_oop_ref(T* p) {
 152   T o = oopDesc::load_heap_oop(p);
 153   if (! oopDesc::is_null(o)) {
 154     oop obj = oopDesc::decode_heap_oop_not_null(o);
 155     return maybe_update_oop_ref_not_null(p, obj);
 156   } else {
 157     return NULL;
 158   }
 159 }
 160 
 161 inline oop ShenandoahHeap::atomic_compare_exchange_oop(oop n, oop* addr, oop c) {
 162   return (oop) Atomic::cmpxchg_ptr(n, addr, c);
 163 }
 164 
 165 inline oop ShenandoahHeap::atomic_compare_exchange_oop(oop n, narrowOop* addr, oop c) {
 166   narrowOop cmp = oopDesc::encode_heap_oop(c);
 167   narrowOop val = oopDesc::encode_heap_oop(n);
 168   return oopDesc::decode_heap_oop((narrowOop) Atomic::cmpxchg(val, addr, cmp));
 169 }
 170 
 171 template <class T>
 172 inline oop ShenandoahHeap::maybe_update_oop_ref_not_null(T* p, oop heap_oop) {
 173 
 174   assert((! is_in(p)) || (! in_collection_set(p))
 175          || is_full_gc_in_progress(),
 176          "never update refs in from-space, unless evacuation has been cancelled");
 177 
 178 #ifdef ASSERT
 179   if (! is_in(heap_oop)) {
 180     print_heap_regions_on(tty);
 181     tty->print_cr("object not in heap: "PTR_FORMAT", referenced by: "PTR_FORMAT, p2i((HeapWord*) heap_oop), p2i(p));
 182     assert(is_in(heap_oop), "object must be in heap");
 183   }
 184 #endif
 185   assert(is_in(heap_oop), "only ever call this on objects in the heap");
 186   if (in_collection_set(heap_oop)) {
 187     oop forwarded_oop = ShenandoahBarrierSet::resolve_oop_static_not_null(heap_oop); // read brooks ptr
 188     if (oopDesc::unsafe_equals(forwarded_oop, heap_oop)) {
 189       // E.g. during evacuation.
 190       return forwarded_oop;
 191     }
 192 
 193     assert(! oopDesc::unsafe_equals(forwarded_oop, heap_oop) || is_full_gc_in_progress(), "expect forwarded object");
 194 
 195     log_develop_trace(gc)("Updating old ref: "PTR_FORMAT" pointing to "PTR_FORMAT" to new ref: "PTR_FORMAT,
 196                           p2i(p), p2i(heap_oop), p2i(forwarded_oop));
 197 
 198     assert(oopDesc::is_oop(forwarded_oop), "oop required");
 199     assert(is_in(forwarded_oop), "forwardee must be in heap");
 200     assert(oopDesc::bs()->is_safe(forwarded_oop), "forwardee must not be in collection set");
 201     // If this fails, another thread wrote to p before us, it will be logged in SATB and the
 202     // reference be updated later.
 203     oop result = atomic_compare_exchange_oop(forwarded_oop, p, heap_oop);
 204 
 205     if (oopDesc::unsafe_equals(result, heap_oop)) { // CAS successful.
 206       return forwarded_oop;
 207     } else {
 208       // Note: we used to assert the following here. This doesn't work because sometimes, during
 209       // marking/updating-refs, it can happen that a Java thread beats us with an arraycopy,
 210       // which first copies the array, which potentially contains from-space refs, and only afterwards
 211       // updates all from-space refs to to-space refs, which leaves a short window where the new array
 212       // elements can be from-space.
 213       // assert(oopDesc::is_null(result) ||
 214       //        oopDesc::unsafe_equals(result, ShenandoahBarrierSet::resolve_oop_static_not_null(result)),
 215       //       "expect not forwarded");
 216       return NULL;
 217     }
 218   } else {
 219     assert(oopDesc::unsafe_equals(heap_oop, ShenandoahBarrierSet::resolve_oop_static_not_null(heap_oop)),
 220            "expect not forwarded");
 221     return heap_oop;
 222   }
 223 }
 224 
 225 inline bool ShenandoahHeap::cancelled_concgc() const {
 226   return OrderAccess::load_acquire((jbyte*) &_cancelled_concgc) == CANCELLED;
 227 }
 228 
 229 inline bool ShenandoahHeap::check_cancelled_concgc_and_yield(bool sts_active) {
 230   if (! (sts_active && ShenandoahSuspendibleWorkers)) {
 231     return cancelled_concgc();
 232   }
 233   jbyte prev = Atomic::cmpxchg((jbyte)NOT_CANCELLED, &_cancelled_concgc, (jbyte)CANCELLABLE);
 234   if (prev == CANCELLABLE || prev == NOT_CANCELLED) {
 235 
 236     if (SuspendibleThreadSet::should_yield()) {
 237       SuspendibleThreadSet::yield();
 238     }
 239 
 240     // Back to CANCELLABLE. The thread that poked NOT_CANCELLED first gets
 241     // to restore to CANCELLABLE.
 242     if (prev == CANCELLABLE) {
 243       OrderAccess::release_store_fence(&_cancelled_concgc, CANCELLABLE);
 244     }
 245     return false;
 246   } else {
 247     return true;
 248   }
 249 }
 250 
 251 inline bool ShenandoahHeap::try_cancel_concgc() {
 252   while (true) {
 253     jbyte prev = Atomic::cmpxchg((jbyte)CANCELLED, &_cancelled_concgc, (jbyte)CANCELLABLE);
 254     if (prev == CANCELLABLE) return true;
 255     else if (prev == CANCELLED) return false;
 256     assert(ShenandoahSuspendibleWorkers, "should not get here when not using suspendible workers");
 257     assert(prev == NOT_CANCELLED, "must be NOT_CANCELLED");
 258     {
 259       // We need to provide a safepoint here, otherwise we might
 260       // spin forever if a SP is pending.
 261       ThreadBlockInVM sp(JavaThread::current());
 262       SpinPause();
 263     }
 264   }
 265 }
 266 
 267 inline void ShenandoahHeap::clear_cancelled_concgc() {
 268   OrderAccess::release_store_fence(&_cancelled_concgc, CANCELLABLE);
 269 }
 270 
 271 inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) {
 272   if (UseTLAB) {
 273     if (!thread->gclab().is_initialized()) {
 274       assert(!thread->is_Java_thread() && !thread->is_Worker_thread(),
 275              "Performance: thread should have GCLAB: %s", thread->name());
 276       // No GCLABs in this thread, fallback to shared allocation
 277       return NULL;
 278     }
 279     HeapWord* obj = thread->gclab().allocate(size);
 280     if (obj != NULL) {
 281       return obj;
 282     }
 283     // Otherwise...
 284     return allocate_from_gclab_slow(thread, size);
 285   } else {
 286     return NULL;
 287   }
 288 }
 289 
 290 inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread, bool& evacuated) {
 291   evacuated = false;
 292 
 293   size_t size_no_fwdptr = (size_t) p->size();
 294   size_t size_with_fwdptr = size_no_fwdptr + BrooksPointer::word_size();
 295 
 296   assert(!heap_region_containing(p)->is_humongous(), "never evacuate humongous objects");
 297 
 298   bool alloc_from_gclab = true;
 299   HeapWord* filler = allocate_from_gclab(thread, size_with_fwdptr);
 300   if (filler == NULL) {
 301     filler = allocate_memory(size_with_fwdptr, _alloc_shared_gc);
 302     alloc_from_gclab = false;
 303   }
 304 
 305 #ifdef ASSERT
 306   // Checking that current Java thread does not hold Threads_lock when we get here.
 307   // If that ever be the case, we'd deadlock in oom_during_evacuation.
 308   if ((! Thread::current()->is_GC_task_thread()) && (! Thread::current()->is_ConcurrentGC_thread())) {
 309     assert(! Threads_lock->owned_by_self()
 310            || SafepointSynchronize::is_at_safepoint(), "must not hold Threads_lock here");
 311   }
 312 #endif
 313 
 314   if (filler == NULL) {
 315     oom_during_evacuation();
 316     // If this is a Java thread, it should have waited
 317     // until all GC threads are done, and then we
 318     // return the forwardee.
 319     oop resolved = ShenandoahBarrierSet::resolve_oop_static(p);
 320     return resolved;
 321   }
 322 
 323   // Copy the object and initialize its forwarding ptr:
 324   HeapWord* copy = filler + BrooksPointer::word_size();
 325   oop copy_val = oop(copy);
 326 
 327   Copy::aligned_disjoint_words((HeapWord*) p, copy, size_no_fwdptr);
 328   BrooksPointer::initialize(oop(copy));
 329 
 330   log_develop_trace(gc, compaction)("Copy object: " PTR_FORMAT " -> " PTR_FORMAT,
 331                                     p2i(p), p2i(copy));
 332 
 333   // String dedup support
 334   bool need_str_dedup = false;
 335   if (ShenandoahStringDedup::is_enabled()
 336     && java_lang_String::is_instance_inlined(copy_val)) {
 337     // We need to increase age before CAS to avoid race condition.
 338     // Once new copy is published, other threads may set hash code,
 339     // or perform locking, etc. which will race age bits manipulation.
 340     copy_val->incr_age();
 341 
 342     need_str_dedup = ShenandoahStringDedup::is_candidate(copy_val);
 343   }
 344 
 345   // Try to install the new forwarding pointer.
 346   oop result = BrooksPointer::try_update_forwardee(p, copy_val);
 347 
 348   if (oopDesc::unsafe_equals(result, p)) {
 349     // Successfully evacuated. Our copy is now the public one!
 350     evacuated = true;
 351     log_develop_trace(gc, compaction)("Copy object: " PTR_FORMAT " -> " PTR_FORMAT " succeeded",
 352                                       p2i(p), p2i(copy));
 353 
 354     // Only dedup evacuated string
 355     if (need_str_dedup) {
 356       // Shenandoah evacuates objects inside and outside of safepoints.
 357       // But string dedup protocol requires deduplication outside of safepoints,
 358       // so we need to queue candidates during safepoints.
 359       if (SafepointSynchronize::is_at_safepoint()) {
 360         assert(thread->is_Worker_thread(), "Must be a worker thread during a safepoint");
 361         // Use worker thread id instead of worker_id to avoid passing down worker_id.
 362         // This may cause imbalance among the queues, but it is okay, since deduplication is
 363         // single threaded.
 364         ShenandoahStringDedup::enqueue_from_safepoint(copy_val, thread->as_Worker_thread()->id());
 365       } else {
 366         ShenandoahStringDedup::deduplicate(copy_val);
 367       }
 368     }
 369 
 370 #ifdef ASSERT
 371     assert(oopDesc::is_oop(copy_val), "expect oop");
 372     assert(p->klass() == copy_val->klass(), "Should have the same class p: "PTR_FORMAT", copy: "PTR_FORMAT,
 373                                               p2i(p), p2i(copy));
 374 #endif
 375     return copy_val;
 376   }  else {
 377     // Failed to evacuate. We need to deal with the object that is left behind. Since this
 378     // new allocation is certainly after TAMS, it will be considered live in the next cycle.
 379     // But if it happens to contain references to evacuated regions, those references would
 380     // not get updated for this stale copy during this cycle, and we will crash while scanning
 381     // it the next cycle.
 382     //
 383     // For GCLAB allocations, it is enough to rollback the allocation ptr. Either the next
 384     // object will overwrite this stale copy, or the filler object on LAB retirement will
 385     // do this. For non-GCLAB allocations, we have no way to retract the allocation, and
 386     // have to explicitly overwrite the copy with the filler object. With that overwrite,
 387     // we have to keep the fwdptr initialized and pointing to our (stale) copy.
 388     if (alloc_from_gclab) {
 389       thread->gclab().rollback(size_with_fwdptr);
 390     } else {
 391       fill_with_object(copy, size_no_fwdptr);
 392     }
 393     log_develop_trace(gc, compaction)("Copy object: " PTR_FORMAT " -> " PTR_FORMAT " failed, use other: " PTR_FORMAT,
 394                                       p2i(p), p2i(copy), p2i(result));
 395     return result;
 396   }
 397 }
 398 
 399 inline bool ShenandoahHeap::requires_marking(const void* entry) const {
 400   return ! is_marked_next(oop(entry));
 401 }
 402 
 403 bool ShenandoahHeap::region_in_collection_set(size_t region_index) const {
 404   assert(collection_set() != NULL, "Sanity");
 405   return collection_set()->is_in(region_index);
 406 }
 407 
 408 bool ShenandoahHeap::in_collection_set(ShenandoahHeapRegion* r) const {
 409   return region_in_collection_set(r->region_number());
 410 }
 411 
 412 template <class T>
 413 inline bool ShenandoahHeap::in_collection_set(T p) const {
 414   HeapWord* obj = (HeapWord*) p;
 415   assert(collection_set() != NULL, "Sanity");
 416   assert(is_in(obj), "should be in heap");
 417 
 418   return collection_set()->is_in(obj);
 419 }
 420 
 421 inline bool ShenandoahHeap::concurrent_mark_in_progress() const {
 422   return _concurrent_mark_in_progress != 0;
 423 }
 424 
 425 inline address ShenandoahHeap::concurrent_mark_in_progress_addr() {
 426   return (address) &(ShenandoahHeap::heap()->_concurrent_mark_in_progress);
 427 }
 428 
 429 inline address ShenandoahHeap::update_refs_in_progress_addr() {
 430   return (address) &(ShenandoahHeap::heap()->_update_refs_in_progress);
 431 }
 432 
 433 inline bool ShenandoahHeap::is_evacuation_in_progress() const {
 434   return _evacuation_in_progress != 0;
 435 }
 436 
 437 inline address ShenandoahHeap::evacuation_in_progress_addr() {
 438   return (address) &(ShenandoahHeap::heap()->_evacuation_in_progress);
 439 }
 440 
 441 inline bool ShenandoahHeap::allocated_after_next_mark_start(HeapWord* addr) const {
 442   uintx index = ((uintx) addr) >> ShenandoahHeapRegion::region_size_bytes_shift();
 443   HeapWord* top_at_mark_start = _next_top_at_mark_starts[index];
 444   bool alloc_after_mark_start = addr >= top_at_mark_start;
 445   return alloc_after_mark_start;
 446 }
 447 
 448 inline bool ShenandoahHeap::allocated_after_complete_mark_start(HeapWord* addr) const {
 449   uintx index = ((uintx) addr) >> ShenandoahHeapRegion::region_size_bytes_shift();
 450   HeapWord* top_at_mark_start = _complete_top_at_mark_starts[index];
 451   bool alloc_after_mark_start = addr >= top_at_mark_start;
 452   return alloc_after_mark_start;
 453 }
 454 
 455 template<class T>
 456 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) {
 457   marked_object_iterate(region, cl, region->top());
 458 }
 459 
 460 template<class T>
 461 inline void ShenandoahHeap::marked_object_safe_iterate(ShenandoahHeapRegion* region, T* cl) {
 462   marked_object_iterate(region, cl, region->concurrent_iteration_safe_limit());
 463 }
 464 
 465 template<class T>
 466 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) {
 467   assert(BrooksPointer::word_offset() < 0, "skip_delta calculation below assumes the forwarding ptr is before obj");
 468 
 469   assert(! region->is_humongous_continuation(), "no humongous continuation regions here");
 470 
 471   MarkBitMap* mark_bit_map = _complete_mark_bit_map;
 472   HeapWord* top_at_mark_start = complete_top_at_mark_start(region->bottom());
 473 
 474   size_t skip_bitmap_delta = BrooksPointer::word_size() + 1;
 475   size_t skip_objsize_delta = BrooksPointer::word_size() /* + actual obj.size() below */;
 476   HeapWord* start = region->bottom() + BrooksPointer::word_size();
 477 
 478   HeapWord* end = MIN2(top_at_mark_start + BrooksPointer::word_size(), region->end());
 479   HeapWord* addr = mark_bit_map->getNextMarkedWordAddress(start, end);
 480 
 481   intx dist = ShenandoahMarkScanPrefetch;
 482   if (dist > 0) {
 483     // Batched scan that prefetches the oop data, anticipating the access to
 484     // either header, oop field, or forwarding pointer. Not that we cannot
 485     // touch anything in oop, while it still being prefetched to get enough
 486     // time for prefetch to work. This is why we try to scan the bitmap linearly,
 487     // disregarding the object size. However, since we know forwarding pointer
 488     // preceeds the object, we can skip over it. Once we cannot trust the bitmap,
 489     // there is no point for prefetching the oop contents, as oop->size() will
 490     // touch it prematurely.
 491 
 492     // No variable-length arrays in standard C++, have enough slots to fit
 493     // the prefetch distance.
 494     static const int SLOT_COUNT = 256;
 495     guarantee(dist <= SLOT_COUNT, "adjust slot count");
 496     oop slots[SLOT_COUNT];
 497 
 498     bool aborting = false;
 499     int avail;
 500     do {
 501       avail = 0;
 502       for (int c = 0; (c < dist) && (addr < limit); c++) {
 503         Prefetch::read(addr, BrooksPointer::byte_offset());
 504         oop obj = oop(addr);
 505         slots[avail++] = obj;
 506         if (addr < top_at_mark_start) {
 507           addr += skip_bitmap_delta;
 508           addr = mark_bit_map->getNextMarkedWordAddress(addr, end);
 509         } else {
 510           // cannot trust mark bitmap anymore, finish the current stride,
 511           // and switch to accurate traversal
 512           addr += obj->size() + skip_objsize_delta;
 513           aborting = true;
 514         }
 515       }
 516 
 517       for (int c = 0; c < avail; c++) {
 518         do_marked_object(mark_bit_map, cl, slots[c]);
 519       }
 520     } while (avail > 0 && !aborting);
 521 
 522     // accurate traversal
 523     while (addr < limit) {
 524       oop obj = oop(addr);
 525       int size = obj->size();
 526       do_marked_object(mark_bit_map, cl, obj);
 527       addr += size + skip_objsize_delta;
 528     }
 529   } else {
 530     while (addr < limit) {
 531       oop obj = oop(addr);
 532       int size = obj->size();
 533       do_marked_object(mark_bit_map, cl, obj);
 534       addr += size + skip_objsize_delta;
 535       if (addr < top_at_mark_start) {
 536         addr = mark_bit_map->getNextMarkedWordAddress(addr, end);
 537       }
 538     }
 539   }
 540 }
 541 
 542 template<class T>
 543 inline void ShenandoahHeap::do_marked_object(MarkBitMap* bitmap, T* cl, oop obj) {
 544   assert(!oopDesc::is_null(obj), "sanity");
 545   assert(oopDesc::is_oop(obj), "sanity");
 546   assert(is_in(obj), "sanity");
 547   assert(bitmap == _complete_mark_bit_map, "only iterate completed mark bitmap");
 548   assert(is_marked_complete(obj), "object expected to be marked");
 549   cl->do_object(obj);
 550 }
 551 
 552 template <class T>
 553 class ShenandoahObjectToOopClosure : public ObjectClosure {
 554   T* _cl;
 555 public:
 556   ShenandoahObjectToOopClosure(T* cl) : _cl(cl) {}
 557 
 558   void do_object(oop obj) {
 559     obj->oop_iterate(_cl);
 560   }
 561 };
 562 
 563 template <class T>
 564 class ShenandoahObjectToOopBoundedClosure : public ObjectClosure {
 565   T* _cl;
 566   MemRegion _bounds;
 567 public:
 568   ShenandoahObjectToOopBoundedClosure(T* cl, HeapWord* bottom, HeapWord* top) :
 569     _cl(cl), _bounds(bottom, top) {}
 570 
 571   void do_object(oop obj) {
 572     obj->oop_iterate(_cl, _bounds);
 573   }
 574 };
 575 
 576 template<class T>
 577 inline void ShenandoahHeap::marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* top) {
 578   if (region->is_humongous()) {
 579     HeapWord* bottom = region->bottom();
 580     if (top > bottom) {
 581       region = region->humongous_start_region();
 582       ShenandoahObjectToOopBoundedClosure<T> objs(cl, bottom, top);
 583       marked_object_iterate(region, &objs);
 584     }
 585   } else {
 586     ShenandoahObjectToOopClosure<T> objs(cl);
 587     marked_object_iterate(region, &objs, top);
 588   }
 589 }
 590 
 591 template<class T>
 592 inline void ShenandoahHeap::marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl) {
 593   marked_object_oop_iterate(region, cl, region->top());
 594 }
 595 
 596 template<class T>
 597 inline void ShenandoahHeap::marked_object_oop_safe_iterate(ShenandoahHeapRegion* region, T* cl) {
 598   marked_object_oop_iterate(region, cl, region->concurrent_iteration_safe_limit());
 599 }
 600 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP