1 /*
   2  * Copyright (c) 2001, 2015, 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 ConcurrentMark;
  35 class DirtyCardToOopClosure;
  36 class CMBitMap;
  37 class CMMarkStack;
  38 class G1ParScanThreadState;
  39 class CMTask;
  40 class ReferenceProcessor;
  41 
  42 // A class that scans oops in a given heap region (much as OopsInGenClosure
  43 // scans oops in a generation.)
  44 class OopsInHeapRegionClosure: public ExtendedOopClosure {
  45 protected:
  46   HeapRegion* _from;
  47 public:
  48   void set_region(HeapRegion* from) { _from = from; }
  49 };
  50 
  51 class G1ParClosureSuper : public OopsInHeapRegionClosure {
  52 protected:
  53   G1CollectedHeap* _g1;
  54   G1ParScanThreadState* _par_scan_state;
  55   uint _worker_id;
  56 public:
  57   // Initializes the instance, leaving _par_scan_state uninitialized. Must be done
  58   // later using the set_par_scan_thread_state() method.
  59   G1ParClosureSuper(G1CollectedHeap* g1);
  60   G1ParClosureSuper(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state);
  61   bool apply_to_weak_ref_discovered_field() { return true; }
  62 
  63   void set_par_scan_thread_state(G1ParScanThreadState* par_scan_state);
  64   void set_ref_processor(ReferenceProcessor* ref_processor) { _ref_processor = ref_processor; }
  65 };
  66 
  67 class G1ParPushHeapRSClosure : public G1ParClosureSuper {
  68 public:
  69   G1ParPushHeapRSClosure(G1CollectedHeap* g1,
  70                          G1ParScanThreadState* par_scan_state):
  71     G1ParClosureSuper(g1, par_scan_state) { }
  72 
  73   template <class T> void do_oop_nv(T* p);
  74   virtual void do_oop(oop* p)          { do_oop_nv(p); }
  75   virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
  76 };
  77 
  78 class G1ParScanClosure : public G1ParClosureSuper {
  79 public:
  80   G1ParScanClosure(G1CollectedHeap* g1) : G1ParClosureSuper(g1) { }
  81 
  82   template <class T> void do_oop_nv(T* p);
  83   virtual void do_oop(oop* p)          { do_oop_nv(p); }
  84   virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
  85 };
  86 
  87 // Add back base class for metadata
  88 class G1ParCopyHelper : public G1ParClosureSuper {
  89 protected:
  90   Klass* _scanned_klass;
  91   ConcurrentMark* _cm;
  92 
  93   // Mark the object if it's not already marked. This is used to mark
  94   // objects pointed to by roots that are guaranteed not to move
  95   // during the GC (i.e., non-CSet objects). It is MT-safe.
  96   void mark_object(oop obj);
  97 
  98   // Mark the object if it's not already marked. This is used to mark
  99   // objects pointed to by roots that have been forwarded during a
 100   // GC. It is MT-safe.
 101   void mark_forwarded_object(oop from_obj, oop to_obj);
 102  public:
 103   G1ParCopyHelper(G1CollectedHeap* g1,  G1ParScanThreadState* par_scan_state);
 104   G1ParCopyHelper(G1CollectedHeap* g1);
 105 
 106   void set_scanned_klass(Klass* k) { _scanned_klass = k; }
 107   template <class T> void do_klass_barrier(T* p, oop new_obj);
 108 };
 109 
 110 enum G1Barrier {
 111   G1BarrierNone,
 112   G1BarrierKlass
 113 };
 114 
 115 enum G1Mark {
 116   G1MarkNone,
 117   G1MarkFromRoot,
 118   G1MarkPromotedFromRoot
 119 };
 120 
 121 template <G1Barrier barrier, G1Mark do_mark_object>
 122 class G1ParCopyClosure : public G1ParCopyHelper {
 123 private:
 124   template <class T> void do_oop_work(T* p);
 125 
 126 public:
 127   G1ParCopyClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state,
 128                    ReferenceProcessor* rp) :
 129       G1ParCopyHelper(g1, par_scan_state) {
 130     assert(_ref_processor == NULL, "sanity");
 131   }
 132 
 133   G1ParCopyClosure(G1CollectedHeap* g1) : G1ParCopyHelper(g1) {
 134     assert(_ref_processor == NULL, "sanity");
 135   }
 136 
 137   template <class T> void do_oop_nv(T* p) { do_oop_work(p); }
 138   virtual void do_oop(oop* p)       { do_oop_nv(p); }
 139   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 140 
 141   G1CollectedHeap*      g1()  { return _g1; };
 142   G1ParScanThreadState* pss() { return _par_scan_state; }
 143   ReferenceProcessor*   rp()  { return _ref_processor; };
 144 };
 145 
 146 typedef G1ParCopyClosure<G1BarrierNone,  G1MarkNone>             G1ParScanExtRootClosure;
 147 typedef G1ParCopyClosure<G1BarrierNone,  G1MarkFromRoot>         G1ParScanAndMarkExtRootClosure;
 148 typedef G1ParCopyClosure<G1BarrierNone,  G1MarkPromotedFromRoot> G1ParScanAndMarkWeakExtRootClosure;
 149 // We use a separate closure to handle references during evacuation
 150 // failure processing.
 151 
 152 class FilterIntoCSClosure: public ExtendedOopClosure {
 153   G1CollectedHeap* _g1;
 154   OopClosure* _oc;
 155   DirtyCardToOopClosure* _dcto_cl;
 156 public:
 157   FilterIntoCSClosure(  DirtyCardToOopClosure* dcto_cl,
 158                         G1CollectedHeap* g1,
 159                         OopClosure* oc) :
 160     _dcto_cl(dcto_cl), _g1(g1), _oc(oc) { }
 161 
 162   template <class T> void do_oop_nv(T* p);
 163   virtual void do_oop(oop* p)        { do_oop_nv(p); }
 164   virtual void do_oop(narrowOop* p)  { do_oop_nv(p); }
 165   bool apply_to_weak_ref_discovered_field() { return true; }
 166 };
 167 
 168 class FilterOutOfRegionClosure: public ExtendedOopClosure {
 169   HeapWord* _r_bottom;
 170   HeapWord* _r_end;
 171   OopClosure* _oc;
 172 public:
 173   FilterOutOfRegionClosure(HeapRegion* r, OopClosure* oc);
 174   template <class T> void do_oop_nv(T* p);
 175   virtual void do_oop(oop* p) { do_oop_nv(p); }
 176   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 177   bool apply_to_weak_ref_discovered_field() { return true; }
 178 };
 179 
 180 // Closure for iterating over object fields during concurrent marking
 181 class G1CMOopClosure : public MetadataAwareOopClosure {
 182 protected:
 183   ConcurrentMark*    _cm;
 184 private:
 185   G1CollectedHeap*   _g1h;
 186   CMTask*            _task;
 187 public:
 188   G1CMOopClosure(G1CollectedHeap* g1h, ConcurrentMark* cm, CMTask* task);
 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 // Closure to scan the root regions during concurrent marking
 195 class G1RootRegionScanClosure : public MetadataAwareOopClosure {
 196 private:
 197   G1CollectedHeap* _g1h;
 198   ConcurrentMark*  _cm;
 199   uint _worker_id;
 200 public:
 201   G1RootRegionScanClosure(G1CollectedHeap* g1h, ConcurrentMark* cm,
 202                           uint worker_id) :
 203     _g1h(g1h), _cm(cm), _worker_id(worker_id) { }
 204   template <class T> void do_oop_nv(T* p);
 205   virtual void do_oop(      oop* p) { do_oop_nv(p); }
 206   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 207 };
 208 
 209 // Closure that applies the given two closures in sequence.
 210 // Used by the RSet refinement code (when updating RSets
 211 // during an evacuation pause) to record cards containing
 212 // pointers into the collection set.
 213 
 214 class G1Mux2Closure : public ExtendedOopClosure {
 215   OopClosure* _c1;
 216   OopClosure* _c2;
 217 public:
 218   G1Mux2Closure(OopClosure *c1, OopClosure *c2);
 219   template <class T> void do_oop_nv(T* p);
 220   virtual void do_oop(oop* p)        { do_oop_nv(p); }
 221   virtual void do_oop(narrowOop* p)  { do_oop_nv(p); }
 222 };
 223 
 224 // A closure that returns true if it is actually applied
 225 // to a reference
 226 
 227 class G1TriggerClosure : public ExtendedOopClosure {
 228   bool _triggered;
 229 public:
 230   G1TriggerClosure();
 231   bool triggered() const { return _triggered; }
 232   template <class T> void do_oop_nv(T* p);
 233   virtual void do_oop(oop* p)        { do_oop_nv(p); }
 234   virtual void do_oop(narrowOop* p)  { do_oop_nv(p); }
 235 };
 236 
 237 // A closure which uses a triggering closure to determine
 238 // whether to apply an oop closure.
 239 
 240 class G1InvokeIfNotTriggeredClosure: public ExtendedOopClosure {
 241   G1TriggerClosure* _trigger_cl;
 242   OopClosure* _oop_cl;
 243 public:
 244   G1InvokeIfNotTriggeredClosure(G1TriggerClosure* t, OopClosure* oc);
 245   template <class T> void do_oop_nv(T* p);
 246   virtual void do_oop(oop* p)        { do_oop_nv(p); }
 247   virtual void do_oop(narrowOop* p)  { do_oop_nv(p); }
 248 };
 249 
 250 class G1UpdateRSOrPushRefOopClosure: public ExtendedOopClosure {
 251   G1CollectedHeap* _g1;
 252   G1RemSet* _g1_rem_set;
 253   HeapRegion* _from;
 254   G1ParPushHeapRSClosure* _push_ref_cl;
 255   bool _record_refs_into_cset;
 256   uint _worker_i;
 257 
 258 public:
 259   G1UpdateRSOrPushRefOopClosure(G1CollectedHeap* g1h,
 260                                 G1RemSet* rs,
 261                                 G1ParPushHeapRSClosure* push_ref_cl,
 262                                 bool record_refs_into_cset,
 263                                 uint worker_i = 0);
 264 
 265   void set_from(HeapRegion* from) {
 266     assert(from != NULL, "from region must be non-NULL");
 267     _from = from;
 268   }
 269 
 270   bool self_forwarded(oop obj) {
 271     markOop m = obj->mark();
 272     bool result = (m->is_marked() && ((oop)m->decode_pointer() == obj));
 273     return result;
 274   }
 275 
 276   bool apply_to_weak_ref_discovered_field() { return true; }
 277 
 278   template <class T> void do_oop_nv(T* p);
 279   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 280   virtual void do_oop(oop* p)       { do_oop_nv(p); }
 281 };
 282 
 283 #endif // SHARE_VM_GC_G1_G1OOPCLOSURES_HPP