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