src/share/vm/memory/iterator.hpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6992
2c6ef90f030a
parent 6876
710a3c8b516e
child 9703
2fdf635bcf28
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_MEMORY_ITERATOR_HPP
aoqi@0 26 #define SHARE_VM_MEMORY_ITERATOR_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "memory/memRegion.hpp"
aoqi@0 30 #include "runtime/prefetch.hpp"
aoqi@0 31 #include "utilities/top.hpp"
aoqi@0 32
aoqi@0 33 // The following classes are C++ `closures` for iterating over objects, roots and spaces
aoqi@0 34
aoqi@0 35 class CodeBlob;
aoqi@0 36 class nmethod;
aoqi@0 37 class ReferenceProcessor;
aoqi@0 38 class DataLayout;
aoqi@0 39 class KlassClosure;
aoqi@0 40 class ClassLoaderData;
aoqi@0 41
aoqi@0 42 // Closure provides abortability.
aoqi@0 43
aoqi@0 44 class Closure : public StackObj {
aoqi@0 45 protected:
aoqi@0 46 bool _abort;
aoqi@0 47 void set_abort() { _abort = true; }
aoqi@0 48 public:
aoqi@0 49 Closure() : _abort(false) {}
aoqi@0 50 // A subtype can use this mechanism to indicate to some iterator mapping
aoqi@0 51 // functions that the iteration should cease.
aoqi@0 52 bool abort() { return _abort; }
aoqi@0 53 void clear_abort() { _abort = false; }
aoqi@0 54 };
aoqi@0 55
aoqi@0 56 // OopClosure is used for iterating through references to Java objects.
aoqi@0 57
aoqi@0 58 class OopClosure : public Closure {
aoqi@0 59 public:
aoqi@0 60 virtual void do_oop(oop* o) = 0;
aoqi@0 61 virtual void do_oop_v(oop* o) { do_oop(o); }
aoqi@0 62 virtual void do_oop(narrowOop* o) = 0;
aoqi@0 63 virtual void do_oop_v(narrowOop* o) { do_oop(o); }
aoqi@0 64 };
aoqi@0 65
aoqi@0 66 // ExtendedOopClosure adds extra code to be run during oop iterations.
aoqi@0 67 // This is needed by the GC and is extracted to a separate type to not
aoqi@0 68 // pollute the OopClosure interface.
aoqi@0 69 class ExtendedOopClosure : public OopClosure {
aoqi@0 70 public:
aoqi@0 71 ReferenceProcessor* _ref_processor;
aoqi@0 72 ExtendedOopClosure(ReferenceProcessor* rp) : _ref_processor(rp) { }
aoqi@0 73 ExtendedOopClosure() : OopClosure(), _ref_processor(NULL) { }
aoqi@0 74
aoqi@0 75 // If the do_metadata functions return "true",
aoqi@0 76 // we invoke the following when running oop_iterate():
aoqi@0 77 //
aoqi@0 78 // 1) do_klass on the header klass pointer.
aoqi@0 79 // 2) do_klass on the klass pointer in the mirrors.
aoqi@0 80 // 3) do_class_loader_data on the class loader data in class loaders.
aoqi@0 81 //
aoqi@0 82 // The virtual (without suffix) and the non-virtual (with _nv suffix) need
aoqi@0 83 // to be updated together, or else the devirtualization will break.
aoqi@0 84 //
aoqi@0 85 // Providing default implementations of the _nv functions unfortunately
aoqi@0 86 // removes the compile-time safeness, but reduces the clutter for the
stefank@6992 87 // ExtendedOopClosures that don't need to walk the metadata.
stefank@6992 88 // Currently, only CMS and G1 need these.
aoqi@0 89
aoqi@0 90 virtual bool do_metadata() { return do_metadata_nv(); }
aoqi@0 91 bool do_metadata_v() { return do_metadata(); }
aoqi@0 92 bool do_metadata_nv() { return false; }
aoqi@0 93
aoqi@0 94 virtual void do_klass(Klass* k) { do_klass_nv(k); }
aoqi@0 95 void do_klass_v(Klass* k) { do_klass(k); }
aoqi@0 96 void do_klass_nv(Klass* k) { ShouldNotReachHere(); }
aoqi@0 97
aoqi@0 98 virtual void do_class_loader_data(ClassLoaderData* cld) { ShouldNotReachHere(); }
aoqi@0 99
aoqi@0 100 // Controls how prefetching is done for invocations of this closure.
aoqi@0 101 Prefetch::style prefetch_style() { // Note that this is non-virtual.
aoqi@0 102 return Prefetch::do_none;
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 // True iff this closure may be safely applied more than once to an oop
aoqi@0 106 // location without an intervening "major reset" (like the end of a GC).
aoqi@0 107 virtual bool idempotent() { return false; }
aoqi@0 108 virtual bool apply_to_weak_ref_discovered_field() { return false; }
aoqi@0 109 };
aoqi@0 110
aoqi@0 111 // Wrapper closure only used to implement oop_iterate_no_header().
aoqi@0 112 class NoHeaderExtendedOopClosure : public ExtendedOopClosure {
aoqi@0 113 OopClosure* _wrapped_closure;
aoqi@0 114 public:
aoqi@0 115 NoHeaderExtendedOopClosure(OopClosure* cl) : _wrapped_closure(cl) {}
aoqi@0 116 // Warning: this calls the virtual version do_oop in the the wrapped closure.
aoqi@0 117 void do_oop_nv(oop* p) { _wrapped_closure->do_oop(p); }
aoqi@0 118 void do_oop_nv(narrowOop* p) { _wrapped_closure->do_oop(p); }
aoqi@0 119
aoqi@0 120 void do_oop(oop* p) { assert(false, "Only the _nv versions should be used");
aoqi@0 121 _wrapped_closure->do_oop(p); }
aoqi@0 122 void do_oop(narrowOop* p) { assert(false, "Only the _nv versions should be used");
aoqi@0 123 _wrapped_closure->do_oop(p);}
aoqi@0 124 };
aoqi@0 125
aoqi@0 126 class KlassClosure : public Closure {
aoqi@0 127 public:
aoqi@0 128 virtual void do_klass(Klass* k) = 0;
aoqi@0 129 };
aoqi@0 130
stefank@6973 131 class CLDClosure : public Closure {
stefank@6973 132 public:
stefank@6973 133 virtual void do_cld(ClassLoaderData* cld) = 0;
stefank@6973 134 };
stefank@6973 135
aoqi@0 136 class KlassToOopClosure : public KlassClosure {
stefank@6982 137 friend class MetadataAwareOopClosure;
stefank@6982 138 friend class MetadataAwareOopsInGenClosure;
stefank@6982 139
aoqi@0 140 OopClosure* _oop_closure;
stefank@6982 141
stefank@6982 142 // Used when _oop_closure couldn't be set in an initialization list.
stefank@6982 143 void initialize(OopClosure* oop_closure) {
stefank@6982 144 assert(_oop_closure == NULL, "Should only be called once");
stefank@6982 145 _oop_closure = oop_closure;
stefank@6982 146 }
stefank@6982 147
aoqi@0 148 public:
stefank@6982 149 KlassToOopClosure(OopClosure* oop_closure = NULL) : _oop_closure(oop_closure) {}
stefank@6992 150
aoqi@0 151 virtual void do_klass(Klass* k);
aoqi@0 152 };
aoqi@0 153
stefank@6973 154 class CLDToOopClosure : public CLDClosure {
stefank@6992 155 OopClosure* _oop_closure;
aoqi@0 156 KlassToOopClosure _klass_closure;
stefank@6992 157 bool _must_claim_cld;
aoqi@0 158
aoqi@0 159 public:
aoqi@0 160 CLDToOopClosure(OopClosure* oop_closure, bool must_claim_cld = true) :
aoqi@0 161 _oop_closure(oop_closure),
aoqi@0 162 _klass_closure(oop_closure),
aoqi@0 163 _must_claim_cld(must_claim_cld) {}
aoqi@0 164
aoqi@0 165 void do_cld(ClassLoaderData* cld);
aoqi@0 166 };
aoqi@0 167
stefank@6992 168 class CLDToKlassAndOopClosure : public CLDClosure {
stefank@6992 169 friend class SharedHeap;
stefank@6992 170 friend class G1CollectedHeap;
stefank@6992 171 protected:
stefank@6992 172 OopClosure* _oop_closure;
stefank@6992 173 KlassClosure* _klass_closure;
stefank@6992 174 bool _must_claim_cld;
stefank@6992 175 public:
stefank@6992 176 CLDToKlassAndOopClosure(KlassClosure* klass_closure,
stefank@6992 177 OopClosure* oop_closure,
stefank@6992 178 bool must_claim_cld) :
stefank@6992 179 _oop_closure(oop_closure),
stefank@6992 180 _klass_closure(klass_closure),
stefank@6992 181 _must_claim_cld(must_claim_cld) {}
stefank@6992 182 void do_cld(ClassLoaderData* cld);
stefank@6992 183 };
stefank@6992 184
stefank@6982 185 // The base class for all concurrent marking closures,
stefank@6982 186 // that participates in class unloading.
stefank@6982 187 // It's used to proxy through the metadata to the oops defined in them.
stefank@6982 188 class MetadataAwareOopClosure: public ExtendedOopClosure {
stefank@6982 189 KlassToOopClosure _klass_closure;
stefank@6982 190
stefank@6982 191 public:
stefank@6982 192 MetadataAwareOopClosure() : ExtendedOopClosure() {
stefank@6982 193 _klass_closure.initialize(this);
stefank@6982 194 }
stefank@6982 195 MetadataAwareOopClosure(ReferenceProcessor* rp) : ExtendedOopClosure(rp) {
stefank@6982 196 _klass_closure.initialize(this);
stefank@6982 197 }
stefank@6982 198
stefank@6982 199 virtual bool do_metadata() { return do_metadata_nv(); }
stefank@6982 200 inline bool do_metadata_nv() { return true; }
stefank@6982 201
stefank@6982 202 virtual void do_klass(Klass* k);
stefank@6982 203 void do_klass_nv(Klass* k);
stefank@6982 204
stefank@6982 205 virtual void do_class_loader_data(ClassLoaderData* cld);
stefank@6982 206 };
stefank@6982 207
aoqi@0 208 // ObjectClosure is used for iterating through an object space
aoqi@0 209
aoqi@0 210 class ObjectClosure : public Closure {
aoqi@0 211 public:
aoqi@0 212 // Called for each object.
aoqi@0 213 virtual void do_object(oop obj) = 0;
aoqi@0 214 };
aoqi@0 215
aoqi@0 216
aoqi@0 217 class BoolObjectClosure : public Closure {
aoqi@0 218 public:
aoqi@0 219 virtual bool do_object_b(oop obj) = 0;
aoqi@0 220 };
aoqi@0 221
aoqi@0 222 // Applies an oop closure to all ref fields in objects iterated over in an
aoqi@0 223 // object iteration.
aoqi@0 224 class ObjectToOopClosure: public ObjectClosure {
aoqi@0 225 ExtendedOopClosure* _cl;
aoqi@0 226 public:
aoqi@0 227 void do_object(oop obj);
aoqi@0 228 ObjectToOopClosure(ExtendedOopClosure* cl) : _cl(cl) {}
aoqi@0 229 };
aoqi@0 230
aoqi@0 231 // A version of ObjectClosure that is expected to be robust
aoqi@0 232 // in the face of possibly uninitialized objects.
aoqi@0 233 class ObjectClosureCareful : public ObjectClosure {
aoqi@0 234 public:
aoqi@0 235 virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0;
aoqi@0 236 virtual size_t do_object_careful(oop p) = 0;
aoqi@0 237 };
aoqi@0 238
aoqi@0 239 // The following are used in CompactibleFreeListSpace and
aoqi@0 240 // ConcurrentMarkSweepGeneration.
aoqi@0 241
aoqi@0 242 // Blk closure (abstract class)
aoqi@0 243 class BlkClosure : public StackObj {
aoqi@0 244 public:
aoqi@0 245 virtual size_t do_blk(HeapWord* addr) = 0;
aoqi@0 246 };
aoqi@0 247
aoqi@0 248 // A version of BlkClosure that is expected to be robust
aoqi@0 249 // in the face of possibly uninitialized objects.
aoqi@0 250 class BlkClosureCareful : public BlkClosure {
aoqi@0 251 public:
aoqi@0 252 size_t do_blk(HeapWord* addr) {
aoqi@0 253 guarantee(false, "call do_blk_careful instead");
aoqi@0 254 return 0;
aoqi@0 255 }
aoqi@0 256 virtual size_t do_blk_careful(HeapWord* addr) = 0;
aoqi@0 257 };
aoqi@0 258
aoqi@0 259 // SpaceClosure is used for iterating over spaces
aoqi@0 260
aoqi@0 261 class Space;
aoqi@0 262 class CompactibleSpace;
aoqi@0 263
aoqi@0 264 class SpaceClosure : public StackObj {
aoqi@0 265 public:
aoqi@0 266 // Called for each space
aoqi@0 267 virtual void do_space(Space* s) = 0;
aoqi@0 268 };
aoqi@0 269
aoqi@0 270 class CompactibleSpaceClosure : public StackObj {
aoqi@0 271 public:
aoqi@0 272 // Called for each compactible space
aoqi@0 273 virtual void do_space(CompactibleSpace* s) = 0;
aoqi@0 274 };
aoqi@0 275
aoqi@0 276
aoqi@0 277 // CodeBlobClosure is used for iterating through code blobs
aoqi@0 278 // in the code cache or on thread stacks
aoqi@0 279
aoqi@0 280 class CodeBlobClosure : public Closure {
aoqi@0 281 public:
aoqi@0 282 // Called for each code blob.
aoqi@0 283 virtual void do_code_blob(CodeBlob* cb) = 0;
aoqi@0 284 };
aoqi@0 285
stefank@6992 286 // Applies an oop closure to all ref fields in code blobs
stefank@6992 287 // iterated over in an object iteration.
stefank@6992 288 class CodeBlobToOopClosure : public CodeBlobClosure {
stefank@6992 289 OopClosure* _cl;
stefank@6992 290 bool _fix_relocations;
stefank@6992 291 protected:
stefank@6992 292 void do_nmethod(nmethod* nm);
stefank@6992 293 public:
stefank@6992 294 CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}
stefank@6992 295 virtual void do_code_blob(CodeBlob* cb);
aoqi@0 296
stefank@6992 297 const static bool FixRelocations = true;
stefank@6992 298 };
stefank@6992 299
stefank@6992 300 class MarkingCodeBlobClosure : public CodeBlobToOopClosure {
aoqi@0 301 public:
stefank@6992 302 MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {}
aoqi@0 303 // Called for each code blob, but at most once per unique blob.
aoqi@0 304
aoqi@0 305 virtual void do_code_blob(CodeBlob* cb);
aoqi@0 306
aoqi@0 307 class MarkScope : public StackObj {
aoqi@0 308 protected:
aoqi@0 309 bool _active;
aoqi@0 310 public:
aoqi@0 311 MarkScope(bool activate = true);
aoqi@0 312 // = { if (active) nmethod::oops_do_marking_prologue(); }
aoqi@0 313 ~MarkScope();
aoqi@0 314 // = { if (active) nmethod::oops_do_marking_epilogue(); }
aoqi@0 315 };
aoqi@0 316 };
aoqi@0 317
aoqi@0 318 // MonitorClosure is used for iterating over monitors in the monitors cache
aoqi@0 319
aoqi@0 320 class ObjectMonitor;
aoqi@0 321
aoqi@0 322 class MonitorClosure : public StackObj {
aoqi@0 323 public:
aoqi@0 324 // called for each monitor in cache
aoqi@0 325 virtual void do_monitor(ObjectMonitor* m) = 0;
aoqi@0 326 };
aoqi@0 327
aoqi@0 328 // A closure that is applied without any arguments.
aoqi@0 329 class VoidClosure : public StackObj {
aoqi@0 330 public:
aoqi@0 331 // I would have liked to declare this a pure virtual, but that breaks
aoqi@0 332 // in mysterious ways, for unknown reasons.
aoqi@0 333 virtual void do_void();
aoqi@0 334 };
aoqi@0 335
aoqi@0 336
aoqi@0 337 // YieldClosure is intended for use by iteration loops
aoqi@0 338 // to incrementalize their work, allowing interleaving
aoqi@0 339 // of an interruptable task so as to allow other
aoqi@0 340 // threads to run (which may not otherwise be able to access
aoqi@0 341 // exclusive resources, for instance). Additionally, the
aoqi@0 342 // closure also allows for aborting an ongoing iteration
aoqi@0 343 // by means of checking the return value from the polling
aoqi@0 344 // call.
aoqi@0 345 class YieldClosure : public StackObj {
aoqi@0 346 public:
aoqi@0 347 virtual bool should_return() = 0;
aoqi@0 348 };
aoqi@0 349
aoqi@0 350 // Abstract closure for serializing data (read or write).
aoqi@0 351
aoqi@0 352 class SerializeClosure : public Closure {
aoqi@0 353 public:
aoqi@0 354 // Return bool indicating whether closure implements read or write.
aoqi@0 355 virtual bool reading() const = 0;
aoqi@0 356
aoqi@0 357 // Read/write the void pointer pointed to by p.
aoqi@0 358 virtual void do_ptr(void** p) = 0;
aoqi@0 359
aoqi@0 360 // Read/write the region specified.
aoqi@0 361 virtual void do_region(u_char* start, size_t size) = 0;
aoqi@0 362
aoqi@0 363 // Check/write the tag. If reading, then compare the tag against
aoqi@0 364 // the passed in value and fail is they don't match. This allows
aoqi@0 365 // for verification that sections of the serialized data are of the
aoqi@0 366 // correct length.
aoqi@0 367 virtual void do_tag(int tag) = 0;
aoqi@0 368 };
aoqi@0 369
aoqi@0 370 class SymbolClosure : public StackObj {
aoqi@0 371 public:
aoqi@0 372 virtual void do_symbol(Symbol**) = 0;
aoqi@0 373
aoqi@0 374 // Clear LSB in symbol address; it can be set by CPSlot.
aoqi@0 375 static Symbol* load_symbol(Symbol** p) {
aoqi@0 376 return (Symbol*)(intptr_t(*p) & ~1);
aoqi@0 377 }
aoqi@0 378
aoqi@0 379 // Store symbol, adjusting new pointer if the original pointer was adjusted
aoqi@0 380 // (symbol references in constant pool slots have their LSB set to 1).
aoqi@0 381 static void store_symbol(Symbol** p, Symbol* sym) {
aoqi@0 382 *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1));
aoqi@0 383 }
aoqi@0 384 };
aoqi@0 385
stefank@6982 386
stefank@6982 387 // Helper defines for ExtendOopClosure
stefank@6982 388
stefank@6982 389 #define if_do_metadata_checked(closure, nv_suffix) \
stefank@6982 390 /* Make sure the non-virtual and the virtual versions match. */ \
stefank@6982 391 assert(closure->do_metadata##nv_suffix() == closure->do_metadata(), \
stefank@6982 392 "Inconsistency in do_metadata"); \
stefank@6982 393 if (closure->do_metadata##nv_suffix())
stefank@6982 394
stefank@6982 395 #define assert_should_ignore_metadata(closure, nv_suffix) \
stefank@6982 396 assert(!closure->do_metadata##nv_suffix(), "Code to handle metadata is not implemented")
stefank@6982 397
aoqi@0 398 #endif // SHARE_VM_MEMORY_ITERATOR_HPP

mercurial