Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/oops/klassVtable.hpp
32285 views
/*1* Copyright (c) 1997, 2015, 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_VM_OOPS_KLASSVTABLE_HPP25#define SHARE_VM_OOPS_KLASSVTABLE_HPP2627#include "memory/allocation.hpp"28#include "oops/oopsHierarchy.hpp"29#include "runtime/handles.hpp"30#include "utilities/growableArray.hpp"3132// A klassVtable abstracts the variable-length vtable that is embedded in InstanceKlass33// and ArrayKlass. klassVtable objects are used just as convenient transient accessors to the vtable,34// not to actually hold the vtable data.35// Note: the klassVtable should not be accessed before the class has been verified36// (until that point, the vtable is uninitialized).3738// Currently a klassVtable contains a direct reference to the vtable data, and is therefore39// not preserved across GCs.4041class vtableEntry;4243class klassVtable : public ResourceObj {44KlassHandle _klass; // my klass45int _tableOffset; // offset of start of vtable data within klass46int _length; // length of vtable (number of entries)47#ifndef PRODUCT48int _verify_count; // to make verify faster49#endif5051// Ordering important, so greater_than (>) can be used as an merge operator.52enum AccessType {53acc_private = 0,54acc_package_private = 1,55acc_publicprotected = 256};5758public:59klassVtable(KlassHandle h_klass, void* base, int length) : _klass(h_klass) {60_tableOffset = (address)base - (address)h_klass(); _length = length;61}6263// accessors64vtableEntry* table() const { return (vtableEntry*)(address(_klass()) + _tableOffset); }65KlassHandle klass() const { return _klass; }66int length() const { return _length; }67inline Method* method_at(int i) const;68inline Method* unchecked_method_at(int i) const;69inline Method** adr_method_at(int i) const;7071// searching; all methods return -1 if not found72int index_of(Method* m) const { return index_of(m, _length); }73int index_of_miranda(Symbol* name, Symbol* signature);7475void initialize_vtable(bool checkconstraints, TRAPS); // initialize vtable of a new klass7677// CDS/RedefineClasses support - clear vtables so they can be reinitialized78// at dump time. Clearing gives us an easy way to tell if the vtable has79// already been reinitialized at dump time (see dump.cpp). Vtables can80// be initialized at run time by RedefineClasses so dumping the right order81// is necessary.82void clear_vtable();83bool is_initialized();8485// computes vtable length (in words) and the number of miranda methods86static void compute_vtable_size_and_num_mirandas(87int* vtable_length, int* num_new_mirandas,88GrowableArray<Method*>* all_mirandas, Klass* super,89Array<Method*>* methods, AccessFlags class_flags, Handle classloader,90Symbol* classname, Array<Klass*>* local_interfaces, TRAPS);9192#if INCLUDE_JVMTI93// RedefineClasses() API support:94// If any entry of this vtable points to any of old_methods,95// replace it with the corresponding new_method.96// trace_name_printed is set to true if the current call has97// printed the klass name so that other routines in the adjust_*98// group don't print the klass name.99bool adjust_default_method(int vtable_index, Method* old_method, Method* new_method);100void adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed);101bool check_no_old_or_obsolete_entries();102void dump_vtable();103#endif // INCLUDE_JVMTI104105// Debugging code106void print() PRODUCT_RETURN;107void verify(outputStream* st, bool force = false);108static void print_statistics() PRODUCT_RETURN;109110protected:111friend class vtableEntry;112private:113enum { VTABLE_TRANSITIVE_OVERRIDE_VERSION = 51 } ;114void copy_vtable_to(vtableEntry* start);115int initialize_from_super(KlassHandle super);116int index_of(Method* m, int len) const; // same as index_of, but search only up to len117void put_method_at(Method* m, int index);118static bool needs_new_vtable_entry(methodHandle m, Klass* super, Handle classloader, Symbol* classname, AccessFlags access_flags, TRAPS);119120bool update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, int default_index, bool checkconstraints, TRAPS);121InstanceKlass* find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method, int vtable_index,122Handle target_loader, Symbol* target_classname, Thread* THREAD);123124// support for miranda methods125bool is_miranda_entry_at(int i);126int fill_in_mirandas(int initialized);127static bool is_miranda(Method* m, Array<Method*>* class_methods,128Array<Method*>* default_methods, Klass* super);129static void add_new_mirandas_to_lists(130GrowableArray<Method*>* new_mirandas,131GrowableArray<Method*>* all_mirandas,132Array<Method*>* current_interface_methods,133Array<Method*>* class_methods,134Array<Method*>* default_methods,135Klass* super);136static void get_mirandas(137GrowableArray<Method*>* new_mirandas,138GrowableArray<Method*>* all_mirandas, Klass* super,139Array<Method*>* class_methods,140Array<Method*>* default_methods,141Array<Klass*>* local_interfaces);142void verify_against(outputStream* st, klassVtable* vt, int index);143inline InstanceKlass* ik() const;144// When loading a class from CDS archive at run time, and no class redefintion145// has happened, it is expected that the class's itable/vtables are146// laid out exactly the same way as they had been during dump time.147// Therefore, in klassVtable::initialize_[iv]table, we do not layout the148// tables again. Instead, we only rerun the process to create/check149// the class loader constraints. In non-product builds, we add asserts to150// guarantee that the table's layout would be the same as at dump time.151//152// If JVMTI redefines any class, the read-only shared memory are remapped153// as read-write. A shared class' vtable/itable are re-initialized and154// might have different layout due to class redefinition of the shared class155// or its super types.156bool is_preinitialized_vtable();157};158159160// private helper class for klassVtable161// description of entry points:162// destination is interpreted:163// from_compiled_code_entry_point -> c2iadapter164// from_interpreter_entry_point -> interpreter entry point165// destination is compiled:166// from_compiled_code_entry_point -> nmethod entry point167// from_interpreter_entry_point -> i2cadapter168class vtableEntry VALUE_OBJ_CLASS_SPEC {169friend class VMStructs;170171public:172// size in words173static int size() {174return sizeof(vtableEntry) / sizeof(HeapWord);175}176static int method_offset_in_bytes() { return offset_of(vtableEntry, _method); }177Method* method() const { return _method; }178179private:180Method* _method;181void set(Method* method) { assert(method != NULL, "use clear"); _method = method; }182void clear() { _method = NULL; }183void print() PRODUCT_RETURN;184void verify(klassVtable* vt, outputStream* st);185186friend class klassVtable;187};188189190inline Method* klassVtable::method_at(int i) const {191assert(i >= 0 && i < _length, "index out of bounds");192assert(table()[i].method() != NULL, "should not be null");193assert(((Metadata*)table()[i].method())->is_method(), "should be method");194return table()[i].method();195}196197inline Method* klassVtable::unchecked_method_at(int i) const {198assert(i >= 0 && i < _length, "index out of bounds");199return table()[i].method();200}201202inline Method** klassVtable::adr_method_at(int i) const {203// Allow one past the last entry to be referenced; useful for loop bounds.204assert(i >= 0 && i <= _length, "index out of bounds");205return (Method**)(address(table() + i) + vtableEntry::method_offset_in_bytes());206}207208// --------------------------------------------------------------------------------209class klassItable;210class itableMethodEntry;211212class itableOffsetEntry VALUE_OBJ_CLASS_SPEC {213private:214Klass* _interface;215int _offset;216public:217Klass* interface_klass() const { return _interface; }218int offset() const { return _offset; }219220static itableMethodEntry* method_entry(Klass* k, int offset) { return (itableMethodEntry*)(((address)k) + offset); }221itableMethodEntry* first_method_entry(Klass* k) { return method_entry(k, _offset); }222223void initialize(Klass* interf, int offset) { _interface = interf; _offset = offset; }224225// Static size and offset accessors226static int size() { return sizeof(itableOffsetEntry) / HeapWordSize; } // size in words227static int interface_offset_in_bytes() { return offset_of(itableOffsetEntry, _interface); }228static int offset_offset_in_bytes() { return offset_of(itableOffsetEntry, _offset); }229230friend class klassItable;231};232233234class itableMethodEntry VALUE_OBJ_CLASS_SPEC {235private:236Method* _method;237238public:239Method* method() const { return _method; }240241void clear() { _method = NULL; }242243void initialize(Method* method);244245// Static size and offset accessors246static int size() { return sizeof(itableMethodEntry) / HeapWordSize; } // size in words247static int method_offset_in_bytes() { return offset_of(itableMethodEntry, _method); }248249friend class klassItable;250};251252//253// Format of an itable254//255// ---- offset table ---256// Klass* of interface 1 \257// offset to vtable from start of oop / offset table entry258// ...259// Klass* of interface n \260// offset to vtable from start of oop / offset table entry261// --- vtable for interface 1 ---262// Method* \263// compiler entry point / method table entry264// ...265// Method* \266// compiler entry point / method table entry267// -- vtable for interface 2 ---268// ...269//270class klassItable : public ResourceObj {271private:272instanceKlassHandle _klass; // my klass273int _table_offset; // offset of start of itable data within klass (in words)274int _size_offset_table; // size of offset table (in itableOffset entries)275int _size_method_table; // size of methodtable (in itableMethodEntry entries)276277void initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS);278public:279klassItable(instanceKlassHandle klass);280281itableOffsetEntry* offset_entry(int i) { assert(0 <= i && i <= _size_offset_table, "index out of bounds");282return &((itableOffsetEntry*)vtable_start())[i]; }283284itableMethodEntry* method_entry(int i) { assert(0 <= i && i <= _size_method_table, "index out of bounds");285return &((itableMethodEntry*)method_start())[i]; }286287int size_offset_table() { return _size_offset_table; }288289// Initialization290void initialize_itable(bool checkconstraints, TRAPS);291292// Updates293void initialize_with_method(Method* m);294295#if INCLUDE_JVMTI296// RedefineClasses() API support:297// if any entry of this itable points to any of old_methods,298// replace it with the corresponding new_method.299// trace_name_printed is set to true if the current call has300// printed the klass name so that other routines in the adjust_*301// group don't print the klass name.302void adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed);303bool check_no_old_or_obsolete_entries();304void dump_itable();305#endif // INCLUDE_JVMTI306307// Setup of itable308static int assign_itable_indices_for_interface(Klass* klass);309static int method_count_for_interface(Klass* klass);310static int compute_itable_size(Array<Klass*>* transitive_interfaces);311static void setup_itable_offset_table(instanceKlassHandle klass);312313// Resolving of method to index314static Method* method_for_itable_index(Klass* klass, int itable_index);315316// Debugging/Statistics317static void print_statistics() PRODUCT_RETURN;318private:319intptr_t* vtable_start() const { return ((intptr_t*)_klass()) + _table_offset; }320intptr_t* method_start() const { return vtable_start() + _size_offset_table * itableOffsetEntry::size(); }321322// Helper methods323static int calc_itable_size(int num_interfaces, int num_methods) { return (num_interfaces * itableOffsetEntry::size()) + (num_methods * itableMethodEntry::size()); }324325// Statistics326NOT_PRODUCT(static int _total_classes;) // Total no. of classes with itables327NOT_PRODUCT(static long _total_size;) // Total no. of bytes used for itables328329static void update_stats(int size) PRODUCT_RETURN NOT_PRODUCT({ _total_classes++; _total_size += size; })330};331332#endif // SHARE_VM_OOPS_KLASSVTABLE_HPP333334335