0N/A/*
2273N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_MEMORY_ITERATOR_HPP
1879N/A#define SHARE_VM_MEMORY_ITERATOR_HPP
1879N/A
1879N/A#include "memory/allocation.hpp"
1879N/A#include "memory/memRegion.hpp"
1879N/A#include "runtime/prefetch.hpp"
1879N/A#include "utilities/top.hpp"
1879N/A
0N/A// The following classes are C++ `closures` for iterating over objects, roots and spaces
0N/A
989N/Aclass CodeBlob;
994N/Aclass nmethod;
0N/Aclass ReferenceProcessor;
941N/Aclass DataLayout;
0N/A
342N/A// Closure provides abortability.
342N/A
342N/Aclass Closure : public StackObj {
342N/A protected:
342N/A bool _abort;
342N/A void set_abort() { _abort = true; }
342N/A public:
342N/A Closure() : _abort(false) {}
342N/A // A subtype can use this mechanism to indicate to some iterator mapping
342N/A // functions that the iteration should cease.
342N/A bool abort() { return _abort; }
342N/A void clear_abort() { _abort = false; }
342N/A};
342N/A
0N/A// OopClosure is used for iterating through roots (oop*)
0N/A
342N/Aclass OopClosure : public Closure {
0N/A public:
0N/A ReferenceProcessor* _ref_processor;
0N/A OopClosure(ReferenceProcessor* rp) : _ref_processor(rp) { }
0N/A OopClosure() : _ref_processor(NULL) { }
0N/A virtual void do_oop(oop* o) = 0;
0N/A virtual void do_oop_v(oop* o) { do_oop(o); }
113N/A virtual void do_oop(narrowOop* o) = 0;
113N/A virtual void do_oop_v(narrowOop* o) { do_oop(o); }
0N/A
0N/A // In support of post-processing of weak links of KlassKlass objects;
0N/A // see KlassKlass::oop_oop_iterate().
935N/A
935N/A virtual const bool should_remember_klasses() const {
935N/A assert(!must_remember_klasses(), "Should have overriden this method.");
935N/A return false;
935N/A }
935N/A
0N/A virtual void remember_klass(Klass* k) { /* do nothing */ }
0N/A
941N/A // In support of post-processing of weak references in
941N/A // ProfileData (MethodDataOop) objects; see, for example,
941N/A // VirtualCallData::oop_iterate().
941N/A virtual const bool should_remember_mdo() const { return false; }
941N/A virtual void remember_mdo(DataLayout* v) { /* do nothing */ }
941N/A
0N/A // The methods below control how object iterations invoking this closure
0N/A // should be performed:
0N/A
0N/A // If "true", invoke on header klass field.
0N/A bool do_header() { return true; } // Note that this is non-virtual.
0N/A // Controls how prefetching is done for invocations of this closure.
0N/A Prefetch::style prefetch_style() { // Note that this is non-virtual.
0N/A return Prefetch::do_none;
0N/A }
342N/A
342N/A // True iff this closure may be safely applied more than once to an oop
342N/A // location without an intervening "major reset" (like the end of a GC).
342N/A virtual bool idempotent() { return false; }
342N/A virtual bool apply_to_weak_ref_discovered_field() { return false; }
935N/A
935N/A#ifdef ASSERT
935N/A static bool _must_remember_klasses;
935N/A static bool must_remember_klasses();
935N/A static void set_must_remember_klasses(bool v);
935N/A#endif
0N/A};
0N/A
0N/A// ObjectClosure is used for iterating through an object space
0N/A
342N/Aclass ObjectClosure : public Closure {
0N/A public:
0N/A // Called for each object.
0N/A virtual void do_object(oop obj) = 0;
0N/A};
0N/A
0N/A
0N/Aclass BoolObjectClosure : public ObjectClosure {
0N/A public:
0N/A virtual bool do_object_b(oop obj) = 0;
0N/A};
0N/A
0N/A// Applies an oop closure to all ref fields in objects iterated over in an
0N/A// object iteration.
0N/Aclass ObjectToOopClosure: public ObjectClosure {
0N/A OopClosure* _cl;
0N/Apublic:
0N/A void do_object(oop obj);
0N/A ObjectToOopClosure(OopClosure* cl) : _cl(cl) {}
0N/A};
0N/A
0N/A// A version of ObjectClosure with "memory" (see _previous_address below)
0N/Aclass UpwardsObjectClosure: public BoolObjectClosure {
0N/A HeapWord* _previous_address;
0N/A public:
0N/A UpwardsObjectClosure() : _previous_address(NULL) { }
0N/A void set_previous(HeapWord* addr) { _previous_address = addr; }
0N/A HeapWord* previous() { return _previous_address; }
0N/A // A return value of "true" can be used by the caller to decide
0N/A // if this object's end should *NOT* be recorded in
0N/A // _previous_address above.
0N/A virtual bool do_object_bm(oop obj, MemRegion mr) = 0;
0N/A};
0N/A
0N/A// A version of ObjectClosure that is expected to be robust
0N/A// in the face of possibly uninitialized objects.
0N/Aclass ObjectClosureCareful : public ObjectClosure {
0N/A public:
0N/A virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0;
0N/A virtual size_t do_object_careful(oop p) = 0;
0N/A};
0N/A
0N/A// The following are used in CompactibleFreeListSpace and
0N/A// ConcurrentMarkSweepGeneration.
0N/A
0N/A// Blk closure (abstract class)
0N/Aclass BlkClosure : public StackObj {
0N/A public:
0N/A virtual size_t do_blk(HeapWord* addr) = 0;
0N/A};
0N/A
0N/A// A version of BlkClosure that is expected to be robust
0N/A// in the face of possibly uninitialized objects.
0N/Aclass BlkClosureCareful : public BlkClosure {
0N/A public:
0N/A size_t do_blk(HeapWord* addr) {
0N/A guarantee(false, "call do_blk_careful instead");
0N/A return 0;
0N/A }
0N/A virtual size_t do_blk_careful(HeapWord* addr) = 0;
0N/A};
0N/A
0N/A// SpaceClosure is used for iterating over spaces
0N/A
0N/Aclass Space;
0N/Aclass CompactibleSpace;
0N/A
0N/Aclass SpaceClosure : public StackObj {
0N/A public:
0N/A // Called for each space
0N/A virtual void do_space(Space* s) = 0;
0N/A};
0N/A
0N/Aclass CompactibleSpaceClosure : public StackObj {
0N/A public:
0N/A // Called for each compactible space
0N/A virtual void do_space(CompactibleSpace* s) = 0;
0N/A};
0N/A
0N/A
989N/A// CodeBlobClosure is used for iterating through code blobs
989N/A// in the code cache or on thread stacks
989N/A
989N/Aclass CodeBlobClosure : public Closure {
989N/A public:
989N/A // Called for each code blob.
989N/A virtual void do_code_blob(CodeBlob* cb) = 0;
989N/A};
989N/A
989N/A
989N/Aclass MarkingCodeBlobClosure : public CodeBlobClosure {
989N/A public:
989N/A // Called for each code blob, but at most once per unique blob.
994N/A virtual void do_newly_marked_nmethod(nmethod* nm) = 0;
989N/A
989N/A virtual void do_code_blob(CodeBlob* cb);
989N/A // = { if (!nmethod(cb)->test_set_oops_do_mark()) do_newly_marked_nmethod(cb); }
989N/A
989N/A class MarkScope : public StackObj {
989N/A protected:
989N/A bool _active;
989N/A public:
989N/A MarkScope(bool activate = true);
989N/A // = { if (active) nmethod::oops_do_marking_prologue(); }
989N/A ~MarkScope();
989N/A // = { if (active) nmethod::oops_do_marking_epilogue(); }
989N/A };
989N/A};
989N/A
989N/A
989N/A// Applies an oop closure to all ref fields in code blobs
989N/A// iterated over in an object iteration.
989N/Aclass CodeBlobToOopClosure: public MarkingCodeBlobClosure {
989N/A OopClosure* _cl;
989N/A bool _do_marking;
989N/Apublic:
994N/A virtual void do_newly_marked_nmethod(nmethod* cb);
989N/A // = { cb->oops_do(_cl); }
989N/A virtual void do_code_blob(CodeBlob* cb);
989N/A // = { if (_do_marking) super::do_code_blob(cb); else cb->oops_do(_cl); }
989N/A CodeBlobToOopClosure(OopClosure* cl, bool do_marking)
989N/A : _cl(cl), _do_marking(do_marking) {}
989N/A};
989N/A
989N/A
0N/A
0N/A// MonitorClosure is used for iterating over monitors in the monitors cache
0N/A
0N/Aclass ObjectMonitor;
0N/A
0N/Aclass MonitorClosure : public StackObj {
0N/A public:
0N/A // called for each monitor in cache
0N/A virtual void do_monitor(ObjectMonitor* m) = 0;
0N/A};
0N/A
0N/A// A closure that is applied without any arguments.
0N/Aclass VoidClosure : public StackObj {
0N/A public:
0N/A // I would have liked to declare this a pure virtual, but that breaks
0N/A // in mysterious ways, for unknown reasons.
0N/A virtual void do_void();
0N/A};
0N/A
0N/A
0N/A// YieldClosure is intended for use by iteration loops
0N/A// to incrementalize their work, allowing interleaving
0N/A// of an interruptable task so as to allow other
0N/A// threads to run (which may not otherwise be able to access
0N/A// exclusive resources, for instance). Additionally, the
0N/A// closure also allows for aborting an ongoing iteration
0N/A// by means of checking the return value from the polling
0N/A// call.
0N/Aclass YieldClosure : public StackObj {
0N/A public:
0N/A virtual bool should_return() = 0;
0N/A};
0N/A
0N/A// Abstract closure for serializing data (read or write).
0N/A
0N/Aclass SerializeOopClosure : public OopClosure {
0N/Apublic:
0N/A // Return bool indicating whether closure implements read or write.
0N/A virtual bool reading() const = 0;
0N/A
0N/A // Read/write the int pointed to by i.
0N/A virtual void do_int(int* i) = 0;
0N/A
0N/A // Read/write the size_t pointed to by i.
0N/A virtual void do_size_t(size_t* i) = 0;
0N/A
0N/A // Read/write the void pointer pointed to by p.
0N/A virtual void do_ptr(void** p) = 0;
0N/A
0N/A // Read/write the HeapWord pointer pointed to be p.
0N/A virtual void do_ptr(HeapWord** p) = 0;
0N/A
0N/A // Read/write the region specified.
0N/A virtual void do_region(u_char* start, size_t size) = 0;
0N/A
0N/A // Check/write the tag. If reading, then compare the tag against
0N/A // the passed in value and fail is they don't match. This allows
0N/A // for verification that sections of the serialized data are of the
0N/A // correct length.
0N/A virtual void do_tag(int tag) = 0;
0N/A};
935N/A
2062N/Aclass SymbolClosure : public StackObj {
2062N/A public:
2062N/A virtual void do_symbol(Symbol**) = 0;
2062N/A
2062N/A // Clear LSB in symbol address; it can be set by CPSlot.
2062N/A static Symbol* load_symbol(Symbol** p) {
2062N/A return (Symbol*)(intptr_t(*p) & ~1);
2062N/A }
2062N/A
2062N/A // Store symbol, adjusting new pointer if the original pointer was adjusted
2062N/A // (symbol references in constant pool slots have their LSB set to 1).
2062N/A static void store_symbol(Symbol** p, Symbol* sym) {
2062N/A *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1));
2062N/A }
2062N/A};
2062N/A
935N/A#ifdef ASSERT
935N/A// This class is used to flag phases of a collection that
935N/A// can unload classes and which should override the
935N/A// should_remember_klasses() and remember_klass() of OopClosure.
935N/A// The _must_remember_klasses is set in the contructor and restored
935N/A// in the destructor. _must_remember_klasses is checked in assertions
935N/A// in the OopClosure implementations of should_remember_klasses() and
935N/A// remember_klass() and the expectation is that the OopClosure
935N/A// implementation should not be in use if _must_remember_klasses is set.
935N/A// Instances of RememberKlassesChecker can be place in
935N/A// marking phases of collections which can do class unloading.
935N/A// RememberKlassesChecker can be passed "false" to turn off checking.
935N/A// It is used by CMS when CMS yields to a different collector.
935N/Aclass RememberKlassesChecker: StackObj {
1190N/A bool _saved_state;
1190N/A bool _do_check;
935N/A public:
1190N/A RememberKlassesChecker(bool checking_on) : _saved_state(false),
1190N/A _do_check(true) {
1190N/A // The ClassUnloading unloading flag affects the collectors except
1190N/A // for CMS.
1190N/A // CMS unloads classes if CMSClassUnloadingEnabled is true or
1190N/A // if ExplicitGCInvokesConcurrentAndUnloadsClasses is true and
1190N/A // the current collection is an explicit collection. Turning
1190N/A // on the checking in general for
1190N/A // ExplicitGCInvokesConcurrentAndUnloadsClasses and
1190N/A // UseConcMarkSweepGC should not lead to false positives.
1190N/A _do_check =
1190N/A ClassUnloading && !UseConcMarkSweepGC ||
1190N/A CMSClassUnloadingEnabled && UseConcMarkSweepGC ||
1190N/A ExplicitGCInvokesConcurrentAndUnloadsClasses && UseConcMarkSweepGC;
1190N/A if (_do_check) {
1190N/A _saved_state = OopClosure::must_remember_klasses();
1190N/A OopClosure::set_must_remember_klasses(checking_on);
935N/A }
935N/A }
935N/A ~RememberKlassesChecker() {
1190N/A if (_do_check) {
1190N/A OopClosure::set_must_remember_klasses(_saved_state);
935N/A }
935N/A }
935N/A};
935N/A#endif // ASSERT
1879N/A
1879N/A#endif // SHARE_VM_MEMORY_ITERATOR_HPP