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