inline bool PSScavenge::should_scavenge(T* p, bool check_to_space) {
  if (check_to_space) {
    ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
    return should_scavenge(p, heap->young_gen()->to_space());
  }
  return should_scavenge(p);
}
inline void PSScavenge::copy_and_push_safe_barrier(PSPromotionManager* pm,
                                                   T*                  p) {
  assert(should_scavenge(p, true), "revisiting object?");

  oop o = oopDesc::load_decode_heap_oop_not_null(p);
  oop new_obj = o->is_forwarded()
        ? o->forwardee()
        : pm->copy_to_survivor_space<promote_immediately>(o);

#ifndef PRODUCT
  // This code must come after the CAS test, or it will print incorrect
  // information.
  if (TraceScavenge &&  o->is_forwarded()) {
    gclog_or_tty->print_cr("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}",
       "forwarding",
       new_obj->klass()->internal_name(), o, new_obj, new_obj->size());
  }
#endif

  oopDesc::encode_store_heap_oop_not_null(p, new_obj);

  // We cannot mark without test, as some code passes us pointers
  // that are outside the heap. These pointers are either from roots
  // or from metadata.
  if ((!PSScavenge::is_obj_in_young((HeapWord*)p)) &&
      Universe::heap()->is_in_reserved(p)) {
    if (PSScavenge::is_obj_in_young((HeapWord*)new_obj)) {
      card_table()->inline_write_ref_field_gc(p, new_obj);
    }
  }
}
inline bool PSScavenge::should_scavenge(T* p, MutableSpace* to_space) {
  if (should_scavenge(p)) {
    oop obj = oopDesc::load_decode_heap_oop_not_null(p);
    // Skip objects copied to to_space since the scavenge started.
    HeapWord* const addr = (HeapWord*)obj;
    return addr < to_space_top_before_gc() || addr >= to_space->end();
  }
  return false;
}
inline void PSScavenge::copy_and_push_safe_barrier(PSPromotionManager* pm,
                                                   T*                  p) {
  assert(should_scavenge(p, true), "revisiting object?");

  oop o = oopDesc::load_decode_heap_oop_not_null(p);
  oop new_obj = o->is_forwarded()
        ? o->forwardee()
        : pm->copy_to_survivor_space(o);
  oopDesc::encode_store_heap_oop_not_null(p, new_obj);

  // We cannot mark without test, as some code passes us pointers
  // that are outside the heap.
  if ((!PSScavenge::is_obj_in_young((HeapWord*)p)) &&
      Universe::heap()->is_in_reserved(p)) {
    if (PSScavenge::is_obj_in_young((HeapWord*)new_obj)) {
      card_table()->inline_write_ref_field_gc(p, new_obj);
    }
  }
}
// Attempt to "claim" oop at p via CAS, push the new obj if successful
// This version tests the oop* to make sure it is within the heap before
// attempting marking.
inline void PSScavenge::copy_and_push_safe_barrier(PSPromotionManager* pm, oop* p) {
  assert(should_scavenge(*p), "Sanity");
  assert(Universe::heap()->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
  assert(!((ParallelScavengeHeap*)Universe::heap())->young_gen()->to_space()->contains(*p),"Attempt to rescan object");

  oop o = *p;
  if (o->is_forwarded()) {
    *p = o->forwardee();
  } else {
    *p = pm->copy_to_survivor_space(o);
  }
  
  // We cannot mark without test, as some code passes us pointers that are outside the heap.
  if (((HeapWord*)p >= _eden_boundary) &&  Universe::heap()->is_in_reserved(p)) {
    o = *p;
    if ((HeapWord*)o < _eden_boundary) {
      card_table()->inline_write_ref_field_gc(p, o);
    }
  }
}
inline void PSPromotionManager::claim_or_forward_depth(T* p) {
    assert(should_scavenge(p, true), "revisiting object?");
    assert(ParallelScavengeHeap::heap()->is_in(p), "pointer outside heap");

    claim_or_forward_internal_depth(p);
}
inline oop PSPromotionManager::copy_to_survivor_space(oop o) {
    assert(should_scavenge(&o), "Sanity");

    oop new_obj = NULL;

    // NOTE! We must be very careful with any methods that access the mark
    // in o. There may be multiple threads racing on it, and it may be forwarded
    // at any time. Do not use oop methods for accessing the mark!
    markOop test_mark = o->mark();

    // The same test as "o->is_forwarded()"
    if (!test_mark->is_marked()) {
        bool new_obj_is_tenured = false;
        size_t new_obj_size = o->size();

        // Find the objects age, MT safe.
        uint age = (test_mark->has_displaced_mark_helper() /* o->has_displaced_mark() */) ?
                   test_mark->displaced_mark_helper()->age() : test_mark->age();

        if (!promote_immediately) {
            // Try allocating obj in to-space (unless too old)
            if (age < PSScavenge::tenuring_threshold()) {
                new_obj = (oop) _young_lab.allocate(new_obj_size);
                if (new_obj == NULL && !_young_gen_is_full) {
                    // Do we allocate directly, or flush and refill?
                    if (new_obj_size > (YoungPLABSize / 2)) {
                        // Allocate this object directly
                        new_obj = (oop)young_space()->cas_allocate(new_obj_size);
                        promotion_trace_event(new_obj, o, new_obj_size, age, false, NULL);
                    } else {
                        // Flush and fill
                        _young_lab.flush();

                        HeapWord* lab_base = young_space()->cas_allocate(YoungPLABSize);
                        if (lab_base != NULL) {
                            _young_lab.initialize(MemRegion(lab_base, YoungPLABSize));
                            // Try the young lab allocation again.
                            new_obj = (oop) _young_lab.allocate(new_obj_size);
                            promotion_trace_event(new_obj, o, new_obj_size, age, false, &_young_lab);
                        } else {
                            _young_gen_is_full = true;
                        }
                    }
                }
            }
        }

        // Otherwise try allocating obj tenured
        if (new_obj == NULL) {
#ifndef PRODUCT
            if (ParallelScavengeHeap::heap()->promotion_should_fail()) {
                return oop_promotion_failed(o, test_mark);
            }
#endif  // #ifndef PRODUCT

            new_obj = (oop) _old_lab.allocate(new_obj_size);
            new_obj_is_tenured = true;

            if (new_obj == NULL) {
                if (!_old_gen_is_full) {
                    // Do we allocate directly, or flush and refill?
                    if (new_obj_size > (OldPLABSize / 2)) {
                        // Allocate this object directly
                        new_obj = (oop)old_gen()->cas_allocate(new_obj_size);
                        promotion_trace_event(new_obj, o, new_obj_size, age, true, NULL);
                    } else {
                        // Flush and fill
                        _old_lab.flush();

                        HeapWord* lab_base = old_gen()->cas_allocate(OldPLABSize);
                        if(lab_base != NULL) {
#ifdef ASSERT
                            // Delay the initialization of the promotion lab (plab).
                            // This exposes uninitialized plabs to card table processing.
                            if (GCWorkerDelayMillis > 0) {
                                os::sleep(Thread::current(), GCWorkerDelayMillis, false);
                            }
#endif
                            _old_lab.initialize(MemRegion(lab_base, OldPLABSize));
                            // Try the old lab allocation again.
                            new_obj = (oop) _old_lab.allocate(new_obj_size);
                            promotion_trace_event(new_obj, o, new_obj_size, age, true, &_old_lab);
                        }
                    }
                }

                // This is the promotion failed test, and code handling.
                // The code belongs here for two reasons. It is slightly
                // different than the code below, and cannot share the
                // CAS testing code. Keeping the code here also minimizes
                // the impact on the common case fast path code.

                if (new_obj == NULL) {
                    _old_gen_is_full = true;
                    return oop_promotion_failed(o, test_mark);
                }
            }
        }

        assert(new_obj != NULL, "allocation should have succeeded");

        // Copy obj
        Copy::aligned_disjoint_words((HeapWord*)o, (HeapWord*)new_obj, new_obj_size);

        // Now we have to CAS in the header.
        if (o->cas_forward_to(new_obj, test_mark)) {
            // We won any races, we "own" this object.
            assert(new_obj == o->forwardee(), "Sanity");

            // Increment age if obj still in new generation. Now that
            // we're dealing with a markOop that cannot change, it is
            // okay to use the non mt safe oop methods.
            if (!new_obj_is_tenured) {
                new_obj->incr_age();
                assert(young_space()->contains(new_obj), "Attempt to push non-promoted obj");
            }

            // Do the size comparison first with new_obj_size, which we
            // already have. Hopefully, only a few objects are larger than
            // _min_array_size_for_chunking, and most of them will be arrays.
            // So, the is->objArray() test would be very infrequent.
            if (new_obj_size > _min_array_size_for_chunking &&
                    new_obj->is_objArray() &&
                    PSChunkLargeArrays) {
                // we'll chunk it
                oop* const masked_o = mask_chunked_array_oop(o);
                push_depth(masked_o);
                TASKQUEUE_STATS_ONLY(++_arrays_chunked; ++_masked_pushes);
            } else {