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