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 KlassClosure : public Closure {
 132  public:
 133   virtual void do_klass(Klass* k) = 0;
 134 };
 135 
 136 class CLDClosure : public Closure {
 137  public:
 138   virtual void do_cld(ClassLoaderData* cld) = 0;
 139 };
 140 
 141 
 142 class CLDToOopClosure : public CLDClosure {
 143   OopClosure*       _oop_closure;
 144   bool              _must_claim_cld;
 145 
 146  public:
 147   CLDToOopClosure(OopClosure* oop_closure, bool must_claim_cld = true) :
 148       _oop_closure(oop_closure),
 149       _must_claim_cld(must_claim_cld) {}
 150 
 151   void do_cld(ClassLoaderData* cld);
 152 };
 153 
 154 // The base class for all concurrent marking closures,
 155 // that participates in class unloading.
 156 // It's used to proxy through the metadata to the oops defined in them.
 157 class MetadataAwareOopClosure: public ExtendedOopClosure {
 158 
 159  public:
 160   MetadataAwareOopClosure() : ExtendedOopClosure() { }
 161   MetadataAwareOopClosure(ReferenceProcessor* rp) : ExtendedOopClosure(rp) { }
 162 
 163   bool do_metadata_nv()      { return true; }
 164   virtual bool do_metadata() { return do_metadata_nv(); }
 165 
 166   void do_klass_nv(Klass* k);
 167   virtual void do_klass(Klass* k) { do_klass_nv(k); }
 168 
 169   void do_cld_nv(ClassLoaderData* cld);
 170   virtual void do_cld(ClassLoaderData* cld) { do_cld_nv(cld); }
 171 };
 172 
 173 // ObjectClosure is used for iterating through an object space
 174 
 175 class ObjectClosure : public Closure {
 176  public:
 177   // Called for each object.
 178   virtual void do_object(oop obj) = 0;
 179 };
 180 
 181 
 182 class BoolObjectClosure : public Closure {
 183  public:
 184   virtual bool do_object_b(oop obj) = 0;
 185 };
 186 
 187 class AlwaysTrueClosure: public BoolObjectClosure {
 188  public:
 189   bool do_object_b(oop p) { return true; }
 190 };
 191 
 192 class AlwaysFalseClosure : public BoolObjectClosure {
 193  public:
 194   bool do_object_b(oop p) { return false; }
 195 };
 196 
 197 // Applies an oop closure to all ref fields in objects iterated over in an
 198 // object iteration.
 199 class ObjectToOopClosure: public ObjectClosure {
 200   ExtendedOopClosure* _cl;
 201 public:
 202   void do_object(oop obj);
 203   ObjectToOopClosure(ExtendedOopClosure* cl) : _cl(cl) {}
 204 };
 205 
 206 // A version of ObjectClosure that is expected to be robust
 207 // in the face of possibly uninitialized objects.
 208 class ObjectClosureCareful : public ObjectClosure {
 209  public:
 210   virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0;
 211   virtual size_t do_object_careful(oop p) = 0;
 212 };
 213 
 214 // The following are used in CompactibleFreeListSpace and
 215 // ConcurrentMarkSweepGeneration.
 216 
 217 // Blk closure (abstract class)
 218 class BlkClosure : public StackObj {
 219  public:
 220   virtual size_t do_blk(HeapWord* addr) = 0;
 221 };
 222 
 223 // A version of BlkClosure that is expected to be robust
 224 // in the face of possibly uninitialized objects.
 225 class BlkClosureCareful : public BlkClosure {
 226  public:
 227   size_t do_blk(HeapWord* addr) {
 228     guarantee(false, "call do_blk_careful instead");
 229     return 0;
 230   }
 231   virtual size_t do_blk_careful(HeapWord* addr) = 0;
 232 };
 233 
 234 // SpaceClosure is used for iterating over spaces
 235 
 236 class Space;
 237 class CompactibleSpace;
 238 
 239 class SpaceClosure : public StackObj {
 240  public:
 241   // Called for each space
 242   virtual void do_space(Space* s) = 0;
 243 };
 244 
 245 class CompactibleSpaceClosure : public StackObj {
 246  public:
 247   // Called for each compactible space
 248   virtual void do_space(CompactibleSpace* s) = 0;
 249 };
 250 
 251 
 252 // CodeBlobClosure is used for iterating through code blobs
 253 // in the code cache or on thread stacks
 254 
 255 class CodeBlobClosure : public Closure {
 256  public:
 257   // Called for each code blob.
 258   virtual void do_code_blob(CodeBlob* cb) = 0;
 259 };
 260 
 261 // Applies an oop closure to all ref fields in code blobs
 262 // iterated over in an object iteration.
 263 class CodeBlobToOopClosure : public CodeBlobClosure {
 264   OopClosure* _cl;
 265   bool _fix_relocations;
 266  protected:
 267   void do_nmethod(nmethod* nm);
 268  public:
 269   // If fix_relocations(), then cl must copy objects to their new location immediately to avoid
 270   // patching nmethods with the old locations.
 271   CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}
 272   virtual void do_code_blob(CodeBlob* cb);
 273 
 274   bool fix_relocations() const { return _fix_relocations; }
 275   const static bool FixRelocations = true;
 276 };
 277 
 278 class MarkingCodeBlobClosure : public CodeBlobToOopClosure {
 279  public:
 280   MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {}
 281   // Called for each code blob, but at most once per unique blob.
 282 
 283   virtual void do_code_blob(CodeBlob* cb);
 284 };
 285 
 286 // MonitorClosure is used for iterating over monitors in the monitors cache
 287 
 288 class ObjectMonitor;
 289 
 290 class MonitorClosure : public StackObj {
 291  public:
 292   // called for each monitor in cache
 293   virtual void do_monitor(ObjectMonitor* m) = 0;
 294 };
 295 
 296 // A closure that is applied without any arguments.
 297 class VoidClosure : public StackObj {
 298  public:
 299   // I would have liked to declare this a pure virtual, but that breaks
 300   // in mysterious ways, for unknown reasons.
 301   virtual void do_void();
 302 };
 303 
 304 
 305 // YieldClosure is intended for use by iteration loops
 306 // to incrementalize their work, allowing interleaving
 307 // of an interruptable task so as to allow other
 308 // threads to run (which may not otherwise be able to access
 309 // exclusive resources, for instance). Additionally, the
 310 // closure also allows for aborting an ongoing iteration
 311 // by means of checking the return value from the polling
 312 // call.
 313 class YieldClosure : public StackObj {
 314   public:
 315    virtual bool should_return() = 0;
 316 };
 317 
 318 // Abstract closure for serializing data (read or write).
 319 
 320 class SerializeClosure : public Closure {
 321 public:
 322   // Return bool indicating whether closure implements read or write.
 323   virtual bool reading() const = 0;
 324 
 325   // Read/write the void pointer pointed to by p.
 326   virtual void do_ptr(void** p) = 0;
 327 
 328   // Read/write the 32-bit unsigned integer pointed to by p.
 329   virtual void do_u4(u4* p) = 0;
 330 
 331   // Read/write the region specified.
 332   virtual void do_region(u_char* start, size_t size) = 0;
 333 
 334   // Check/write the tag.  If reading, then compare the tag against
 335   // the passed in value and fail is they don't match.  This allows
 336   // for verification that sections of the serialized data are of the
 337   // correct length.
 338   virtual void do_tag(int tag) = 0;
 339 
 340   bool writing() {
 341     return !reading();
 342   }
 343 };
 344 
 345 class SymbolClosure : public StackObj {
 346  public:
 347   virtual void do_symbol(Symbol**) = 0;
 348 
 349   // Clear LSB in symbol address; it can be set by CPSlot.
 350   static Symbol* load_symbol(Symbol** p) {
 351     return (Symbol*)(intptr_t(*p) & ~1);
 352   }
 353 
 354   // Store symbol, adjusting new pointer if the original pointer was adjusted
 355   // (symbol references in constant pool slots have their LSB set to 1).
 356   static void store_symbol(Symbol** p, Symbol* sym) {
 357     *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1));
 358   }
 359 };
 360 
 361 // The two class template specializations are used to dispatch calls
 362 // to the ExtendedOopClosure functions. If use_non_virtual_call is true,
 363 // the non-virtual versions are called (E.g. do_oop_nv), otherwise the
 364 // virtual versions are called (E.g. do_oop).
 365 
 366 template <bool use_non_virtual_call>
 367 class Devirtualizer {};
 368 
 369 // Dispatches to the non-virtual functions.
 370 template <> class Devirtualizer<true> {
 371  public:
 372   template <class OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);
 373   template <class OopClosureType>             static void do_klass(OopClosureType* closure, Klass* k);
 374   template <class OopClosureType>             static void do_cld(OopClosureType* closure, ClassLoaderData* cld);
 375   template <class OopClosureType>             static bool do_metadata(OopClosureType* closure);
 376 };
 377 
 378 // Dispatches to the virtual functions.
 379 template <> class Devirtualizer<false> {
 380  public:
 381   template <class OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);
 382   template <class OopClosureType>             static void do_klass(OopClosureType* closure, Klass* k);
 383   template <class OopClosureType>             static void do_cld(OopClosureType* closure, ClassLoaderData* cld);
 384   template <class OopClosureType>             static bool do_metadata(OopClosureType* closure);
 385 };
 386 
 387 #endif // SHARE_VM_MEMORY_ITERATOR_HPP