1 /*
   2  * Copyright (c) 2001, 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_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   virtual bool apply_to_weak_ref_discovered_field() { return true; }
  60 };
  61 
  62 class G1ParPushHeapRSClosure : public G1ParClosureSuper {
  63 public:
  64   G1ParPushHeapRSClosure(G1CollectedHeap* g1,
  65                          G1ParScanThreadState* par_scan_state):
  66     G1ParClosureSuper(g1, par_scan_state) { }
  67 
  68   template <class T> void do_oop_nv(T* p);
  69   virtual void do_oop(oop* p)          { do_oop_nv(p); }
  70   virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
  71 };
  72 
  73 class G1ParScanClosure : public G1ParClosureSuper {
  74 public:
  75   G1ParScanClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state) :
  76     G1ParClosureSuper(g1, par_scan_state) { }
  77 
  78   template <class T> void do_oop_nv(T* p);
  79   virtual void do_oop(oop* p)          { do_oop_nv(p); }
  80   virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
  81 
  82   void set_ref_processor(ReferenceProcessor* rp) {
  83     set_ref_processor_internal(rp);
  84   }
  85 };
  86 
  87 // Add back base class for metadata
  88 class G1ParCopyHelper : public OopClosure {
  89 protected:
  90   G1CollectedHeap* _g1;
  91   G1ParScanThreadState* _par_scan_state;
  92   uint _worker_id;              // Cache value from par_scan_state.
  93   Klass* _scanned_klass;
  94   G1ConcurrentMark* _cm;
  95 
  96   // Mark the object if it's not already marked. This is used to mark
  97   // objects pointed to by roots that are guaranteed not to move
  98   // during the GC (i.e., non-CSet objects). It is MT-safe.
  99   inline void mark_object(oop obj);
 100 
 101   // Mark the object if it's not already marked. This is used to mark
 102   // objects pointed to by roots that have been forwarded during a
 103   // GC. It is MT-safe.
 104   inline void mark_forwarded_object(oop from_obj, oop to_obj);
 105 
 106   G1ParCopyHelper(G1CollectedHeap* g1,  G1ParScanThreadState* par_scan_state);
 107   ~G1ParCopyHelper() { }
 108 
 109  public:
 110   void set_scanned_klass(Klass* k) { _scanned_klass = k; }
 111   template <class T> inline void do_klass_barrier(T* p, oop new_obj);
 112 };
 113 
 114 enum G1Barrier {
 115   G1BarrierNone,
 116   G1BarrierKlass
 117 };
 118 
 119 enum G1Mark {
 120   G1MarkNone,
 121   G1MarkFromRoot,
 122   G1MarkPromotedFromRoot
 123 };
 124 
 125 template <G1Barrier barrier, G1Mark do_mark_object, bool use_ext>
 126 class G1ParCopyClosure : public G1ParCopyHelper {
 127 public:
 128   G1ParCopyClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state) :
 129       G1ParCopyHelper(g1, par_scan_state) { }
 130 
 131   template <class T> void do_oop_work(T* p);
 132   virtual void do_oop(oop* p)       { do_oop_work(p); }
 133   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 134 };
 135 
 136 class G1KlassScanClosure : public KlassClosure {
 137  G1ParCopyHelper* _closure;
 138  bool             _process_only_dirty;
 139  int              _count;
 140  public:
 141   G1KlassScanClosure(G1ParCopyHelper* closure, bool process_only_dirty)
 142       : _process_only_dirty(process_only_dirty), _closure(closure), _count(0) {}
 143   void do_klass(Klass* klass);
 144 };
 145 
 146 class FilterIntoCSClosure: public OopClosure {
 147   G1CollectedHeap* _g1;
 148   OopClosure* _oc;
 149 public:
 150   FilterIntoCSClosure(G1CollectedHeap* g1, OopClosure* oc) : _g1(g1), _oc(oc) { }
 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 FilterOutOfRegionClosure: public ExtendedOopClosure {
 158   HeapWord* _r_bottom;
 159   HeapWord* _r_end;
 160   OopClosure* _oc;
 161 public:
 162   FilterOutOfRegionClosure(HeapRegion* r, OopClosure* oc);
 163   template <class T> void do_oop_nv(T* p);
 164   virtual void do_oop(oop* p) { do_oop_nv(p); }
 165   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 166   bool apply_to_weak_ref_discovered_field() { return true; }
 167 };
 168 
 169 // Closure for iterating over object fields during concurrent marking
 170 class G1CMOopClosure : public MetadataAwareOopClosure {
 171 protected:
 172   G1ConcurrentMark*  _cm;
 173 private:
 174   G1CollectedHeap*   _g1h;
 175   G1CMTask*          _task;
 176 public:
 177   G1CMOopClosure(G1CollectedHeap* g1h, G1ConcurrentMark* cm, G1CMTask* task);
 178   template <class T> void do_oop_nv(T* p);
 179   virtual void do_oop(      oop* p) { do_oop_nv(p); }
 180   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 181 };
 182 
 183 // Closure to scan the root regions during concurrent marking
 184 class G1RootRegionScanClosure : public MetadataAwareOopClosure {
 185 private:
 186   G1CollectedHeap* _g1h;
 187   G1ConcurrentMark* _cm;
 188 public:
 189   G1RootRegionScanClosure(G1CollectedHeap* g1h, G1ConcurrentMark* cm) :
 190     _g1h(g1h), _cm(cm) { }
 191   template <class T> void do_oop_nv(T* p);
 192   virtual void do_oop(      oop* p) { do_oop_nv(p); }
 193   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 194 };
 195 
 196 // Closure that applies the given two closures in sequence.
 197 // Used by the RSet refinement code (when updating RSets
 198 // during an evacuation pause) to record cards containing
 199 // pointers into the collection set.
 200 
 201 class G1Mux2Closure : public OopClosure {
 202   OopClosure* _c1;
 203   OopClosure* _c2;
 204 public:
 205   G1Mux2Closure(OopClosure *c1, OopClosure *c2);
 206   template <class T> inline void do_oop_work(T* p);
 207   virtual inline void do_oop(oop* p);
 208   virtual inline void do_oop(narrowOop* p);
 209 };
 210 
 211 // A closure that returns true if it is actually applied
 212 // to a reference
 213 
 214 class G1TriggerClosure : public OopClosure {
 215   bool _triggered;
 216 public:
 217   G1TriggerClosure();
 218   bool triggered() const { return _triggered; }
 219   template <class T> inline void do_oop_work(T* p);
 220   virtual inline void do_oop(oop* p);
 221   virtual inline void do_oop(narrowOop* p);
 222 };
 223 
 224 // A closure which uses a triggering closure to determine
 225 // whether to apply an oop closure.
 226 
 227 class G1InvokeIfNotTriggeredClosure: public OopClosure {
 228   G1TriggerClosure* _trigger_cl;
 229   OopClosure* _oop_cl;
 230 public:
 231   G1InvokeIfNotTriggeredClosure(G1TriggerClosure* t, OopClosure* oc);
 232   template <class T> inline void do_oop_work(T* p);
 233   virtual inline void do_oop(oop* p);
 234   virtual inline void do_oop(narrowOop* p);
 235 };
 236 
 237 class G1UpdateRSOrPushRefOopClosure: public OopClosure {
 238   G1CollectedHeap* _g1;
 239   G1RemSet* _g1_rem_set;
 240   HeapRegion* _from;
 241   G1ParPushHeapRSClosure* _push_ref_cl;
 242   bool _record_refs_into_cset;
 243   uint _worker_i;
 244 
 245 public:
 246   G1UpdateRSOrPushRefOopClosure(G1CollectedHeap* g1h,
 247                                 G1RemSet* rs,
 248                                 G1ParPushHeapRSClosure* push_ref_cl,
 249                                 bool record_refs_into_cset,
 250                                 uint worker_i = 0);
 251 
 252   void set_from(HeapRegion* from) {
 253     assert(from != NULL, "from region must be non-NULL");
 254     _from = from;
 255   }
 256 
 257   bool self_forwarded(oop obj) {
 258     markOop m = obj->mark();
 259     bool result = (m->is_marked() && ((oop)m->decode_pointer() == obj));
 260     return result;
 261   }
 262 
 263   template <class T> inline void do_oop_work(T* p);
 264   virtual inline void do_oop(narrowOop* p);
 265   virtual inline void do_oop(oop* p);
 266 };
 267 
 268 #endif // SHARE_VM_GC_G1_G1OOPCLOSURES_HPP