Path: blob/master/src/hotspot/share/memory/iterator.hpp
40949 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_MEMORY_ITERATOR_HPP25#define SHARE_MEMORY_ITERATOR_HPP2627#include "memory/allocation.hpp"28#include "memory/memRegion.hpp"29#include "oops/oopsHierarchy.hpp"3031class CodeBlob;32class nmethod;33class ReferenceDiscoverer;34class DataLayout;35class KlassClosure;36class ClassLoaderData;37class Symbol;38class Metadata;39class Thread;4041// The following classes are C++ `closures` for iterating over objects, roots and spaces4243class Closure : public StackObj { };4445// Thread iterator46class ThreadClosure {47public:48virtual void do_thread(Thread* thread) = 0;49};5051// OopClosure is used for iterating through references to Java objects.52class OopClosure : public Closure {53public:54virtual void do_oop(oop* o) = 0;55virtual void do_oop(narrowOop* o) = 0;56};5758class DoNothingClosure : public OopClosure {59public:60virtual void do_oop(oop* p) {}61virtual void do_oop(narrowOop* p) {}62};63extern DoNothingClosure do_nothing_cl;6465// OopIterateClosure adds extra code to be run during oop iterations.66// This is needed by the GC and is extracted to a separate type to not67// pollute the OopClosure interface.68class OopIterateClosure : public OopClosure {69private:70ReferenceDiscoverer* _ref_discoverer;7172protected:73OopIterateClosure(ReferenceDiscoverer* rd) : _ref_discoverer(rd) { }74OopIterateClosure() : _ref_discoverer(NULL) { }75~OopIterateClosure() { }7677void set_ref_discoverer_internal(ReferenceDiscoverer* rd) { _ref_discoverer = rd; }7879public:80ReferenceDiscoverer* ref_discoverer() const { return _ref_discoverer; }8182// Iteration of InstanceRefKlasses differ depending on the closure,83// the below enum describes the different alternatives.84enum ReferenceIterationMode {85DO_DISCOVERY, // Apply closure and discover references86DO_DISCOVERED_AND_DISCOVERY, // Apply closure to discovered field and do discovery87DO_FIELDS, // Apply closure to all fields88DO_FIELDS_EXCEPT_REFERENT // Apply closure to all fields except the referent field89};9091// The default iteration mode is to do discovery.92virtual ReferenceIterationMode reference_iteration_mode() { return DO_DISCOVERY; }9394// If the do_metadata functions return "true",95// we invoke the following when running oop_iterate():96//97// 1) do_klass on the header klass pointer.98// 2) do_klass on the klass pointer in the mirrors.99// 3) do_cld on the class loader data in class loaders.100101virtual bool do_metadata() = 0;102virtual void do_klass(Klass* k) = 0;103virtual void do_cld(ClassLoaderData* cld) = 0;104};105106// An OopIterateClosure that can be used when there's no need to visit the Metadata.107class BasicOopIterateClosure : public OopIterateClosure {108public:109BasicOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) {}110111virtual bool do_metadata() { return false; }112virtual void do_klass(Klass* k) { ShouldNotReachHere(); }113virtual void do_cld(ClassLoaderData* cld) { ShouldNotReachHere(); }114};115116class KlassClosure : public Closure {117public:118virtual void do_klass(Klass* k) = 0;119};120121class CLDClosure : public Closure {122public:123virtual void do_cld(ClassLoaderData* cld) = 0;124};125126class MetadataClosure : public Closure {127public:128virtual void do_metadata(Metadata* md) = 0;129};130131132class CLDToOopClosure : public CLDClosure {133OopClosure* _oop_closure;134int _cld_claim;135136public:137CLDToOopClosure(OopClosure* oop_closure,138int cld_claim) :139_oop_closure(oop_closure),140_cld_claim(cld_claim) {}141142void do_cld(ClassLoaderData* cld);143};144145template <int claim>146class ClaimingCLDToOopClosure : public CLDToOopClosure {147public:148ClaimingCLDToOopClosure(OopClosure* cl) : CLDToOopClosure(cl, claim) {}149};150151class ClaimMetadataVisitingOopIterateClosure : public OopIterateClosure {152protected:153const int _claim;154155public:156ClaimMetadataVisitingOopIterateClosure(int claim, ReferenceDiscoverer* rd = NULL) :157OopIterateClosure(rd),158_claim(claim) { }159160virtual bool do_metadata() { return true; }161virtual void do_klass(Klass* k);162virtual void do_cld(ClassLoaderData* cld);163};164165// The base class for all concurrent marking closures,166// that participates in class unloading.167// It's used to proxy through the metadata to the oops defined in them.168class MetadataVisitingOopIterateClosure: public ClaimMetadataVisitingOopIterateClosure {169public:170MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd = NULL);171};172173// ObjectClosure is used for iterating through an object space174175class ObjectClosure : public Closure {176public:177// Called for each object.178virtual void do_object(oop obj) = 0;179};180181182class BoolObjectClosure : public Closure {183public:184virtual bool do_object_b(oop obj) = 0;185};186187class AlwaysTrueClosure: public BoolObjectClosure {188public:189bool do_object_b(oop p) { return true; }190};191192class AlwaysFalseClosure : public BoolObjectClosure {193public:194bool do_object_b(oop p) { return false; }195};196197// Applies an oop closure to all ref fields in objects iterated over in an198// object iteration.199class ObjectToOopClosure: public ObjectClosure {200OopIterateClosure* _cl;201public:202void do_object(oop obj);203ObjectToOopClosure(OopIterateClosure* cl) : _cl(cl) {}204};205206// SpaceClosure is used for iterating over spaces207208class Space;209class CompactibleSpace;210211class SpaceClosure : public StackObj {212public:213// Called for each space214virtual void do_space(Space* s) = 0;215};216217class CompactibleSpaceClosure : public StackObj {218public:219// Called for each compactible space220virtual void do_space(CompactibleSpace* s) = 0;221};222223224// CodeBlobClosure is used for iterating through code blobs225// in the code cache or on thread stacks226227class CodeBlobClosure : public Closure {228public:229// Called for each code blob.230virtual void do_code_blob(CodeBlob* cb) = 0;231};232233// Applies an oop closure to all ref fields in code blobs234// iterated over in an object iteration.235class CodeBlobToOopClosure : public CodeBlobClosure {236OopClosure* _cl;237bool _fix_relocations;238protected:239void do_nmethod(nmethod* nm);240public:241// If fix_relocations(), then cl must copy objects to their new location immediately to avoid242// patching nmethods with the old locations.243CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}244virtual void do_code_blob(CodeBlob* cb);245246bool fix_relocations() const { return _fix_relocations; }247const static bool FixRelocations = true;248};249250class MarkingCodeBlobClosure : public CodeBlobToOopClosure {251public:252MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {}253// Called for each code blob, but at most once per unique blob.254255virtual void do_code_blob(CodeBlob* cb);256};257258class NMethodClosure : public Closure {259public:260virtual void do_nmethod(nmethod* n) = 0;261};262263class CodeBlobToNMethodClosure : public CodeBlobClosure {264NMethodClosure* const _nm_cl;265266public:267CodeBlobToNMethodClosure(NMethodClosure* nm_cl) : _nm_cl(nm_cl) {}268269virtual void do_code_blob(CodeBlob* cb);270};271272// MonitorClosure is used for iterating over monitors in the monitors cache273274class ObjectMonitor;275276class MonitorClosure : public StackObj {277public:278// called for each monitor in cache279virtual void do_monitor(ObjectMonitor* m) = 0;280};281282// A closure that is applied without any arguments.283class VoidClosure : public StackObj {284public:285// I would have liked to declare this a pure virtual, but that breaks286// in mysterious ways, for unknown reasons.287virtual void do_void();288};289290291// YieldClosure is intended for use by iteration loops292// to incrementalize their work, allowing interleaving293// of an interruptable task so as to allow other294// threads to run (which may not otherwise be able to access295// exclusive resources, for instance). Additionally, the296// closure also allows for aborting an ongoing iteration297// by means of checking the return value from the polling298// call.299class YieldClosure : public StackObj {300public:301virtual bool should_return() = 0;302303// Yield on a fine-grain level. The check in case of not yielding should be very fast.304virtual bool should_return_fine_grain() { return false; }305};306307// Abstract closure for serializing data (read or write).308309class SerializeClosure : public Closure {310public:311// Return bool indicating whether closure implements read or write.312virtual bool reading() const = 0;313314// Read/write the void pointer pointed to by p.315virtual void do_ptr(void** p) = 0;316317// Read/write the 32-bit unsigned integer pointed to by p.318virtual void do_u4(u4* p) = 0;319320// Read/write the bool pointed to by p.321virtual void do_bool(bool* p) = 0;322323// Read/write the region specified.324virtual void do_region(u_char* start, size_t size) = 0;325326// Check/write the tag. If reading, then compare the tag against327// the passed in value and fail is they don't match. This allows328// for verification that sections of the serialized data are of the329// correct length.330virtual void do_tag(int tag) = 0;331332// Read/write the oop333virtual void do_oop(oop* o) = 0;334335bool writing() {336return !reading();337}338};339340class SymbolClosure : public StackObj {341public:342virtual void do_symbol(Symbol**) = 0;343344// Clear LSB in symbol address; it can be set by CPSlot.345static Symbol* load_symbol(Symbol** p) {346return (Symbol*)(intptr_t(*p) & ~1);347}348349// Store symbol, adjusting new pointer if the original pointer was adjusted350// (symbol references in constant pool slots have their LSB set to 1).351static void store_symbol(Symbol** p, Symbol* sym) {352*p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1));353}354};355356template <typename E>357class CompareClosure : public Closure {358public:359virtual int do_compare(const E&, const E&) = 0;360};361362// Dispatches to the non-virtual functions if OopClosureType has363// a concrete implementation, otherwise a virtual call is taken.364class Devirtualizer {365public:366template <typename OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);367template <typename OopClosureType> static void do_klass(OopClosureType* closure, Klass* k);368template <typename OopClosureType> static void do_cld(OopClosureType* closure, ClassLoaderData* cld);369template <typename OopClosureType> static bool do_metadata(OopClosureType* closure);370};371372class OopIteratorClosureDispatch {373public:374template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass);375template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass, MemRegion mr);376template <typename OopClosureType> static void oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* klass);377};378379#endif // SHARE_MEMORY_ITERATOR_HPP380381382