1 /*
   2  * Copyright (c) 2010, 2016, Oracle and/or its affiliates. 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_VM_GC_PARALLEL_PSCOMPACTIONMANAGER_INLINE_HPP
  26 #define SHARE_VM_GC_PARALLEL_PSCOMPACTIONMANAGER_INLINE_HPP
  27 
  28 #include "gc/parallel/parMarkBitMap.hpp"
  29 #include "gc/parallel/psCompactionManager.hpp"
  30 #include "gc/parallel/psParallelCompact.inline.hpp"
  31 #include "gc/shared/taskqueue.inline.hpp"
  32 #include "oops/objArrayOop.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "utilities/debug.hpp"
  35 #include "utilities/globalDefinitions.hpp"
  36 
  37 inline bool ParCompactionManager::steal(int queue_num, int* seed, oop& t) {
  38   return stack_array()->steal(queue_num, seed, t);
  39 }
  40 
  41 inline bool ParCompactionManager::steal_objarray(int queue_num, int* seed, ObjArrayTask& t) {
  42   return _objarray_queues->steal(queue_num, seed, t);
  43 }
  44 
  45 inline bool ParCompactionManager::steal(int queue_num, int* seed, size_t& region) {
  46   return region_array()->steal(queue_num, seed, region);
  47 }
  48 
  49 inline void ParCompactionManager::push(oop obj) {
  50   _marking_stack.push(obj);
  51 }
  52 
  53 void ParCompactionManager::push_objarray(oop obj, size_t index)
  54 {
  55   ObjArrayTask task(obj, index);
  56   assert(task.is_valid(), "bad ObjArrayTask");
  57   _objarray_stack.push(task);
  58 }
  59 
  60 void ParCompactionManager::push_region(size_t index)
  61 {
  62 #ifdef ASSERT
  63   const ParallelCompactData& sd = PSParallelCompact::summary_data();
  64   ParallelCompactData::RegionData* const region_ptr = sd.region(index);
  65   assert(region_ptr->claimed(), "must be claimed");
  66   assert(region_ptr->_pushed++ == 0, "should only be pushed once");
  67 #endif
  68   region_stack()->push(index);
  69 }
  70 
  71 template <typename T>
  72 inline void ParCompactionManager::mark_and_push(T* p) {
  73   T heap_oop = oopDesc::load_heap_oop(p);
  74   if (!oopDesc::is_null(heap_oop)) {
  75     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
  76     assert(ParallelScavengeHeap::heap()->is_in(obj), "should be in heap");
  77 
  78     if (mark_bitmap()->is_unmarked(obj) && PSParallelCompact::mark_obj(obj)) {
  79       push(obj);
  80     }
  81   }
  82 }
  83 
  84 template <typename T>
  85 inline void ParCompactionManager::MarkAndPushClosure::do_oop_nv(T* p) {
  86   _compaction_manager->mark_and_push(p);
  87 }
  88 
  89 inline void ParCompactionManager::MarkAndPushClosure::do_oop(oop* p)       { do_oop_nv(p); }
  90 inline void ParCompactionManager::MarkAndPushClosure::do_oop(narrowOop* p) { do_oop_nv(p); }
  91 
  92 inline void ParCompactionManager::follow_klass(Klass* klass) {
  93   oop holder = klass->klass_holder();
  94   mark_and_push(&holder);
  95 }
  96 
  97 inline void ParCompactionManager::FollowStackClosure::do_void() {
  98   _compaction_manager->follow_marking_stacks();
  99 }
 100 
 101 inline void ParCompactionManager::FollowKlassClosure::do_klass(Klass* klass) {
 102   klass->oops_do(_mark_and_push_closure);
 103 }
 104 
 105 inline void ParCompactionManager::follow_class_loader(ClassLoaderData* cld) {
 106   MarkAndPushClosure mark_and_push_closure(this);
 107   FollowKlassClosure follow_klass_closure(&mark_and_push_closure);
 108 
 109   cld->oops_do(&mark_and_push_closure, &follow_klass_closure, true);
 110 }
 111 
 112 inline void ParCompactionManager::follow_contents(oop obj) {
 113   assert(PSParallelCompact::mark_bitmap()->is_marked(obj), "should be marked");
 114   obj->pc_follow_contents(this);
 115 }
 116 
 117 template <class T>
 118 inline void oop_pc_follow_contents_specialized(objArrayOop obj, int index, ParCompactionManager* cm) {
 119   const size_t len = size_t(obj->length());
 120   const size_t beg_index = size_t(index);
 121   assert(beg_index < len || len == 0, "index too large");
 122 
 123   const size_t stride = MIN2(len - beg_index, ObjArrayMarkingStride);
 124   const size_t end_index = beg_index + stride;
 125   T* const base = (T*)obj->base();
 126   T* const beg = base + beg_index;
 127   T* const end = base + end_index;
 128 
 129   if (end_index < len) {
 130     cm->push_objarray(obj, end_index); // Push the continuation.
 131   }
 132 
 133   // Push the non-NULL elements of the next stride on the marking stack.
 134   for (T* e = beg; e < end; e++) {
 135     cm->mark_and_push<T>(e);
 136   }
 137 }
 138 
 139 inline void ParCompactionManager::follow_contents(objArrayOop obj, int index) {
 140   if (UseCompressedOops) {
 141     oop_pc_follow_contents_specialized<narrowOop>(obj, index, this);
 142   } else {
 143     oop_pc_follow_contents_specialized<oop>(obj, index, this);
 144   }
 145 }
 146 
 147 inline void ParCompactionManager::update_contents(oop obj) {
 148   obj->pc_update_contents(this);
 149 }
 150 
 151 #endif // SHARE_VM_GC_PARALLEL_PSCOMPACTIONMANAGER_INLINE_HPP