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