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