iterator.hpp revision 2062:3582bf76420e
1101704Smjacob/* 2139749Simp * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 3101704Smjacob * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4101704Smjacob * 5101704Smjacob * This code is free software; you can redistribute it and/or modify it 6101704Smjacob * under the terms of the GNU General Public License version 2 only, as 7101704Smjacob * published by the Free Software Foundation. 8101704Smjacob * 9101704Smjacob * This code is distributed in the hope that it will be useful, but WITHOUT 10101704Smjacob * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11101704Smjacob * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12101704Smjacob * version 2 for more details (a copy is included in the LICENSE file that 13101704Smjacob * accompanied this code). 14101704Smjacob * 15101704Smjacob * You should have received a copy of the GNU General Public License version 16101704Smjacob * 2 along with this work; if not, write to the Free Software Foundation, 17101704Smjacob * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18101704Smjacob * 19101704Smjacob * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20101704Smjacob * or visit www.oracle.com if you need additional information or have any 21101704Smjacob * questions. 22101704Smjacob * 23101704Smjacob */ 24101704Smjacob 25101704Smjacob#ifndef SHARE_VM_MEMORY_ITERATOR_HPP 26101704Smjacob#define SHARE_VM_MEMORY_ITERATOR_HPP 27101704Smjacob 28147883Sscottl#include "memory/allocation.hpp" 29147883Sscottl#include "memory/memRegion.hpp" 30101704Smjacob#include "runtime/prefetch.hpp" 31101704Smjacob#include "utilities/top.hpp" 32147883Sscottl 33147883Sscottl// The following classes are C++ `closures` for iterating over objects, roots and spaces 34147883Sscottl 35147883Sscottlclass CodeBlob; 36147883Sscottlclass nmethod; 37147883Sscottlclass ReferenceProcessor; 38147883Sscottlclass DataLayout; 39147883Sscottl 40147883Sscottl// Closure provides abortability. 41147883Sscottl 42147883Sscottlclass Closure : public StackObj { 43147883Sscottl protected: 44147883Sscottl bool _abort; 45147883Sscottl void set_abort() { _abort = true; } 46147883Sscottl public: 47148679Sgibbs Closure() : _abort(false) {} 48148679Sgibbs // A subtype can use this mechanism to indicate to some iterator mapping 49148679Sgibbs // functions that the iteration should cease. 50147883Sscottl bool abort() { return _abort; } 51147883Sscottl void clear_abort() { _abort = false; } 52147883Sscottl}; 53147883Sscottl 54147883Sscottl// OopClosure is used for iterating through roots (oop*) 55147883Sscottl 56147883Sscottlclass OopClosure : public Closure { 57147883Sscottl public: 58147883Sscottl ReferenceProcessor* _ref_processor; 59147883Sscottl OopClosure(ReferenceProcessor* rp) : _ref_processor(rp) { } 60147883Sscottl OopClosure() : _ref_processor(NULL) { } 61147883Sscottl virtual void do_oop(oop* o) = 0; 62101704Smjacob virtual void do_oop_v(oop* o) { do_oop(o); } 63101704Smjacob virtual void do_oop(narrowOop* o) = 0; 64101704Smjacob virtual void do_oop_v(narrowOop* o) { do_oop(o); } 65101704Smjacob 66147883Sscottl // In support of post-processing of weak links of KlassKlass objects; 67147883Sscottl // see KlassKlass::oop_oop_iterate(). 68147883Sscottl 69147883Sscottl virtual const bool should_remember_klasses() const { 70147883Sscottl assert(!must_remember_klasses(), "Should have overriden this method."); 71147883Sscottl return false; 72147883Sscottl } 73147883Sscottl 74147883Sscottl virtual void remember_klass(Klass* k) { /* do nothing */ } 75147883Sscottl 76147883Sscottl // In support of post-processing of weak references in 77147883Sscottl // ProfileData (MethodDataOop) objects; see, for example, 78147883Sscottl // VirtualCallData::oop_iterate(). 79147883Sscottl virtual const bool should_remember_mdo() const { return false; } 80147883Sscottl virtual void remember_mdo(DataLayout* v) { /* do nothing */ } 81147883Sscottl 82147883Sscottl // The methods below control how object iterations invoking this closure 83147883Sscottl // should be performed: 84147883Sscottl 85147883Sscottl // If "true", invoke on header klass field. 86147883Sscottl bool do_header() { return true; } // Note that this is non-virtual. 87147883Sscottl // Controls how prefetching is done for invocations of this closure. 88147883Sscottl Prefetch::style prefetch_style() { // Note that this is non-virtual. 89147883Sscottl return Prefetch::do_none; 90147883Sscottl } 91147883Sscottl 92147883Sscottl // True iff this closure may be safely applied more than once to an oop 93147883Sscottl // location without an intervening "major reset" (like the end of a GC). 94147883Sscottl virtual bool idempotent() { return false; } 95147883Sscottl virtual bool apply_to_weak_ref_discovered_field() { return false; } 96147883Sscottl 97147883Sscottl#ifdef ASSERT 98147883Sscottl static bool _must_remember_klasses; 99147883Sscottl static bool must_remember_klasses(); 100147883Sscottl static void set_must_remember_klasses(bool v); 101147883Sscottl#endif 102147883Sscottl}; 103147883Sscottl 104147883Sscottl// ObjectClosure is used for iterating through an object space 105147883Sscottl 106147883Sscottlclass ObjectClosure : public Closure { 107147883Sscottl public: 108147883Sscottl // Called for each object. 109147883Sscottl virtual void do_object(oop obj) = 0; 110147883Sscottl}; 111147883Sscottl 112101704Smjacob 113101704Smjacobclass BoolObjectClosure : public ObjectClosure { 114101704Smjacob public: 115147883Sscottl virtual bool do_object_b(oop obj) = 0; 116101704Smjacob}; 117147883Sscottl 118147883Sscottl// Applies an oop closure to all ref fields in objects iterated over in an 119147883Sscottl// object iteration. 120147883Sscottlclass ObjectToOopClosure: public ObjectClosure { 121101704Smjacob OopClosure* _cl; 122147883Sscottlpublic: 123147883Sscottl void do_object(oop obj); 124147883Sscottl ObjectToOopClosure(OopClosure* cl) : _cl(cl) {} 125147883Sscottl}; 126147883Sscottl 127147883Sscottl// A version of ObjectClosure with "memory" (see _previous_address below) 128147883Sscottlclass UpwardsObjectClosure: public BoolObjectClosure { 129147883Sscottl HeapWord* _previous_address; 130147883Sscottl public: 131147883Sscottl UpwardsObjectClosure() : _previous_address(NULL) { } 132147883Sscottl void set_previous(HeapWord* addr) { _previous_address = addr; } 133147883Sscottl HeapWord* previous() { return _previous_address; } 134147883Sscottl // A return value of "true" can be used by the caller to decide 135147883Sscottl // if this object's end should *NOT* be recorded in 136147883Sscottl // _previous_address above. 137147883Sscottl virtual bool do_object_bm(oop obj, MemRegion mr) = 0; 138147883Sscottl}; 139147883Sscottl 140147883Sscottl// A version of ObjectClosure that is expected to be robust 141147883Sscottl// in the face of possibly uninitialized objects. 142147883Sscottlclass ObjectClosureCareful : public ObjectClosure { 143147883Sscottl public: 144147883Sscottl virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0; 145147883Sscottl virtual size_t do_object_careful(oop p) = 0; 146147883Sscottl}; 147147883Sscottl 148147883Sscottl// The following are used in CompactibleFreeListSpace and 149101704Smjacob// ConcurrentMarkSweepGeneration. 150101704Smjacob 151147883Sscottl// Blk closure (abstract class) 152101704Smjacobclass BlkClosure : public StackObj { 153147883Sscottl public: 154147883Sscottl virtual size_t do_blk(HeapWord* addr) = 0; 155101704Smjacob}; 156147883Sscottl 157147883Sscottl// A version of BlkClosure that is expected to be robust 158101704Smjacob// in the face of possibly uninitialized objects. 159147883Sscottlclass BlkClosureCareful : public BlkClosure { 160147883Sscottl public: 161147883Sscottl size_t do_blk(HeapWord* addr) { 162147883Sscottl guarantee(false, "call do_blk_careful instead"); 163147883Sscottl return 0; 164147883Sscottl } 165147883Sscottl virtual size_t do_blk_careful(HeapWord* addr) = 0; 166147883Sscottl}; 167147883Sscottl 168147883Sscottl// SpaceClosure is used for iterating over spaces 169147883Sscottl 170147883Sscottlclass Space; 171147883Sscottlclass CompactibleSpace; 172147883Sscottl 173147883Sscottlclass SpaceClosure : public StackObj { 174147883Sscottl public: 175147883Sscottl // Called for each space 176147883Sscottl virtual void do_space(Space* s) = 0; 177147883Sscottl}; 178147883Sscottl 179147883Sscottlclass CompactibleSpaceClosure : public StackObj { 180147883Sscottl public: 181147883Sscottl // Called for each compactible space 182147883Sscottl virtual void do_space(CompactibleSpace* s) = 0; 183147883Sscottl}; 184147883Sscottl 185147883Sscottl 186147883Sscottl// CodeBlobClosure is used for iterating through code blobs 187147883Sscottl// in the code cache or on thread stacks 188147883Sscottl 189147883Sscottlclass CodeBlobClosure : public Closure { 190147883Sscottl public: 191147883Sscottl // Called for each code blob. 192147883Sscottl virtual void do_code_blob(CodeBlob* cb) = 0; 193147883Sscottl}; 194147883Sscottl 195147883Sscottl 196147883Sscottlclass MarkingCodeBlobClosure : public CodeBlobClosure { 197147883Sscottl public: 198147883Sscottl // Called for each code blob, but at most once per unique blob. 199147883Sscottl virtual void do_newly_marked_nmethod(nmethod* nm) = 0; 200147883Sscottl 201147883Sscottl virtual void do_code_blob(CodeBlob* cb); 202147883Sscottl // = { if (!nmethod(cb)->test_set_oops_do_mark()) do_newly_marked_nmethod(cb); } 203147883Sscottl 204147883Sscottl class MarkScope : public StackObj { 205147883Sscottl protected: 206147883Sscottl bool _active; 207147883Sscottl public: 208147883Sscottl MarkScope(bool activate = true); 209147883Sscottl // = { if (active) nmethod::oops_do_marking_prologue(); } 210147883Sscottl ~MarkScope(); 211147883Sscottl // = { if (active) nmethod::oops_do_marking_epilogue(); } 212147883Sscottl }; 213147883Sscottl}; 214147883Sscottl 215147883Sscottl 216147883Sscottl// Applies an oop closure to all ref fields in code blobs 217147883Sscottl// iterated over in an object iteration. 218147883Sscottlclass CodeBlobToOopClosure: public MarkingCodeBlobClosure { 219147883Sscottl OopClosure* _cl; 220147883Sscottl bool _do_marking; 221147883Sscottlpublic: 222147883Sscottl virtual void do_newly_marked_nmethod(nmethod* cb); 223147883Sscottl // = { cb->oops_do(_cl); } 224147883Sscottl virtual void do_code_blob(CodeBlob* cb); 225147883Sscottl // = { if (_do_marking) super::do_code_blob(cb); else cb->oops_do(_cl); } 226147883Sscottl CodeBlobToOopClosure(OopClosure* cl, bool do_marking) 227147883Sscottl : _cl(cl), _do_marking(do_marking) {} 228147883Sscottl}; 229147883Sscottl 230147883Sscottl 231147883Sscottl 232147883Sscottl// MonitorClosure is used for iterating over monitors in the monitors cache 233147883Sscottl 234147883Sscottlclass ObjectMonitor; 235147883Sscottl 236147883Sscottlclass MonitorClosure : public StackObj { 237147883Sscottl public: 238147883Sscottl // called for each monitor in cache 239147883Sscottl virtual void do_monitor(ObjectMonitor* m) = 0; 240147883Sscottl}; 241147883Sscottl 242147883Sscottl// A closure that is applied without any arguments. 243147883Sscottlclass VoidClosure : public StackObj { 244147883Sscottl public: 245147883Sscottl // I would have liked to declare this a pure virtual, but that breaks 246147883Sscottl // in mysterious ways, for unknown reasons. 247147883Sscottl virtual void do_void(); 248147883Sscottl}; 249147883Sscottl 250147883Sscottl 251147883Sscottl// YieldClosure is intended for use by iteration loops 252147883Sscottl// to incrementalize their work, allowing interleaving 253101704Smjacob// of an interruptable task so as to allow other 254147883Sscottl// threads to run (which may not otherwise be able to access 255147883Sscottl// exclusive resources, for instance). Additionally, the 256147883Sscottl// closure also allows for aborting an ongoing iteration 257147883Sscottl// by means of checking the return value from the polling 258147883Sscottl// call. 259147883Sscottlclass YieldClosure : public StackObj { 260147883Sscottl public: 261147883Sscottl virtual bool should_return() = 0; 262147883Sscottl}; 263147883Sscottl 264147883Sscottl// Abstract closure for serializing data (read or write). 265147883Sscottl 266147883Sscottlclass SerializeOopClosure : public OopClosure { 267147883Sscottlpublic: 268147883Sscottl // Return bool indicating whether closure implements read or write. 269147883Sscottl virtual bool reading() const = 0; 270147883Sscottl 271147883Sscottl // Read/write the int pointed to by i. 272101704Smjacob virtual void do_int(int* i) = 0; 273147883Sscottl 274147883Sscottl // Read/write the size_t pointed to by i. 275147883Sscottl virtual void do_size_t(size_t* i) = 0; 276147883Sscottl 277147883Sscottl // Read/write the void pointer pointed to by p. 278147883Sscottl virtual void do_ptr(void** p) = 0; 279147883Sscottl 280147883Sscottl // Read/write the HeapWord pointer pointed to be p. 281147883Sscottl virtual void do_ptr(HeapWord** p) = 0; 282101704Smjacob 283147883Sscottl // Read/write the region specified. 284147883Sscottl virtual void do_region(u_char* start, size_t size) = 0; 285147883Sscottl 286147883Sscottl // Check/write the tag. If reading, then compare the tag against 287147883Sscottl // the passed in value and fail is they don't match. This allows 288147883Sscottl // for verification that sections of the serialized data are of the 289101704Smjacob // correct length. 290147883Sscottl virtual void do_tag(int tag) = 0; 291147883Sscottl}; 292147883Sscottl 293147883Sscottlclass SymbolClosure : public StackObj { 294101704Smjacob public: 295101704Smjacob virtual void do_symbol(Symbol**) = 0; 296147883Sscottl 297147883Sscottl // Clear LSB in symbol address; it can be set by CPSlot. 298147883Sscottl static Symbol* load_symbol(Symbol** p) { 299147883Sscottl return (Symbol*)(intptr_t(*p) & ~1); 300147883Sscottl } 301147883Sscottl 302147883Sscottl // Store symbol, adjusting new pointer if the original pointer was adjusted 303147883Sscottl // (symbol references in constant pool slots have their LSB set to 1). 304147883Sscottl static void store_symbol(Symbol** p, Symbol* sym) { 305147883Sscottl *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1)); 306147883Sscottl } 307147883Sscottl}; 308147883Sscottl 309147883Sscottl#ifdef ASSERT 310147883Sscottl// This class is used to flag phases of a collection that 311147883Sscottl// can unload classes and which should override the 312147883Sscottl// should_remember_klasses() and remember_klass() of OopClosure. 313147883Sscottl// The _must_remember_klasses is set in the contructor and restored 314147883Sscottl// in the destructor. _must_remember_klasses is checked in assertions 315147883Sscottl// in the OopClosure implementations of should_remember_klasses() and 316147883Sscottl// remember_klass() and the expectation is that the OopClosure 317147883Sscottl// implementation should not be in use if _must_remember_klasses is set. 318147883Sscottl// Instances of RememberKlassesChecker can be place in 319147883Sscottl// marking phases of collections which can do class unloading. 320147883Sscottl// RememberKlassesChecker can be passed "false" to turn off checking. 321147883Sscottl// It is used by CMS when CMS yields to a different collector. 322147883Sscottlclass RememberKlassesChecker: StackObj { 323147883Sscottl bool _saved_state; 324147883Sscottl bool _do_check; 325147883Sscottl public: 326101704Smjacob RememberKlassesChecker(bool checking_on) : _saved_state(false), 327101704Smjacob _do_check(true) { 328147883Sscottl // The ClassUnloading unloading flag affects the collectors except 329147883Sscottl // for CMS. 330147883Sscottl // CMS unloads classes if CMSClassUnloadingEnabled is true or 331147883Sscottl // if ExplicitGCInvokesConcurrentAndUnloadsClasses is true and 332147883Sscottl // the current collection is an explicit collection. Turning 333147883Sscottl // on the checking in general for 334147883Sscottl // ExplicitGCInvokesConcurrentAndUnloadsClasses and 335147883Sscottl // UseConcMarkSweepGC should not lead to false positives. 336101704Smjacob _do_check = 337147883Sscottl ClassUnloading && !UseConcMarkSweepGC || 338147883Sscottl CMSClassUnloadingEnabled && UseConcMarkSweepGC || 339147883Sscottl ExplicitGCInvokesConcurrentAndUnloadsClasses && UseConcMarkSweepGC; 340147883Sscottl if (_do_check) { 341147883Sscottl _saved_state = OopClosure::must_remember_klasses(); 342101704Smjacob OopClosure::set_must_remember_klasses(checking_on); 343101704Smjacob } 344147883Sscottl } 345147883Sscottl ~RememberKlassesChecker() { 346147883Sscottl if (_do_check) { 347147883Sscottl OopClosure::set_must_remember_klasses(_saved_state); 348147883Sscottl } 349147883Sscottl } 350147883Sscottl}; 351147883Sscottl#endif // ASSERT 352147883Sscottl 353147883Sscottl#endif // SHARE_VM_MEMORY_ITERATOR_HPP 354147883Sscottl