aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_MEMORY_ITERATOR_HPP aoqi@0: #define SHARE_VM_MEMORY_ITERATOR_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "memory/memRegion.hpp" aoqi@0: #include "runtime/prefetch.hpp" aoqi@0: #include "utilities/top.hpp" aoqi@0: aoqi@0: // The following classes are C++ `closures` for iterating over objects, roots and spaces aoqi@0: aoqi@0: class CodeBlob; aoqi@0: class nmethod; aoqi@0: class ReferenceProcessor; aoqi@0: class DataLayout; aoqi@0: class KlassClosure; aoqi@0: class ClassLoaderData; aoqi@0: aoqi@0: // Closure provides abortability. aoqi@0: aoqi@0: class Closure : public StackObj { aoqi@0: protected: aoqi@0: bool _abort; aoqi@0: void set_abort() { _abort = true; } aoqi@0: public: aoqi@0: Closure() : _abort(false) {} aoqi@0: // A subtype can use this mechanism to indicate to some iterator mapping aoqi@0: // functions that the iteration should cease. aoqi@0: bool abort() { return _abort; } aoqi@0: void clear_abort() { _abort = false; } aoqi@0: }; aoqi@0: aoqi@0: // OopClosure is used for iterating through references to Java objects. aoqi@0: aoqi@0: class OopClosure : public Closure { aoqi@0: public: aoqi@0: virtual void do_oop(oop* o) = 0; aoqi@0: virtual void do_oop_v(oop* o) { do_oop(o); } aoqi@0: virtual void do_oop(narrowOop* o) = 0; aoqi@0: virtual void do_oop_v(narrowOop* o) { do_oop(o); } aoqi@0: }; aoqi@0: aoqi@0: // ExtendedOopClosure adds extra code to be run during oop iterations. aoqi@0: // This is needed by the GC and is extracted to a separate type to not aoqi@0: // pollute the OopClosure interface. aoqi@0: class ExtendedOopClosure : public OopClosure { aoqi@0: public: aoqi@0: ReferenceProcessor* _ref_processor; aoqi@0: ExtendedOopClosure(ReferenceProcessor* rp) : _ref_processor(rp) { } aoqi@0: ExtendedOopClosure() : OopClosure(), _ref_processor(NULL) { } aoqi@0: aoqi@0: // If the do_metadata functions return "true", aoqi@0: // we invoke the following when running oop_iterate(): aoqi@0: // aoqi@0: // 1) do_klass on the header klass pointer. aoqi@0: // 2) do_klass on the klass pointer in the mirrors. aoqi@0: // 3) do_class_loader_data on the class loader data in class loaders. aoqi@0: // aoqi@0: // The virtual (without suffix) and the non-virtual (with _nv suffix) need aoqi@0: // to be updated together, or else the devirtualization will break. aoqi@0: // aoqi@0: // Providing default implementations of the _nv functions unfortunately aoqi@0: // removes the compile-time safeness, but reduces the clutter for the stefank@6992: // ExtendedOopClosures that don't need to walk the metadata. stefank@6992: // Currently, only CMS and G1 need these. aoqi@0: aoqi@0: virtual bool do_metadata() { return do_metadata_nv(); } aoqi@0: bool do_metadata_v() { return do_metadata(); } aoqi@0: bool do_metadata_nv() { return false; } aoqi@0: aoqi@0: virtual void do_klass(Klass* k) { do_klass_nv(k); } aoqi@0: void do_klass_v(Klass* k) { do_klass(k); } aoqi@0: void do_klass_nv(Klass* k) { ShouldNotReachHere(); } aoqi@0: aoqi@0: virtual void do_class_loader_data(ClassLoaderData* cld) { ShouldNotReachHere(); } aoqi@0: aoqi@0: // Controls how prefetching is done for invocations of this closure. aoqi@0: Prefetch::style prefetch_style() { // Note that this is non-virtual. aoqi@0: return Prefetch::do_none; aoqi@0: } aoqi@0: aoqi@0: // True iff this closure may be safely applied more than once to an oop aoqi@0: // location without an intervening "major reset" (like the end of a GC). aoqi@0: virtual bool idempotent() { return false; } aoqi@0: virtual bool apply_to_weak_ref_discovered_field() { return false; } aoqi@0: }; aoqi@0: aoqi@0: // Wrapper closure only used to implement oop_iterate_no_header(). aoqi@0: class NoHeaderExtendedOopClosure : public ExtendedOopClosure { aoqi@0: OopClosure* _wrapped_closure; aoqi@0: public: aoqi@0: NoHeaderExtendedOopClosure(OopClosure* cl) : _wrapped_closure(cl) {} aoqi@0: // Warning: this calls the virtual version do_oop in the the wrapped closure. aoqi@0: void do_oop_nv(oop* p) { _wrapped_closure->do_oop(p); } aoqi@0: void do_oop_nv(narrowOop* p) { _wrapped_closure->do_oop(p); } aoqi@0: aoqi@0: void do_oop(oop* p) { assert(false, "Only the _nv versions should be used"); aoqi@0: _wrapped_closure->do_oop(p); } aoqi@0: void do_oop(narrowOop* p) { assert(false, "Only the _nv versions should be used"); aoqi@0: _wrapped_closure->do_oop(p);} aoqi@0: }; aoqi@0: aoqi@0: class KlassClosure : public Closure { aoqi@0: public: aoqi@0: virtual void do_klass(Klass* k) = 0; aoqi@0: }; aoqi@0: stefank@6973: class CLDClosure : public Closure { stefank@6973: public: stefank@6973: virtual void do_cld(ClassLoaderData* cld) = 0; stefank@6973: }; stefank@6973: aoqi@0: class KlassToOopClosure : public KlassClosure { stefank@6982: friend class MetadataAwareOopClosure; stefank@6982: friend class MetadataAwareOopsInGenClosure; stefank@6982: aoqi@0: OopClosure* _oop_closure; stefank@6982: stefank@6982: // Used when _oop_closure couldn't be set in an initialization list. stefank@6982: void initialize(OopClosure* oop_closure) { stefank@6982: assert(_oop_closure == NULL, "Should only be called once"); stefank@6982: _oop_closure = oop_closure; stefank@6982: } stefank@6982: aoqi@0: public: stefank@6982: KlassToOopClosure(OopClosure* oop_closure = NULL) : _oop_closure(oop_closure) {} stefank@6992: aoqi@0: virtual void do_klass(Klass* k); aoqi@0: }; aoqi@0: stefank@6973: class CLDToOopClosure : public CLDClosure { stefank@6992: OopClosure* _oop_closure; aoqi@0: KlassToOopClosure _klass_closure; stefank@6992: bool _must_claim_cld; aoqi@0: aoqi@0: public: aoqi@0: CLDToOopClosure(OopClosure* oop_closure, bool must_claim_cld = true) : aoqi@0: _oop_closure(oop_closure), aoqi@0: _klass_closure(oop_closure), aoqi@0: _must_claim_cld(must_claim_cld) {} aoqi@0: aoqi@0: void do_cld(ClassLoaderData* cld); aoqi@0: }; aoqi@0: stefank@6992: class CLDToKlassAndOopClosure : public CLDClosure { stefank@6992: friend class SharedHeap; stefank@6992: friend class G1CollectedHeap; stefank@6992: protected: stefank@6992: OopClosure* _oop_closure; stefank@6992: KlassClosure* _klass_closure; stefank@6992: bool _must_claim_cld; stefank@6992: public: stefank@6992: CLDToKlassAndOopClosure(KlassClosure* klass_closure, stefank@6992: OopClosure* oop_closure, stefank@6992: bool must_claim_cld) : stefank@6992: _oop_closure(oop_closure), stefank@6992: _klass_closure(klass_closure), stefank@6992: _must_claim_cld(must_claim_cld) {} stefank@6992: void do_cld(ClassLoaderData* cld); stefank@6992: }; stefank@6992: stefank@6982: // The base class for all concurrent marking closures, stefank@6982: // that participates in class unloading. stefank@6982: // It's used to proxy through the metadata to the oops defined in them. stefank@6982: class MetadataAwareOopClosure: public ExtendedOopClosure { stefank@6982: KlassToOopClosure _klass_closure; stefank@6982: stefank@6982: public: stefank@6982: MetadataAwareOopClosure() : ExtendedOopClosure() { stefank@6982: _klass_closure.initialize(this); stefank@6982: } stefank@6982: MetadataAwareOopClosure(ReferenceProcessor* rp) : ExtendedOopClosure(rp) { stefank@6982: _klass_closure.initialize(this); stefank@6982: } stefank@6982: stefank@6982: virtual bool do_metadata() { return do_metadata_nv(); } stefank@6982: inline bool do_metadata_nv() { return true; } stefank@6982: stefank@6982: virtual void do_klass(Klass* k); stefank@6982: void do_klass_nv(Klass* k); stefank@6982: stefank@6982: virtual void do_class_loader_data(ClassLoaderData* cld); stefank@6982: }; stefank@6982: aoqi@0: // ObjectClosure is used for iterating through an object space aoqi@0: aoqi@0: class ObjectClosure : public Closure { aoqi@0: public: aoqi@0: // Called for each object. aoqi@0: virtual void do_object(oop obj) = 0; aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class BoolObjectClosure : public Closure { aoqi@0: public: aoqi@0: virtual bool do_object_b(oop obj) = 0; aoqi@0: }; aoqi@0: aoqi@0: // Applies an oop closure to all ref fields in objects iterated over in an aoqi@0: // object iteration. aoqi@0: class ObjectToOopClosure: public ObjectClosure { aoqi@0: ExtendedOopClosure* _cl; aoqi@0: public: aoqi@0: void do_object(oop obj); aoqi@0: ObjectToOopClosure(ExtendedOopClosure* cl) : _cl(cl) {} aoqi@0: }; aoqi@0: aoqi@0: // A version of ObjectClosure that is expected to be robust aoqi@0: // in the face of possibly uninitialized objects. aoqi@0: class ObjectClosureCareful : public ObjectClosure { aoqi@0: public: aoqi@0: virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0; aoqi@0: virtual size_t do_object_careful(oop p) = 0; aoqi@0: }; aoqi@0: aoqi@0: // The following are used in CompactibleFreeListSpace and aoqi@0: // ConcurrentMarkSweepGeneration. aoqi@0: aoqi@0: // Blk closure (abstract class) aoqi@0: class BlkClosure : public StackObj { aoqi@0: public: aoqi@0: virtual size_t do_blk(HeapWord* addr) = 0; aoqi@0: }; aoqi@0: aoqi@0: // A version of BlkClosure that is expected to be robust aoqi@0: // in the face of possibly uninitialized objects. aoqi@0: class BlkClosureCareful : public BlkClosure { aoqi@0: public: aoqi@0: size_t do_blk(HeapWord* addr) { aoqi@0: guarantee(false, "call do_blk_careful instead"); aoqi@0: return 0; aoqi@0: } aoqi@0: virtual size_t do_blk_careful(HeapWord* addr) = 0; aoqi@0: }; aoqi@0: aoqi@0: // SpaceClosure is used for iterating over spaces aoqi@0: aoqi@0: class Space; aoqi@0: class CompactibleSpace; aoqi@0: aoqi@0: class SpaceClosure : public StackObj { aoqi@0: public: aoqi@0: // Called for each space aoqi@0: virtual void do_space(Space* s) = 0; aoqi@0: }; aoqi@0: aoqi@0: class CompactibleSpaceClosure : public StackObj { aoqi@0: public: aoqi@0: // Called for each compactible space aoqi@0: virtual void do_space(CompactibleSpace* s) = 0; aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // CodeBlobClosure is used for iterating through code blobs aoqi@0: // in the code cache or on thread stacks aoqi@0: aoqi@0: class CodeBlobClosure : public Closure { aoqi@0: public: aoqi@0: // Called for each code blob. aoqi@0: virtual void do_code_blob(CodeBlob* cb) = 0; aoqi@0: }; aoqi@0: stefank@6992: // Applies an oop closure to all ref fields in code blobs stefank@6992: // iterated over in an object iteration. stefank@6992: class CodeBlobToOopClosure : public CodeBlobClosure { stefank@6992: OopClosure* _cl; stefank@6992: bool _fix_relocations; stefank@6992: protected: stefank@6992: void do_nmethod(nmethod* nm); stefank@6992: public: stefank@6992: CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {} stefank@6992: virtual void do_code_blob(CodeBlob* cb); aoqi@0: stefank@6992: const static bool FixRelocations = true; stefank@6992: }; stefank@6992: stefank@6992: class MarkingCodeBlobClosure : public CodeBlobToOopClosure { aoqi@0: public: stefank@6992: MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {} aoqi@0: // Called for each code blob, but at most once per unique blob. aoqi@0: aoqi@0: virtual void do_code_blob(CodeBlob* cb); aoqi@0: aoqi@0: class MarkScope : public StackObj { aoqi@0: protected: aoqi@0: bool _active; aoqi@0: public: aoqi@0: MarkScope(bool activate = true); aoqi@0: // = { if (active) nmethod::oops_do_marking_prologue(); } aoqi@0: ~MarkScope(); aoqi@0: // = { if (active) nmethod::oops_do_marking_epilogue(); } aoqi@0: }; aoqi@0: }; aoqi@0: aoqi@0: // MonitorClosure is used for iterating over monitors in the monitors cache aoqi@0: aoqi@0: class ObjectMonitor; aoqi@0: aoqi@0: class MonitorClosure : public StackObj { aoqi@0: public: aoqi@0: // called for each monitor in cache aoqi@0: virtual void do_monitor(ObjectMonitor* m) = 0; aoqi@0: }; aoqi@0: aoqi@0: // A closure that is applied without any arguments. aoqi@0: class VoidClosure : public StackObj { aoqi@0: public: aoqi@0: // I would have liked to declare this a pure virtual, but that breaks aoqi@0: // in mysterious ways, for unknown reasons. aoqi@0: virtual void do_void(); aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // YieldClosure is intended for use by iteration loops aoqi@0: // to incrementalize their work, allowing interleaving aoqi@0: // of an interruptable task so as to allow other aoqi@0: // threads to run (which may not otherwise be able to access aoqi@0: // exclusive resources, for instance). Additionally, the aoqi@0: // closure also allows for aborting an ongoing iteration aoqi@0: // by means of checking the return value from the polling aoqi@0: // call. aoqi@0: class YieldClosure : public StackObj { aoqi@0: public: aoqi@0: virtual bool should_return() = 0; aoqi@0: }; aoqi@0: aoqi@0: // Abstract closure for serializing data (read or write). aoqi@0: aoqi@0: class SerializeClosure : public Closure { aoqi@0: public: aoqi@0: // Return bool indicating whether closure implements read or write. aoqi@0: virtual bool reading() const = 0; aoqi@0: aoqi@0: // Read/write the void pointer pointed to by p. aoqi@0: virtual void do_ptr(void** p) = 0; aoqi@0: aoqi@0: // Read/write the region specified. aoqi@0: virtual void do_region(u_char* start, size_t size) = 0; aoqi@0: aoqi@0: // Check/write the tag. If reading, then compare the tag against aoqi@0: // the passed in value and fail is they don't match. This allows aoqi@0: // for verification that sections of the serialized data are of the aoqi@0: // correct length. aoqi@0: virtual void do_tag(int tag) = 0; aoqi@0: }; aoqi@0: aoqi@0: class SymbolClosure : public StackObj { aoqi@0: public: aoqi@0: virtual void do_symbol(Symbol**) = 0; aoqi@0: aoqi@0: // Clear LSB in symbol address; it can be set by CPSlot. aoqi@0: static Symbol* load_symbol(Symbol** p) { aoqi@0: return (Symbol*)(intptr_t(*p) & ~1); aoqi@0: } aoqi@0: aoqi@0: // Store symbol, adjusting new pointer if the original pointer was adjusted aoqi@0: // (symbol references in constant pool slots have their LSB set to 1). aoqi@0: static void store_symbol(Symbol** p, Symbol* sym) { aoqi@0: *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1)); aoqi@0: } aoqi@0: }; aoqi@0: stefank@6982: stefank@6982: // Helper defines for ExtendOopClosure stefank@6982: stefank@6982: #define if_do_metadata_checked(closure, nv_suffix) \ stefank@6982: /* Make sure the non-virtual and the virtual versions match. */ \ stefank@6982: assert(closure->do_metadata##nv_suffix() == closure->do_metadata(), \ stefank@6982: "Inconsistency in do_metadata"); \ stefank@6982: if (closure->do_metadata##nv_suffix()) stefank@6982: stefank@6982: #define assert_should_ignore_metadata(closure, nv_suffix) \ stefank@6982: assert(!closure->do_metadata##nv_suffix(), "Code to handle metadata is not implemented") stefank@6982: aoqi@0: #endif // SHARE_VM_MEMORY_ITERATOR_HPP