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