Path: blob/master/src/hotspot/share/classfile/classLoaderData.hpp
40949 views
/*1* Copyright (c) 2012, 2021, 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_CLASSFILE_CLASSLOADERDATA_HPP25#define SHARE_CLASSFILE_CLASSLOADERDATA_HPP2627#include "memory/allocation.hpp"28#include "oops/oopHandle.hpp"29#include "oops/weakHandle.hpp"30#include "runtime/atomic.hpp"31#include "runtime/mutex.hpp"32#include "utilities/growableArray.hpp"33#include "utilities/macros.hpp"34#if INCLUDE_JFR35#include "jfr/support/jfrTraceIdExtension.hpp"36#endif3738// external name (synthetic) for the primordial "bootstrap" class loader instance39#define BOOTSTRAP_LOADER_NAME "bootstrap"40#define BOOTSTRAP_LOADER_NAME_LEN 94142//43// A class loader represents a linkset. Conceptually, a linkset identifies44// the complete transitive closure of resolved links that a dynamic linker can45// produce.46//47// A ClassLoaderData also encapsulates the allocation space, called a metaspace,48// used by the dynamic linker to allocate the runtime representation of all49// the types it defines.50//51// ClassLoaderData are stored in the runtime representation of classes,52// and provides iterators for root tracing and other GC operations.5354class ClassLoaderDataGraph;55class JNIMethodBlock;56class ModuleEntry;57class PackageEntry;58class ModuleEntryTable;59class PackageEntryTable;60class DictionaryEntry;61class Dictionary;62class ClassLoaderMetaspace;6364// ClassLoaderData class6566class ClassLoaderData : public CHeapObj<mtClass> {67friend class VMStructs;6869private:70class ChunkedHandleList {71struct Chunk : public CHeapObj<mtClass> {72static const size_t CAPACITY = 32;7374oop _data[CAPACITY];75volatile juint _size;76Chunk* _next;7778Chunk(Chunk* c) : _size(0), _next(c) { }79};8081Chunk* volatile _head;8283void oops_do_chunk(OopClosure* f, Chunk* c, const juint size);8485public:86ChunkedHandleList() : _head(NULL) {}87~ChunkedHandleList();8889// Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock().90// However, multiple threads can execute oops_do concurrently with add.91OopHandle add(oop o);92bool contains(oop p);93NOT_PRODUCT(bool owner_of(oop* p);)94void oops_do(OopClosure* f);9596int count() const;97};9899friend class ClassLoaderDataGraph;100friend class ClassLoaderDataGraphIterator;101friend class ClassLoaderDataGraphKlassIteratorAtomic;102friend class ClassLoaderDataGraphKlassIteratorStatic;103friend class ClassLoaderDataGraphMetaspaceIterator;104friend class Klass;105friend class MetaDataFactory;106friend class Method;107108static ClassLoaderData * _the_null_class_loader_data;109110WeakHandle _holder; // The oop that determines lifetime of this class loader111OopHandle _class_loader; // The instance of java/lang/ClassLoader associated with112// this ClassLoaderData113114ClassLoaderMetaspace * volatile _metaspace; // Meta-space where meta-data defined by the115// classes in the class loader are allocated.116Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.117bool _unloading; // true if this class loader goes away118bool _has_class_mirror_holder; // If true, CLD is dedicated to one class and that class determines119// the CLDs lifecycle. For example, a non-strong hidden class.120// Arrays of these classes are also assigned121// to these class loader datas.122123// Remembered sets support for the oops in the class loader data.124bool _modified_oops; // Card Table Equivalent125126int _keep_alive; // if this CLD is kept alive.127// Used for non-strong hidden classes and the128// boot class loader. _keep_alive does not need to be volatile or129// atomic since there is one unique CLD per non-strong hidden class.130131volatile int _claim; // non-zero if claimed, for example during GC traces.132// To avoid applying oop closure more than once.133ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which134// have the same life cycle of the corresponding ClassLoader.135136NOT_PRODUCT(volatile int _dependency_count;) // number of class loader dependencies137138Klass* volatile _klasses; // The classes defined by the class loader.139PackageEntryTable* volatile _packages; // The packages defined by the class loader.140ModuleEntryTable* volatile _modules; // The modules defined by the class loader.141ModuleEntry* _unnamed_module; // This class loader's unnamed module.142Dictionary* _dictionary; // The loaded InstanceKlasses, including initiated by this class loader143144// These method IDs are created for the class loader and set to NULL when the145// class loader is unloaded. They are rarely freed, only for redefine classes146// and if they lose a data race in InstanceKlass.147JNIMethodBlock* _jmethod_ids;148149// Metadata to be deallocated when it's safe at class unloading, when150// this class loader isn't unloaded itself.151GrowableArray<Metadata*>* _deallocate_list;152153// Support for walking class loader data objects154ClassLoaderData* _next; /// Next loader_datas created155156Klass* _class_loader_klass;157Symbol* _name;158Symbol* _name_and_id;159JFR_ONLY(DEFINE_TRACE_ID_FIELD;)160161void set_next(ClassLoaderData* next) { _next = next; }162ClassLoaderData* next() const { return Atomic::load(&_next); }163164ClassLoaderData(Handle h_class_loader, bool has_class_mirror_holder);165~ClassLoaderData();166167// The CLD are not placed in the Heap, so the Card Table or168// the Mod Union Table can't be used to mark when CLD have modified oops.169// The CT and MUT bits saves this information for the whole class loader data.170void clear_modified_oops() { _modified_oops = false; }171public:172void record_modified_oops() { _modified_oops = true; }173bool has_modified_oops() { return _modified_oops; }174175oop holder_no_keepalive() const;176oop holder_phantom() const;177178private:179void unload();180bool keep_alive() const { return _keep_alive > 0; }181182void classes_do(void f(Klass* const));183void loaded_classes_do(KlassClosure* klass_closure);184void classes_do(void f(InstanceKlass*));185void methods_do(void f(Method*));186void modules_do(void f(ModuleEntry*));187void packages_do(void f(PackageEntry*));188189// Deallocate free list during class unloading.190void free_deallocate_list(); // for the classes that are not unloaded191void free_deallocate_list_C_heap_structures(); // for the classes that are unloaded192193Dictionary* create_dictionary();194195void initialize_name(Handle class_loader);196197public:198// GC interface.199200// The "claim" is typically used to check if oops_do needs to be applied on201// the CLD or not. Most GCs only perform strong marking during the marking phase.202enum Claim {203_claim_none = 0,204_claim_finalizable = 2,205_claim_strong = 3,206_claim_other = 4207};208void clear_claim() { _claim = 0; }209void clear_claim(int claim);210bool claimed() const { return _claim != 0; }211bool claimed(int claim) const { return (_claim & claim) == claim; }212bool try_claim(int claim);213214// Computes if the CLD is alive or not. This is safe to call in concurrent215// contexts.216bool is_alive() const;217218// Accessors219ClassLoaderMetaspace* metaspace_or_null() const { return _metaspace; }220221static ClassLoaderData* the_null_class_loader_data() {222return _the_null_class_loader_data;223}224225Mutex* metaspace_lock() const { return _metaspace_lock; }226227bool has_class_mirror_holder() const { return _has_class_mirror_holder; }228229static void init_null_class_loader_data();230231bool is_the_null_class_loader_data() const {232return this == _the_null_class_loader_data;233}234235// Returns true if this class loader data is for the system class loader.236// (Note that the class loader data may be for a non-strong hidden class)237bool is_system_class_loader_data() const;238239// Returns true if this class loader data is for the platform class loader.240// (Note that the class loader data may be for a non-strong hidden class)241bool is_platform_class_loader_data() const;242243// Returns true if this class loader data is for the boot class loader.244// (Note that the class loader data may be for a non-strong hidden class)245inline bool is_boot_class_loader_data() const;246247bool is_builtin_class_loader_data() const;248bool is_permanent_class_loader_data() const;249250// The Metaspace is created lazily so may be NULL. This251// method will allocate a Metaspace if needed.252ClassLoaderMetaspace* metaspace_non_null();253254inline oop class_loader() const;255256// Returns true if this class loader data is for a loader going away.257// Note that this is only safe after the GC has computed if the CLD is258// unloading or not. In concurrent contexts where there are no such259// guarantees, is_alive() should be used instead.260bool is_unloading() const {261assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");262return _unloading;263}264265// Used to refcount a non-strong hidden class's s CLD in order to indicate their aliveness.266void inc_keep_alive();267void dec_keep_alive();268269void initialize_holder(Handle holder);270271void oops_do(OopClosure* f, int claim_value, bool clear_modified_oops = false);272273void classes_do(KlassClosure* klass_closure);274Klass* klasses() { return _klasses; }275276JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }277void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }278279void print() const;280void print_on(outputStream* out) const PRODUCT_RETURN;281void print_value() const;282void print_value_on(outputStream* out) const;283void verify();284285OopHandle add_handle(Handle h);286void remove_handle(OopHandle h);287void init_handle_locked(OopHandle& pd, Handle h); // used for concurrent access to ModuleEntry::_pd field288void add_class(Klass* k, bool publicize = true);289void remove_class(Klass* k);290bool contains_klass(Klass* k);291void record_dependency(const Klass* to);292PackageEntryTable* packages() { return _packages; }293ModuleEntry* unnamed_module() { return _unnamed_module; }294ModuleEntryTable* modules();295bool modules_defined() { return (_modules != NULL); }296297// Offsets298static ByteSize holder_offset() { return in_ByteSize(offset_of(ClassLoaderData, _holder)); }299static ByteSize keep_alive_offset() { return in_ByteSize(offset_of(ClassLoaderData, _keep_alive)); }300301// Loaded class dictionary302Dictionary* dictionary() const { return _dictionary; }303304void add_to_deallocate_list(Metadata* m);305306static ClassLoaderData* class_loader_data(oop loader);307static ClassLoaderData* class_loader_data_or_null(oop loader);308309// Returns Klass* of associated class loader, or NULL if associated loader is 'bootstrap'.310// Also works if unloading.311Klass* class_loader_klass() const { return _class_loader_klass; }312313// Returns the class loader's explict name as specified during314// construction or the class loader's qualified class name.315// Works during unloading.316const char* loader_name() const;317// Returns the explicitly specified class loader name or NULL.318Symbol* name() const { return _name; }319320// Obtain the class loader's _name_and_id, works during unloading.321const char* loader_name_and_id() const;322Symbol* name_and_id() const { return _name_and_id; }323324unsigned identity_hash() const {325return (unsigned)((uintptr_t)this >> 3);326}327328JFR_ONLY(DEFINE_TRACE_ID_METHODS;)329};330331#endif // SHARE_CLASSFILE_CLASSLOADERDATA_HPP332333334