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_SHARED_GENOOPCLOSURES_HPP
  26 #define SHARE_VM_GC_SHARED_GENOOPCLOSURES_HPP
  27 
  28 #include "memory/iterator.hpp"
  29 #include "oops/oop.hpp"
  30 
  31 class Generation;
  32 class HeapWord;
  33 class CardTableRS;
  34 class CardTableModRefBS;
  35 class DefNewGeneration;
  36 class KlassRemSet;
  37 
  38 // Closure for iterating roots from a particular generation
  39 // Note: all classes deriving from this MUST call this do_barrier
  40 // method at the end of their own do_oop method!
  41 // Note: no do_oop defined, this is an abstract class.
  42 
  43 class OopsInGenClosure : public ExtendedOopClosure {
  44  private:
  45   Generation*  _gen;          // generation being scanned
  46 
  47  protected:
  48   // Some subtypes need access.
  49   HeapWord*    _gen_boundary; // start of generation
  50   CardTableRS* _rs;           // remembered set
  51 
  52   // For assertions
  53   Generation* generation() { return _gen; }
  54   CardTableRS* rs() { return _rs; }
  55 
  56   // Derived classes that modify oops so that they might be old-to-young
  57   // pointers must call the method below.
  58   template <class T> void do_barrier(T* p);
  59 
  60   // Version for use by closures that may be called in parallel code.
  61   template <class T> void par_do_barrier(T* p);
  62 
  63  public:
  64   OopsInGenClosure() : ExtendedOopClosure(NULL),
  65     _gen(NULL), _gen_boundary(NULL), _rs(NULL) {};
  66 
  67   OopsInGenClosure(Generation* gen);
  68 
  69   // Problem with static closures: must have _gen_boundary set at some point,
  70   // but cannot do this until after the heap is initialized.
  71   void set_generation(Generation* gen);
  72   void assert_generation(Generation* gen);
  73 
  74   HeapWord* gen_boundary() { return _gen_boundary; }
  75 };
  76 
  77 // Super class for scan closures. It contains code to dirty scanned Klasses.
  78 class OopsInKlassOrGenClosure: public OopsInGenClosure {
  79   Klass* _scanned_klass;
  80  public:
  81   OopsInKlassOrGenClosure(Generation* g) : OopsInGenClosure(g), _scanned_klass(NULL) {}
  82   void set_scanned_klass(Klass* k) {
  83     assert(k == NULL || _scanned_klass == NULL, "Must be");
  84     _scanned_klass = k;
  85   }
  86   bool is_scanning_a_klass() { return _scanned_klass != NULL; }
  87   void do_klass_barrier();
  88 };
  89 
  90 // Closure for scanning DefNewGeneration.
  91 //
  92 // This closure will perform barrier store calls for ALL
  93 // pointers in scanned oops.
  94 class ScanClosure: public OopsInKlassOrGenClosure {
  95  protected:
  96   DefNewGeneration* _g;
  97   HeapWord*         _boundary;
  98   bool              _gc_barrier;
  99   template <class T> inline void do_oop_work(T* p);
 100  public:
 101   ScanClosure(DefNewGeneration* g, bool gc_barrier);
 102   virtual void do_oop(oop* p);
 103   virtual void do_oop(narrowOop* p);
 104   inline void do_oop_nv(oop* p);
 105   inline void do_oop_nv(narrowOop* p);
 106 };
 107 
 108 // Closure for scanning DefNewGeneration.
 109 //
 110 // This closure only performs barrier store calls on
 111 // pointers into the DefNewGeneration. This is less
 112 // precise, but faster, than a ScanClosure
 113 class FastScanClosure: public OopsInKlassOrGenClosure {
 114  protected:
 115   DefNewGeneration* _g;
 116   HeapWord*         _boundary;
 117   bool              _gc_barrier;
 118   template <class T> inline void do_oop_work(T* p);
 119  public:
 120   FastScanClosure(DefNewGeneration* g, bool gc_barrier);
 121   virtual void do_oop(oop* p);
 122   virtual void do_oop(narrowOop* p);
 123   inline void do_oop_nv(oop* p);
 124   inline void do_oop_nv(narrowOop* p);
 125 };
 126 
 127 class KlassScanClosure: public KlassClosure {
 128   OopsInKlassOrGenClosure* _scavenge_closure;
 129   // true if the the modified oops state should be saved.
 130   bool                     _accumulate_modified_oops;
 131  public:
 132   KlassScanClosure(OopsInKlassOrGenClosure* scavenge_closure,
 133                    KlassRemSet* klass_rem_set_policy);
 134   void do_klass(Klass* k);
 135 };
 136 
 137 class FilteringClosure: public ExtendedOopClosure {
 138  private:
 139   HeapWord*   _boundary;
 140   ExtendedOopClosure* _cl;
 141  protected:
 142   template <class T> inline void do_oop_work(T* p) {
 143     T heap_oop = oopDesc::load_heap_oop(p);
 144     if (!oopDesc::is_null(heap_oop)) {
 145       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 146       if ((HeapWord*)obj < _boundary) {
 147         _cl->do_oop(p);
 148       }
 149     }
 150   }
 151  public:
 152   FilteringClosure(HeapWord* boundary, ExtendedOopClosure* cl) :
 153     ExtendedOopClosure(cl->_ref_processor), _boundary(boundary),
 154     _cl(cl) {}
 155   virtual void do_oop(oop* p);
 156   virtual void do_oop(narrowOop* p);
 157   inline void do_oop_nv(oop* p)       { FilteringClosure::do_oop_work(p); }
 158   inline void do_oop_nv(narrowOop* p) { FilteringClosure::do_oop_work(p); }
 159   virtual bool do_metadata()          { return do_metadata_nv(); }
 160   inline bool do_metadata_nv()        { assert(!_cl->do_metadata(), "assumption broken, must change to 'return _cl->do_metadata()'"); return false; }
 161 };
 162 
 163 // Closure for scanning DefNewGeneration's weak references.
 164 // NOTE: very much like ScanClosure but not derived from
 165 //  OopsInGenClosure -- weak references are processed all
 166 //  at once, with no notion of which generation they were in.
 167 class ScanWeakRefClosure: public OopClosure {
 168  protected:
 169   DefNewGeneration* _g;
 170   HeapWord*         _boundary;
 171   template <class T> inline void do_oop_work(T* p);
 172  public:
 173   ScanWeakRefClosure(DefNewGeneration* g);
 174   virtual void do_oop(oop* p);
 175   virtual void do_oop(narrowOop* p);
 176   inline void do_oop_nv(oop* p);
 177   inline void do_oop_nv(narrowOop* p);
 178 };
 179 
 180 #endif // SHARE_VM_GC_SHARED_GENOOPCLOSURES_HPP