1 /*
   2  * Copyright (c) 2001, 2017, 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_G1_G1OOPCLOSURES_HPP
  26 #define SHARE_VM_GC_G1_G1OOPCLOSURES_HPP
  27 
  28 #include "memory/iterator.hpp"
  29 #include "oops/markOop.hpp"
  30 
  31 class HeapRegion;
  32 class G1CollectedHeap;
  33 class G1RemSet;
  34 class G1ConcurrentMark;
  35 class DirtyCardToOopClosure;
  36 class G1CMBitMap;
  37 class G1ParScanThreadState;
  38 class G1CMTask;
  39 class ReferenceProcessor;
  40 
  41 // A class that scans oops in a given heap region (much as OopsInGenClosure
  42 // scans oops in a generation.)
  43 class OopsInHeapRegionClosure: public ExtendedOopClosure {
  44 protected:
  45   HeapRegion* _from;
  46 public:
  47   void set_region(HeapRegion* from) { _from = from; }
  48 };
  49 
  50 class G1ParClosureSuper : public OopsInHeapRegionClosure {
  51 protected:
  52   G1CollectedHeap* _g1;
  53   G1ParScanThreadState* _par_scan_state;
  54 
  55   G1ParClosureSuper(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state);
  56   ~G1ParClosureSuper() { }
  57 
  58 public:
  59   // This closure needs special handling for InstanceRefKlass.
  60   virtual ReferenceIterationMode reference_iteration_mode() { return DO_DISCOVERED_AND_DISCOVERY; }
  61 };
  62 
  63 class G1ParPushHeapRSClosure : public G1ParClosureSuper {
  64 public:
  65   G1ParPushHeapRSClosure(G1CollectedHeap* g1,
  66                          G1ParScanThreadState* par_scan_state):
  67     G1ParClosureSuper(g1, par_scan_state) { }
  68 
  69   template <class T> void do_oop_nv(T* p);
  70   virtual void do_oop(oop* p)          { do_oop_nv(p); }
  71   virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
  72 };
  73 
  74 class G1ParScanClosure : public G1ParClosureSuper {
  75 public:
  76   G1ParScanClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state) :
  77     G1ParClosureSuper(g1, par_scan_state) { }
  78 
  79   template <class T> void do_oop_nv(T* p);
  80   virtual void do_oop(oop* p)          { do_oop_nv(p); }
  81   virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
  82 
  83   void set_ref_processor(ReferenceProcessor* rp) {
  84     set_ref_processor_internal(rp);
  85   }
  86 };
  87 
  88 // Add back base class for metadata
  89 class G1ParCopyHelper : public OopClosure {
  90 protected:
  91   G1CollectedHeap* _g1;
  92   G1ParScanThreadState* _par_scan_state;
  93   uint _worker_id;              // Cache value from par_scan_state.
  94   Klass* _scanned_klass;
  95   G1ConcurrentMark* _cm;
  96 
  97   // Mark the object if it's not already marked. This is used to mark
  98   // objects pointed to by roots that are guaranteed not to move
  99   // during the GC (i.e., non-CSet objects). It is MT-safe.
 100   inline void mark_object(oop obj);
 101 
 102   // Mark the object if it's not already marked. This is used to mark
 103   // objects pointed to by roots that have been forwarded during a
 104   // GC. It is MT-safe.
 105   inline void mark_forwarded_object(oop from_obj, oop to_obj);
 106 
 107   G1ParCopyHelper(G1CollectedHeap* g1,  G1ParScanThreadState* par_scan_state);
 108   ~G1ParCopyHelper() { }
 109 
 110  public:
 111   void set_scanned_klass(Klass* k) { _scanned_klass = k; }
 112   template <class T> inline void do_klass_barrier(T* p, oop new_obj);
 113 };
 114 
 115 enum G1Barrier {
 116   G1BarrierNone,
 117   G1BarrierKlass
 118 };
 119 
 120 enum G1Mark {
 121   G1MarkNone,
 122   G1MarkFromRoot,
 123   G1MarkPromotedFromRoot
 124 };
 125 
 126 template <G1Barrier barrier, G1Mark do_mark_object, bool use_ext>
 127 class G1ParCopyClosure : public G1ParCopyHelper {
 128 public:
 129   G1ParCopyClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state) :
 130       G1ParCopyHelper(g1, par_scan_state) { }
 131 
 132   template <class T> void do_oop_work(T* p);
 133   virtual void do_oop(oop* p)       { do_oop_work(p); }
 134   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 135 };
 136 
 137 class G1KlassScanClosure : public KlassClosure {
 138  G1ParCopyHelper* _closure;
 139  bool             _process_only_dirty;
 140  int              _count;
 141  public:
 142   G1KlassScanClosure(G1ParCopyHelper* closure, bool process_only_dirty)
 143       : _process_only_dirty(process_only_dirty), _closure(closure), _count(0) {}
 144   void do_klass(Klass* klass);
 145 };
 146 
 147 // Closure for iterating over object fields during concurrent marking
 148 class G1CMOopClosure : public MetadataAwareOopClosure {
 149 protected:
 150   G1ConcurrentMark*  _cm;
 151 private:
 152   G1CollectedHeap*   _g1h;
 153   G1CMTask*          _task;
 154 public:
 155   G1CMOopClosure(G1CollectedHeap* g1h, G1ConcurrentMark* cm, G1CMTask* task);
 156   template <class T> void do_oop_nv(T* p);
 157   virtual void do_oop(      oop* p) { do_oop_nv(p); }
 158   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 159 };
 160 
 161 // Closure to scan the root regions during concurrent marking
 162 class G1RootRegionScanClosure : public MetadataAwareOopClosure {
 163 private:
 164   G1CollectedHeap* _g1h;
 165   G1ConcurrentMark* _cm;
 166 public:
 167   G1RootRegionScanClosure(G1CollectedHeap* g1h, G1ConcurrentMark* cm) :
 168     _g1h(g1h), _cm(cm) { }
 169   template <class T> void do_oop_nv(T* p);
 170   virtual void do_oop(      oop* p) { do_oop_nv(p); }
 171   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 172 };
 173 
 174 class G1ConcurrentRefineOopClosure: public ExtendedOopClosure {
 175   G1CollectedHeap* _g1;
 176   uint _worker_i;
 177 
 178 public:
 179   G1ConcurrentRefineOopClosure(G1CollectedHeap* g1h, uint worker_i) :
 180     _g1(g1h),
 181     _worker_i(worker_i) {
 182   }
 183 
 184   // This closure needs special handling for InstanceRefKlass.
 185   virtual ReferenceIterationMode reference_iteration_mode() { return DO_DISCOVERED_AND_DISCOVERY; }
 186 
 187   template <class T> void do_oop_nv(T* p);
 188   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 189   virtual void do_oop(oop* p) { do_oop_nv(p); }
 190 };
 191 
 192 class G1UpdateRSOrPushRefOopClosure: public ExtendedOopClosure {
 193   G1CollectedHeap* _g1;
 194   HeapRegion* _from;
 195   G1ParPushHeapRSClosure* _push_ref_cl;
 196   bool _record_refs_into_cset;
 197   uint _worker_i;
 198   bool _has_refs_into_cset;
 199 
 200 public:
 201   G1UpdateRSOrPushRefOopClosure(G1CollectedHeap* g1h,
 202                                 G1ParPushHeapRSClosure* push_ref_cl,
 203                                 bool record_refs_into_cset,
 204                                 uint worker_i = 0);
 205 
 206   void set_from(HeapRegion* from) {
 207     assert(from != NULL, "from region must be non-NULL");
 208     _from = from;
 209   }
 210 
 211   bool self_forwarded(oop obj) {
 212     markOop m = obj->mark();
 213     bool result = (m->is_marked() && ((oop)m->decode_pointer() == obj));
 214     return result;
 215   }
 216 
 217   bool has_refs_into_cset() const { return _has_refs_into_cset; }
 218 
 219   template <class T> inline void do_oop_nv(T* p);
 220   virtual inline void do_oop(narrowOop* p);
 221   virtual inline void do_oop(oop* p);
 222 
 223   // This closure needs special handling for InstanceRefKlass.
 224   virtual ReferenceIterationMode reference_iteration_mode() { return DO_DISCOVERED_AND_DISCOVERY; }
 225 };
 226 
 227 #endif // SHARE_VM_GC_G1_G1OOPCLOSURES_HPP