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