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 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 class DoNothingClosure : public OopClosure { 54 public: 55 virtual void do_oop(oop* p) {} 56 virtual void do_oop(narrowOop* p) {} 57 }; 58 extern DoNothingClosure do_nothing_cl; 59 60 // OopIterateClosure adds extra code to be run during oop iterations. 61 // This is needed by the GC and is extracted to a separate type to not 62 // pollute the OopClosure interface. 63 class OopIterateClosure : public OopClosure { 64 private: 65 ReferenceDiscoverer* _ref_discoverer; 66 67 protected: 68 OopIterateClosure(ReferenceDiscoverer* rd) : _ref_discoverer(rd) { } 69 OopIterateClosure() : _ref_discoverer(NULL) { } 70 ~OopIterateClosure() { } 71 72 void set_ref_discoverer_internal(ReferenceDiscoverer* rd) { _ref_discoverer = rd; } 73 74 public: 75 ReferenceDiscoverer* ref_discoverer() const { return _ref_discoverer; } 76 77 // Iteration of InstanceRefKlasses differ depending on the closure, 78 // the below enum describes the different alternatives. 79 enum ReferenceIterationMode { 80 DO_DISCOVERY, // Apply closure and discover references 81 DO_DISCOVERED_AND_DISCOVERY, // Apply closure to discovered field and do discovery 82 DO_FIELDS, // Apply closure to all fields 83 DO_FIELDS_EXCEPT_REFERENT // Apply closure to all fields except the referent field 84 }; 85 86 // The default iteration mode is to do discovery. 87 virtual ReferenceIterationMode reference_iteration_mode() { return DO_DISCOVERY; } 88 89 // If the do_metadata functions return "true", 90 // we invoke the following when running oop_iterate(): 91 // 92 // 1) do_klass on the header klass pointer. 93 // 2) do_klass on the klass pointer in the mirrors. 94 // 3) do_cld on the class loader data in class loaders. 95 96 virtual bool do_metadata() = 0; 97 virtual void do_klass(Klass* k) = 0; 98 virtual void do_cld(ClassLoaderData* cld) = 0; 99 100 #ifdef ASSERT 101 // Default verification of each visited oop field. 102 template <typename T> void verify(T* p); 103 104 // Can be used by subclasses to turn off the default verification of oop fields. 105 virtual bool should_verify_oops() { return true; } 106 #endif 107 }; 108 109 // An OopIterateClosure that can be used when there's no need to visit the Metadata. 110 class BasicOopIterateClosure : public OopIterateClosure { 111 public: 112 BasicOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) {} 113 114 virtual bool do_metadata() { return false; } 115 virtual void do_klass(Klass* k) { ShouldNotReachHere(); } 116 virtual void do_cld(ClassLoaderData* cld) { ShouldNotReachHere(); } 117 }; 118 119 class BufferedValueClosure : public Closure { 120 public: 121 virtual void do_buffered_value(oop* p) = 0; 122 }; 123 124 class KlassClosure : public Closure { 125 public: 126 virtual void do_klass(Klass* k) = 0; 127 }; 128 129 class CLDClosure : public Closure { 130 public: 131 virtual void do_cld(ClassLoaderData* cld) = 0; 132 }; 133 134 135 class CLDToOopClosure : public CLDClosure { 136 OopClosure* _oop_closure; 137 int _cld_claim; 138 139 public: 140 CLDToOopClosure(OopClosure* oop_closure, 141 int cld_claim) : 142 _oop_closure(oop_closure), 143 _cld_claim(cld_claim) {} 144 145 void do_cld(ClassLoaderData* cld); 146 }; 147 148 // The base class for all concurrent marking closures, 149 // that participates in class unloading. 150 // It's used to proxy through the metadata to the oops defined in them. 151 class MetadataVisitingOopIterateClosure: public OopIterateClosure { 152 public: 153 MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) { } 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 // ObjectClosure is used for iterating through an object space 161 162 class ObjectClosure : public Closure { 163 public: 164 // Called for each object. 165 virtual void do_object(oop obj) = 0; 166 }; 167 168 169 class BoolObjectClosure : public Closure { 170 public: 171 virtual bool do_object_b(oop obj) = 0; 172 }; 173 174 class AlwaysTrueClosure: public BoolObjectClosure { 175 public: 176 bool do_object_b(oop p) { return true; } 177 }; 178 179 class AlwaysFalseClosure : public BoolObjectClosure { 180 public: 181 bool do_object_b(oop p) { return false; } 182 }; 183 184 // Applies an oop closure to all ref fields in objects iterated over in an 185 // object iteration. 186 class ObjectToOopClosure: public ObjectClosure { 187 OopIterateClosure* _cl; 188 public: 189 void do_object(oop obj); 190 ObjectToOopClosure(OopIterateClosure* cl) : _cl(cl) {} 191 }; 192 193 // A version of ObjectClosure that is expected to be robust 194 // in the face of possibly uninitialized objects. 195 class ObjectClosureCareful : public ObjectClosure { 196 public: 197 virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0; 198 virtual size_t do_object_careful(oop p) = 0; 199 }; 200 201 // The following are used in CompactibleFreeListSpace and 202 // ConcurrentMarkSweepGeneration. 203 204 // Blk closure (abstract class) 205 class BlkClosure : public StackObj { 206 public: 207 virtual size_t do_blk(HeapWord* addr) = 0; 208 }; 209 210 // A version of BlkClosure that is expected to be robust 211 // in the face of possibly uninitialized objects. 212 class BlkClosureCareful : public BlkClosure { 213 public: 214 size_t do_blk(HeapWord* addr) { 215 guarantee(false, "call do_blk_careful instead"); 216 return 0; 217 } 218 virtual size_t do_blk_careful(HeapWord* addr) = 0; 219 }; 220 221 // SpaceClosure is used for iterating over spaces 222 223 class Space; 224 class CompactibleSpace; 225 226 class SpaceClosure : public StackObj { 227 public: 228 // Called for each space 229 virtual void do_space(Space* s) = 0; 230 }; 231 232 class CompactibleSpaceClosure : public StackObj { 233 public: 234 // Called for each compactible space 235 virtual void do_space(CompactibleSpace* s) = 0; 236 }; 237 238 239 // CodeBlobClosure is used for iterating through code blobs 240 // in the code cache or on thread stacks 241 242 class CodeBlobClosure : public Closure { 243 public: 244 // Called for each code blob. 245 virtual void do_code_blob(CodeBlob* cb) = 0; 246 }; 247 248 // Applies an oop closure to all ref fields in code blobs 249 // iterated over in an object iteration. 250 class CodeBlobToOopClosure : public CodeBlobClosure { 251 OopClosure* _cl; 252 bool _fix_relocations; 253 protected: 254 void do_nmethod(nmethod* nm); 255 public: 256 // If fix_relocations(), then cl must copy objects to their new location immediately to avoid 257 // patching nmethods with the old locations. 258 CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {} 259 virtual void do_code_blob(CodeBlob* cb); 260 261 bool fix_relocations() const { return _fix_relocations; } 262 const static bool FixRelocations = true; 263 }; 264 265 class MarkingCodeBlobClosure : public CodeBlobToOopClosure { 266 public: 267 MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {} 268 // Called for each code blob, but at most once per unique blob. 269 270 virtual void do_code_blob(CodeBlob* cb); 271 }; 272 273 class NMethodClosure : public Closure { 274 public: 275 virtual void do_nmethod(nmethod* n) = 0; 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 OopIteratorClosureDispatch { 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_MEMORY_ITERATOR_HPP