1 /*
   2  * Copyright (c) 1997, 2013, 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_ITERATOR_HPP
  26 #define SHARE_VM_MEMORY_ITERATOR_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/memRegion.hpp"
  30 #include "utilities/top.hpp"
  31 
  32 class CodeBlob;
  33 class nmethod;
  34 class ReferenceProcessor;
  35 class DataLayout;
  36 class KlassClosure;
  37 class ClassLoaderData;
  38 
  39 // The following classes are C++ `closures` for iterating over objects, roots and spaces
  40 
  41 class Closure : public StackObj { };
  42 
  43 // OopClosure is used for iterating through references to Java objects.
  44 class OopClosure : public Closure {
  45  public:
  46   virtual void do_oop(oop* o) = 0;
  47   virtual void do_oop(narrowOop* o) = 0;
  48 };
  49 
  50 // ExtendedOopClosure adds extra code to be run during oop iterations.
  51 // This is needed by the GC and is extracted to a separate type to not
  52 // pollute the OopClosure interface.
  53 class ExtendedOopClosure : public OopClosure {
  54  public:
  55   ReferenceProcessor* _ref_processor;
  56   ExtendedOopClosure(ReferenceProcessor* rp) : _ref_processor(rp) { }
  57   ExtendedOopClosure() : OopClosure(), _ref_processor(NULL) { }
  58 
  59   // If the do_metadata functions return "true",
  60   // we invoke the following when running oop_iterate():
  61   //
  62   // 1) do_klass on the header klass pointer.
  63   // 2) do_klass on the klass pointer in the mirrors.
  64   // 3) do_class_loader_data on the class loader data in class loaders.
  65   //
  66   // The virtual (without suffix) and the non-virtual (with _nv suffix) need
  67   // to be updated together, or else the devirtualization will break.
  68   //
  69   // Providing default implementations of the _nv functions unfortunately
  70   // removes the compile-time safeness, but reduces the clutter for the
  71   // ExtendedOopClosures that don't need to walk the metadata.
  72   // Currently, only CMS and G1 need these.
  73 
  74   virtual bool do_metadata() { return do_metadata_nv(); }
  75   bool do_metadata_nv()      { return false; }
  76 
  77   virtual void do_klass(Klass* k)   { do_klass_nv(k); }
  78   void do_klass_nv(Klass* k)        { ShouldNotReachHere(); }
  79 
  80   virtual void do_class_loader_data(ClassLoaderData* cld) { ShouldNotReachHere(); }
  81 
  82   // True iff this closure may be safely applied more than once to an oop
  83   // location without an intervening "major reset" (like the end of a GC).
  84   virtual bool idempotent() { return false; }
  85   virtual bool apply_to_weak_ref_discovered_field() { return false; }
  86 
  87 #ifdef ASSERT
  88   // Default verification of each visited oop field.
  89   template <typename T> void verify(T* p);
  90 
  91   // Can be used by subclasses to turn off the default verification of oop fields.
  92   virtual bool should_verify_oops() { return true; }
  93 #endif
  94 };
  95 
  96 // Wrapper closure only used to implement oop_iterate_no_header().
  97 class NoHeaderExtendedOopClosure : public ExtendedOopClosure {
  98   OopClosure* _wrapped_closure;
  99  public:
 100   NoHeaderExtendedOopClosure(OopClosure* cl) : _wrapped_closure(cl) {}
 101   // Warning: this calls the virtual version do_oop in the the wrapped closure.
 102   void do_oop_nv(oop* p)       { _wrapped_closure->do_oop(p); }
 103   void do_oop_nv(narrowOop* p) { _wrapped_closure->do_oop(p); }
 104 
 105   void do_oop(oop* p)          { assert(false, "Only the _nv versions should be used");
 106                                  _wrapped_closure->do_oop(p); }
 107   void do_oop(narrowOop* p)    { assert(false, "Only the _nv versions should be used");
 108                                  _wrapped_closure->do_oop(p);}
 109 };
 110 
 111 class KlassClosure : public Closure {
 112  public:
 113   virtual void do_klass(Klass* k) = 0;
 114 };
 115 
 116 class CLDClosure : public Closure {
 117  public:
 118   virtual void do_cld(ClassLoaderData* cld) = 0;
 119 };
 120 
 121 class KlassToOopClosure : public KlassClosure {
 122   friend class MetadataAwareOopClosure;
 123   friend class MetadataAwareOopsInGenClosure;
 124 
 125   OopClosure* _oop_closure;
 126 
 127   // Used when _oop_closure couldn't be set in an initialization list.
 128   void initialize(OopClosure* oop_closure) {
 129     assert(_oop_closure == NULL, "Should only be called once");
 130     _oop_closure = oop_closure;
 131   }
 132 
 133  public:
 134   KlassToOopClosure(OopClosure* oop_closure = NULL) : _oop_closure(oop_closure) {}
 135 
 136   virtual void do_klass(Klass* k);
 137 };
 138 
 139 class CLDToOopClosure : public CLDClosure {
 140   OopClosure*       _oop_closure;
 141   KlassToOopClosure _klass_closure;
 142   bool              _must_claim_cld;
 143 
 144  public:
 145   CLDToOopClosure(OopClosure* oop_closure, bool must_claim_cld = true) :
 146       _oop_closure(oop_closure),
 147       _klass_closure(oop_closure),
 148       _must_claim_cld(must_claim_cld) {}
 149 
 150   void do_cld(ClassLoaderData* cld);
 151 };
 152 
 153 class CLDToKlassAndOopClosure : public CLDClosure {
 154   friend class G1CollectedHeap;
 155  protected:
 156   OopClosure*   _oop_closure;
 157   KlassClosure* _klass_closure;
 158   bool          _must_claim_cld;
 159  public:
 160   CLDToKlassAndOopClosure(KlassClosure* klass_closure,
 161                           OopClosure* oop_closure,
 162                           bool must_claim_cld) :
 163                               _oop_closure(oop_closure),
 164                               _klass_closure(klass_closure),
 165                               _must_claim_cld(must_claim_cld) {}
 166   void do_cld(ClassLoaderData* cld);
 167 };
 168 
 169 // The base class for all concurrent marking closures,
 170 // that participates in class unloading.
 171 // It's used to proxy through the metadata to the oops defined in them.
 172 class MetadataAwareOopClosure: public ExtendedOopClosure {
 173   KlassToOopClosure _klass_closure;
 174 
 175  public:
 176   MetadataAwareOopClosure() : ExtendedOopClosure() {
 177     _klass_closure.initialize(this);
 178   }
 179   MetadataAwareOopClosure(ReferenceProcessor* rp) : ExtendedOopClosure(rp) {
 180     _klass_closure.initialize(this);
 181   }
 182 
 183   virtual bool do_metadata()    { return do_metadata_nv(); }
 184   inline  bool do_metadata_nv() { return true; }
 185 
 186   virtual void do_klass(Klass* k);
 187   void do_klass_nv(Klass* k);
 188 
 189   virtual void do_class_loader_data(ClassLoaderData* cld);
 190 };
 191 
 192 // ObjectClosure is used for iterating through an object space
 193 
 194 class ObjectClosure : public Closure {
 195  public:
 196   // Called for each object.
 197   virtual void do_object(oop obj) = 0;
 198 };
 199 
 200 
 201 class BoolObjectClosure : public Closure {
 202  public:
 203   virtual bool do_object_b(oop obj) = 0;
 204 };
 205 
 206 // Applies an oop closure to all ref fields in objects iterated over in an
 207 // object iteration.
 208 class ObjectToOopClosure: public ObjectClosure {
 209   ExtendedOopClosure* _cl;
 210 public:
 211   void do_object(oop obj);
 212   ObjectToOopClosure(ExtendedOopClosure* cl) : _cl(cl) {}
 213 };
 214 
 215 // A version of ObjectClosure that is expected to be robust
 216 // in the face of possibly uninitialized objects.
 217 class ObjectClosureCareful : public ObjectClosure {
 218  public:
 219   virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0;
 220   virtual size_t do_object_careful(oop p) = 0;
 221 };
 222 
 223 // The following are used in CompactibleFreeListSpace and
 224 // ConcurrentMarkSweepGeneration.
 225 
 226 // Blk closure (abstract class)
 227 class BlkClosure : public StackObj {
 228  public:
 229   virtual size_t do_blk(HeapWord* addr) = 0;
 230 };
 231 
 232 // A version of BlkClosure that is expected to be robust
 233 // in the face of possibly uninitialized objects.
 234 class BlkClosureCareful : public BlkClosure {
 235  public:
 236   size_t do_blk(HeapWord* addr) {
 237     guarantee(false, "call do_blk_careful instead");
 238     return 0;
 239   }
 240   virtual size_t do_blk_careful(HeapWord* addr) = 0;
 241 };
 242 
 243 // SpaceClosure is used for iterating over spaces
 244 
 245 class Space;
 246 class CompactibleSpace;
 247 
 248 class SpaceClosure : public StackObj {
 249  public:
 250   // Called for each space
 251   virtual void do_space(Space* s) = 0;
 252 };
 253 
 254 class CompactibleSpaceClosure : public StackObj {
 255  public:
 256   // Called for each compactible space
 257   virtual void do_space(CompactibleSpace* s) = 0;
 258 };
 259 
 260 
 261 // CodeBlobClosure is used for iterating through code blobs
 262 // in the code cache or on thread stacks
 263 
 264 class CodeBlobClosure : public Closure {
 265  public:
 266   // Called for each code blob.
 267   virtual void do_code_blob(CodeBlob* cb) = 0;
 268 };
 269 
 270 // Applies an oop closure to all ref fields in code blobs
 271 // iterated over in an object iteration.
 272 class CodeBlobToOopClosure : public CodeBlobClosure {
 273   OopClosure* _cl;
 274   bool _fix_relocations;
 275  protected:
 276   void do_nmethod(nmethod* nm);
 277  public:
 278   CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}
 279   virtual void do_code_blob(CodeBlob* cb);
 280 
 281   const static bool FixRelocations = true;
 282 };
 283 
 284 class MarkingCodeBlobClosure : public CodeBlobToOopClosure {
 285  public:
 286   MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {}
 287   // Called for each code blob, but at most once per unique blob.
 288 
 289   virtual void do_code_blob(CodeBlob* cb);
 290 };
 291 
 292 // MonitorClosure is used for iterating over monitors in the monitors cache
 293 
 294 class ObjectMonitor;
 295 
 296 class MonitorClosure : public StackObj {
 297  public:
 298   // called for each monitor in cache
 299   virtual void do_monitor(ObjectMonitor* m) = 0;
 300 };
 301 
 302 // A closure that is applied without any arguments.
 303 class VoidClosure : public StackObj {
 304  public:
 305   // I would have liked to declare this a pure virtual, but that breaks
 306   // in mysterious ways, for unknown reasons.
 307   virtual void do_void();
 308 };
 309 
 310 
 311 // YieldClosure is intended for use by iteration loops
 312 // to incrementalize their work, allowing interleaving
 313 // of an interruptable task so as to allow other
 314 // threads to run (which may not otherwise be able to access
 315 // exclusive resources, for instance). Additionally, the
 316 // closure also allows for aborting an ongoing iteration
 317 // by means of checking the return value from the polling
 318 // call.
 319 class YieldClosure : public StackObj {
 320   public:
 321    virtual bool should_return() = 0;
 322 };
 323 
 324 // Abstract closure for serializing data (read or write).
 325 
 326 class SerializeClosure : public Closure {
 327 public:
 328   // Return bool indicating whether closure implements read or write.
 329   virtual bool reading() const = 0;
 330 
 331   // Read/write the void pointer pointed to by p.
 332   virtual void do_ptr(void** p) = 0;
 333 
 334   // Read/write the region specified.
 335   virtual void do_region(u_char* start, size_t size) = 0;
 336 
 337   // Check/write the tag.  If reading, then compare the tag against
 338   // the passed in value and fail is they don't match.  This allows
 339   // for verification that sections of the serialized data are of the
 340   // correct length.
 341   virtual void do_tag(int tag) = 0;
 342 };
 343 
 344 class SymbolClosure : public StackObj {
 345  public:
 346   virtual void do_symbol(Symbol**) = 0;
 347 
 348   // Clear LSB in symbol address; it can be set by CPSlot.
 349   static Symbol* load_symbol(Symbol** p) {
 350     return (Symbol*)(intptr_t(*p) & ~1);
 351   }
 352 
 353   // Store symbol, adjusting new pointer if the original pointer was adjusted
 354   // (symbol references in constant pool slots have their LSB set to 1).
 355   static void store_symbol(Symbol** p, Symbol* sym) {
 356     *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1));
 357   }
 358 };
 359 
 360 // The two class template specializations are used to dispatch calls
 361 // to the ExtendedOopClosure functions. If use_non_virtual_call is true,
 362 // the non-virtual versions are called (E.g. do_oop_nv), otherwise the
 363 // virtual versions are called (E.g. do_oop).
 364 
 365 template <bool use_non_virtual_call>
 366 class Devirtualizer {};
 367 
 368 // Dispatches to the non-virtual functions.
 369 template <> class Devirtualizer<true> {
 370  public:
 371   template <class OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);
 372   template <class OopClosureType>             static void do_klass(OopClosureType* closure, Klass* k);
 373   template <class OopClosureType>             static bool do_metadata(OopClosureType* closure);
 374 };
 375 
 376 // Dispatches to the virtual functions.
 377 template <> class Devirtualizer<false> {
 378  public:
 379   template <class OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);
 380   template <class OopClosureType>             static void do_klass(OopClosureType* closure, Klass* k);
 381   template <class OopClosureType>             static bool do_metadata(OopClosureType* closure);
 382 };
 383 
 384 #endif // SHARE_VM_MEMORY_ITERATOR_HPP