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