1 /*
  2  * Copyright (c) 2015, 2020, Red Hat, Inc. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP
 27 
 28 #include "gc/shenandoah/shenandoahHeap.hpp"
 29 
 30 #include "classfile/javaClasses.inline.hpp"
 31 #include "gc/shared/markBitMap.inline.hpp"
 32 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
 33 #include "gc/shared/continuationGCSupport.inline.hpp"
 34 #include "gc/shared/suspendibleThreadSet.hpp"
 35 #include "gc/shared/tlab_globals.hpp"
 36 #include "gc/shenandoah/shenandoahAsserts.hpp"
 37 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
 38 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
 39 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
 40 #include "gc/shenandoah/shenandoahWorkGroup.hpp"
 41 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
 42 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 43 #include "gc/shenandoah/shenandoahControlThread.hpp"
 44 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 45 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 46 #include "oops/compressedOops.inline.hpp"
 47 #include "oops/oop.inline.hpp"
 48 #include "runtime/atomic.hpp"
 49 #include "runtime/javaThread.hpp"
 50 #include "runtime/prefetch.inline.hpp"
 51 #include "utilities/copy.hpp"
 52 #include "utilities/globalDefinitions.hpp"
 53 
 54 inline ShenandoahHeap* ShenandoahHeap::heap() {
 55   return named_heap<ShenandoahHeap>(CollectedHeap::Shenandoah);
 56 }
 57 
 58 inline ShenandoahHeapRegion* ShenandoahRegionIterator::next() {
 59   size_t new_index = Atomic::add(&_index, (size_t) 1, memory_order_relaxed);
 60   // get_region() provides the bounds-check and returns null on OOB.
 61   return _heap->get_region(new_index - 1);
 62 }
 63 
 64 inline bool ShenandoahHeap::has_forwarded_objects() const {
 65   return _gc_state.is_set(HAS_FORWARDED);
 66 }
 67 
 68 inline WorkerThreads* ShenandoahHeap::workers() const {
 69   return _workers;
 70 }
 71 
 72 inline WorkerThreads* ShenandoahHeap::safepoint_workers() {
 73   return _safepoint_workers;
 74 }
 75 
 76 inline size_t ShenandoahHeap::heap_region_index_containing(const void* addr) const {
 77   uintptr_t region_start = ((uintptr_t) addr);
 78   uintptr_t index = (region_start - (uintptr_t) base()) >> ShenandoahHeapRegion::region_size_bytes_shift();
 79   assert(index < num_regions(), "Region index is in bounds: " PTR_FORMAT, p2i(addr));
 80   return index;
 81 }
 82 
 83 inline ShenandoahHeapRegion* const ShenandoahHeap::heap_region_containing(const void* addr) const {
 84   size_t index = heap_region_index_containing(addr);
 85   ShenandoahHeapRegion* const result = get_region(index);
 86   assert(addr >= result->bottom() && addr < result->end(), "Heap region contains the address: " PTR_FORMAT, p2i(addr));
 87   return result;
 88 }
 89 
 90 inline void ShenandoahHeap::enter_evacuation(Thread* t) {
 91   _oom_evac_handler.enter_evacuation(t);
 92 }
 93 
 94 inline void ShenandoahHeap::leave_evacuation(Thread* t) {
 95   _oom_evac_handler.leave_evacuation(t);
 96 }
 97 
 98 template <class T>
 99 inline void ShenandoahHeap::update_with_forwarded(T* p) {
100   T o = RawAccess<>::oop_load(p);
101   if (!CompressedOops::is_null(o)) {
102     oop obj = CompressedOops::decode_not_null(o);
103     if (in_collection_set(obj)) {
104       // Corner case: when evacuation fails, there are objects in collection
105       // set that are not really forwarded. We can still go and try and update them
106       // (uselessly) to simplify the common path.
107       shenandoah_assert_forwarded_except(p, obj, cancelled_gc());
108       oop fwd = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
109       shenandoah_assert_not_in_cset_except(p, fwd, cancelled_gc());
110 
111       // Unconditionally store the update: no concurrent updates expected.
112       RawAccess<IS_NOT_NULL>::oop_store(p, fwd);
113     }
114   }
115 }
116 
117 template <class T>
118 inline void ShenandoahHeap::conc_update_with_forwarded(T* p) {
119   T o = RawAccess<>::oop_load(p);
120   if (!CompressedOops::is_null(o)) {
121     oop obj = CompressedOops::decode_not_null(o);
122     if (in_collection_set(obj)) {
123       // Corner case: when evacuation fails, there are objects in collection
124       // set that are not really forwarded. We can still go and try CAS-update them
125       // (uselessly) to simplify the common path.
126       shenandoah_assert_forwarded_except(p, obj, cancelled_gc());
127       oop fwd = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
128       shenandoah_assert_not_in_cset_except(p, fwd, cancelled_gc());
129 
130       // Sanity check: we should not be updating the cset regions themselves,
131       // unless we are recovering from the evacuation failure.
132       shenandoah_assert_not_in_cset_loc_except(p, !is_in(p) || cancelled_gc());
133 
134       // Either we succeed in updating the reference, or something else gets in our way.
135       // We don't care if that is another concurrent GC update, or another mutator update.
136       atomic_update_oop(fwd, p, obj);
137     }
138   }
139 }
140 
141 // Atomic updates of heap location. This is only expected to work with updating the same
142 // logical object with its forwardee. The reason why we need stronger-than-relaxed memory
143 // ordering has to do with coordination with GC barriers and mutator accesses.
144 //
145 // In essence, stronger CAS access is required to maintain the transitive chains that mutator
146 // accesses build by themselves. To illustrate this point, consider the following example.
147 //
148 // Suppose "o" is the object that has a field "x" and the reference to "o" is stored
149 // to field at "addr", which happens to be Java volatile field. Normally, the accesses to volatile
150 // field at "addr" would be matched with release/acquire barriers. This changes when GC moves
151 // the object under mutator feet.
152 //
153 // Thread 1 (Java)
154 //         // --- previous access starts here
155 //         ...
156 //   T1.1: store(&o.x, 1, mo_relaxed)
157 //   T1.2: store(&addr, o, mo_release) // volatile store
158 //
159 //         // --- new access starts here
160 //         // LRB: copy and install the new copy to fwdptr
161 //   T1.3: var copy = copy(o)
162 //   T1.4: cas(&fwd, t, copy, mo_release) // pointer-mediated publication
163 //         <access continues>
164 //
165 // Thread 2 (GC updater)
166 //   T2.1: var f = load(&fwd, mo_{consume|acquire}) // pointer-mediated acquisition
167 //   T2.2: cas(&addr, o, f, mo_release) // this method
168 //
169 // Thread 3 (Java)
170 //   T3.1: var o = load(&addr, mo_acquire) // volatile read
171 //   T3.2: if (o != null)
172 //   T3.3:   var r = load(&o.x, mo_relaxed)
173 //
174 // r is guaranteed to contain "1".
175 //
176 // Without GC involvement, there is synchronizes-with edge from T1.2 to T3.1,
177 // which guarantees this. With GC involvement, when LRB copies the object and
178 // another thread updates the reference to it, we need to have the transitive edge
179 // from T1.4 to T2.1 (that one is guaranteed by forwarding accesses), plus the edge
180 // from T2.2 to T3.1 (which is brought by this CAS).
181 //
182 // Note that we do not need to "acquire" in these methods, because we do not read the
183 // failure witnesses contents on any path, and "release" is enough.
184 //
185 
186 inline void ShenandoahHeap::atomic_update_oop(oop update, oop* addr, oop compare) {
187   assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr));
188   Atomic::cmpxchg(addr, compare, update, memory_order_release);
189 }
190 
191 inline void ShenandoahHeap::atomic_update_oop(oop update, narrowOop* addr, narrowOop compare) {
192   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
193   narrowOop u = CompressedOops::encode(update);
194   Atomic::cmpxchg(addr, compare, u, memory_order_release);
195 }
196 
197 inline void ShenandoahHeap::atomic_update_oop(oop update, narrowOop* addr, oop compare) {
198   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
199   narrowOop c = CompressedOops::encode(compare);
200   narrowOop u = CompressedOops::encode(update);
201   Atomic::cmpxchg(addr, c, u, memory_order_release);
202 }
203 
204 inline bool ShenandoahHeap::atomic_update_oop_check(oop update, oop* addr, oop compare) {
205   assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr));
206   return (oop) Atomic::cmpxchg(addr, compare, update, memory_order_release) == compare;
207 }
208 
209 inline bool ShenandoahHeap::atomic_update_oop_check(oop update, narrowOop* addr, narrowOop compare) {
210   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
211   narrowOop u = CompressedOops::encode(update);
212   return (narrowOop) Atomic::cmpxchg(addr, compare, u, memory_order_release) == compare;
213 }
214 
215 inline bool ShenandoahHeap::atomic_update_oop_check(oop update, narrowOop* addr, oop compare) {
216   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
217   narrowOop c = CompressedOops::encode(compare);
218   narrowOop u = CompressedOops::encode(update);
219   return CompressedOops::decode(Atomic::cmpxchg(addr, c, u, memory_order_release)) == compare;
220 }
221 
222 // The memory ordering discussion above does not apply for methods that store nulls:
223 // then, there is no transitive reads in mutator (as we see nulls), and we can do
224 // relaxed memory ordering there.
225 
226 inline void ShenandoahHeap::atomic_clear_oop(oop* addr, oop compare) {
227   assert(is_aligned(addr, HeapWordSize), "Address should be aligned: " PTR_FORMAT, p2i(addr));
228   Atomic::cmpxchg(addr, compare, oop(), memory_order_relaxed);
229 }
230 
231 inline void ShenandoahHeap::atomic_clear_oop(narrowOop* addr, oop compare) {
232   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
233   narrowOop cmp = CompressedOops::encode(compare);
234   Atomic::cmpxchg(addr, cmp, narrowOop(), memory_order_relaxed);
235 }
236 
237 inline void ShenandoahHeap::atomic_clear_oop(narrowOop* addr, narrowOop compare) {
238   assert(is_aligned(addr, sizeof(narrowOop)), "Address should be aligned: " PTR_FORMAT, p2i(addr));
239   Atomic::cmpxchg(addr, compare, narrowOop(), memory_order_relaxed);
240 }
241 
242 inline bool ShenandoahHeap::cancelled_gc() const {
243   return _cancelled_gc.get() == CANCELLED;
244 }
245 
246 inline bool ShenandoahHeap::check_cancelled_gc_and_yield(bool sts_active) {
247   if (! (sts_active && ShenandoahSuspendibleWorkers)) {
248     return cancelled_gc();
249   }
250 
251   jbyte prev = _cancelled_gc.cmpxchg(NOT_CANCELLED, CANCELLABLE);
252   if (prev == CANCELLABLE || prev == NOT_CANCELLED) {
253     if (SuspendibleThreadSet::should_yield()) {
254       SuspendibleThreadSet::yield();
255     }
256 
257     // Back to CANCELLABLE. The thread that poked NOT_CANCELLED first gets
258     // to restore to CANCELLABLE.
259     if (prev == CANCELLABLE) {
260       _cancelled_gc.set(CANCELLABLE);
261     }
262     return false;
263   } else {
264     return true;
265   }
266 }
267 
268 inline void ShenandoahHeap::clear_cancelled_gc() {
269   _cancelled_gc.set(CANCELLABLE);
270   _oom_evac_handler.clear();
271 }
272 
273 inline HeapWord* ShenandoahHeap::allocate_from_gclab(Thread* thread, size_t size) {
274   assert(UseTLAB, "TLABs should be enabled");
275 
276   PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
277   if (gclab == nullptr) {
278     assert(!thread->is_Java_thread() && !thread->is_Worker_thread(),
279            "Performance: thread should have GCLAB: %s", thread->name());
280     // No GCLABs in this thread, fallback to shared allocation
281     return nullptr;
282   }
283   HeapWord* obj = gclab->allocate(size);
284   if (obj != nullptr) {
285     return obj;
286   }
287   // Otherwise...
288   return allocate_from_gclab_slow(thread, size);
289 }
290 
291 inline oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
292   if (ShenandoahThreadLocalData::is_oom_during_evac(Thread::current())) {
293     // This thread went through the OOM during evac protocol and it is safe to return
294     // the forward pointer. It must not attempt to evacuate any more.
295     return ShenandoahBarrierSet::resolve_forwarded(p);
296   }
297 
298   assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
299 
300   size_t size = p->size();
301 
302   assert(!heap_region_containing(p)->is_humongous(), "never evacuate humongous objects");
303 
304   bool alloc_from_gclab = true;
305   HeapWord* copy = nullptr;
306 
307 #ifdef ASSERT
308   if (ShenandoahOOMDuringEvacALot &&
309       (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
310         copy = nullptr;
311   } else {
312 #endif
313     if (UseTLAB) {
314       copy = allocate_from_gclab(thread, size);
315     }
316     if (copy == nullptr) {
317       ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size);
318       copy = allocate_memory(req);
319       alloc_from_gclab = false;
320     }
321 #ifdef ASSERT
322   }
323 #endif
324 
325   if (copy == nullptr) {
326     control_thread()->handle_alloc_failure_evac(size);
327 
328     _oom_evac_handler.handle_out_of_memory_during_evacuation();
329 
330     return ShenandoahBarrierSet::resolve_forwarded(p);
331   }
332 
333   // Copy the object:
334   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size);
335 
336   // Try to install the new forwarding pointer.
337   oop copy_val = cast_to_oop(copy);
338   ContinuationGCSupport::relativize_stack_chunk(copy_val);
339 
340   oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
341   if (result == copy_val) {
342     // Successfully evacuated. Our copy is now the public one!
343     shenandoah_assert_correct(nullptr, copy_val);
344     return copy_val;
345   }  else {
346     // Failed to evacuate. We need to deal with the object that is left behind. Since this
347     // new allocation is certainly after TAMS, it will be considered live in the next cycle.
348     // But if it happens to contain references to evacuated regions, those references would
349     // not get updated for this stale copy during this cycle, and we will crash while scanning
350     // it the next cycle.
351     //
352     // For GCLAB allocations, it is enough to rollback the allocation ptr. Either the next
353     // object will overwrite this stale copy, or the filler object on LAB retirement will
354     // do this. For non-GCLAB allocations, we have no way to retract the allocation, and
355     // have to explicitly overwrite the copy with the filler object. With that overwrite,
356     // we have to keep the fwdptr initialized and pointing to our (stale) copy.
357     if (alloc_from_gclab) {
358       ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
359     } else {
360       fill_with_object(copy, size);
361       shenandoah_assert_correct(nullptr, copy_val);
362     }
363     shenandoah_assert_correct(nullptr, result);
364     return result;
365   }
366 }
367 
368 inline bool ShenandoahHeap::requires_marking(const void* entry) const {
369   oop obj = cast_to_oop(entry);
370   return !_marking_context->is_marked_strong(obj);
371 }
372 
373 inline bool ShenandoahHeap::in_collection_set(oop p) const {
374   assert(collection_set() != nullptr, "Sanity");
375   return collection_set()->is_in(p);
376 }
377 
378 inline bool ShenandoahHeap::in_collection_set_loc(void* p) const {
379   assert(collection_set() != nullptr, "Sanity");
380   return collection_set()->is_in_loc(p);
381 }
382 
383 inline bool ShenandoahHeap::is_stable() const {
384   return _gc_state.is_clear();
385 }
386 
387 inline bool ShenandoahHeap::is_idle() const {
388   return _gc_state.is_unset(MARKING | EVACUATION | UPDATEREFS);
389 }
390 
391 inline bool ShenandoahHeap::is_concurrent_mark_in_progress() const {
392   return _gc_state.is_set(MARKING);
393 }
394 
395 inline bool ShenandoahHeap::is_evacuation_in_progress() const {
396   return _gc_state.is_set(EVACUATION);
397 }
398 
399 inline bool ShenandoahHeap::is_gc_in_progress_mask(uint mask) const {
400   return _gc_state.is_set(mask);
401 }
402 
403 inline bool ShenandoahHeap::is_degenerated_gc_in_progress() const {
404   return _degenerated_gc_in_progress.is_set();
405 }
406 
407 inline bool ShenandoahHeap::is_full_gc_in_progress() const {
408   return _full_gc_in_progress.is_set();
409 }
410 
411 inline bool ShenandoahHeap::is_full_gc_move_in_progress() const {
412   return _full_gc_move_in_progress.is_set();
413 }
414 
415 inline bool ShenandoahHeap::is_update_refs_in_progress() const {
416   return _gc_state.is_set(UPDATEREFS);
417 }
418 
419 inline bool ShenandoahHeap::is_stw_gc_in_progress() const {
420   return is_full_gc_in_progress() || is_degenerated_gc_in_progress();
421 }
422 
423 inline bool ShenandoahHeap::is_concurrent_strong_root_in_progress() const {
424   return _concurrent_strong_root_in_progress.is_set();
425 }
426 
427 inline bool ShenandoahHeap::is_concurrent_weak_root_in_progress() const {
428   return _gc_state.is_set(WEAK_ROOTS);
429 }
430 
431 template<class T>
432 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl) {
433   marked_object_iterate(region, cl, region->top());
434 }
435 
436 template<class T>
437 inline void ShenandoahHeap::marked_object_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* limit) {
438   assert(! region->is_humongous_continuation(), "no humongous continuation regions here");
439 
440   ShenandoahMarkingContext* const ctx = complete_marking_context();
441   assert(ctx->is_complete(), "sanity");
442 
443   HeapWord* tams = ctx->top_at_mark_start(region);
444 
445   size_t skip_bitmap_delta = 1;
446   HeapWord* start = region->bottom();
447   HeapWord* end = MIN2(tams, region->end());
448 
449   // Step 1. Scan below the TAMS based on bitmap data.
450   HeapWord* limit_bitmap = MIN2(limit, tams);
451 
452   // Try to scan the initial candidate. If the candidate is above the TAMS, it would
453   // fail the subsequent "< limit_bitmap" checks, and fall through to Step 2.
454   HeapWord* cb = ctx->get_next_marked_addr(start, end);
455 
456   intx dist = ShenandoahMarkScanPrefetch;
457   if (dist > 0) {
458     // Batched scan that prefetches the oop data, anticipating the access to
459     // either header, oop field, or forwarding pointer. Not that we cannot
460     // touch anything in oop, while it still being prefetched to get enough
461     // time for prefetch to work. This is why we try to scan the bitmap linearly,
462     // disregarding the object size. However, since we know forwarding pointer
463     // precedes the object, we can skip over it. Once we cannot trust the bitmap,
464     // there is no point for prefetching the oop contents, as oop->size() will
465     // touch it prematurely.
466 
467     // No variable-length arrays in standard C++, have enough slots to fit
468     // the prefetch distance.
469     static const int SLOT_COUNT = 256;
470     guarantee(dist <= SLOT_COUNT, "adjust slot count");
471     HeapWord* slots[SLOT_COUNT];
472 
473     int avail;
474     do {
475       avail = 0;
476       for (int c = 0; (c < dist) && (cb < limit_bitmap); c++) {
477         Prefetch::read(cb, oopDesc::mark_offset_in_bytes());
478         slots[avail++] = cb;
479         cb += skip_bitmap_delta;
480         if (cb < limit_bitmap) {
481           cb = ctx->get_next_marked_addr(cb, limit_bitmap);
482         }
483       }
484 
485       for (int c = 0; c < avail; c++) {
486         assert (slots[c] < tams,  "only objects below TAMS here: "  PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(tams));
487         assert (slots[c] < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(slots[c]), p2i(limit));
488         oop obj = cast_to_oop(slots[c]);
489         assert(oopDesc::is_oop(obj), "sanity");
490         assert(ctx->is_marked(obj), "object expected to be marked");
491         cl->do_object(obj);
492       }
493     } while (avail > 0);
494   } else {
495     while (cb < limit_bitmap) {
496       assert (cb < tams,  "only objects below TAMS here: "  PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(tams));
497       assert (cb < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cb), p2i(limit));
498       oop obj = cast_to_oop(cb);
499       assert(oopDesc::is_oop(obj), "sanity");
500       assert(ctx->is_marked(obj), "object expected to be marked");
501       cl->do_object(obj);
502       cb += skip_bitmap_delta;
503       if (cb < limit_bitmap) {
504         cb = ctx->get_next_marked_addr(cb, limit_bitmap);
505       }
506     }
507   }
508 
509   // Step 2. Accurate size-based traversal, happens past the TAMS.
510   // This restarts the scan at TAMS, which makes sure we traverse all objects,
511   // regardless of what happened at Step 1.
512   HeapWord* cs = tams;
513   while (cs < limit) {
514     assert (cs >= tams, "only objects past TAMS here: "   PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(tams));
515     assert (cs < limit, "only objects below limit here: " PTR_FORMAT " (" PTR_FORMAT ")", p2i(cs), p2i(limit));
516     oop obj = cast_to_oop(cs);
517     assert(oopDesc::is_oop(obj), "sanity");
518     assert(ctx->is_marked(obj), "object expected to be marked");
519     size_t size = obj->size();
520     cl->do_object(obj);
521     cs += size;
522   }
523 }
524 
525 template <class T>
526 class ShenandoahObjectToOopClosure : public ObjectClosure {
527   T* _cl;
528 public:
529   ShenandoahObjectToOopClosure(T* cl) : _cl(cl) {}
530 
531   void do_object(oop obj) {
532     obj->oop_iterate(_cl);
533   }
534 };
535 
536 template <class T>
537 class ShenandoahObjectToOopBoundedClosure : public ObjectClosure {
538   T* _cl;
539   MemRegion _bounds;
540 public:
541   ShenandoahObjectToOopBoundedClosure(T* cl, HeapWord* bottom, HeapWord* top) :
542     _cl(cl), _bounds(bottom, top) {}
543 
544   void do_object(oop obj) {
545     obj->oop_iterate(_cl, _bounds);
546   }
547 };
548 
549 template<class T>
550 inline void ShenandoahHeap::marked_object_oop_iterate(ShenandoahHeapRegion* region, T* cl, HeapWord* top) {
551   if (region->is_humongous()) {
552     HeapWord* bottom = region->bottom();
553     if (top > bottom) {
554       region = region->humongous_start_region();
555       ShenandoahObjectToOopBoundedClosure<T> objs(cl, bottom, top);
556       marked_object_iterate(region, &objs);
557     }
558   } else {
559     ShenandoahObjectToOopClosure<T> objs(cl);
560     marked_object_iterate(region, &objs, top);
561   }
562 }
563 
564 inline ShenandoahHeapRegion* const ShenandoahHeap::get_region(size_t region_idx) const {
565   if (region_idx < _num_regions) {
566     return _regions[region_idx];
567   } else {
568     return nullptr;
569   }
570 }
571 
572 inline void ShenandoahHeap::mark_complete_marking_context() {
573   _marking_context->mark_complete();
574 }
575 
576 inline void ShenandoahHeap::mark_incomplete_marking_context() {
577   _marking_context->mark_incomplete();
578 }
579 
580 inline ShenandoahMarkingContext* ShenandoahHeap::complete_marking_context() const {
581   assert (_marking_context->is_complete()," sanity");
582   return _marking_context;
583 }
584 
585 inline ShenandoahMarkingContext* ShenandoahHeap::marking_context() const {
586   return _marking_context;
587 }
588 
589 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAP_INLINE_HPP