1 /*
   2  * Copyright (c) 2001, 2018, 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 "gc/g1/g1InCSetState.hpp"
  29 #include "memory/iterator.hpp"
  30 #include "oops/markOop.hpp"
  31 
  32 class HeapRegion;
  33 class G1CollectedHeap;
  34 class G1RemSet;
  35 class G1ConcurrentMark;
  36 class DirtyCardToOopClosure;
  37 class G1CMBitMap;
  38 class G1ParScanThreadState;
  39 class G1CMTask;
  40 class ReferenceProcessor;
  41 
  42 class G1ScanClosureBase : public BasicOopIterateClosure {
  43 protected:
  44   G1CollectedHeap* _g1h;
  45   G1ParScanThreadState* _par_scan_state;
  46 
  47   G1ScanClosureBase(G1CollectedHeap* g1h, G1ParScanThreadState* par_scan_state);
  48   ~G1ScanClosureBase() { }
  49 
  50   template <class T>
  51   inline void prefetch_and_push(T* p, oop const obj);
  52 
  53   template <class T>
  54   inline void handle_non_cset_obj_common(InCSetState const state, T* p, oop const obj);
  55 public:
  56   virtual ReferenceIterationMode reference_iteration_mode() { return DO_FIELDS; }
  57 
  58   inline void trim_queue_partially();
  59 };
  60 
  61 // Used during the Update RS phase to refine remaining cards in the DCQ during garbage collection.
  62 class G1ScanObjsDuringUpdateRSClosure : public G1ScanClosureBase {
  63 public:
  64   G1ScanObjsDuringUpdateRSClosure(G1CollectedHeap* g1h,
  65                                   G1ParScanThreadState* pss) :
  66     G1ScanClosureBase(g1h, pss) { }
  67 
  68   template <class T> void do_oop_work(T* p);
  69   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
  70   virtual void do_oop(oop* p)       { do_oop_work(p); }
  71 };
  72 
  73 // Used during the Scan RS phase to scan cards from the remembered set during garbage collection.
  74 class G1ScanObjsDuringScanRSClosure : public G1ScanClosureBase {
  75 public:
  76   G1ScanObjsDuringScanRSClosure(G1CollectedHeap* g1h,
  77                                 G1ParScanThreadState* par_scan_state):
  78     G1ScanClosureBase(g1h, par_scan_state) { }
  79 
  80   template <class T> void do_oop_work(T* p);
  81   virtual void do_oop(oop* p)          { do_oop_work(p); }
  82   virtual void do_oop(narrowOop* p)    { do_oop_work(p); }
  83 };
  84 
  85 // This closure is applied to the fields of the objects that have just been copied during evacuation.
  86 class G1ScanEvacuatedObjClosure : public G1ScanClosureBase {
  87   bool _scanning_in_young;
  88 
  89 public:
  90   G1ScanEvacuatedObjClosure(G1CollectedHeap* g1h, G1ParScanThreadState* par_scan_state) :
  91     G1ScanClosureBase(g1h, par_scan_state), _scanning_in_young(false) { }
  92 
  93   void set_scanning_in_young(bool scanning_in_young) { _scanning_in_young = scanning_in_young; }
  94 
  95   template <class T> void do_oop_work(T* p);
  96   virtual void do_oop(oop* p)          { do_oop_work(p); }
  97   virtual void do_oop(narrowOop* p)    { do_oop_work(p); }
  98 
  99   // We need to do reference discovery while processing evacuated objects.
 100   virtual ReferenceIterationMode reference_iteration_mode() { return DO_DISCOVERED_AND_DISCOVERY; }
 101 
 102   void set_ref_discoverer(ReferenceDiscoverer* rd) {
 103     set_ref_discoverer_internal(rd);
 104   }
 105 };
 106 
 107 // Add back base class for metadata
 108 class G1ParCopyHelper : public OopClosure {
 109 protected:
 110   G1CollectedHeap* _g1h;
 111   G1ParScanThreadState* _par_scan_state;
 112   uint _worker_id;              // Cache value from par_scan_state.
 113   ClassLoaderData* _scanned_cld;
 114   G1ConcurrentMark* _cm;
 115 
 116   // Mark the object if it's not already marked. This is used to mark
 117   // objects pointed to by roots that are guaranteed not to move
 118   // during the GC (i.e., non-CSet objects). It is MT-safe.
 119   inline void mark_object(oop obj);
 120 
 121   // Mark the object if it's not already marked. This is used to mark
 122   // objects pointed to by roots that have been forwarded during a
 123   // GC. It is MT-safe.
 124   inline void mark_forwarded_object(oop from_obj, oop to_obj);
 125 
 126   G1ParCopyHelper(G1CollectedHeap* g1h,  G1ParScanThreadState* par_scan_state);
 127   ~G1ParCopyHelper() { }
 128 
 129  public:
 130   void set_scanned_cld(ClassLoaderData* cld) { _scanned_cld = cld; }
 131   inline void do_cld_barrier(oop new_obj);
 132 
 133   inline void trim_queue_partially();
 134 };
 135 
 136 enum G1Barrier {
 137   G1BarrierNone,
 138   G1BarrierCLD
 139 };
 140 
 141 enum G1Mark {
 142   G1MarkNone,
 143   G1MarkFromRoot,
 144   G1MarkPromotedFromRoot
 145 };
 146 
 147 template <G1Barrier barrier, G1Mark do_mark_object>
 148 class G1ParCopyClosure : public G1ParCopyHelper {
 149 public:
 150   G1ParCopyClosure(G1CollectedHeap* g1h, G1ParScanThreadState* par_scan_state) :
 151       G1ParCopyHelper(g1h, par_scan_state) { }
 152 
 153   template <class T> void do_oop_work(T* p);
 154   virtual void do_oop(oop* p)       { do_oop_work(p); }
 155   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 156 };
 157 
 158 class G1CLDScanClosure : public CLDClosure {
 159   G1ParCopyHelper* _closure;
 160   bool             _process_only_dirty;
 161   int              _claim;
 162   int              _count;
 163 public:
 164   G1CLDScanClosure(G1ParCopyHelper* closure,
 165                    bool process_only_dirty, int claim_value)
 166   : _closure(closure), _process_only_dirty(process_only_dirty), _claim(claim_value), _count(0) {}
 167   void do_cld(ClassLoaderData* cld);
 168 };
 169 
 170 // Closure for iterating over object fields during concurrent marking
 171 class G1CMOopClosure : public MetadataVisitingOopIterateClosure {
 172   G1CollectedHeap*   _g1h;
 173   G1CMTask*          _task;
 174 public:
 175   G1CMOopClosure(G1CollectedHeap* g1h,G1CMTask* task);
 176   template <class T> void do_oop_work(T* p);
 177   virtual void do_oop(      oop* p) { do_oop_work(p); }
 178   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 179 };
 180 
 181 // Closure to scan the root regions during concurrent marking
 182 class G1RootRegionScanClosure : public MetadataVisitingOopIterateClosure {
 183 private:
 184   G1CollectedHeap* _g1h;
 185   G1ConcurrentMark* _cm;
 186   uint _worker_id;
 187 public:
 188   G1RootRegionScanClosure(G1CollectedHeap* g1h, G1ConcurrentMark* cm, uint worker_id) :
 189     _g1h(g1h), _cm(cm), _worker_id(worker_id) { }
 190   template <class T> void do_oop_work(T* p);
 191   virtual void do_oop(      oop* p) { do_oop_work(p); }
 192   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 193 };
 194 
 195 class G1ConcurrentRefineOopClosure: public BasicOopIterateClosure {
 196   G1CollectedHeap* _g1h;
 197   uint _worker_i;
 198 
 199 public:
 200   G1ConcurrentRefineOopClosure(G1CollectedHeap* g1h, uint worker_i) :
 201     _g1h(g1h),
 202     _worker_i(worker_i) {
 203   }
 204 
 205   virtual ReferenceIterationMode reference_iteration_mode() { return DO_FIELDS; }
 206 
 207   template <class T> void do_oop_work(T* p);
 208   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 209   virtual void do_oop(oop* p)       { do_oop_work(p); }
 210 };
 211 
 212 class G1RebuildRemSetClosure : public BasicOopIterateClosure {
 213   G1CollectedHeap* _g1h;
 214   uint _worker_id;
 215 public:
 216   G1RebuildRemSetClosure(G1CollectedHeap* g1h, uint worker_id) : _g1h(g1h), _worker_id(worker_id) {
 217   }
 218 
 219   template <class T> void do_oop_work(T* p);
 220   virtual void do_oop(oop* p)       { do_oop_work(p); }
 221   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 222 
 223   virtual ReferenceIterationMode reference_iteration_mode() { return DO_FIELDS; }
 224 };
 225 
 226 #endif // SHARE_VM_GC_G1_G1OOPCLOSURES_HPP