1 /*
   2  * Copyright (c) 2001, 2010, 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 HRInto_G1RemSet;
  32 class G1RemSet;
  33 class ConcurrentMark;
  34 class DirtyCardToOopClosure;
  35 class CMBitMap;
  36 class CMMarkStack;
  37 class G1ParScanThreadState;
  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 OopsInGenClosure {
  42 protected:
  43   HeapRegion* _from;
  44 public:
  45   virtual void set_region(HeapRegion* from) { _from = from; }
  46 };
  47 
  48 class G1ParClosureSuper : public OopsInHeapRegionClosure {
  49 protected:
  50   G1CollectedHeap* _g1;
  51   G1RemSet* _g1_rem;
  52   ConcurrentMark* _cm;
  53   G1ParScanThreadState* _par_scan_state;
  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, G1ParScanThreadState* par_scan_state) :
  62     G1ParClosureSuper(g1, par_scan_state) { }
  63   template <class T> void do_oop_nv(T* p);
  64   virtual void do_oop(oop* p)          { do_oop_nv(p); }
  65   virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
  66 };
  67 
  68 class G1ParScanClosure : public G1ParClosureSuper {
  69 public:
  70   G1ParScanClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state) :
  71     G1ParClosureSuper(g1, par_scan_state) { }
  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 #define G1_PARTIAL_ARRAY_MASK 0x2
  78 
  79 template <class T> inline bool has_partial_array_mask(T* ref) {
  80   return ((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) == G1_PARTIAL_ARRAY_MASK;
  81 }
  82 
  83 template <class T> inline T* set_partial_array_mask(T obj) {
  84   assert(((uintptr_t)obj & G1_PARTIAL_ARRAY_MASK) == 0, "Information loss!");
  85   return (T*) ((uintptr_t)obj | G1_PARTIAL_ARRAY_MASK);
  86 }
  87 
  88 template <class T> inline oop clear_partial_array_mask(T* ref) {
  89   return oop((intptr_t)ref & ~G1_PARTIAL_ARRAY_MASK);
  90 }
  91 
  92 class G1ParScanPartialArrayClosure : public G1ParClosureSuper {
  93   G1ParScanClosure _scanner;
  94 public:
  95   G1ParScanPartialArrayClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state) :
  96     G1ParClosureSuper(g1, par_scan_state), _scanner(g1, par_scan_state) { }
  97   template <class T> void do_oop_nv(T* p);
  98   virtual void do_oop(oop* p)       { do_oop_nv(p); }
  99   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 100 };
 101 
 102 
 103 class G1ParCopyHelper : public G1ParClosureSuper {
 104   G1ParScanClosure *_scanner;
 105 protected:
 106   template <class T> void mark_forwardee(T* p);
 107   oop copy_to_survivor_space(oop obj);
 108 public:
 109   G1ParCopyHelper(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state,
 110                   G1ParScanClosure *scanner) :
 111     G1ParClosureSuper(g1, par_scan_state), _scanner(scanner) { }
 112 };
 113 
 114 template<bool do_gen_barrier, G1Barrier barrier,
 115          bool do_mark_forwardee>
 116 class G1ParCopyClosure : public G1ParCopyHelper {
 117   G1ParScanClosure _scanner;
 118   template <class T> void do_oop_work(T* p);
 119 public:
 120   G1ParCopyClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state) :
 121     _scanner(g1, par_scan_state), G1ParCopyHelper(g1, par_scan_state, &_scanner) { }
 122   template <class T> void do_oop_nv(T* p) {
 123     do_oop_work(p);
 124     if (do_mark_forwardee)
 125       mark_forwardee(p);
 126   }
 127   virtual void do_oop(oop* p)       { do_oop_nv(p); }
 128   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 129 };
 130 
 131 typedef G1ParCopyClosure<false, G1BarrierNone, false> G1ParScanExtRootClosure;
 132 typedef G1ParCopyClosure<true,  G1BarrierNone, false> G1ParScanPermClosure;
 133 typedef G1ParCopyClosure<false, G1BarrierRS,   false> G1ParScanHeapRSClosure;
 134 typedef G1ParCopyClosure<false, G1BarrierNone, true> G1ParScanAndMarkExtRootClosure;
 135 typedef G1ParCopyClosure<true,  G1BarrierNone, true> G1ParScanAndMarkPermClosure;
 136 typedef G1ParCopyClosure<false, G1BarrierRS,   true> G1ParScanAndMarkHeapRSClosure;
 137 
 138 // This is the only case when we set skip_cset_test. Basically, this
 139 // closure is (should?) only be called directly while we're draining
 140 // the overflow and task queues. In that case we know that the
 141 // reference in question points into the collection set, otherwise we
 142 // would not have pushed it on the queue. The following is defined in
 143 // g1_specialized_oop_closures.hpp.
 144 // typedef G1ParCopyClosure<false, G1BarrierEvac, false, true> G1ParScanHeapEvacClosure;
 145 // We need a separate closure to handle references during evacuation
 146 // failure processing, as we cannot asume that the reference already
 147 // points into the collection set (like G1ParScanHeapEvacClosure does).
 148 typedef G1ParCopyClosure<false, G1BarrierEvac, false> G1ParScanHeapEvacFailureClosure;
 149 
 150 class FilterIntoCSClosure: public OopClosure {
 151   G1CollectedHeap* _g1;
 152   OopClosure* _oc;
 153   DirtyCardToOopClosure* _dcto_cl;
 154 public:
 155   FilterIntoCSClosure(  DirtyCardToOopClosure* dcto_cl,
 156                         G1CollectedHeap* g1, OopClosure* oc) :
 157     _dcto_cl(dcto_cl), _g1(g1), _oc(oc)
 158   {}
 159   template <class T> void do_oop_nv(T* p);
 160   virtual void do_oop(oop* p)        { do_oop_nv(p); }
 161   virtual void do_oop(narrowOop* p)  { do_oop_nv(p); }
 162   bool apply_to_weak_ref_discovered_field() { return true; }
 163   bool do_header() { return false; }
 164 };
 165 
 166 class FilterInHeapRegionAndIntoCSClosure : public OopsInHeapRegionClosure {
 167   G1CollectedHeap* _g1;
 168   OopsInHeapRegionClosure* _oc;
 169 public:
 170   FilterInHeapRegionAndIntoCSClosure(G1CollectedHeap* g1,
 171                                      OopsInHeapRegionClosure* oc) :
 172     _g1(g1), _oc(oc)
 173   {}
 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   bool do_header() { return false; }
 179   void set_region(HeapRegion* from) {
 180     _oc->set_region(from);
 181   }
 182 };
 183 
 184 class FilterAndMarkInHeapRegionAndIntoCSClosure : public OopsInHeapRegionClosure {
 185   G1CollectedHeap* _g1;
 186   ConcurrentMark* _cm;
 187   OopsInHeapRegionClosure* _oc;
 188 public:
 189   FilterAndMarkInHeapRegionAndIntoCSClosure(G1CollectedHeap* g1,
 190                                             OopsInHeapRegionClosure* oc,
 191                                             ConcurrentMark* cm)
 192   : _g1(g1), _oc(oc), _cm(cm) { }
 193 
 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   bool apply_to_weak_ref_discovered_field() { return true; }
 198   bool do_header() { return false; }
 199   void set_region(HeapRegion* from) {
 200     _oc->set_region(from);
 201   }
 202 };
 203 
 204 class FilterOutOfRegionClosure: public OopClosure {
 205   HeapWord* _r_bottom;
 206   HeapWord* _r_end;
 207   OopClosure* _oc;
 208   int _out_of_region;
 209 public:
 210   FilterOutOfRegionClosure(HeapRegion* r, OopClosure* oc);
 211   template <class T> void do_oop_nv(T* p);
 212   virtual void do_oop(oop* p) { do_oop_nv(p); }
 213   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 214   bool apply_to_weak_ref_discovered_field() { return true; }
 215   bool do_header() { return false; }
 216   int out_of_region() { return _out_of_region; }
 217 };
 218 
 219 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1OOPCLOSURES_HPP