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_MEMORY_GENOOPCLOSURES_HPP
  26 #define SHARE_VM_MEMORY_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 
  37 template<class E, unsigned int N> class GenericTaskQueue;
  38 typedef GenericTaskQueue<oop, TASKQUEUE_SIZE> OopTaskQueue;
  39 template<class T> class GenericTaskQueueSet;
  40 typedef GenericTaskQueueSet<OopTaskQueue> OopTaskQueueSet;
  41 
  42 // Closure for iterating roots from a particular generation
  43 // Note: all classes deriving from this MUST call this do_barrier
  44 // method at the end of their own do_oop method!
  45 // Note: no do_oop defined, this is an abstract class.
  46 
  47 class OopsInGenClosure : public OopClosure {
  48  private:
  49   Generation*  _orig_gen;     // generation originally set in ctor
  50   Generation*  _gen;          // generation being scanned
  51 
  52  protected:
  53   // Some subtypes need access.
  54   HeapWord*    _gen_boundary; // start of generation
  55   CardTableRS* _rs;           // remembered set
  56 
  57   // For assertions
  58   Generation* generation() { return _gen; }
  59   CardTableRS* rs() { return _rs; }
  60 
  61   // Derived classes that modify oops so that they might be old-to-young
  62   // pointers must call the method below.
  63   template <class T> void do_barrier(T* p);
  64 
  65   // Version for use by closures that may be called in parallel code.
  66   template <class T> void par_do_barrier(T* p);
  67 
  68  public:
  69   OopsInGenClosure() : OopClosure(NULL),
  70     _orig_gen(NULL), _gen(NULL), _gen_boundary(NULL), _rs(NULL) {};
  71 
  72   OopsInGenClosure(Generation* gen);
  73   void set_generation(Generation* gen);
  74 
  75   void reset_generation() { _gen = _orig_gen; }
  76 
  77   // Problem with static closures: must have _gen_boundary set at some point,
  78   // but cannot do this until after the heap is initialized.
  79   void set_orig_generation(Generation* gen) {
  80     _orig_gen = gen;
  81     set_generation(gen);
  82   }
  83 
  84   HeapWord* gen_boundary() { return _gen_boundary; }
  85 };
  86 
  87 // Closure for scanning DefNewGeneration.
  88 //
  89 // This closure will perform barrier store calls for ALL
  90 // pointers in scanned oops.
  91 class ScanClosure: public OopsInGenClosure {
  92  protected:
  93   DefNewGeneration* _g;
  94   HeapWord*         _boundary;
  95   bool              _gc_barrier;
  96   template <class T> inline void do_oop_work(T* p);
  97  public:
  98   ScanClosure(DefNewGeneration* g, bool gc_barrier);
  99   virtual void do_oop(oop* p);
 100   virtual void do_oop(narrowOop* p);
 101   inline void do_oop_nv(oop* p);
 102   inline void do_oop_nv(narrowOop* p);
 103   bool do_header() { return false; }
 104   Prefetch::style prefetch_style() {
 105     return Prefetch::do_write;
 106   }
 107 };
 108 
 109 // Closure for scanning DefNewGeneration.
 110 //
 111 // This closure only performs barrier store calls on
 112 // pointers into the DefNewGeneration. This is less
 113 // precise, but faster, than a ScanClosure
 114 class FastScanClosure: public OopsInGenClosure {
 115  protected:
 116   DefNewGeneration* _g;
 117   HeapWord*         _boundary;
 118   bool              _gc_barrier;
 119   template <class T> inline void do_oop_work(T* p);
 120  public:
 121   FastScanClosure(DefNewGeneration* g, bool gc_barrier);
 122   virtual void do_oop(oop* p);
 123   virtual void do_oop(narrowOop* p);
 124   inline void do_oop_nv(oop* p);
 125   inline void do_oop_nv(narrowOop* p);
 126   bool do_header() { return false; }
 127   Prefetch::style prefetch_style() {
 128     return Prefetch::do_write;
 129   }
 130 };
 131 
 132 class FilteringClosure: public OopClosure {
 133  private:
 134   HeapWord*   _boundary;
 135   OopClosure* _cl;
 136  protected:
 137   template <class T> inline void do_oop_work(T* p) {
 138     T heap_oop = oopDesc::load_heap_oop(p);
 139     if (!oopDesc::is_null(heap_oop)) {
 140       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
 141       if ((HeapWord*)obj < _boundary) {
 142         _cl->do_oop(p);
 143       }
 144     }
 145   }
 146  public:
 147   FilteringClosure(HeapWord* boundary, OopClosure* cl) :
 148     OopClosure(cl->_ref_processor), _boundary(boundary),
 149     _cl(cl) {}
 150   virtual void do_oop(oop* p);
 151   virtual void do_oop(narrowOop* p);
 152   inline void do_oop_nv(oop* p)       { FilteringClosure::do_oop_work(p); }
 153   inline void do_oop_nv(narrowOop* p) { FilteringClosure::do_oop_work(p); }
 154   bool do_header() { return false; }
 155 };
 156 
 157 // Closure for scanning DefNewGeneration's weak references.
 158 // NOTE: very much like ScanClosure but not derived from
 159 //  OopsInGenClosure -- weak references are processed all
 160 //  at once, with no notion of which generation they were in.
 161 class ScanWeakRefClosure: public OopClosure {
 162  protected:
 163   DefNewGeneration* _g;
 164   HeapWord*         _boundary;
 165   template <class T> inline void do_oop_work(T* p);
 166  public:
 167   ScanWeakRefClosure(DefNewGeneration* g);
 168   virtual void do_oop(oop* p);
 169   virtual void do_oop(narrowOop* p);
 170   inline void do_oop_nv(oop* p);
 171   inline void do_oop_nv(narrowOop* p);
 172 };
 173 
 174 class VerifyOopClosure: public OopClosure {
 175  protected:
 176   template <class T> inline void do_oop_work(T* p) {
 177     oop obj = oopDesc::load_decode_heap_oop(p);
 178     guarantee(obj->is_oop_or_null(), "invalid oop");
 179   }
 180  public:
 181   virtual void do_oop(oop* p);
 182   virtual void do_oop(narrowOop* p);
 183   static VerifyOopClosure verify_oop;
 184 };
 185 
 186 #endif // SHARE_VM_MEMORY_GENOOPCLOSURES_HPP