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 ReferenceDiscoverer;
  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 // OopIterateClosure 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 OopIterateClosure : public OopClosure {
  62  private:
  63   ReferenceDiscoverer* _ref_discoverer;
  64 
  65  protected:
  66   OopIterateClosure(ReferenceDiscoverer* rd) : _ref_discoverer(rd) { }
  67   OopIterateClosure() : _ref_discoverer(NULL) { }
  68   ~OopIterateClosure() { }
  69 
  70   void set_ref_discoverer_internal(ReferenceDiscoverer* rd) { _ref_discoverer = rd; }
  71 
  72  public:
  73   ReferenceDiscoverer* ref_discoverer() const { return _ref_discoverer; }
  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   virtual bool do_metadata() = 0;
  94   virtual void do_klass(Klass* k) = 0;
  95   virtual void do_cld(ClassLoaderData* cld) = 0;
  96 
  97   // True iff this closure may be safely applied more than once to an oop
  98   // location without an intervening "major reset" (like the end of a GC).
  99   virtual bool idempotent() { return false; }
 100 
 101 #ifdef ASSERT
 102   // Default verification of each visited oop field.
 103   template <typename T> void verify(T* p);
 104 
 105   // Can be used by subclasses to turn off the default verification of oop fields.
 106   virtual bool should_verify_oops() { return true; }
 107 #endif
 108 };
 109 
 110 // An OopIterateClosure that can be used when there's no need to visit the Metadata.
 111 class BasicOopIterateClosure : public OopIterateClosure {
 112 public:
 113   BasicOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) {}
 114 
 115   virtual bool do_metadata() { return false; }
 116   virtual void do_klass(Klass* k) { ShouldNotReachHere(); }
 117   virtual void do_cld(ClassLoaderData* cld) { ShouldNotReachHere(); }
 118 };
 119 
 120 // Wrapper closure only used to implement oop_iterate_no_header().
 121 class NoHeaderExtendedOopClosure : public BasicOopIterateClosure {
 122   OopClosure* _wrapped_closure;
 123  public:
 124   NoHeaderExtendedOopClosure(OopClosure* cl) : _wrapped_closure(cl) {}
 125   // Warning: this calls the virtual version do_oop in the the wrapped closure.
 126   virtual void do_oop(oop* p)       { _wrapped_closure->do_oop(p); }
 127   virtual void do_oop(narrowOop* p) { _wrapped_closure->do_oop(p); }
 128 };
 129 
 130 class KlassClosure : public Closure {
 131  public:
 132   virtual void do_klass(Klass* k) = 0;
 133 };
 134 
 135 class CLDClosure : public Closure {
 136  public:
 137   virtual void do_cld(ClassLoaderData* cld) = 0;
 138 };
 139 
 140 
 141 class CLDToOopClosure : public CLDClosure {
 142   OopClosure*       _oop_closure;
 143   bool              _must_claim_cld;
 144 
 145  public:
 146   CLDToOopClosure(OopClosure* oop_closure, bool must_claim_cld = true) :
 147       _oop_closure(oop_closure),
 148       _must_claim_cld(must_claim_cld) {}
 149 
 150   void do_cld(ClassLoaderData* cld);
 151 };
 152 
 153 // The base class for all concurrent marking closures,
 154 // that participates in class unloading.
 155 // It's used to proxy through the metadata to the oops defined in them.
 156 class MetadataVisitingOopIterateClosure: public OopIterateClosure {
 157  public:
 158   MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) { }
 159 
 160   virtual bool do_metadata() { return true; }
 161   virtual void do_klass(Klass* k);
 162   virtual void do_cld(ClassLoaderData* cld);
 163 };
 164 
 165 // ObjectClosure is used for iterating through an object space
 166 
 167 class ObjectClosure : public Closure {
 168  public:
 169   // Called for each object.
 170   virtual void do_object(oop obj) = 0;
 171 };
 172 
 173 
 174 class BoolObjectClosure : public Closure {
 175  public:
 176   virtual bool do_object_b(oop obj) = 0;
 177 };
 178 
 179 class AlwaysTrueClosure: public BoolObjectClosure {
 180  public:
 181   bool do_object_b(oop p) { return true; }
 182 };
 183 
 184 class AlwaysFalseClosure : public BoolObjectClosure {
 185  public:
 186   bool do_object_b(oop p) { return false; }
 187 };
 188 
 189 // Applies an oop closure to all ref fields in objects iterated over in an
 190 // object iteration.
 191 class ObjectToOopClosure: public ObjectClosure {
 192   OopIterateClosure* _cl;
 193 public:
 194   void do_object(oop obj);
 195   ObjectToOopClosure(OopIterateClosure* cl) : _cl(cl) {}
 196 };
 197 
 198 // A version of ObjectClosure that is expected to be robust
 199 // in the face of possibly uninitialized objects.
 200 class ObjectClosureCareful : public ObjectClosure {
 201  public:
 202   virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0;
 203   virtual size_t do_object_careful(oop p) = 0;
 204 };
 205 
 206 // The following are used in CompactibleFreeListSpace and
 207 // ConcurrentMarkSweepGeneration.
 208 
 209 // Blk closure (abstract class)
 210 class BlkClosure : public StackObj {
 211  public:
 212   virtual size_t do_blk(HeapWord* addr) = 0;
 213 };
 214 
 215 // A version of BlkClosure that is expected to be robust
 216 // in the face of possibly uninitialized objects.
 217 class BlkClosureCareful : public BlkClosure {
 218  public:
 219   size_t do_blk(HeapWord* addr) {
 220     guarantee(false, "call do_blk_careful instead");
 221     return 0;
 222   }
 223   virtual size_t do_blk_careful(HeapWord* addr) = 0;
 224 };
 225 
 226 // SpaceClosure is used for iterating over spaces
 227 
 228 class Space;
 229 class CompactibleSpace;
 230 
 231 class SpaceClosure : public StackObj {
 232  public:
 233   // Called for each space
 234   virtual void do_space(Space* s) = 0;
 235 };
 236 
 237 class CompactibleSpaceClosure : public StackObj {
 238  public:
 239   // Called for each compactible space
 240   virtual void do_space(CompactibleSpace* s) = 0;
 241 };
 242 
 243 
 244 // CodeBlobClosure is used for iterating through code blobs
 245 // in the code cache or on thread stacks
 246 
 247 class CodeBlobClosure : public Closure {
 248  public:
 249   // Called for each code blob.
 250   virtual void do_code_blob(CodeBlob* cb) = 0;
 251 };
 252 
 253 // Applies an oop closure to all ref fields in code blobs
 254 // iterated over in an object iteration.
 255 class CodeBlobToOopClosure : public CodeBlobClosure {
 256   OopClosure* _cl;
 257   bool _fix_relocations;
 258  protected:
 259   void do_nmethod(nmethod* nm);
 260  public:
 261   // If fix_relocations(), then cl must copy objects to their new location immediately to avoid
 262   // patching nmethods with the old locations.
 263   CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}
 264   virtual void do_code_blob(CodeBlob* cb);
 265 
 266   bool fix_relocations() const { return _fix_relocations; }
 267   const static bool FixRelocations = true;
 268 };
 269 
 270 class MarkingCodeBlobClosure : public CodeBlobToOopClosure {
 271  public:
 272   MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {}
 273   // Called for each code blob, but at most once per unique blob.
 274 
 275   virtual void do_code_blob(CodeBlob* cb);
 276 };
 277 
 278 // MonitorClosure is used for iterating over monitors in the monitors cache
 279 
 280 class ObjectMonitor;
 281 
 282 class MonitorClosure : public StackObj {
 283  public:
 284   // called for each monitor in cache
 285   virtual void do_monitor(ObjectMonitor* m) = 0;
 286 };
 287 
 288 // A closure that is applied without any arguments.
 289 class VoidClosure : public StackObj {
 290  public:
 291   // I would have liked to declare this a pure virtual, but that breaks
 292   // in mysterious ways, for unknown reasons.
 293   virtual void do_void();
 294 };
 295 
 296 
 297 // YieldClosure is intended for use by iteration loops
 298 // to incrementalize their work, allowing interleaving
 299 // of an interruptable task so as to allow other
 300 // threads to run (which may not otherwise be able to access
 301 // exclusive resources, for instance). Additionally, the
 302 // closure also allows for aborting an ongoing iteration
 303 // by means of checking the return value from the polling
 304 // call.
 305 class YieldClosure : public StackObj {
 306 public:
 307  virtual bool should_return() = 0;
 308 
 309  // Yield on a fine-grain level. The check in case of not yielding should be very fast.
 310  virtual bool should_return_fine_grain() { return false; }
 311 };
 312 
 313 // Abstract closure for serializing data (read or write).
 314 
 315 class SerializeClosure : public Closure {
 316 public:
 317   // Return bool indicating whether closure implements read or write.
 318   virtual bool reading() const = 0;
 319 
 320   // Read/write the void pointer pointed to by p.
 321   virtual void do_ptr(void** p) = 0;
 322 
 323   // Read/write the 32-bit unsigned integer pointed to by p.
 324   virtual void do_u4(u4* p) = 0;
 325 
 326   // Read/write the region specified.
 327   virtual void do_region(u_char* start, size_t size) = 0;
 328 
 329   // Check/write the tag.  If reading, then compare the tag against
 330   // the passed in value and fail is they don't match.  This allows
 331   // for verification that sections of the serialized data are of the
 332   // correct length.
 333   virtual void do_tag(int tag) = 0;
 334 
 335   // Read/write the oop
 336   virtual void do_oop(oop* o) = 0;
 337 
 338   bool writing() {
 339     return !reading();
 340   }
 341 };
 342 
 343 class SymbolClosure : public StackObj {
 344  public:
 345   virtual void do_symbol(Symbol**) = 0;
 346 
 347   // Clear LSB in symbol address; it can be set by CPSlot.
 348   static Symbol* load_symbol(Symbol** p) {
 349     return (Symbol*)(intptr_t(*p) & ~1);
 350   }
 351 
 352   // Store symbol, adjusting new pointer if the original pointer was adjusted
 353   // (symbol references in constant pool slots have their LSB set to 1).
 354   static void store_symbol(Symbol** p, Symbol* sym) {
 355     *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1));
 356   }
 357 };
 358 
 359 // Dispatches to the non-virtual functions if OopClosureType has
 360 // a concrete implementation, otherwise a virtual call is taken.
 361 class Devirtualizer {
 362  public:
 363   template <typename OopClosureType, typename T> static void do_oop_no_verify(OopClosureType* closure, T* p);
 364   template <typename OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);
 365   template <typename OopClosureType>             static void do_klass(OopClosureType* closure, Klass* k);
 366   template <typename OopClosureType>             static void do_cld(OopClosureType* closure, ClassLoaderData* cld);
 367   template <typename OopClosureType>             static bool do_metadata(OopClosureType* closure);
 368 };
 369 
 370 class OopClosureDispatch {
 371  public:
 372   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass);
 373   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass, MemRegion mr);
 374   template <typename OopClosureType> static void oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* klass);
 375 };
 376 
 377 #endif // SHARE_VM_MEMORY_ITERATOR_HPP