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