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