1 /*
   2  * Copyright (c) 2001, 2011, 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 OopsInGenClosure {
  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   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,
  62                          G1ParScanThreadState* par_scan_state,
  63                          ReferenceProcessor* rp) :
  64     G1ParClosureSuper(g1, par_scan_state)
  65   {
  66     assert(_ref_processor == NULL, "sanity");
  67     _ref_processor = rp;
  68   }
  69 
  70   template <class T> void do_oop_nv(T* p);
  71   virtual void do_oop(oop* p)          { do_oop_nv(p); }
  72   virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
  73 };
  74 
  75 class G1ParScanClosure : public G1ParClosureSuper {
  76 public:
  77   G1ParScanClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state, ReferenceProcessor* rp) :
  78     G1ParClosureSuper(g1, par_scan_state)
  79   {
  80     assert(_ref_processor == NULL, "sanity");
  81     _ref_processor = rp;
  82   }
  83 
  84   template <class T> void do_oop_nv(T* p);
  85   virtual void do_oop(oop* p)          { do_oop_nv(p); }
  86   virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
  87 };
  88 
  89 #define G1_PARTIAL_ARRAY_MASK 0x2
  90 
  91 template <class T> inline bool has_partial_array_mask(T* ref) {
  92   return ((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) == G1_PARTIAL_ARRAY_MASK;
  93 }
  94 
  95 template <class T> inline T* set_partial_array_mask(T obj) {
  96   assert(((uintptr_t)obj & G1_PARTIAL_ARRAY_MASK) == 0, "Information loss!");
  97   return (T*) ((uintptr_t)obj | G1_PARTIAL_ARRAY_MASK);
  98 }
  99 
 100 template <class T> inline oop clear_partial_array_mask(T* ref) {
 101   return oop((intptr_t)ref & ~G1_PARTIAL_ARRAY_MASK);
 102 }
 103 
 104 class G1ParScanPartialArrayClosure : public G1ParClosureSuper {
 105   G1ParScanClosure _scanner;
 106 
 107 public:
 108   G1ParScanPartialArrayClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state, ReferenceProcessor* rp) :
 109     G1ParClosureSuper(g1, par_scan_state), _scanner(g1, par_scan_state, rp)
 110   {
 111     assert(_ref_processor == NULL, "sanity");
 112   }
 113 
 114   G1ParScanClosure* scanner() {
 115     return &_scanner;
 116   }
 117 
 118   template <class T> void do_oop_nv(T* p);
 119   virtual void do_oop(oop* p)       { do_oop_nv(p); }
 120   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 121 };
 122 
 123 
 124 class G1ParCopyHelper : public G1ParClosureSuper {
 125   G1ParScanClosure *_scanner;
 126 protected:
 127   template <class T> void mark_forwardee(T* p);
 128   oop copy_to_survivor_space(oop obj);
 129 
 130 public:
 131   G1ParCopyHelper(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state,
 132                   G1ParScanClosure *scanner) :
 133     G1ParClosureSuper(g1, par_scan_state), _scanner(scanner) { }
 134 };
 135 
 136 template<bool do_gen_barrier, G1Barrier barrier, bool do_mark_forwardee>
 137 class G1ParCopyClosure : public G1ParCopyHelper {
 138   G1ParScanClosure _scanner;
 139 
 140   template <class T> void do_oop_work(T* p);
 141 
 142 public:
 143   G1ParCopyClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state,
 144                    ReferenceProcessor* rp) :
 145     _scanner(g1, par_scan_state, rp),
 146     G1ParCopyHelper(g1, par_scan_state, &_scanner)
 147   {
 148     assert(_ref_processor == NULL, "sanity");
 149   }
 150 
 151   G1ParScanClosure* scanner() { return &_scanner; }
 152 
 153   template <class T> void do_oop_nv(T* p) {
 154     do_oop_work(p);
 155     if (do_mark_forwardee)
 156       mark_forwardee(p);
 157   }
 158   virtual void do_oop(oop* p)       { do_oop_nv(p); }
 159   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 160 };
 161 
 162 typedef G1ParCopyClosure<false, G1BarrierNone, false> G1ParScanExtRootClosure;
 163 typedef G1ParCopyClosure<true,  G1BarrierNone, false> G1ParScanPermClosure;
 164 
 165 typedef G1ParCopyClosure<false, G1BarrierNone, true> G1ParScanAndMarkExtRootClosure;
 166 typedef G1ParCopyClosure<true,  G1BarrierNone, true> G1ParScanAndMarkPermClosure;
 167 
 168 // The following closure types are no longer used but are retained
 169 // for historical reasons:
 170 // typedef G1ParCopyClosure<false, G1BarrierRS,   false> G1ParScanHeapRSClosure;
 171 // typedef G1ParCopyClosure<false, G1BarrierRS,   true> G1ParScanAndMarkHeapRSClosure;
 172 
 173 // The following closure type is defined in g1_specialized_oop_closures.hpp:
 174 //
 175 // typedef G1ParCopyClosure<false, G1BarrierEvac, false> G1ParScanHeapEvacClosure;
 176 
 177 // We use a separate closure to handle references during evacuation
 178 // failure processing.
 179 // We could have used another instance of G1ParScanHeapEvacClosure
 180 // (since that closure no longer assumes that the references it
 181 // handles point into the collection set).
 182 
 183 typedef G1ParCopyClosure<false, G1BarrierEvac, false> G1ParScanHeapEvacFailureClosure;
 184 
 185 class FilterIntoCSClosure: public OopClosure {
 186   G1CollectedHeap* _g1;
 187   OopClosure* _oc;
 188   DirtyCardToOopClosure* _dcto_cl;
 189 public:
 190   FilterIntoCSClosure(  DirtyCardToOopClosure* dcto_cl,
 191                         G1CollectedHeap* g1,
 192                         OopClosure* oc,
 193                         ReferenceProcessor* rp) :
 194     _dcto_cl(dcto_cl), _g1(g1), _oc(oc)
 195   {
 196     assert(_ref_processor == NULL, "sanity");
 197     _ref_processor = rp;
 198   }
 199 
 200   template <class T> void do_oop_nv(T* p);
 201   virtual void do_oop(oop* p)        { do_oop_nv(p); }
 202   virtual void do_oop(narrowOop* p)  { do_oop_nv(p); }
 203   bool apply_to_weak_ref_discovered_field() { return true; }
 204   bool do_header() { return false; }
 205 };
 206 
 207 class FilterOutOfRegionClosure: public OopClosure {
 208   HeapWord* _r_bottom;
 209   HeapWord* _r_end;
 210   OopClosure* _oc;
 211   int _out_of_region;
 212 public:
 213   FilterOutOfRegionClosure(HeapRegion* r, OopClosure* oc);
 214   template <class T> void do_oop_nv(T* p);
 215   virtual void do_oop(oop* p) { do_oop_nv(p); }
 216   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 217   bool apply_to_weak_ref_discovered_field() { return true; }
 218   bool do_header() { return false; }
 219   int out_of_region() { return _out_of_region; }
 220 };
 221 
 222 // Closure for iterating over object fields during concurrent marking
 223 class G1CMOopClosure : public OopClosure {
 224   G1CollectedHeap*   _g1h;
 225   ConcurrentMark*    _cm;
 226   CMTask*            _task;
 227 public:
 228   G1CMOopClosure(G1CollectedHeap* g1h, ConcurrentMark* cm, CMTask* task);
 229   template <class T> void do_oop_nv(T* p);
 230   virtual void do_oop(      oop* p) { do_oop_nv(p); }
 231   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
 232 };
 233 
 234 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1OOPCLOSURES_HPP