1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)iterator.hpp 1.38 07/05/05 17:05:52 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 // The following classes are C++ `closures` for iterating over objects, roots and spaces
  29 
  30 class ReferenceProcessor;
  31 
  32 // Closure provides abortability.
  33 
  34 class Closure : public StackObj {
  35  protected:
  36   bool _abort;
  37   void set_abort() { _abort = true; }
  38  public:
  39   Closure() : _abort(false) {}
  40   // A subtype can use this mechanism to indicate to some iterator mapping
  41   // functions that the iteration should cease.
  42   bool abort() { return _abort; }
  43   void clear_abort() { _abort = false; }
  44 };
  45 
  46 // OopClosure is used for iterating through roots (oop*)
  47 
  48 class OopClosure : public Closure {
  49  public:
  50   ReferenceProcessor* _ref_processor;
  51   OopClosure(ReferenceProcessor* rp) : _ref_processor(rp) { }
  52   OopClosure() : _ref_processor(NULL) { }
  53   virtual void do_oop(oop* o) = 0;
  54   virtual void do_oop_v(oop* o) { do_oop(o); }
  55   virtual void do_oop(narrowOop* o) = 0;
  56   virtual void do_oop_v(narrowOop* o) { do_oop(o); }
  57 
  58   // In support of post-processing of weak links of KlassKlass objects;
  59   // see KlassKlass::oop_oop_iterate().
  60   virtual const bool should_remember_klasses() const { return false;    }
  61   virtual void remember_klass(Klass* k) { /* do nothing */ }
  62 
  63   // If "true", invoke on nmethods (when scanning compiled frames).
  64   virtual const bool do_nmethods() const { return false; }
  65 
  66   // The methods below control how object iterations invoking this closure
  67   // should be performed:
  68 
  69   // If "true", invoke on header klass field.
  70   bool do_header() { return true; } // Note that this is non-virtual.
  71   // Controls how prefetching is done for invocations of this closure.
  72   Prefetch::style prefetch_style() { // Note that this is non-virtual.
  73     return Prefetch::do_none;
  74   }
  75 
  76   // True iff this closure may be safely applied more than once to an oop
  77   // location without an intervening "major reset" (like the end of a GC).
  78   virtual bool idempotent() { return false; }
  79   virtual bool apply_to_weak_ref_discovered_field() { return false; }
  80 };
  81 
  82 // ObjectClosure is used for iterating through an object space
  83 
  84 class ObjectClosure : public Closure {
  85  public:
  86   // Called for each object.
  87   virtual void do_object(oop obj) = 0;
  88 };
  89 
  90 
  91 class BoolObjectClosure : public ObjectClosure {
  92  public:
  93   virtual bool do_object_b(oop obj) = 0;
  94 };
  95 
  96 // Applies an oop closure to all ref fields in objects iterated over in an
  97 // object iteration.
  98 class ObjectToOopClosure: public ObjectClosure {
  99   OopClosure* _cl;
 100 public:
 101   void do_object(oop obj);
 102   ObjectToOopClosure(OopClosure* cl) : _cl(cl) {}
 103 };
 104 
 105 // A version of ObjectClosure with "memory" (see _previous_address below)
 106 class UpwardsObjectClosure: public BoolObjectClosure {
 107   HeapWord* _previous_address;
 108  public:
 109   UpwardsObjectClosure() : _previous_address(NULL) { }
 110   void set_previous(HeapWord* addr) { _previous_address = addr; }
 111   HeapWord* previous()              { return _previous_address; }
 112   // A return value of "true" can be used by the caller to decide
 113   // if this object's end should *NOT* be recorded in
 114   // _previous_address above.
 115   virtual bool do_object_bm(oop obj, MemRegion mr) = 0;
 116 };
 117 
 118 // A version of ObjectClosure that is expected to be robust
 119 // in the face of possibly uninitialized objects.
 120 class ObjectClosureCareful : public ObjectClosure {
 121  public:
 122   virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0;
 123   virtual size_t do_object_careful(oop p) = 0;
 124 };
 125 
 126 // The following are used in CompactibleFreeListSpace and
 127 // ConcurrentMarkSweepGeneration.
 128 
 129 // Blk closure (abstract class)
 130 class BlkClosure : public StackObj {
 131  public:
 132   virtual size_t do_blk(HeapWord* addr) = 0;
 133 };
 134 
 135 // A version of BlkClosure that is expected to be robust
 136 // in the face of possibly uninitialized objects.
 137 class BlkClosureCareful : public BlkClosure {
 138  public:
 139   size_t do_blk(HeapWord* addr) {
 140     guarantee(false, "call do_blk_careful instead");
 141     return 0;
 142   }
 143   virtual size_t do_blk_careful(HeapWord* addr) = 0;
 144 };
 145 
 146 // SpaceClosure is used for iterating over spaces
 147 
 148 class Space;
 149 class CompactibleSpace;
 150 
 151 class SpaceClosure : public StackObj {
 152  public:
 153   // Called for each space
 154   virtual void do_space(Space* s) = 0;
 155 };
 156 
 157 class CompactibleSpaceClosure : public StackObj {
 158  public:
 159   // Called for each compactible space
 160   virtual void do_space(CompactibleSpace* s) = 0;
 161 };
 162 
 163 
 164 
 165 // MonitorClosure is used for iterating over monitors in the monitors cache
 166 
 167 class ObjectMonitor;
 168 
 169 class MonitorClosure : public StackObj {
 170  public:
 171   // called for each monitor in cache
 172   virtual void do_monitor(ObjectMonitor* m) = 0;
 173 };
 174 
 175 // A closure that is applied without any arguments.
 176 class VoidClosure : public StackObj {
 177  public:
 178   // I would have liked to declare this a pure virtual, but that breaks
 179   // in mysterious ways, for unknown reasons.
 180   virtual void do_void();
 181 };
 182 
 183 
 184 // YieldClosure is intended for use by iteration loops
 185 // to incrementalize their work, allowing interleaving
 186 // of an interruptable task so as to allow other
 187 // threads to run (which may not otherwise be able to access
 188 // exclusive resources, for instance). Additionally, the
 189 // closure also allows for aborting an ongoing iteration
 190 // by means of checking the return value from the polling
 191 // call.
 192 class YieldClosure : public StackObj {
 193   public:
 194    virtual bool should_return() = 0;
 195 };
 196 
 197 // Abstract closure for serializing data (read or write).
 198 
 199 class SerializeOopClosure : public OopClosure {
 200 public:
 201   // Return bool indicating whether closure implements read or write.
 202   virtual bool reading() const = 0;
 203 
 204   // Read/write the int pointed to by i.
 205   virtual void do_int(int* i) = 0;
 206 
 207   // Read/write the size_t pointed to by i.
 208   virtual void do_size_t(size_t* i) = 0;
 209 
 210   // Read/write the void pointer pointed to by p.
 211   virtual void do_ptr(void** p) = 0;
 212 
 213   // Read/write the HeapWord pointer pointed to be p.
 214   virtual void do_ptr(HeapWord** p) = 0;
 215 
 216   // Read/write the region specified.
 217   virtual void do_region(u_char* start, size_t size) = 0;
 218 
 219   // Check/write the tag.  If reading, then compare the tag against
 220   // the passed in value and fail is they don't match.  This allows
 221   // for verification that sections of the serialized data are of the
 222   // correct length.
 223   virtual void do_tag(int tag) = 0;
 224 };