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