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 class ClaimMetadataVisitingOopIterateClosure : public OopIterateClosure {
 148  protected:
 149   const int _claim;
 150 
 151  public:
 152   ClaimMetadataVisitingOopIterateClosure(int claim, ReferenceDiscoverer* rd = NULL) :
 153       OopIterateClosure(rd),
 154       _claim(claim) { }
 155 
 156   virtual bool do_metadata() { return true; }
 157   virtual void do_klass(Klass* k);
 158   virtual 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 MetadataVisitingOopIterateClosure: public ClaimMetadataVisitingOopIterateClosure {
 165  public:
 166   MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd = NULL);
 167 };
 168 
 169 // ObjectClosure is used for iterating through an object space
 170 
 171 class ObjectClosure : public Closure {
 172  public:
 173   // Called for each object.
 174   virtual void do_object(oop obj) = 0;
 175 };
 176 
 177 
 178 class BoolObjectClosure : public Closure {
 179  public:
 180   virtual bool do_object_b(oop obj) = 0;
 181 };
 182 
 183 class AlwaysTrueClosure: public BoolObjectClosure {
 184  public:
 185   bool do_object_b(oop p) { return true; }
 186 };
 187 
 188 class AlwaysFalseClosure : public BoolObjectClosure {
 189  public:
 190   bool do_object_b(oop p) { return false; }
 191 };
 192 
 193 // Applies an oop closure to all ref fields in objects iterated over in an
 194 // object iteration.
 195 class ObjectToOopClosure: public ObjectClosure {
 196   OopIterateClosure* _cl;
 197 public:
 198   void do_object(oop obj);
 199   ObjectToOopClosure(OopIterateClosure* cl) : _cl(cl) {}
 200 };
 201 
 202 // SpaceClosure is used for iterating over spaces
 203 
 204 class Space;
 205 class CompactibleSpace;
 206 
 207 class SpaceClosure : public StackObj {
 208  public:
 209   // Called for each space
 210   virtual void do_space(Space* s) = 0;
 211 };
 212 
 213 class CompactibleSpaceClosure : public StackObj {
 214  public:
 215   // Called for each compactible space
 216   virtual void do_space(CompactibleSpace* s) = 0;
 217 };
 218 
 219 
 220 // CodeBlobClosure is used for iterating through code blobs
 221 // in the code cache or on thread stacks
 222 
 223 class CodeBlobClosure : public Closure {
 224  public:
 225   // Called for each code blob.
 226   virtual void do_code_blob(CodeBlob* cb) = 0;
 227 };
 228 
 229 // Applies an oop closure to all ref fields in code blobs
 230 // iterated over in an object iteration.
 231 class CodeBlobToOopClosure : public CodeBlobClosure {
 232   OopClosure* _cl;
 233   bool _fix_relocations;
 234  protected:
 235   void do_nmethod(nmethod* nm);
 236  public:
 237   // If fix_relocations(), then cl must copy objects to their new location immediately to avoid
 238   // patching nmethods with the old locations.
 239   CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}
 240   virtual void do_code_blob(CodeBlob* cb);
 241 
 242   bool fix_relocations() const { return _fix_relocations; }
 243   const static bool FixRelocations = true;
 244 };
 245 
 246 class MarkingCodeBlobClosure : public CodeBlobToOopClosure {
 247  public:
 248   MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {}
 249   // Called for each code blob, but at most once per unique blob.
 250 
 251   virtual void do_code_blob(CodeBlob* cb);
 252 };
 253 
 254 class NMethodClosure : public Closure {
 255  public:
 256   virtual void do_nmethod(nmethod* n) = 0;
 257 };
 258 
 259 // MonitorClosure is used for iterating over monitors in the monitors cache
 260 
 261 class ObjectMonitor;
 262 
 263 class MonitorClosure : public StackObj {
 264  public:
 265   // called for each monitor in cache
 266   virtual void do_monitor(ObjectMonitor* m) = 0;
 267 };
 268 
 269 // A closure that is applied without any arguments.
 270 class VoidClosure : public StackObj {
 271  public:
 272   // I would have liked to declare this a pure virtual, but that breaks
 273   // in mysterious ways, for unknown reasons.
 274   virtual void do_void();
 275 };
 276 
 277 
 278 // YieldClosure is intended for use by iteration loops
 279 // to incrementalize their work, allowing interleaving
 280 // of an interruptable task so as to allow other
 281 // threads to run (which may not otherwise be able to access
 282 // exclusive resources, for instance). Additionally, the
 283 // closure also allows for aborting an ongoing iteration
 284 // by means of checking the return value from the polling
 285 // call.
 286 class YieldClosure : public StackObj {
 287 public:
 288  virtual bool should_return() = 0;
 289 
 290  // Yield on a fine-grain level. The check in case of not yielding should be very fast.
 291  virtual bool should_return_fine_grain() { return false; }
 292 };
 293 
 294 // Abstract closure for serializing data (read or write).
 295 
 296 class SerializeClosure : public Closure {
 297 public:
 298   // Return bool indicating whether closure implements read or write.
 299   virtual bool reading() const = 0;
 300 
 301   // Read/write the void pointer pointed to by p.
 302   virtual void do_ptr(void** p) = 0;
 303 
 304   // Read/write the 32-bit unsigned integer pointed to by p.
 305   virtual void do_u4(u4* p) = 0;
 306 
 307   // Read/write the bool pointed to by p.
 308   virtual void do_bool(bool* p) = 0;
 309 
 310   // Read/write the region specified.
 311   virtual void do_region(u_char* start, size_t size) = 0;
 312 
 313   // Check/write the tag.  If reading, then compare the tag against
 314   // the passed in value and fail is they don't match.  This allows
 315   // for verification that sections of the serialized data are of the
 316   // correct length.
 317   virtual void do_tag(int tag) = 0;
 318 
 319   // Read/write the oop
 320   virtual void do_oop(oop* o) = 0;
 321 
 322   bool writing() {
 323     return !reading();
 324   }
 325 };
 326 
 327 class SymbolClosure : public StackObj {
 328  public:
 329   virtual void do_symbol(Symbol**) = 0;
 330 
 331   // Clear LSB in symbol address; it can be set by CPSlot.
 332   static Symbol* load_symbol(Symbol** p) {
 333     return (Symbol*)(intptr_t(*p) & ~1);
 334   }
 335 
 336   // Store symbol, adjusting new pointer if the original pointer was adjusted
 337   // (symbol references in constant pool slots have their LSB set to 1).
 338   static void store_symbol(Symbol** p, Symbol* sym) {
 339     *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1));
 340   }
 341 };
 342 
 343 // Dispatches to the non-virtual functions if OopClosureType has
 344 // a concrete implementation, otherwise a virtual call is taken.
 345 class Devirtualizer {
 346  public:
 347   template <typename OopClosureType, typename T> static void do_oop_no_verify(OopClosureType* closure, T* p);
 348   template <typename OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);
 349   template <typename OopClosureType>             static void do_klass(OopClosureType* closure, Klass* k);
 350   template <typename OopClosureType>             static void do_cld(OopClosureType* closure, ClassLoaderData* cld);
 351   template <typename OopClosureType>             static bool do_metadata(OopClosureType* closure);
 352 };
 353 
 354 class OopIteratorClosureDispatch {
 355  public:
 356   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass);
 357   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass, MemRegion mr);
 358   template <typename OopClosureType> static void oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* klass);
 359 };
 360 
 361 #endif // SHARE_MEMORY_ITERATOR_HPP