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