1 /*
   2  * Copyright (c) 1997, 2019, 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_MEMORY_ITERATOR_HPP
  26 #define SHARE_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 ReferenceDiscoverer;
  35 class DataLayout;
  36 class KlassClosure;
  37 class ClassLoaderData;
  38 class Symbol;
  39 class Metadata;
  40 
  41 // The following classes are C++ `closures` for iterating over objects, roots and spaces
  42 
  43 class Closure : public StackObj { };
  44 
  45 // OopClosure is used for iterating through references to Java objects.
  46 class OopClosure : public Closure {
  47  public:
  48   virtual void do_oop(oop* o) = 0;
  49   virtual void do_oop(narrowOop* o) = 0;
  50 };
  51 
  52 class DoNothingClosure : public OopClosure {
  53  public:
  54   virtual void do_oop(oop* p)       {}
  55   virtual void do_oop(narrowOop* p) {}
  56 };
  57 extern DoNothingClosure do_nothing_cl;
  58 
  59 // OopIterateClosure adds extra code to be run during oop iterations.
  60 // This is needed by the GC and is extracted to a separate type to not
  61 // pollute the OopClosure interface.
  62 class OopIterateClosure : public OopClosure {
  63  private:
  64   ReferenceDiscoverer* _ref_discoverer;
  65 
  66  protected:
  67   OopIterateClosure(ReferenceDiscoverer* rd) : _ref_discoverer(rd) { }
  68   OopIterateClosure() : _ref_discoverer(NULL) { }
  69   ~OopIterateClosure() { }
  70 
  71   void set_ref_discoverer_internal(ReferenceDiscoverer* rd) { _ref_discoverer = rd; }
  72 
  73  public:
  74   ReferenceDiscoverer* ref_discoverer() const { return _ref_discoverer; }
  75 
  76   // Iteration of InstanceRefKlasses differ depending on the closure,
  77   // the below enum describes the different alternatives.
  78   enum ReferenceIterationMode {
  79     DO_DISCOVERY,                // Apply closure and discover references
  80     DO_DISCOVERED_AND_DISCOVERY, // Apply closure to discovered field and do discovery
  81     DO_FIELDS,                   // Apply closure to all fields
  82     DO_FIELDS_EXCEPT_REFERENT    // Apply closure to all fields except the referent field
  83   };
  84 
  85   // The default iteration mode is to do discovery.
  86   virtual ReferenceIterationMode reference_iteration_mode() { return DO_DISCOVERY; }
  87 
  88   // If the do_metadata functions return "true",
  89   // we invoke the following when running oop_iterate():
  90   //
  91   // 1) do_klass on the header klass pointer.
  92   // 2) do_klass on the klass pointer in the mirrors.
  93   // 3) do_cld   on the class loader data in class loaders.
  94 
  95   virtual bool do_metadata() = 0;
  96   virtual void do_klass(Klass* k) = 0;
  97   virtual void do_cld(ClassLoaderData* cld) = 0;
  98 
  99 #ifdef ASSERT
 100   // Default verification of each visited oop field.
 101   template <typename T> void verify(T* p);
 102 
 103   // Can be used by subclasses to turn off the default verification of oop fields.
 104   virtual bool should_verify_oops() { return true; }
 105 #endif
 106 };
 107 
 108 // An OopIterateClosure that can be used when there's no need to visit the Metadata.
 109 class BasicOopIterateClosure : public OopIterateClosure {
 110 public:
 111   BasicOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) {}
 112 
 113   virtual bool do_metadata() { return false; }
 114   virtual void do_klass(Klass* k) { ShouldNotReachHere(); }
 115   virtual void do_cld(ClassLoaderData* cld) { ShouldNotReachHere(); }
 116 };
 117 
 118 class KlassClosure : public Closure {
 119  public:
 120   virtual void do_klass(Klass* k) = 0;
 121 };
 122 
 123 class CLDClosure : public Closure {
 124  public:
 125   virtual void do_cld(ClassLoaderData* cld) = 0;
 126 };
 127 
 128 class MetadataClosure : public Closure {
 129  public:
 130   virtual void do_metadata(Metadata* md) = 0;
 131 };
 132 
 133 
 134 class CLDToOopClosure : public CLDClosure {
 135   OopClosure*       _oop_closure;
 136   int               _cld_claim;
 137 
 138  public:
 139   CLDToOopClosure(OopClosure* oop_closure,
 140                   int cld_claim) :
 141       _oop_closure(oop_closure),
 142       _cld_claim(cld_claim) {}
 143 
 144   void do_cld(ClassLoaderData* cld);
 145 };
 146 
 147 // The base class for all concurrent marking closures,
 148 // that participates in class unloading.
 149 // It's used to proxy through the metadata to the oops defined in them.
 150 class MetadataVisitingOopIterateClosure: public OopIterateClosure {
 151  public:
 152   MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) { }
 153 
 154   virtual bool do_metadata() { return true; }
 155   virtual void do_klass(Klass* k);
 156   virtual void do_cld(ClassLoaderData* cld);
 157 };
 158 
 159 // ObjectClosure is used for iterating through an object space
 160 
 161 class ObjectClosure : public Closure {
 162  public:
 163   // Called for each object.
 164   virtual void do_object(oop obj) = 0;
 165 };
 166 
 167 
 168 class BoolObjectClosure : public Closure {
 169  public:
 170   virtual bool do_object_b(oop obj) = 0;
 171 };
 172 
 173 class AlwaysTrueClosure: public BoolObjectClosure {
 174  public:
 175   bool do_object_b(oop p) { return true; }
 176 };
 177 
 178 class AlwaysFalseClosure : public BoolObjectClosure {
 179  public:
 180   bool do_object_b(oop p) { return false; }
 181 };
 182 
 183 // Applies an oop closure to all ref fields in objects iterated over in an
 184 // object iteration.
 185 class ObjectToOopClosure: public ObjectClosure {
 186   OopIterateClosure* _cl;
 187 public:
 188   void do_object(oop obj);
 189   ObjectToOopClosure(OopIterateClosure* cl) : _cl(cl) {}
 190 };
 191 
 192 // A version of ObjectClosure that is expected to be robust
 193 // in the face of possibly uninitialized objects.
 194 class ObjectClosureCareful : public ObjectClosure {
 195  public:
 196   virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0;
 197   virtual size_t do_object_careful(oop p) = 0;
 198 };
 199 
 200 // The following are used in CompactibleFreeListSpace and
 201 // ConcurrentMarkSweepGeneration.
 202 
 203 // Blk closure (abstract class)
 204 class BlkClosure : public StackObj {
 205  public:
 206   virtual size_t do_blk(HeapWord* addr) = 0;
 207 };
 208 
 209 // A version of BlkClosure that is expected to be robust
 210 // in the face of possibly uninitialized objects.
 211 class BlkClosureCareful : public BlkClosure {
 212  public:
 213   size_t do_blk(HeapWord* addr) {
 214     guarantee(false, "call do_blk_careful instead");
 215     return 0;
 216   }
 217   virtual size_t do_blk_careful(HeapWord* addr) = 0;
 218 };
 219 
 220 // SpaceClosure is used for iterating over spaces
 221 
 222 class Space;
 223 class CompactibleSpace;
 224 
 225 class SpaceClosure : public StackObj {
 226  public:
 227   // Called for each space
 228   virtual void do_space(Space* s) = 0;
 229 };
 230 
 231 class CompactibleSpaceClosure : public StackObj {
 232  public:
 233   // Called for each compactible space
 234   virtual void do_space(CompactibleSpace* s) = 0;
 235 };
 236 
 237 
 238 // CodeBlobClosure is used for iterating through code blobs
 239 // in the code cache or on thread stacks
 240 
 241 class CodeBlobClosure : public Closure {
 242  public:
 243   // Called for each code blob.
 244   virtual void do_code_blob(CodeBlob* cb) = 0;
 245 };
 246 
 247 // Applies an oop closure to all ref fields in code blobs
 248 // iterated over in an object iteration.
 249 class CodeBlobToOopClosure : public CodeBlobClosure {
 250   OopClosure* _cl;
 251   bool _fix_relocations;
 252  protected:
 253   void do_nmethod(nmethod* nm);
 254  public:
 255   // If fix_relocations(), then cl must copy objects to their new location immediately to avoid
 256   // patching nmethods with the old locations.
 257   CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}
 258   virtual void do_code_blob(CodeBlob* cb);
 259 
 260   bool fix_relocations() const { return _fix_relocations; }
 261   const static bool FixRelocations = true;
 262 };
 263 
 264 class MarkingCodeBlobClosure : public CodeBlobToOopClosure {
 265  public:
 266   MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {}
 267   // Called for each code blob, but at most once per unique blob.
 268 
 269   virtual void do_code_blob(CodeBlob* cb);
 270 };
 271 
 272 class NMethodClosure : public Closure {
 273  public:
 274   virtual void do_nmethod(nmethod* n) = 0;
 275 };
 276 
 277 // MonitorClosure is used for iterating over monitors in the monitors cache
 278 
 279 class ObjectMonitor;
 280 
 281 class MonitorClosure : public StackObj {
 282  public:
 283   // called for each monitor in cache
 284   virtual void do_monitor(ObjectMonitor* m) = 0;
 285 };
 286 
 287 // A closure that is applied without any arguments.
 288 class VoidClosure : public StackObj {
 289  public:
 290   // I would have liked to declare this a pure virtual, but that breaks
 291   // in mysterious ways, for unknown reasons.
 292   virtual void do_void();
 293 };
 294 
 295 
 296 // YieldClosure is intended for use by iteration loops
 297 // to incrementalize their work, allowing interleaving
 298 // of an interruptable task so as to allow other
 299 // threads to run (which may not otherwise be able to access
 300 // exclusive resources, for instance). Additionally, the
 301 // closure also allows for aborting an ongoing iteration
 302 // by means of checking the return value from the polling
 303 // call.
 304 class YieldClosure : public StackObj {
 305 public:
 306  virtual bool should_return() = 0;
 307 
 308  // Yield on a fine-grain level. The check in case of not yielding should be very fast.
 309  virtual bool should_return_fine_grain() { return false; }
 310 };
 311 
 312 // Abstract closure for serializing data (read or write).
 313 
 314 class SerializeClosure : public Closure {
 315 public:
 316   // Return bool indicating whether closure implements read or write.
 317   virtual bool reading() const = 0;
 318 
 319   // Read/write the void pointer pointed to by p.
 320   virtual void do_ptr(void** p) = 0;
 321 
 322   // Read/write the 32-bit unsigned integer pointed to by p.
 323   virtual void do_u4(u4* p) = 0;
 324 
 325   // Read/write the bool pointed to by p.
 326   virtual void do_bool(bool* p) = 0;
 327 
 328   // Read/write the region specified.
 329   virtual void do_region(u_char* start, size_t size) = 0;
 330 
 331   // Check/write the tag.  If reading, then compare the tag against
 332   // the passed in value and fail is they don't match.  This allows
 333   // for verification that sections of the serialized data are of the
 334   // correct length.
 335   virtual void do_tag(int tag) = 0;
 336 
 337   // Read/write the oop
 338   virtual void do_oop(oop* o) = 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 // Dispatches to the non-virtual functions if OopClosureType has
 362 // a concrete implementation, otherwise a virtual call is taken.
 363 class Devirtualizer {
 364  public:
 365   template <typename OopClosureType, typename T> static void do_oop_no_verify(OopClosureType* closure, T* p);
 366   template <typename OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);
 367   template <typename OopClosureType>             static void do_klass(OopClosureType* closure, Klass* k);
 368   template <typename OopClosureType>             static void do_cld(OopClosureType* closure, ClassLoaderData* cld);
 369   template <typename OopClosureType>             static bool do_metadata(OopClosureType* closure);
 370 };
 371 
 372 class OopIteratorClosureDispatch {
 373  public:
 374   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass);
 375   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass, MemRegion mr);
 376   template <typename OopClosureType> static void oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* klass);
 377 };
 378 
 379 #endif // SHARE_MEMORY_ITERATOR_HPP