1 /* 2 * Copyright (c) 1997, 2021, 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 class Thread; 41 42 // The following classes are C++ `closures` for iterating over objects, roots and spaces 43 44 class Closure : public StackObj { }; 45 46 // Thread iterator 47 class ThreadClosure { 48 public: 49 virtual void do_thread(Thread* thread) = 0; 50 }; 51 52 // OopClosure is used for iterating through references to Java objects. 53 class OopClosure : public Closure { 54 public: 55 virtual void do_oop(oop* o) = 0; 56 virtual void do_oop(narrowOop* o) = 0; 57 virtual void do_oop_no_buffering(oop* o) { do_oop(o); } 58 virtual void do_oop_no_buffering(narrowOop* o) { do_oop(o); } 59 }; 60 61 class DoNothingClosure : public OopClosure { 62 public: 63 virtual void do_oop(oop* p) {} 64 virtual void do_oop(narrowOop* p) {} 65 }; 66 extern DoNothingClosure do_nothing_cl; 67 68 // OopIterateClosure adds extra code to be run during oop iterations. 69 // This is needed by the GC and is extracted to a separate type to not 70 // pollute the OopClosure interface. 71 class OopIterateClosure : public OopClosure { 72 private: 73 ReferenceDiscoverer* _ref_discoverer; 74 75 protected: 76 OopIterateClosure(ReferenceDiscoverer* rd) : _ref_discoverer(rd) { } 77 OopIterateClosure() : _ref_discoverer(NULL) { } 78 ~OopIterateClosure() { } 79 80 void set_ref_discoverer_internal(ReferenceDiscoverer* rd) { _ref_discoverer = rd; } 81 82 public: 83 ReferenceDiscoverer* ref_discoverer() const { return _ref_discoverer; } 84 85 // Iteration of InstanceRefKlasses differ depending on the closure, 86 // the below enum describes the different alternatives. 87 enum ReferenceIterationMode { 88 DO_DISCOVERY, // Apply closure and discover references 89 DO_DISCOVERED_AND_DISCOVERY, // Apply closure to discovered field and do discovery 90 DO_FIELDS, // Apply closure to all fields 91 DO_FIELDS_EXCEPT_REFERENT // Apply closure to all fields except the referent field 92 }; 93 94 // The default iteration mode is to do discovery. 95 virtual ReferenceIterationMode reference_iteration_mode() { return DO_DISCOVERY; } 96 97 // If the do_metadata functions return "true", 98 // we invoke the following when running oop_iterate(): 99 // 100 // 1) do_klass on the header klass pointer. 101 // 2) do_klass on the klass pointer in the mirrors. 102 // 3) do_cld on the class loader data in class loaders. 103 104 virtual bool do_metadata() = 0; 105 virtual void do_klass(Klass* k) = 0; 106 virtual void do_cld(ClassLoaderData* cld) = 0; 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 class MetadataClosure : public Closure { 135 public: 136 virtual void do_metadata(Metadata* md) = 0; 137 }; 138 139 140 class CLDToOopClosure : public CLDClosure { 141 OopClosure* _oop_closure; 142 int _cld_claim; 143 144 public: 145 CLDToOopClosure(OopClosure* oop_closure, 146 int cld_claim) : 147 _oop_closure(oop_closure), 148 _cld_claim(cld_claim) {} 149 150 void do_cld(ClassLoaderData* cld); 151 }; 152 153 template <int claim> 154 class ClaimingCLDToOopClosure : public CLDToOopClosure { 155 public: 156 ClaimingCLDToOopClosure(OopClosure* cl) : CLDToOopClosure(cl, claim) {} 157 }; 158 159 class ClaimMetadataVisitingOopIterateClosure : public OopIterateClosure { 160 protected: 161 const int _claim; 162 163 public: 164 ClaimMetadataVisitingOopIterateClosure(int claim, ReferenceDiscoverer* rd = NULL) : 165 OopIterateClosure(rd), 166 _claim(claim) { } 167 168 virtual bool do_metadata() { return true; } 169 virtual void do_klass(Klass* k); 170 virtual void do_cld(ClassLoaderData* cld); 171 }; 172 173 // The base class for all concurrent marking closures, 174 // that participates in class unloading. 175 // It's used to proxy through the metadata to the oops defined in them. 176 class MetadataVisitingOopIterateClosure: public ClaimMetadataVisitingOopIterateClosure { 177 public: 178 MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd = NULL); 179 }; 180 181 // ObjectClosure is used for iterating through an object space 182 183 class ObjectClosure : public Closure { 184 public: 185 // Called for each object. 186 virtual void do_object(oop obj) = 0; 187 }; 188 189 190 class BoolObjectClosure : public Closure { 191 public: 192 virtual bool do_object_b(oop obj) = 0; 193 }; 194 195 class AlwaysTrueClosure: public BoolObjectClosure { 196 public: 197 bool do_object_b(oop p) { return true; } 198 }; 199 200 class AlwaysFalseClosure : public BoolObjectClosure { 201 public: 202 bool do_object_b(oop p) { return false; } 203 }; 204 205 // Applies an oop closure to all ref fields in objects iterated over in an 206 // object iteration. 207 class ObjectToOopClosure: public ObjectClosure { 208 OopIterateClosure* _cl; 209 public: 210 void do_object(oop obj); 211 ObjectToOopClosure(OopIterateClosure* cl) : _cl(cl) {} 212 }; 213 214 // SpaceClosure is used for iterating over spaces 215 216 class Space; 217 class CompactibleSpace; 218 219 class SpaceClosure : public StackObj { 220 public: 221 // Called for each space 222 virtual void do_space(Space* s) = 0; 223 }; 224 225 class CompactibleSpaceClosure : public StackObj { 226 public: 227 // Called for each compactible space 228 virtual void do_space(CompactibleSpace* s) = 0; 229 }; 230 231 232 // CodeBlobClosure is used for iterating through code blobs 233 // in the code cache or on thread stacks 234 235 class CodeBlobClosure : public Closure { 236 public: 237 // Called for each code blob. 238 virtual void do_code_blob(CodeBlob* cb) = 0; 239 }; 240 241 // Applies an oop closure to all ref fields in code blobs 242 // iterated over in an object iteration. 243 class CodeBlobToOopClosure : public CodeBlobClosure { 244 OopClosure* _cl; 245 bool _fix_relocations; 246 protected: 247 void do_nmethod(nmethod* nm); 248 public: 249 // If fix_relocations(), then cl must copy objects to their new location immediately to avoid 250 // patching nmethods with the old locations. 251 CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {} 252 virtual void do_code_blob(CodeBlob* cb); 253 254 bool fix_relocations() const { return _fix_relocations; } 255 const static bool FixRelocations = true; 256 }; 257 258 class MarkingCodeBlobClosure : public CodeBlobToOopClosure { 259 public: 260 MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {} 261 // Called for each code blob, but at most once per unique blob. 262 263 virtual void do_code_blob(CodeBlob* cb); 264 }; 265 266 class NMethodClosure : public Closure { 267 public: 268 virtual void do_nmethod(nmethod* n) = 0; 269 }; 270 271 class CodeBlobToNMethodClosure : public CodeBlobClosure { 272 NMethodClosure* const _nm_cl; 273 274 public: 275 CodeBlobToNMethodClosure(NMethodClosure* nm_cl) : _nm_cl(nm_cl) {} 276 277 virtual void do_code_blob(CodeBlob* cb); 278 }; 279 280 // MonitorClosure is used for iterating over monitors in the monitors cache 281 282 class ObjectMonitor; 283 284 class MonitorClosure : public StackObj { 285 public: 286 // called for each monitor in cache 287 virtual void do_monitor(ObjectMonitor* m) = 0; 288 }; 289 290 // A closure that is applied without any arguments. 291 class VoidClosure : public StackObj { 292 public: 293 virtual void do_void() = 0; 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 bool pointed to by p. 327 virtual void do_bool(bool* p) = 0; 328 329 // Read/write the region specified. 330 virtual void do_region(u_char* start, size_t size) = 0; 331 332 // Check/write the tag. If reading, then compare the tag against 333 // the passed in value and fail is they don't match. This allows 334 // for verification that sections of the serialized data are of the 335 // correct length. 336 virtual void do_tag(int tag) = 0; 337 338 // Read/write the oop 339 virtual void do_oop(oop* o) = 0; 340 341 bool writing() { 342 return !reading(); 343 } 344 }; 345 346 class SymbolClosure : public StackObj { 347 public: 348 virtual void do_symbol(Symbol**) = 0; 349 }; 350 351 template <typename E> 352 class CompareClosure : public Closure { 353 public: 354 virtual int do_compare(const E&, const E&) = 0; 355 }; 356 357 // Dispatches to the non-virtual functions if OopClosureType has 358 // a concrete implementation, otherwise a virtual call is taken. 359 class Devirtualizer { 360 public: 361 template <typename OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p); 362 template <typename OopClosureType> static void do_klass(OopClosureType* closure, Klass* k); 363 template <typename OopClosureType> static void do_cld(OopClosureType* closure, ClassLoaderData* cld); 364 template <typename OopClosureType> static bool do_metadata(OopClosureType* closure); 365 }; 366 367 class OopIteratorClosureDispatch { 368 public: 369 template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass); 370 template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass, MemRegion mr); 371 template <typename OopClosureType> static void oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* klass); 372 }; 373 374 #endif // SHARE_MEMORY_ITERATOR_HPP