1 /*
   2  * Copyright (c) 2015, 2018, 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/shared/markBitMap.inline.hpp"
  29 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
  30 #include "gc/shared/suspendibleThreadSet.hpp"
  31 #include "gc/shenandoah/brooksPointer.inline.hpp"
  32 #include "gc/shenandoah/shenandoahAsserts.hpp"
  33 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
  34 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  35 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
  36 #include "gc/shenandoah/shenandoahControlThread.hpp"
  37 #include "gc/shenandoah/shenandoahConnectionMatrix.inline.hpp"
  38 #include "gc/shenandoah/shenandoahHeap.hpp"
  39 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
  40 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  41 #include "gc/shenandoah/shenandoahUtils.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "runtime/atomic.hpp"
  44 #include "runtime/interfaceSupport.hpp"
  45 #include "runtime/prefetch.hpp"
  46 #include "runtime/prefetch.inline.hpp"
  47 #include "runtime/thread.hpp"
  48 #include "utilities/copy.hpp"
  49 
  50 template <class T>
  51 void ShenandoahUpdateRefsClosure::do_oop_work(T* p) {
  52   T o = oopDesc::load_heap_oop(p);
  53   if (! oopDesc::is_null(o)) {
  54     oop obj = oopDesc::decode_heap_oop_not_null(o);
  55     _heap->update_with_forwarded_not_null(p, obj);
  56   }
  57 }
  58 
  59 void ShenandoahUpdateRefsClosure::do_oop(oop* p)       { do_oop_work(p); }
  60 void ShenandoahUpdateRefsClosure::do_oop(narrowOop* p) { do_oop_work(p); }
  61 
  62 inline ShenandoahHeapRegion* ShenandoahRegionIterator::next() {
  63   size_t new_index = Atomic::add((size_t) 1, &_index);
  64   // get_region() provides the bounds-check and returns NULL on OOB.
  65   return _heap->get_region(new_index - 1);
  66 }
  67 
  68 /*
  69  * Marks the object. Returns true if the object has not been marked before and has
  70  * been marked by this thread. Returns false if the object has already been marked,
  71  * or if a competing thread succeeded in marking this object.
  72  */
  73 inline bool ShenandoahHeap::mark_next(oop obj) const {
  74   shenandoah_assert_not_forwarded(NULL, obj);
  75   HeapWord* addr = (HeapWord*) obj;
  76   return (! allocated_after_next_mark_start(addr)) && _next_mark_bit_map->parMark(addr);
  77 }
  78 
  79 inline bool ShenandoahHeap::is_marked_next(oop obj) const {
  80   HeapWord* addr = (HeapWord*) obj;
  81   return allocated_after_next_mark_start(addr) || _next_mark_bit_map->isMarked(addr);
  82 }
  83 
  84 inline bool ShenandoahHeap::is_marked_complete(oop obj) const {
  85   HeapWord* addr = (HeapWord*) obj;
  86   return allocated_after_complete_mark_start(addr) || _complete_mark_bit_map->isMarked(addr);
  87 }
  88 
  89 inline bool ShenandoahHeap::has_forwarded_objects() const {
  90   return _gc_state.is_set(HAS_FORWARDED);
  91 }
  92 
  93 inline size_t ShenandoahHeap::heap_region_index_containing(const void* addr) const {
  94   uintptr_t region_start = ((uintptr_t) addr);
  95   uintptr_t index = (region_start - (uintptr_t) base()) >> ShenandoahHeapRegion::region_size_bytes_shift();
  96   assert(index < num_regions(), "Region index is in bounds: " PTR_FORMAT, p2i(addr));
  97   return index;
  98 }
  99 
 100 inline ShenandoahHeapRegion* const ShenandoahHeap::heap_region_containing(const void* addr) const {
 101   size_t index = heap_region_index_containing(addr);
 102   ShenandoahHeapRegion* const result = get_region(index);
 103   assert(addr >= result->bottom() && addr < result->end(), "Heap region contains the address: " PTR_FORMAT, p2i(addr));
 104   return result;
 105 }
 106 
 107 template <class T>
 108 inline oop ShenandoahHeap::update_with_forwarded_not_null(T* p, oop obj) {
 109   if (in_collection_set(obj)) {
 110     shenandoah_assert_forwarded_except(p, obj, is_full_gc_in_progress() || cancelled_concgc());
 111     obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 112     oopDesc::encode_store_heap_oop(p, obj);
 113   }
 114 #ifdef ASSERT
 115   else {
 116     shenandoah_assert_not_forwarded(p, obj);
 117   }
 118 #endif
 119   return obj;
 120 }
 121 
 122 template <class T>
 123 inline oop ShenandoahHeap::maybe_update_with_forwarded(T* p) {
 124   T o = oopDesc::load_heap_oop(p);
 125   if (! oopDesc::is_null(o)) {
 126     oop obj = oopDesc::decode_heap_oop_not_null(o);
 127     return maybe_update_with_forwarded_not_null(p, obj);
 128   } else {
 129     return NULL;
 130   }
 131 }
 132 
 133 template <class T>
 134 inline oop ShenandoahHeap::evac_update_with_forwarded(T* p) {
 135   T o = oopDesc::load_heap_oop(p);
 136   if (! oopDesc::is_null(o)) {
 137     oop heap_oop = oopDesc::decode_heap_oop_not_null(o);
 138     if (in_collection_set(heap_oop)) {
 139       oop forwarded_oop = ShenandoahBarrierSet::resolve_forwarded_not_null(heap_oop);
 140       if (oopDesc::unsafe_equals(forwarded_oop, heap_oop)) {
 141         forwarded_oop = evacuate_object(heap_oop, Thread::current());
 142       }
 143       oop prev = atomic_compare_exchange_oop(forwarded_oop, p, heap_oop);
 144       if (oopDesc::unsafe_equals(prev, heap_oop)) {
 145         return forwarded_oop;
 146       } else {
 147         return NULL;
 148       }
 149     }
 150     return heap_oop;
 151   } else {
 152     return NULL;
 153   }
 154 }
 155 
 156 inline oop ShenandoahHeap::atomic_compare_exchange_oop(oop n, oop* addr, oop c) {
 157   return (oop) Atomic::cmpxchg(n, addr, c);
 158 }
 159 
 160 inline oop ShenandoahHeap::atomic_compare_exchange_oop(oop n, narrowOop* addr, oop c) {
 161   narrowOop cmp = oopDesc::encode_heap_oop(c);
 162   narrowOop val = oopDesc::encode_heap_oop(n);
 163   return oopDesc::decode_heap_oop((narrowOop) Atomic::cmpxchg(val, addr, cmp));
 164 }
 165 
 166 template <class T>
 167 inline oop ShenandoahHeap::maybe_update_with_forwarded_not_null(T* p, oop heap_oop) {
 168   shenandoah_assert_not_in_cset_loc_except(p, !is_in(p) || is_full_gc_in_progress());
 169   shenandoah_assert_correct(p, heap_oop);
 170 
 171   if (in_collection_set(heap_oop)) {
 172     oop forwarded_oop = ShenandoahBarrierSet::resolve_forwarded_not_null(heap_oop);
 173     if (oopDesc::unsafe_equals(forwarded_oop, heap_oop)) {
 174       // E.g. during evacuation.
 175       return forwarded_oop;
 176     }
 177 
 178     shenandoah_assert_forwarded_except(p, heap_oop, is_full_gc_in_progress());
 179     shenandoah_assert_not_in_cset_except(p, forwarded_oop, cancelled_concgc());
 180 
 181     log_develop_trace(gc)("Updating old ref: "PTR_FORMAT" pointing to "PTR_FORMAT" to new ref: "PTR_FORMAT,
 182                           p2i(p), p2i(heap_oop), p2i(forwarded_oop));
 183 
 184     // If this fails, another thread wrote to p before us, it will be logged in SATB and the
 185     // reference be updated later.
 186     oop result = atomic_compare_exchange_oop(forwarded_oop, p, heap_oop);
 187 
 188     if (oopDesc::unsafe_equals(result, heap_oop)) { // CAS successful.
 189       return forwarded_oop;
 190     } else {
 191       // Note: we used to assert the following here. This doesn't work because sometimes, during
 192       // marking/updating-refs, it can happen that a Java thread beats us with an arraycopy,
 193       // which first copies the array, which potentially contains from-space refs, and only afterwards
 194       // updates all from-space refs to to-space refs, which leaves a short window where the new array
 195       // elements can be from-space.
 196       // assert(oopDesc::is_null(result) ||
 197       //        oopDesc::unsafe_equals(result, ShenandoahBarrierSet::resolve_oop_static_not_null(result)),
 198       //       "expect not forwarded");
 199       return NULL;
 200     }
 201   } else {
 202     shenandoah_assert_not_forwarded(p, heap_oop);
 203     return heap_oop;
 204   }
 205 }
 206 
 207 inline bool ShenandoahHeap::cancelled_concgc() const {
 208   return _cancelled_concgc.get() == CANCELLED;
 209 }
 210 
 211 inline bool ShenandoahHeap::check_cancelled_concgc_and_yield(bool sts_active) {
 212   if (! (sts_active && ShenandoahSuspendibleWorkers)) {
 213     return cancelled_concgc();
 214   }
 215 
 216   jbyte prev = _cancelled_concgc.cmpxchg(NOT_CANCELLED, CANCELLABLE);
 217   if (prev == CANCELLABLE || prev == NOT_CANCELLED) {
 218 
 219     if (SuspendibleThreadSet::should_yield()) {
 220       SuspendibleThreadSet::yield();
 221     }
 222 
 223     // Back to CANCELLABLE. The thread that poked NOT_CANCELLED first gets
 224     // to restore to CANCELLABLE.
 225     if (prev == CANCELLABLE) {
 226       _cancelled_concgc.set(CANCELLABLE);
 227     }
 228     return false;
 229   } else {
 230     return true;
 231   }
 232 }
 233 
 234 inline bool ShenandoahHeap::try_cancel_concgc() {
 235   while (true) {
 236     jbyte prev = _cancelled_concgc.cmpxchg(CANCELLED, CANCELLABLE);
 237     if (prev == CANCELLABLE) return true;
 238     else if (prev == CANCELLED) return false;
 239     assert(ShenandoahSuspendibleWorkers, "should not get here when not using suspendible workers");
 240     assert(prev == NOT_CANCELLED, "must be NOT_CANCELLED");
 241     {
 242       // We need to provide a safepoint here, otherwise we might
 243       // spin forever if a SP is pending.
 244       ThreadBlockInVM sp(JavaThread::current());
 245       SpinPause();
 246     }
 247   }
 248 }
 249 
 250 inline void ShenandoahHeap::clear_cancelled_concgc() {
 251   _cancelled_concgc.set(CANCELLABLE);
 252   _oom_evac_handler.clear();
 253 }
 254 
 255 inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) {
 256   if (UseTLAB) {
 257     if (!thread->gclab().is_initialized()) {
 258       assert(!thread->is_Java_thread() && !thread->is_Worker_thread(),
 259              "Performance: thread should have GCLAB: %s", thread->name());
 260       // No GCLABs in this thread, fallback to shared allocation
 261       return NULL;
 262     }
 263     HeapWord* obj = thread->gclab().allocate(size);
 264     if (obj != NULL) {
 265       return obj;
 266     }
 267     // Otherwise...
 268     return allocate_from_gclab_slow(thread, size);
 269   } else {
 270     return NULL;
 271   }
 272 }
 273 
 274 inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
 275   if (Thread::current()->is_oom_during_evac()) {
 276     // This thread went through the OOM during evac protocol and it is safe to return
 277     // the forward pointer. It must not attempt to evacuate any more.
 278     return ShenandoahBarrierSet::resolve_forwarded(p);
 279   }
 280 
 281   size_t size_no_fwdptr = (size_t) p->size();
 282   size_t size_with_fwdptr = size_no_fwdptr + BrooksPointer::word_size();
 283 
 284   assert(!heap_region_containing(p)->is_humongous(), "never evacuate humongous objects");
 285 
 286   bool alloc_from_gclab = true;
 287   HeapWord* filler;
 288 #ifdef ASSERT
 289 
 290   assert(thread->is_evac_allowed(), "must be enclosed in ShenandoahOOMDuringEvacHandler");
 291 
 292   if (ShenandoahOOMDuringEvacALot &&
 293       (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
 294         filler = NULL;
 295   } else {
 296 #endif
 297     filler = allocate_from_gclab(thread, size_with_fwdptr);
 298     if (filler == NULL) {
 299       filler = allocate_memory(size_with_fwdptr, _alloc_shared_gc);
 300       alloc_from_gclab = false;
 301     }
 302 #ifdef ASSERT
 303   }
 304 #endif
 305 
 306   if (filler == NULL) {
 307     control_thread()->handle_alloc_failure_evac(size_with_fwdptr);
 308 
 309     _oom_evac_handler.handle_out_of_memory_during_evacuation();
 310 
 311     return ShenandoahBarrierSet::resolve_forwarded(p);
 312   }
 313 
 314   // Copy the object and initialize its forwarding ptr:
 315   HeapWord* copy = filler + BrooksPointer::word_size();
 316   oop copy_val = oop(copy);
 317 
 318   Copy::aligned_disjoint_words((HeapWord*) p, copy, size_no_fwdptr);
 319   BrooksPointer::initialize(oop(copy));
 320 
 321   log_develop_trace(gc, compaction)("Copy object: " PTR_FORMAT " -> " PTR_FORMAT,
 322                                     p2i(p), p2i(copy));
 323 
 324   // Try to install the new forwarding pointer.
 325   oop result = BrooksPointer::try_update_forwardee(p, copy_val);
 326 
 327   if (oopDesc::unsafe_equals(result, p)) {
 328     // Successfully evacuated. Our copy is now the public one!
 329     log_develop_trace(gc, compaction)("Copy object: " PTR_FORMAT " -> " PTR_FORMAT " succeeded",
 330                                       p2i(p), p2i(copy));
 331 
 332 
 333 #ifdef ASSERT
 334     assert(oopDesc::is_oop(copy_val), "expect oop");
 335     assert(p->klass() == copy_val->klass(), "Should have the same class p: "PTR_FORMAT", copy: "PTR_FORMAT,
 336                                               p2i(p), p2i(copy));
 337 #endif
 338     return copy_val;
 339   }  else {
 340     // Failed to evacuate. We need to deal with the object that is left behind. Since this
 341     // new allocation is certainly after TAMS, it will be considered live in the next cycle.
 342     // But if it happens to contain references to evacuated regions, those references would
 343     // not get updated for this stale copy during this cycle, and we will crash while scanning
 344     // it the next cycle.
 345     //
 346     // For GCLAB allocations, it is enough to rollback the allocation ptr. Either the next
 347     // object will overwrite this stale copy, or the filler object on LAB retirement will
 348     // do this. For non-GCLAB allocations, we have no way to retract the allocation, and
 349     // have to explicitly overwrite the copy with the filler object. With that overwrite,
 350     // we have to keep the fwdptr initialized and pointing to our (stale) copy.
 351     if (alloc_from_gclab) {
 352       thread->gclab().rollback(size_with_fwdptr);
 353     } else {
 354       fill_with_object(copy, size_no_fwdptr);
 355     }
 356     log_develop_trace(gc, compaction)("Copy object: " PTR_FORMAT " -> " PTR_FORMAT " failed, use other: " PTR_FORMAT,
 357                                       p2i(p), p2i(copy), p2i(result));
 358     return result;
 359   }
 360 }
 361 
 362 inline bool ShenandoahHeap::requires_marking(const void* entry) const {
 363   return ! is_marked_next(oop(entry));
 364 }
 365 
 366 bool ShenandoahHeap::region_in_collection_set(size_t region_index) const {
 367   assert(collection_set() != NULL, "Sanity");
 368   return collection_set()->is_in(region_index);
 369 }
 370 
 371 bool ShenandoahHeap::in_collection_set(ShenandoahHeapRegion* r) const {
 372   return region_in_collection_set(r->region_number());
 373 }
 374 
 375 template <class T>
 376 inline bool ShenandoahHeap::in_collection_set(T p) const {
 377   HeapWord* obj = (HeapWord*) p;
 378   assert(collection_set() != NULL, "Sanity");
 379   assert(is_in(obj), "should be in heap");
 380 
 381   return collection_set()->is_in(obj);
 382 }
 383 
 384 inline bool ShenandoahHeap::is_stable() const {
 385   return _gc_state.is_clear();
 386 }
 387 
 388 inline bool ShenandoahHeap::is_idle() const {
 389   return _gc_state.is_unset(MARKING | EVACUATION | UPDATEREFS | TRAVERSAL);
 390 }
 391 
 392 inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const {
 393   return _gc_state.is_set(MARKING);
 394 }
 395 
 396 inline bool ShenandoahHeap::is_concurrent_traversal_in_progress() const {
 397   return _gc_state.is_set(TRAVERSAL);
 398 }
 399 
 400 inline bool ShenandoahHeap::is_evacuation_in_progress() const {
 401   return _gc_state.is_set(EVACUATION);
 402 }
 403 
 404 inline bool ShenandoahHeap::is_gc_in_progress_mask(uint mask) const {
 405   return _gc_state.is_set(mask);
 406 }
 407 
 408 inline bool ShenandoahHeap::is_degenerated_gc_in_progress() const {
 409   return _degenerated_gc_in_progress.is_set();
 410 }
 411 
 412 inline bool ShenandoahHeap::is_full_gc_in_progress() const {
 413   return _full_gc_in_progress.is_set();
 414 }
 415 
 416 inline bool ShenandoahHeap::is_full_gc_move_in_progress() const {
 417   return _full_gc_move_in_progress.is_set();
 418 }
 419 
 420 inline bool ShenandoahHeap::is_update_refs_in_progress() const {
 421   return _gc_state.is_set(UPDATEREFS);
 422 }
 423 
 424 inline bool ShenandoahHeap::allocated_after_next_mark_start(HeapWord* addr) const {
 425   uintx index = ((uintx) addr) >> ShenandoahHeapRegion::region_size_bytes_shift();
 426   HeapWord* top_at_mark_start = _next_top_at_mark_starts[index];
 427   bool alloc_after_mark_start = addr >= top_at_mark_start;
 428   return alloc_after_mark_start;
 429 }
 430 
 431 inline bool ShenandoahHeap::allocated_after_complete_mark_start(HeapWord* addr) const {
 432   uintx index = ((uintx) addr) >> ShenandoahHeapRegion::region_size_bytes_shift();
 433   HeapWord* top_at_mark_start = _complete_top_at_mark_starts[index];
 434   bool alloc_after_mark_start = addr >= top_at_mark_start;
 435   return alloc_after_mark_start;
 436 }
 437 
 438 template<class T>
 439 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) {
 440   marked_object_iterate(region, cl, region->top());
 441 }
 442 
 443 template<class T>
 444 inline void ShenandoahHeap::marked_object_safe_iterate(ShenandoahHeapRegion* region, T* cl) {
 445   marked_object_iterate(region, cl, region->concurrent_iteration_safe_limit());
 446 }
 447 
 448 template<class T>
 449 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) {
 450   assert(BrooksPointer::word_offset() < 0, "skip_delta calculation below assumes the forwarding ptr is before obj");
 451   assert(! region->is_humongous_continuation(), "no humongous continuation regions here");
 452 
 453   MarkBitMap* mark_bit_map = _complete_mark_bit_map;
 454   HeapWord* tams = complete_top_at_mark_start(region->bottom());
 455 
 456   size_t skip_bitmap_delta = BrooksPointer::word_size() + 1;
 457   size_t skip_objsize_delta = BrooksPointer::word_size() /* + actual obj.size() below */;
 458   HeapWord* start = region->bottom() + BrooksPointer::word_size();
 459   HeapWord* end = MIN2(tams + BrooksPointer::word_size(), region->end());
 460 
 461   // Step 1. Scan below the TAMS based on bitmap data.
 462   HeapWord* limit_bitmap = MIN2(limit, tams);
 463 
 464   // Try to scan the initial candidate. If the candidate is above the TAMS, it would
 465   // fail the subsequent "< limit_bitmap" checks, and fall through to Step 2.
 466   HeapWord* cb = mark_bit_map->getNextMarkedWordAddress(start, end);
 467 
 468   intx dist = ShenandoahMarkScanPrefetch;
 469   if (dist > 0) {
 470     // Batched scan that prefetches the oop data, anticipating the access to
 471     // either header, oop field, or forwarding pointer. Not that we cannot
 472     // touch anything in oop, while it still being prefetched to get enough
 473     // time for prefetch to work. This is why we try to scan the bitmap linearly,
 474     // disregarding the object size. However, since we know forwarding pointer
 475     // preceeds the object, we can skip over it. Once we cannot trust the bitmap,
 476     // there is no point for prefetching the oop contents, as oop->size() will
 477     // touch it prematurely.
 478 
 479     // No variable-length arrays in standard C++, have enough slots to fit
 480     // the prefetch distance.
 481     static const int SLOT_COUNT = 256;
 482     guarantee(dist <= SLOT_COUNT, "adjust slot count");
 483     HeapWord* slots[SLOT_COUNT];
 484 
 485     int avail;
 486     do {
 487       avail = 0;
 488       for (int c = 0; (c < dist) && (cb < limit_bitmap); c++) {
 489         Prefetch::read(cb, BrooksPointer::byte_offset());
 490         slots[avail++] = cb;
 491         cb += skip_bitmap_delta;
 492         if (cb < limit_bitmap) {
 493           cb = mark_bit_map->getNextMarkedWordAddress(cb, limit_bitmap);
 494         }
 495       }
 496 
 497       for (int c = 0; c < avail; c++) {
 498         assert (slots[c] < tams,  "only objects below TAMS here: "  PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(tams));
 499         assert (slots[c] < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(limit));
 500         oop obj = oop(slots[c]);
 501         do_object_marked_complete(cl, obj);
 502       }
 503     } while (avail > 0);
 504   } else {
 505     while (cb < limit_bitmap) {
 506       assert (cb < tams,  "only objects below TAMS here: "  PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(tams));
 507       assert (cb < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(limit));
 508       oop obj = oop(cb);
 509       do_object_marked_complete(cl, obj);
 510       cb += skip_bitmap_delta;
 511       if (cb < limit_bitmap) {
 512         cb = mark_bit_map->getNextMarkedWordAddress(cb, limit_bitmap);
 513       }
 514     }
 515   }
 516 
 517   // Step 2. Accurate size-based traversal, happens past the TAMS.
 518   // This restarts the scan at TAMS, which makes sure we traverse all objects,
 519   // regardless of what happened at Step 1.
 520   HeapWord* cs = tams + BrooksPointer::word_size();
 521   while (cs < limit) {
 522     assert (cs > tams,  "only objects past TAMS here: "   PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(tams));
 523     assert (cs < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(limit));
 524     oop obj = oop(cs);
 525     int size = obj->size();
 526     do_object_marked_complete(cl, obj);
 527     cs += size + skip_objsize_delta;
 528   }
 529 }
 530 
 531 template<class T>
 532 inline void ShenandoahHeap::do_object_marked_complete(T* cl, oop obj) {
 533   assert(oopDesc::is_oop(obj), "sanity");
 534   assert(is_marked_complete(obj), "object expected to be marked");
 535   cl->do_object(obj);
 536 }
 537 
 538 template <class T>
 539 class ShenandoahObjectToOopClosure : public ObjectClosure {
 540   T* _cl;
 541 public:
 542   ShenandoahObjectToOopClosure(T* cl) : _cl(cl) {}
 543 
 544   void do_object(oop obj) {
 545     obj->oop_iterate(_cl);
 546   }
 547 };
 548 
 549 template <class T>
 550 class ShenandoahObjectToOopBoundedClosure : public ObjectClosure {
 551   T* _cl;
 552   MemRegion _bounds;
 553 public:
 554   ShenandoahObjectToOopBoundedClosure(T* cl, HeapWord* bottom, HeapWord* top) :
 555     _cl(cl), _bounds(bottom, top) {}
 556 
 557   void do_object(oop obj) {
 558     obj->oop_iterate(_cl, _bounds);
 559   }
 560 };
 561 
 562 template<class T>
 563 inline void ShenandoahHeap::marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* top) {
 564   if (region->is_humongous()) {
 565     HeapWord* bottom = region->bottom();
 566     if (top > bottom) {
 567       region = region->humongous_start_region();
 568       ShenandoahObjectToOopBoundedClosure<T> objs(cl, bottom, top);
 569       marked_object_iterate(region, &objs);
 570     }
 571   } else {
 572     ShenandoahObjectToOopClosure<T> objs(cl);
 573     marked_object_iterate(region, &objs, top);
 574   }
 575 }
 576 
 577 template<class T>
 578 inline void ShenandoahHeap::marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl) {
 579   marked_object_oop_iterate(region, cl, region->top());
 580 }
 581 
 582 template<class T>
 583 inline void ShenandoahHeap::marked_object_oop_safe_iterate(ShenandoahHeapRegion* region, T* cl) {
 584   marked_object_oop_iterate(region, cl, region->concurrent_iteration_safe_limit());
 585 }
 586 
 587 inline ShenandoahHeapRegion* const ShenandoahHeap::get_region(size_t region_idx) const {
 588   if (region_idx >= _num_regions) {
 589     return NULL;
 590   } else {
 591     return _regions[region_idx];
 592   }
 593 }
 594 
 595 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP