src/share/vm/oops/instanceKlass.cpp

Thu, 24 May 2018 17:06:56 +0800

author
aoqi
date
Thu, 24 May 2018 17:06:56 +0800
changeset 8604
04d83ba48607
parent 8509
cb4af293fe70
parent 7994
04ff2f6cd0eb
child 8856
ac27a9c85bea
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
iklam@8497 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/javaClasses.hpp"
aoqi@0 27 #include "classfile/systemDictionary.hpp"
iklam@8497 28 #include "classfile/systemDictionaryShared.hpp"
aoqi@0 29 #include "classfile/verifier.hpp"
aoqi@0 30 #include "classfile/vmSymbols.hpp"
aoqi@0 31 #include "compiler/compileBroker.hpp"
aoqi@0 32 #include "gc_implementation/shared/markSweep.inline.hpp"
aoqi@0 33 #include "gc_interface/collectedHeap.inline.hpp"
aoqi@0 34 #include "interpreter/oopMapCache.hpp"
aoqi@0 35 #include "interpreter/rewriter.hpp"
aoqi@0 36 #include "jvmtifiles/jvmti.h"
aoqi@0 37 #include "memory/genOopClosures.inline.hpp"
aoqi@0 38 #include "memory/heapInspection.hpp"
stefank@6982 39 #include "memory/iterator.inline.hpp"
aoqi@0 40 #include "memory/metadataFactory.hpp"
aoqi@0 41 #include "memory/oopFactory.hpp"
aoqi@0 42 #include "oops/fieldStreams.hpp"
aoqi@0 43 #include "oops/instanceClassLoaderKlass.hpp"
aoqi@0 44 #include "oops/instanceKlass.hpp"
aoqi@0 45 #include "oops/instanceMirrorKlass.hpp"
aoqi@0 46 #include "oops/instanceOop.hpp"
aoqi@0 47 #include "oops/klass.inline.hpp"
aoqi@0 48 #include "oops/method.hpp"
aoqi@0 49 #include "oops/oop.inline.hpp"
aoqi@0 50 #include "oops/symbol.hpp"
aoqi@0 51 #include "prims/jvmtiExport.hpp"
aoqi@0 52 #include "prims/jvmtiRedefineClassesTrace.hpp"
aoqi@0 53 #include "prims/jvmtiRedefineClasses.hpp"
jbachorik@7812 54 #include "prims/jvmtiThreadState.hpp"
aoqi@0 55 #include "prims/methodComparator.hpp"
aoqi@0 56 #include "runtime/fieldDescriptor.hpp"
aoqi@0 57 #include "runtime/handles.inline.hpp"
aoqi@0 58 #include "runtime/javaCalls.hpp"
aoqi@0 59 #include "runtime/mutexLocker.hpp"
goetz@6911 60 #include "runtime/orderAccess.inline.hpp"
aoqi@0 61 #include "runtime/thread.inline.hpp"
aoqi@0 62 #include "services/classLoadingService.hpp"
aoqi@0 63 #include "services/threadService.hpp"
aoqi@0 64 #include "utilities/dtrace.hpp"
aoqi@0 65 #include "utilities/macros.hpp"
aoqi@0 66 #if INCLUDE_ALL_GCS
aoqi@0 67 #include "gc_implementation/concurrentMarkSweep/cmsOopClosures.inline.hpp"
aoqi@0 68 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
aoqi@0 69 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
aoqi@0 70 #include "gc_implementation/g1/g1RemSet.inline.hpp"
tschatzl@7091 71 #include "gc_implementation/g1/heapRegionManager.inline.hpp"
aoqi@0 72 #include "gc_implementation/parNew/parOopClosures.inline.hpp"
aoqi@0 73 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.inline.hpp"
aoqi@0 74 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
aoqi@0 75 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
aoqi@0 76 #include "oops/oop.pcgc.inline.hpp"
aoqi@0 77 #endif // INCLUDE_ALL_GCS
aoqi@0 78 #ifdef COMPILER1
aoqi@0 79 #include "c1/c1_Compiler.hpp"
aoqi@0 80 #endif
aoqi@0 81
aoqi@0 82 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 83
aoqi@0 84 #ifdef DTRACE_ENABLED
aoqi@0 85
aoqi@0 86 #ifndef USDT2
aoqi@0 87
aoqi@0 88 HS_DTRACE_PROBE_DECL4(hotspot, class__initialization__required,
aoqi@0 89 char*, intptr_t, oop, intptr_t);
aoqi@0 90 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__recursive,
aoqi@0 91 char*, intptr_t, oop, intptr_t, int);
aoqi@0 92 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__concurrent,
aoqi@0 93 char*, intptr_t, oop, intptr_t, int);
aoqi@0 94 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__erroneous,
aoqi@0 95 char*, intptr_t, oop, intptr_t, int);
aoqi@0 96 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__super__failed,
aoqi@0 97 char*, intptr_t, oop, intptr_t, int);
aoqi@0 98 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__clinit,
aoqi@0 99 char*, intptr_t, oop, intptr_t, int);
aoqi@0 100 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__error,
aoqi@0 101 char*, intptr_t, oop, intptr_t, int);
aoqi@0 102 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__end,
aoqi@0 103 char*, intptr_t, oop, intptr_t, int);
aoqi@0 104
aoqi@0 105 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) \
aoqi@0 106 { \
aoqi@0 107 char* data = NULL; \
aoqi@0 108 int len = 0; \
aoqi@0 109 Symbol* name = (clss)->name(); \
aoqi@0 110 if (name != NULL) { \
aoqi@0 111 data = (char*)name->bytes(); \
aoqi@0 112 len = name->utf8_length(); \
aoqi@0 113 } \
aoqi@0 114 HS_DTRACE_PROBE4(hotspot, class__initialization__##type, \
aoqi@423 115 data, len, (void *)(clss)->class_loader(), thread_type); \
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) \
aoqi@0 119 { \
aoqi@0 120 char* data = NULL; \
aoqi@0 121 int len = 0; \
aoqi@0 122 Symbol* name = (clss)->name(); \
aoqi@0 123 if (name != NULL) { \
aoqi@0 124 data = (char*)name->bytes(); \
aoqi@0 125 len = name->utf8_length(); \
aoqi@0 126 } \
aoqi@0 127 HS_DTRACE_PROBE5(hotspot, class__initialization__##type, \
aoqi@423 128 data, len, (void *)(clss)->class_loader(), thread_type, wait); \
aoqi@0 129 }
aoqi@0 130 #else /* USDT2 */
aoqi@0 131
aoqi@0 132 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED
aoqi@0 133 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE
aoqi@0 134 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT
aoqi@0 135 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
aoqi@0 136 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
aoqi@0 137 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
aoqi@0 138 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
aoqi@0 139 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
aoqi@0 140 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) \
aoqi@0 141 { \
aoqi@0 142 char* data = NULL; \
aoqi@0 143 int len = 0; \
aoqi@0 144 Symbol* name = (clss)->name(); \
aoqi@0 145 if (name != NULL) { \
aoqi@0 146 data = (char*)name->bytes(); \
aoqi@0 147 len = name->utf8_length(); \
aoqi@0 148 } \
aoqi@0 149 HOTSPOT_CLASS_INITIALIZATION_##type( \
aoqi@0 150 data, len, (clss)->class_loader(), thread_type); \
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) \
aoqi@0 154 { \
aoqi@0 155 char* data = NULL; \
aoqi@0 156 int len = 0; \
aoqi@0 157 Symbol* name = (clss)->name(); \
aoqi@0 158 if (name != NULL) { \
aoqi@0 159 data = (char*)name->bytes(); \
aoqi@0 160 len = name->utf8_length(); \
aoqi@0 161 } \
aoqi@0 162 HOTSPOT_CLASS_INITIALIZATION_##type( \
aoqi@0 163 data, len, (clss)->class_loader(), thread_type, wait); \
aoqi@0 164 }
aoqi@0 165 #endif /* USDT2 */
aoqi@0 166
aoqi@0 167 #else // ndef DTRACE_ENABLED
aoqi@0 168
aoqi@0 169 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type)
aoqi@0 170 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait)
aoqi@0 171
aoqi@0 172 #endif // ndef DTRACE_ENABLED
aoqi@0 173
aoqi@0 174 volatile int InstanceKlass::_total_instanceKlass_count = 0;
aoqi@0 175
aoqi@0 176 InstanceKlass* InstanceKlass::allocate_instance_klass(
aoqi@0 177 ClassLoaderData* loader_data,
aoqi@0 178 int vtable_len,
aoqi@0 179 int itable_len,
aoqi@0 180 int static_field_size,
aoqi@0 181 int nonstatic_oop_map_size,
aoqi@0 182 ReferenceType rt,
aoqi@0 183 AccessFlags access_flags,
aoqi@0 184 Symbol* name,
aoqi@0 185 Klass* super_klass,
aoqi@0 186 bool is_anonymous,
aoqi@0 187 TRAPS) {
aoqi@0 188
aoqi@0 189 int size = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size,
aoqi@0 190 access_flags.is_interface(), is_anonymous);
aoqi@0 191
aoqi@0 192 // Allocation
aoqi@0 193 InstanceKlass* ik;
aoqi@0 194 if (rt == REF_NONE) {
aoqi@0 195 if (name == vmSymbols::java_lang_Class()) {
aoqi@0 196 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(
aoqi@0 197 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
aoqi@0 198 access_flags, is_anonymous);
aoqi@0 199 } else if (name == vmSymbols::java_lang_ClassLoader() ||
aoqi@0 200 (SystemDictionary::ClassLoader_klass_loaded() &&
aoqi@0 201 super_klass != NULL &&
aoqi@0 202 super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass()))) {
aoqi@0 203 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(
aoqi@0 204 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
aoqi@0 205 access_flags, is_anonymous);
aoqi@0 206 } else {
aoqi@0 207 // normal class
aoqi@0 208 ik = new (loader_data, size, THREAD) InstanceKlass(
aoqi@0 209 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
aoqi@0 210 access_flags, is_anonymous);
aoqi@0 211 }
aoqi@0 212 } else {
aoqi@0 213 // reference klass
aoqi@0 214 ik = new (loader_data, size, THREAD) InstanceRefKlass(
aoqi@0 215 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
aoqi@0 216 access_flags, is_anonymous);
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 // Check for pending exception before adding to the loader data and incrementing
aoqi@0 220 // class count. Can get OOM here.
aoqi@0 221 if (HAS_PENDING_EXCEPTION) {
aoqi@0 222 return NULL;
aoqi@0 223 }
aoqi@0 224
aoqi@0 225 // Add all classes to our internal class loader list here,
aoqi@0 226 // including classes in the bootstrap (NULL) class loader.
aoqi@0 227 loader_data->add_class(ik);
aoqi@0 228
aoqi@0 229 Atomic::inc(&_total_instanceKlass_count);
aoqi@0 230 return ik;
aoqi@0 231 }
aoqi@0 232
aoqi@0 233
aoqi@0 234 // copy method ordering from resource area to Metaspace
aoqi@0 235 void InstanceKlass::copy_method_ordering(intArray* m, TRAPS) {
aoqi@0 236 if (m != NULL) {
aoqi@0 237 // allocate a new array and copy contents (memcpy?)
aoqi@0 238 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
aoqi@0 239 for (int i = 0; i < m->length(); i++) {
aoqi@0 240 _method_ordering->at_put(i, m->at(i));
aoqi@0 241 }
aoqi@0 242 } else {
aoqi@0 243 _method_ordering = Universe::the_empty_int_array();
aoqi@0 244 }
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 // create a new array of vtable_indices for default methods
aoqi@0 248 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
aoqi@0 249 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
aoqi@0 250 assert(default_vtable_indices() == NULL, "only create once");
aoqi@0 251 set_default_vtable_indices(vtable_indices);
aoqi@0 252 return vtable_indices;
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 InstanceKlass::InstanceKlass(int vtable_len,
aoqi@0 256 int itable_len,
aoqi@0 257 int static_field_size,
aoqi@0 258 int nonstatic_oop_map_size,
aoqi@0 259 ReferenceType rt,
aoqi@0 260 AccessFlags access_flags,
aoqi@0 261 bool is_anonymous) {
aoqi@0 262 No_Safepoint_Verifier no_safepoint; // until k becomes parsable
aoqi@0 263
aoqi@0 264 int iksize = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size,
aoqi@0 265 access_flags.is_interface(), is_anonymous);
aoqi@0 266
aoqi@0 267 set_vtable_length(vtable_len);
aoqi@0 268 set_itable_length(itable_len);
aoqi@0 269 set_static_field_size(static_field_size);
aoqi@0 270 set_nonstatic_oop_map_size(nonstatic_oop_map_size);
aoqi@0 271 set_access_flags(access_flags);
aoqi@0 272 _misc_flags = 0; // initialize to zero
aoqi@0 273 set_is_anonymous(is_anonymous);
aoqi@0 274 assert(size() == iksize, "wrong size for object");
aoqi@0 275
aoqi@0 276 set_array_klasses(NULL);
aoqi@0 277 set_methods(NULL);
aoqi@0 278 set_method_ordering(NULL);
aoqi@0 279 set_default_methods(NULL);
aoqi@0 280 set_default_vtable_indices(NULL);
aoqi@0 281 set_local_interfaces(NULL);
aoqi@0 282 set_transitive_interfaces(NULL);
aoqi@0 283 init_implementor();
aoqi@0 284 set_fields(NULL, 0);
aoqi@0 285 set_constants(NULL);
aoqi@0 286 set_class_loader_data(NULL);
aoqi@0 287 set_source_file_name_index(0);
aoqi@0 288 set_source_debug_extension(NULL, 0);
aoqi@0 289 set_array_name(NULL);
aoqi@0 290 set_inner_classes(NULL);
aoqi@0 291 set_static_oop_field_count(0);
aoqi@0 292 set_nonstatic_field_size(0);
aoqi@0 293 set_is_marked_dependent(false);
stefank@6992 294 set_has_unloaded_dependent(false);
aoqi@0 295 set_init_state(InstanceKlass::allocated);
aoqi@0 296 set_init_thread(NULL);
aoqi@0 297 set_reference_type(rt);
aoqi@0 298 set_oop_map_cache(NULL);
aoqi@0 299 set_jni_ids(NULL);
aoqi@0 300 set_osr_nmethods_head(NULL);
aoqi@0 301 set_breakpoints(NULL);
aoqi@0 302 init_previous_versions();
aoqi@0 303 set_generic_signature_index(0);
aoqi@0 304 release_set_methods_jmethod_ids(NULL);
aoqi@0 305 set_annotations(NULL);
aoqi@0 306 set_jvmti_cached_class_field_map(NULL);
aoqi@0 307 set_initial_method_idnum(0);
aoqi@0 308 _dependencies = NULL;
aoqi@0 309 set_jvmti_cached_class_field_map(NULL);
aoqi@0 310 set_cached_class_file(NULL);
aoqi@0 311 set_initial_method_idnum(0);
aoqi@0 312 set_minor_version(0);
aoqi@0 313 set_major_version(0);
aoqi@0 314 NOT_PRODUCT(_verify_count = 0;)
aoqi@0 315
aoqi@0 316 // initialize the non-header words to zero
aoqi@0 317 intptr_t* p = (intptr_t*)this;
aoqi@0 318 for (int index = InstanceKlass::header_size(); index < iksize; index++) {
aoqi@0 319 p[index] = NULL_WORD;
aoqi@0 320 }
aoqi@0 321
aoqi@0 322 // Set temporary value until parseClassFile updates it with the real instance
aoqi@0 323 // size.
aoqi@0 324 set_layout_helper(Klass::instance_layout_helper(0, true));
aoqi@0 325 }
aoqi@0 326
aoqi@0 327
aoqi@0 328 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
aoqi@0 329 Array<Method*>* methods) {
aoqi@0 330 if (methods != NULL && methods != Universe::the_empty_method_array() &&
aoqi@0 331 !methods->is_shared()) {
aoqi@0 332 for (int i = 0; i < methods->length(); i++) {
aoqi@0 333 Method* method = methods->at(i);
aoqi@0 334 if (method == NULL) continue; // maybe null if error processing
aoqi@0 335 // Only want to delete methods that are not executing for RedefineClasses.
aoqi@0 336 // The previous version will point to them so they're not totally dangling
aoqi@0 337 assert (!method->on_stack(), "shouldn't be called with methods on stack");
aoqi@0 338 MetadataFactory::free_metadata(loader_data, method);
aoqi@0 339 }
aoqi@0 340 MetadataFactory::free_array<Method*>(loader_data, methods);
aoqi@0 341 }
aoqi@0 342 }
aoqi@0 343
aoqi@0 344 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
aoqi@0 345 Klass* super_klass,
aoqi@0 346 Array<Klass*>* local_interfaces,
aoqi@0 347 Array<Klass*>* transitive_interfaces) {
aoqi@0 348 // Only deallocate transitive interfaces if not empty, same as super class
aoqi@0 349 // or same as local interfaces. See code in parseClassFile.
aoqi@0 350 Array<Klass*>* ti = transitive_interfaces;
aoqi@0 351 if (ti != Universe::the_empty_klass_array() && ti != local_interfaces) {
aoqi@0 352 // check that the interfaces don't come from super class
aoqi@0 353 Array<Klass*>* sti = (super_klass == NULL) ? NULL :
aoqi@0 354 InstanceKlass::cast(super_klass)->transitive_interfaces();
aoqi@0 355 if (ti != sti && ti != NULL && !ti->is_shared()) {
aoqi@0 356 MetadataFactory::free_array<Klass*>(loader_data, ti);
aoqi@0 357 }
aoqi@0 358 }
aoqi@0 359
aoqi@0 360 // local interfaces can be empty
aoqi@0 361 if (local_interfaces != Universe::the_empty_klass_array() &&
aoqi@0 362 local_interfaces != NULL && !local_interfaces->is_shared()) {
aoqi@0 363 MetadataFactory::free_array<Klass*>(loader_data, local_interfaces);
aoqi@0 364 }
aoqi@0 365 }
aoqi@0 366
aoqi@0 367 // This function deallocates the metadata and C heap pointers that the
aoqi@0 368 // InstanceKlass points to.
aoqi@0 369 void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
aoqi@0 370
aoqi@0 371 // Orphan the mirror first, CMS thinks it's still live.
aoqi@0 372 if (java_mirror() != NULL) {
aoqi@0 373 java_lang_Class::set_klass(java_mirror(), NULL);
aoqi@0 374 }
aoqi@0 375
aoqi@0 376 // Need to take this class off the class loader data list.
aoqi@0 377 loader_data->remove_class(this);
aoqi@0 378
aoqi@0 379 // The array_klass for this class is created later, after error handling.
aoqi@0 380 // For class redefinition, we keep the original class so this scratch class
aoqi@0 381 // doesn't have an array class. Either way, assert that there is nothing
aoqi@0 382 // to deallocate.
aoqi@0 383 assert(array_klasses() == NULL, "array classes shouldn't be created for this class yet");
aoqi@0 384
aoqi@0 385 // Release C heap allocated data that this might point to, which includes
aoqi@0 386 // reference counting symbol names.
aoqi@0 387 release_C_heap_structures();
aoqi@0 388
aoqi@0 389 deallocate_methods(loader_data, methods());
aoqi@0 390 set_methods(NULL);
aoqi@0 391
aoqi@0 392 if (method_ordering() != NULL &&
aoqi@0 393 method_ordering() != Universe::the_empty_int_array() &&
aoqi@0 394 !method_ordering()->is_shared()) {
aoqi@0 395 MetadataFactory::free_array<int>(loader_data, method_ordering());
aoqi@0 396 }
aoqi@0 397 set_method_ordering(NULL);
aoqi@0 398
aoqi@0 399 // default methods can be empty
aoqi@0 400 if (default_methods() != NULL &&
aoqi@0 401 default_methods() != Universe::the_empty_method_array() &&
aoqi@0 402 !default_methods()->is_shared()) {
aoqi@0 403 MetadataFactory::free_array<Method*>(loader_data, default_methods());
aoqi@0 404 }
aoqi@0 405 // Do NOT deallocate the default methods, they are owned by superinterfaces.
aoqi@0 406 set_default_methods(NULL);
aoqi@0 407
aoqi@0 408 // default methods vtable indices can be empty
aoqi@0 409 if (default_vtable_indices() != NULL &&
aoqi@0 410 !default_vtable_indices()->is_shared()) {
aoqi@0 411 MetadataFactory::free_array<int>(loader_data, default_vtable_indices());
aoqi@0 412 }
aoqi@0 413 set_default_vtable_indices(NULL);
aoqi@0 414
aoqi@0 415
aoqi@0 416 // This array is in Klass, but remove it with the InstanceKlass since
aoqi@0 417 // this place would be the only caller and it can share memory with transitive
aoqi@0 418 // interfaces.
aoqi@0 419 if (secondary_supers() != NULL &&
aoqi@0 420 secondary_supers() != Universe::the_empty_klass_array() &&
aoqi@0 421 secondary_supers() != transitive_interfaces() &&
aoqi@0 422 !secondary_supers()->is_shared()) {
aoqi@0 423 MetadataFactory::free_array<Klass*>(loader_data, secondary_supers());
aoqi@0 424 }
aoqi@0 425 set_secondary_supers(NULL);
aoqi@0 426
aoqi@0 427 deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
aoqi@0 428 set_transitive_interfaces(NULL);
aoqi@0 429 set_local_interfaces(NULL);
aoqi@0 430
aoqi@0 431 if (fields() != NULL && !fields()->is_shared()) {
aoqi@0 432 MetadataFactory::free_array<jushort>(loader_data, fields());
aoqi@0 433 }
aoqi@0 434 set_fields(NULL, 0);
aoqi@0 435
aoqi@0 436 // If a method from a redefined class is using this constant pool, don't
aoqi@0 437 // delete it, yet. The new class's previous version will point to this.
aoqi@0 438 if (constants() != NULL) {
aoqi@0 439 assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
aoqi@0 440 if (!constants()->is_shared()) {
aoqi@0 441 MetadataFactory::free_metadata(loader_data, constants());
aoqi@0 442 }
aeriksso@8042 443 // Delete any cached resolution errors for the constant pool
aeriksso@8042 444 SystemDictionary::delete_resolution_error(constants());
aeriksso@8042 445
aoqi@0 446 set_constants(NULL);
aoqi@0 447 }
aoqi@0 448
aoqi@0 449 if (inner_classes() != NULL &&
aoqi@0 450 inner_classes() != Universe::the_empty_short_array() &&
aoqi@0 451 !inner_classes()->is_shared()) {
aoqi@0 452 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
aoqi@0 453 }
aoqi@0 454 set_inner_classes(NULL);
aoqi@0 455
aoqi@0 456 // We should deallocate the Annotations instance if it's not in shared spaces.
aoqi@0 457 if (annotations() != NULL && !annotations()->is_shared()) {
aoqi@0 458 MetadataFactory::free_metadata(loader_data, annotations());
aoqi@0 459 }
aoqi@0 460 set_annotations(NULL);
aoqi@0 461 }
aoqi@0 462
aoqi@0 463 bool InstanceKlass::should_be_initialized() const {
aoqi@0 464 return !is_initialized();
aoqi@0 465 }
aoqi@0 466
aoqi@0 467 klassVtable* InstanceKlass::vtable() const {
aoqi@0 468 return new klassVtable(this, start_of_vtable(), vtable_length() / vtableEntry::size());
aoqi@0 469 }
aoqi@0 470
aoqi@0 471 klassItable* InstanceKlass::itable() const {
aoqi@0 472 return new klassItable(instanceKlassHandle(this));
aoqi@0 473 }
aoqi@0 474
aoqi@0 475 void InstanceKlass::eager_initialize(Thread *thread) {
aoqi@0 476 if (!EagerInitialization) return;
aoqi@0 477
aoqi@0 478 if (this->is_not_initialized()) {
aoqi@0 479 // abort if the the class has a class initializer
aoqi@0 480 if (this->class_initializer() != NULL) return;
aoqi@0 481
aoqi@0 482 // abort if it is java.lang.Object (initialization is handled in genesis)
aoqi@0 483 Klass* super = this->super();
aoqi@0 484 if (super == NULL) return;
aoqi@0 485
aoqi@0 486 // abort if the super class should be initialized
aoqi@0 487 if (!InstanceKlass::cast(super)->is_initialized()) return;
aoqi@0 488
aoqi@0 489 // call body to expose the this pointer
aoqi@0 490 instanceKlassHandle this_oop(thread, this);
aoqi@0 491 eager_initialize_impl(this_oop);
aoqi@0 492 }
aoqi@0 493 }
aoqi@0 494
aoqi@0 495 // JVMTI spec thinks there are signers and protection domain in the
aoqi@0 496 // instanceKlass. These accessors pretend these fields are there.
aoqi@0 497 // The hprof specification also thinks these fields are in InstanceKlass.
aoqi@0 498 oop InstanceKlass::protection_domain() const {
aoqi@0 499 // return the protection_domain from the mirror
aoqi@0 500 return java_lang_Class::protection_domain(java_mirror());
aoqi@0 501 }
aoqi@0 502
aoqi@0 503 // To remove these from requires an incompatible change and CCC request.
aoqi@0 504 objArrayOop InstanceKlass::signers() const {
aoqi@0 505 // return the signers from the mirror
aoqi@0 506 return java_lang_Class::signers(java_mirror());
aoqi@0 507 }
aoqi@0 508
aoqi@0 509 oop InstanceKlass::init_lock() const {
aoqi@0 510 // return the init lock from the mirror
aoqi@0 511 oop lock = java_lang_Class::init_lock(java_mirror());
dholmes@7191 512 // Prevent reordering with any access of initialization state
dholmes@7191 513 OrderAccess::loadload();
aoqi@0 514 assert((oop)lock != NULL || !is_not_initialized(), // initialized or in_error state
aoqi@0 515 "only fully initialized state can have a null lock");
aoqi@0 516 return lock;
aoqi@0 517 }
aoqi@0 518
aoqi@0 519 // Set the initialization lock to null so the object can be GC'ed. Any racing
aoqi@0 520 // threads to get this lock will see a null lock and will not lock.
aoqi@0 521 // That's okay because they all check for initialized state after getting
aoqi@0 522 // the lock and return.
aoqi@0 523 void InstanceKlass::fence_and_clear_init_lock() {
aoqi@0 524 // make sure previous stores are all done, notably the init_state.
aoqi@0 525 OrderAccess::storestore();
aoqi@0 526 java_lang_Class::set_init_lock(java_mirror(), NULL);
aoqi@0 527 assert(!is_not_initialized(), "class must be initialized now");
aoqi@0 528 }
aoqi@0 529
aoqi@0 530 void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_oop) {
aoqi@0 531 EXCEPTION_MARK;
aoqi@0 532 oop init_lock = this_oop->init_lock();
aoqi@0 533 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
aoqi@0 534
aoqi@0 535 // abort if someone beat us to the initialization
aoqi@0 536 if (!this_oop->is_not_initialized()) return; // note: not equivalent to is_initialized()
aoqi@0 537
aoqi@0 538 ClassState old_state = this_oop->init_state();
aoqi@0 539 link_class_impl(this_oop, true, THREAD);
aoqi@0 540 if (HAS_PENDING_EXCEPTION) {
aoqi@0 541 CLEAR_PENDING_EXCEPTION;
aoqi@0 542 // Abort if linking the class throws an exception.
aoqi@0 543
aoqi@0 544 // Use a test to avoid redundantly resetting the state if there's
aoqi@0 545 // no change. Set_init_state() asserts that state changes make
aoqi@0 546 // progress, whereas here we might just be spinning in place.
aoqi@0 547 if( old_state != this_oop->_init_state )
aoqi@0 548 this_oop->set_init_state (old_state);
aoqi@0 549 } else {
aoqi@0 550 // linking successfull, mark class as initialized
aoqi@0 551 this_oop->set_init_state (fully_initialized);
aoqi@0 552 this_oop->fence_and_clear_init_lock();
aoqi@0 553 // trace
aoqi@0 554 if (TraceClassInitialization) {
aoqi@0 555 ResourceMark rm(THREAD);
aoqi@0 556 tty->print_cr("[Initialized %s without side effects]", this_oop->external_name());
aoqi@0 557 }
aoqi@0 558 }
aoqi@0 559 }
aoqi@0 560
aoqi@0 561
aoqi@0 562 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
aoqi@0 563 // process. The step comments refers to the procedure described in that section.
aoqi@0 564 // Note: implementation moved to static method to expose the this pointer.
aoqi@0 565 void InstanceKlass::initialize(TRAPS) {
aoqi@0 566 if (this->should_be_initialized()) {
aoqi@0 567 HandleMark hm(THREAD);
aoqi@0 568 instanceKlassHandle this_oop(THREAD, this);
aoqi@0 569 initialize_impl(this_oop, CHECK);
aoqi@0 570 // Note: at this point the class may be initialized
aoqi@0 571 // OR it may be in the state of being initialized
aoqi@0 572 // in case of recursive initialization!
aoqi@0 573 } else {
aoqi@0 574 assert(is_initialized(), "sanity check");
aoqi@0 575 }
aoqi@0 576 }
aoqi@0 577
aoqi@0 578
aoqi@0 579 bool InstanceKlass::verify_code(
aoqi@0 580 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) {
aoqi@0 581 // 1) Verify the bytecodes
aoqi@0 582 Verifier::Mode mode =
aoqi@0 583 throw_verifyerror ? Verifier::ThrowException : Verifier::NoException;
aoqi@0 584 return Verifier::verify(this_oop, mode, this_oop->should_verify_class(), CHECK_false);
aoqi@0 585 }
aoqi@0 586
aoqi@0 587
aoqi@0 588 // Used exclusively by the shared spaces dump mechanism to prevent
aoqi@0 589 // classes mapped into the shared regions in new VMs from appearing linked.
aoqi@0 590
aoqi@0 591 void InstanceKlass::unlink_class() {
aoqi@0 592 assert(is_linked(), "must be linked");
aoqi@0 593 _init_state = loaded;
aoqi@0 594 }
aoqi@0 595
aoqi@0 596 void InstanceKlass::link_class(TRAPS) {
aoqi@0 597 assert(is_loaded(), "must be loaded");
aoqi@0 598 if (!is_linked()) {
aoqi@0 599 HandleMark hm(THREAD);
aoqi@0 600 instanceKlassHandle this_oop(THREAD, this);
aoqi@0 601 link_class_impl(this_oop, true, CHECK);
aoqi@0 602 }
aoqi@0 603 }
aoqi@0 604
aoqi@0 605 // Called to verify that a class can link during initialization, without
aoqi@0 606 // throwing a VerifyError.
aoqi@0 607 bool InstanceKlass::link_class_or_fail(TRAPS) {
aoqi@0 608 assert(is_loaded(), "must be loaded");
aoqi@0 609 if (!is_linked()) {
aoqi@0 610 HandleMark hm(THREAD);
aoqi@0 611 instanceKlassHandle this_oop(THREAD, this);
aoqi@0 612 link_class_impl(this_oop, false, CHECK_false);
aoqi@0 613 }
aoqi@0 614 return is_linked();
aoqi@0 615 }
aoqi@0 616
aoqi@0 617 bool InstanceKlass::link_class_impl(
aoqi@0 618 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) {
aoqi@0 619 // check for error state
aoqi@0 620 if (this_oop->is_in_error_state()) {
aoqi@0 621 ResourceMark rm(THREAD);
aoqi@0 622 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(),
aoqi@0 623 this_oop->external_name(), false);
aoqi@0 624 }
aoqi@0 625 // return if already verified
aoqi@0 626 if (this_oop->is_linked()) {
aoqi@0 627 return true;
aoqi@0 628 }
aoqi@0 629
aoqi@0 630 // Timing
aoqi@0 631 // timer handles recursion
aoqi@0 632 assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl");
aoqi@0 633 JavaThread* jt = (JavaThread*)THREAD;
aoqi@0 634
aoqi@0 635 // link super class before linking this class
aoqi@0 636 instanceKlassHandle super(THREAD, this_oop->super());
aoqi@0 637 if (super.not_null()) {
aoqi@0 638 if (super->is_interface()) { // check if super class is an interface
aoqi@0 639 ResourceMark rm(THREAD);
aoqi@0 640 Exceptions::fthrow(
aoqi@0 641 THREAD_AND_LOCATION,
aoqi@0 642 vmSymbols::java_lang_IncompatibleClassChangeError(),
aoqi@0 643 "class %s has interface %s as super class",
aoqi@0 644 this_oop->external_name(),
aoqi@0 645 super->external_name()
aoqi@0 646 );
aoqi@0 647 return false;
aoqi@0 648 }
aoqi@0 649
aoqi@0 650 link_class_impl(super, throw_verifyerror, CHECK_false);
aoqi@0 651 }
aoqi@0 652
aoqi@0 653 // link all interfaces implemented by this class before linking this class
aoqi@0 654 Array<Klass*>* interfaces = this_oop->local_interfaces();
aoqi@0 655 int num_interfaces = interfaces->length();
aoqi@0 656 for (int index = 0; index < num_interfaces; index++) {
aoqi@0 657 HandleMark hm(THREAD);
aoqi@0 658 instanceKlassHandle ih(THREAD, interfaces->at(index));
aoqi@0 659 link_class_impl(ih, throw_verifyerror, CHECK_false);
aoqi@0 660 }
aoqi@0 661
aoqi@0 662 // in case the class is linked in the process of linking its superclasses
aoqi@0 663 if (this_oop->is_linked()) {
aoqi@0 664 return true;
aoqi@0 665 }
aoqi@0 666
aoqi@0 667 // trace only the link time for this klass that includes
aoqi@0 668 // the verification time
aoqi@0 669 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
aoqi@0 670 ClassLoader::perf_class_link_selftime(),
aoqi@0 671 ClassLoader::perf_classes_linked(),
aoqi@0 672 jt->get_thread_stat()->perf_recursion_counts_addr(),
aoqi@0 673 jt->get_thread_stat()->perf_timers_addr(),
aoqi@0 674 PerfClassTraceTime::CLASS_LINK);
aoqi@0 675
aoqi@0 676 // verification & rewriting
aoqi@0 677 {
aoqi@0 678 oop init_lock = this_oop->init_lock();
aoqi@0 679 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
aoqi@0 680 // rewritten will have been set if loader constraint error found
aoqi@0 681 // on an earlier link attempt
aoqi@0 682 // don't verify or rewrite if already rewritten
aoqi@0 683
aoqi@0 684 if (!this_oop->is_linked()) {
aoqi@0 685 if (!this_oop->is_rewritten()) {
aoqi@0 686 {
aoqi@0 687 // Timer includes any side effects of class verification (resolution,
aoqi@0 688 // etc), but not recursive entry into verify_code().
aoqi@0 689 PerfClassTraceTime timer(ClassLoader::perf_class_verify_time(),
aoqi@0 690 ClassLoader::perf_class_verify_selftime(),
aoqi@0 691 ClassLoader::perf_classes_verified(),
aoqi@0 692 jt->get_thread_stat()->perf_recursion_counts_addr(),
aoqi@0 693 jt->get_thread_stat()->perf_timers_addr(),
aoqi@0 694 PerfClassTraceTime::CLASS_VERIFY);
aoqi@0 695 bool verify_ok = verify_code(this_oop, throw_verifyerror, THREAD);
aoqi@0 696 if (!verify_ok) {
aoqi@0 697 return false;
aoqi@0 698 }
aoqi@0 699 }
aoqi@0 700
aoqi@0 701 // Just in case a side-effect of verify linked this class already
aoqi@0 702 // (which can sometimes happen since the verifier loads classes
aoqi@0 703 // using custom class loaders, which are free to initialize things)
aoqi@0 704 if (this_oop->is_linked()) {
aoqi@0 705 return true;
aoqi@0 706 }
aoqi@0 707
aoqi@0 708 // also sets rewritten
aoqi@0 709 this_oop->rewrite_class(CHECK_false);
iklam@8497 710 } else if (this_oop()->is_shared()) {
iklam@8497 711 ResourceMark rm(THREAD);
iklam@8497 712 char* message_buffer; // res-allocated by check_verification_dependencies
iklam@8497 713 Handle loader = this_oop()->class_loader();
iklam@8497 714 Handle pd = this_oop()->protection_domain();
iklam@8497 715 bool verified = SystemDictionaryShared::check_verification_dependencies(this_oop(),
iklam@8497 716 loader, pd, &message_buffer, THREAD);
iklam@8497 717 if (!verified) {
iklam@8497 718 THROW_MSG_(vmSymbols::java_lang_VerifyError(), message_buffer, false);
iklam@8497 719 }
aoqi@0 720 }
aoqi@0 721
aoqi@0 722 // relocate jsrs and link methods after they are all rewritten
aoqi@0 723 this_oop->link_methods(CHECK_false);
aoqi@0 724
aoqi@0 725 // Initialize the vtable and interface table after
aoqi@0 726 // methods have been rewritten since rewrite may
aoqi@0 727 // fabricate new Method*s.
aoqi@0 728 // also does loader constraint checking
jiangli@8509 729 //
jiangli@8509 730 // Initialize_vtable and initialize_itable need to be rerun for
jiangli@8509 731 // a shared class if the class is not loaded by the NULL classloader.
jiangli@8509 732 ClassLoaderData * loader_data = this_oop->class_loader_data();
jiangli@8509 733 if (!(this_oop()->is_shared() &&
jiangli@8509 734 loader_data->is_the_null_class_loader_data())) {
aoqi@0 735 ResourceMark rm(THREAD);
aoqi@0 736 this_oop->vtable()->initialize_vtable(true, CHECK_false);
aoqi@0 737 this_oop->itable()->initialize_itable(true, CHECK_false);
aoqi@0 738 }
aoqi@0 739 #ifdef ASSERT
aoqi@0 740 else {
aoqi@0 741 ResourceMark rm(THREAD);
aoqi@0 742 this_oop->vtable()->verify(tty, true);
aoqi@0 743 // In case itable verification is ever added.
aoqi@0 744 // this_oop->itable()->verify(tty, true);
aoqi@0 745 }
aoqi@0 746 #endif
aoqi@0 747 this_oop->set_init_state(linked);
aoqi@0 748 if (JvmtiExport::should_post_class_prepare()) {
aoqi@0 749 Thread *thread = THREAD;
aoqi@0 750 assert(thread->is_Java_thread(), "thread->is_Java_thread()");
aoqi@0 751 JvmtiExport::post_class_prepare((JavaThread *) thread, this_oop());
aoqi@0 752 }
aoqi@0 753 }
aoqi@0 754 }
aoqi@0 755 return true;
aoqi@0 756 }
aoqi@0 757
aoqi@0 758
aoqi@0 759 // Rewrite the byte codes of all of the methods of a class.
aoqi@0 760 // The rewriter must be called exactly once. Rewriting must happen after
aoqi@0 761 // verification but before the first method of the class is executed.
aoqi@0 762 void InstanceKlass::rewrite_class(TRAPS) {
aoqi@0 763 assert(is_loaded(), "must be loaded");
aoqi@0 764 instanceKlassHandle this_oop(THREAD, this);
aoqi@0 765 if (this_oop->is_rewritten()) {
aoqi@0 766 assert(this_oop()->is_shared(), "rewriting an unshared class?");
aoqi@0 767 return;
aoqi@0 768 }
aoqi@0 769 Rewriter::rewrite(this_oop, CHECK);
aoqi@0 770 this_oop->set_rewritten();
aoqi@0 771 }
aoqi@0 772
aoqi@0 773 // Now relocate and link method entry points after class is rewritten.
aoqi@0 774 // This is outside is_rewritten flag. In case of an exception, it can be
aoqi@0 775 // executed more than once.
aoqi@0 776 void InstanceKlass::link_methods(TRAPS) {
aoqi@0 777 int len = methods()->length();
aoqi@0 778 for (int i = len-1; i >= 0; i--) {
aoqi@0 779 methodHandle m(THREAD, methods()->at(i));
aoqi@0 780
aoqi@0 781 // Set up method entry points for compiler and interpreter .
aoqi@0 782 m->link_method(m, CHECK);
aoqi@0 783
aoqi@0 784 // This is for JVMTI and unrelated to relocator but the last thing we do
aoqi@0 785 #ifdef ASSERT
aoqi@0 786 if (StressMethodComparator) {
aoqi@0 787 ResourceMark rm(THREAD);
aoqi@0 788 static int nmc = 0;
aoqi@0 789 for (int j = i; j >= 0 && j >= i-4; j--) {
aoqi@0 790 if ((++nmc % 1000) == 0) tty->print_cr("Have run MethodComparator %d times...", nmc);
aoqi@0 791 bool z = MethodComparator::methods_EMCP(m(),
aoqi@0 792 methods()->at(j));
aoqi@0 793 if (j == i && !z) {
aoqi@0 794 tty->print("MethodComparator FAIL: "); m->print(); m->print_codes();
aoqi@0 795 assert(z, "method must compare equal to itself");
aoqi@0 796 }
aoqi@0 797 }
aoqi@0 798 }
aoqi@0 799 #endif //ASSERT
aoqi@0 800 }
aoqi@0 801 }
aoqi@0 802
acorn@7290 803 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
acorn@7290 804 void InstanceKlass::initialize_super_interfaces(instanceKlassHandle this_oop, TRAPS) {
acorn@7290 805 if (this_oop->has_default_methods()) {
acorn@7290 806 for (int i = 0; i < this_oop->local_interfaces()->length(); ++i) {
acorn@7290 807 Klass* iface = this_oop->local_interfaces()->at(i);
acorn@7290 808 InstanceKlass* ik = InstanceKlass::cast(iface);
acorn@7290 809 if (ik->should_be_initialized()) {
acorn@7290 810 if (ik->has_default_methods()) {
acorn@7290 811 ik->initialize_super_interfaces(ik, THREAD);
acorn@7290 812 }
acorn@7290 813 // Only initialize() interfaces that "declare" concrete methods.
acorn@7290 814 // has_default_methods drives searching superinterfaces since it
acorn@7290 815 // means has_default_methods in its superinterface hierarchy
acorn@7290 816 if (!HAS_PENDING_EXCEPTION && ik->declares_default_methods()) {
acorn@7290 817 ik->initialize(THREAD);
acorn@7290 818 }
acorn@7290 819 if (HAS_PENDING_EXCEPTION) {
acorn@7290 820 Handle e(THREAD, PENDING_EXCEPTION);
acorn@7290 821 CLEAR_PENDING_EXCEPTION;
acorn@7290 822 {
acorn@7290 823 EXCEPTION_MARK;
acorn@7290 824 // Locks object, set state, and notify all waiting threads
acorn@7290 825 this_oop->set_initialization_state_and_notify(
acorn@7290 826 initialization_error, THREAD);
acorn@7290 827
acorn@7290 828 // ignore any exception thrown, superclass initialization error is
acorn@7290 829 // thrown below
acorn@7290 830 CLEAR_PENDING_EXCEPTION;
acorn@7290 831 }
acorn@7290 832 THROW_OOP(e());
acorn@7290 833 }
acorn@7290 834 }
acorn@7290 835 }
acorn@7290 836 }
acorn@7290 837 }
aoqi@0 838
aoqi@0 839 void InstanceKlass::initialize_impl(instanceKlassHandle this_oop, TRAPS) {
aoqi@0 840 // Make sure klass is linked (verified) before initialization
aoqi@0 841 // A class could already be verified, since it has been reflected upon.
aoqi@0 842 this_oop->link_class(CHECK);
aoqi@0 843
aoqi@0 844 DTRACE_CLASSINIT_PROBE(required, InstanceKlass::cast(this_oop()), -1);
aoqi@0 845
aoqi@0 846 bool wait = false;
aoqi@0 847
aoqi@0 848 // refer to the JVM book page 47 for description of steps
aoqi@0 849 // Step 1
aoqi@0 850 {
aoqi@0 851 oop init_lock = this_oop->init_lock();
aoqi@0 852 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
aoqi@0 853
aoqi@0 854 Thread *self = THREAD; // it's passed the current thread
aoqi@0 855
aoqi@0 856 // Step 2
aoqi@0 857 // If we were to use wait() instead of waitInterruptibly() then
aoqi@0 858 // we might end up throwing IE from link/symbol resolution sites
aoqi@0 859 // that aren't expected to throw. This would wreak havoc. See 6320309.
aoqi@0 860 while(this_oop->is_being_initialized() && !this_oop->is_reentrant_initialization(self)) {
aoqi@0 861 wait = true;
aoqi@0 862 ol.waitUninterruptibly(CHECK);
aoqi@0 863 }
aoqi@0 864
aoqi@0 865 // Step 3
aoqi@0 866 if (this_oop->is_being_initialized() && this_oop->is_reentrant_initialization(self)) {
aoqi@0 867 DTRACE_CLASSINIT_PROBE_WAIT(recursive, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 868 return;
aoqi@0 869 }
aoqi@0 870
aoqi@0 871 // Step 4
aoqi@0 872 if (this_oop->is_initialized()) {
aoqi@0 873 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 874 return;
aoqi@0 875 }
aoqi@0 876
aoqi@0 877 // Step 5
aoqi@0 878 if (this_oop->is_in_error_state()) {
aoqi@0 879 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 880 ResourceMark rm(THREAD);
aoqi@0 881 const char* desc = "Could not initialize class ";
aoqi@0 882 const char* className = this_oop->external_name();
aoqi@0 883 size_t msglen = strlen(desc) + strlen(className) + 1;
aoqi@0 884 char* message = NEW_RESOURCE_ARRAY(char, msglen);
aoqi@0 885 if (NULL == message) {
aoqi@0 886 // Out of memory: can't create detailed error message
aoqi@0 887 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className);
aoqi@0 888 } else {
aoqi@0 889 jio_snprintf(message, msglen, "%s%s", desc, className);
aoqi@0 890 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message);
aoqi@0 891 }
aoqi@0 892 }
aoqi@0 893
aoqi@0 894 // Step 6
aoqi@0 895 this_oop->set_init_state(being_initialized);
aoqi@0 896 this_oop->set_init_thread(self);
aoqi@0 897 }
aoqi@0 898
aoqi@0 899 // Step 7
aoqi@0 900 Klass* super_klass = this_oop->super();
aoqi@0 901 if (super_klass != NULL && !this_oop->is_interface() && super_klass->should_be_initialized()) {
aoqi@0 902 super_klass->initialize(THREAD);
aoqi@0 903
aoqi@0 904 if (HAS_PENDING_EXCEPTION) {
aoqi@0 905 Handle e(THREAD, PENDING_EXCEPTION);
aoqi@0 906 CLEAR_PENDING_EXCEPTION;
aoqi@0 907 {
aoqi@0 908 EXCEPTION_MARK;
aoqi@0 909 this_oop->set_initialization_state_and_notify(initialization_error, THREAD); // Locks object, set state, and notify all waiting threads
aoqi@0 910 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, superclass initialization error is thrown below
aoqi@0 911 }
aoqi@0 912 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 913 THROW_OOP(e());
aoqi@0 914 }
aoqi@0 915 }
aoqi@0 916
acorn@7290 917 // Recursively initialize any superinterfaces that declare default methods
acorn@7290 918 // Only need to recurse if has_default_methods which includes declaring and
acorn@7290 919 // inheriting default methods
aoqi@0 920 if (this_oop->has_default_methods()) {
acorn@7290 921 this_oop->initialize_super_interfaces(this_oop, CHECK);
aoqi@0 922 }
aoqi@0 923
aoqi@0 924 // Step 8
aoqi@0 925 {
aoqi@0 926 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
aoqi@0 927 JavaThread* jt = (JavaThread*)THREAD;
aoqi@0 928 DTRACE_CLASSINIT_PROBE_WAIT(clinit, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 929 // Timer includes any side effects of class initialization (resolution,
aoqi@0 930 // etc), but not recursive entry into call_class_initializer().
aoqi@0 931 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
aoqi@0 932 ClassLoader::perf_class_init_selftime(),
aoqi@0 933 ClassLoader::perf_classes_inited(),
aoqi@0 934 jt->get_thread_stat()->perf_recursion_counts_addr(),
aoqi@0 935 jt->get_thread_stat()->perf_timers_addr(),
aoqi@0 936 PerfClassTraceTime::CLASS_CLINIT);
aoqi@0 937 this_oop->call_class_initializer(THREAD);
aoqi@0 938 }
aoqi@0 939
aoqi@0 940 // Step 9
aoqi@0 941 if (!HAS_PENDING_EXCEPTION) {
aoqi@0 942 this_oop->set_initialization_state_and_notify(fully_initialized, CHECK);
aoqi@0 943 { ResourceMark rm(THREAD);
aoqi@0 944 debug_only(this_oop->vtable()->verify(tty, true);)
aoqi@0 945 }
aoqi@0 946 }
aoqi@0 947 else {
aoqi@0 948 // Step 10 and 11
aoqi@0 949 Handle e(THREAD, PENDING_EXCEPTION);
aoqi@0 950 CLEAR_PENDING_EXCEPTION;
jbachorik@7812 951 // JVMTI has already reported the pending exception
jbachorik@7812 952 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
jbachorik@7812 953 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
aoqi@0 954 {
aoqi@0 955 EXCEPTION_MARK;
aoqi@0 956 this_oop->set_initialization_state_and_notify(initialization_error, THREAD);
aoqi@0 957 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
jbachorik@7812 958 // JVMTI has already reported the pending exception
jbachorik@7812 959 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
jbachorik@7812 960 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
aoqi@0 961 }
aoqi@0 962 DTRACE_CLASSINIT_PROBE_WAIT(error, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 963 if (e->is_a(SystemDictionary::Error_klass())) {
aoqi@0 964 THROW_OOP(e());
aoqi@0 965 } else {
aoqi@0 966 JavaCallArguments args(e);
aoqi@0 967 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
aoqi@0 968 vmSymbols::throwable_void_signature(),
aoqi@0 969 &args);
aoqi@0 970 }
aoqi@0 971 }
aoqi@0 972 DTRACE_CLASSINIT_PROBE_WAIT(end, InstanceKlass::cast(this_oop()), -1,wait);
aoqi@0 973 }
aoqi@0 974
aoqi@0 975
aoqi@0 976 // Note: implementation moved to static method to expose the this pointer.
aoqi@0 977 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
aoqi@0 978 instanceKlassHandle kh(THREAD, this);
aoqi@0 979 set_initialization_state_and_notify_impl(kh, state, CHECK);
aoqi@0 980 }
aoqi@0 981
aoqi@0 982 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_oop, ClassState state, TRAPS) {
aoqi@0 983 oop init_lock = this_oop->init_lock();
aoqi@0 984 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
aoqi@0 985 this_oop->set_init_state(state);
aoqi@0 986 this_oop->fence_and_clear_init_lock();
aoqi@0 987 ol.notify_all(CHECK);
aoqi@0 988 }
aoqi@0 989
aoqi@0 990 // The embedded _implementor field can only record one implementor.
aoqi@0 991 // When there are more than one implementors, the _implementor field
aoqi@0 992 // is set to the interface Klass* itself. Following are the possible
aoqi@0 993 // values for the _implementor field:
aoqi@0 994 // NULL - no implementor
aoqi@0 995 // implementor Klass* - one implementor
aoqi@0 996 // self - more than one implementor
aoqi@0 997 //
aoqi@0 998 // The _implementor field only exists for interfaces.
aoqi@0 999 void InstanceKlass::add_implementor(Klass* k) {
aoqi@0 1000 assert(Compile_lock->owned_by_self(), "");
aoqi@0 1001 assert(is_interface(), "not interface");
aoqi@0 1002 // Filter out my subinterfaces.
aoqi@0 1003 // (Note: Interfaces are never on the subklass list.)
aoqi@0 1004 if (InstanceKlass::cast(k)->is_interface()) return;
aoqi@0 1005
aoqi@0 1006 // Filter out subclasses whose supers already implement me.
aoqi@0 1007 // (Note: CHA must walk subclasses of direct implementors
aoqi@0 1008 // in order to locate indirect implementors.)
aoqi@0 1009 Klass* sk = InstanceKlass::cast(k)->super();
aoqi@0 1010 if (sk != NULL && InstanceKlass::cast(sk)->implements_interface(this))
aoqi@0 1011 // We only need to check one immediate superclass, since the
aoqi@0 1012 // implements_interface query looks at transitive_interfaces.
aoqi@0 1013 // Any supers of the super have the same (or fewer) transitive_interfaces.
aoqi@0 1014 return;
aoqi@0 1015
aoqi@0 1016 Klass* ik = implementor();
aoqi@0 1017 if (ik == NULL) {
aoqi@0 1018 set_implementor(k);
aoqi@0 1019 } else if (ik != this) {
aoqi@0 1020 // There is already an implementor. Use itself as an indicator of
aoqi@0 1021 // more than one implementors.
aoqi@0 1022 set_implementor(this);
aoqi@0 1023 }
aoqi@0 1024
aoqi@0 1025 // The implementor also implements the transitive_interfaces
aoqi@0 1026 for (int index = 0; index < local_interfaces()->length(); index++) {
aoqi@0 1027 InstanceKlass::cast(local_interfaces()->at(index))->add_implementor(k);
aoqi@0 1028 }
aoqi@0 1029 }
aoqi@0 1030
aoqi@0 1031 void InstanceKlass::init_implementor() {
aoqi@0 1032 if (is_interface()) {
aoqi@0 1033 set_implementor(NULL);
aoqi@0 1034 }
aoqi@0 1035 }
aoqi@0 1036
aoqi@0 1037
aoqi@0 1038 void InstanceKlass::process_interfaces(Thread *thread) {
aoqi@0 1039 // link this class into the implementors list of every interface it implements
aoqi@0 1040 Klass* this_as_klass_oop = this;
aoqi@0 1041 for (int i = local_interfaces()->length() - 1; i >= 0; i--) {
aoqi@0 1042 assert(local_interfaces()->at(i)->is_klass(), "must be a klass");
aoqi@0 1043 InstanceKlass* interf = InstanceKlass::cast(local_interfaces()->at(i));
aoqi@0 1044 assert(interf->is_interface(), "expected interface");
aoqi@0 1045 interf->add_implementor(this_as_klass_oop);
aoqi@0 1046 }
aoqi@0 1047 }
aoqi@0 1048
aoqi@0 1049 bool InstanceKlass::can_be_primary_super_slow() const {
aoqi@0 1050 if (is_interface())
aoqi@0 1051 return false;
aoqi@0 1052 else
aoqi@0 1053 return Klass::can_be_primary_super_slow();
aoqi@0 1054 }
aoqi@0 1055
aoqi@0 1056 GrowableArray<Klass*>* InstanceKlass::compute_secondary_supers(int num_extra_slots) {
aoqi@0 1057 // The secondaries are the implemented interfaces.
aoqi@0 1058 InstanceKlass* ik = InstanceKlass::cast(this);
aoqi@0 1059 Array<Klass*>* interfaces = ik->transitive_interfaces();
aoqi@0 1060 int num_secondaries = num_extra_slots + interfaces->length();
aoqi@0 1061 if (num_secondaries == 0) {
aoqi@0 1062 // Must share this for correct bootstrapping!
aoqi@0 1063 set_secondary_supers(Universe::the_empty_klass_array());
aoqi@0 1064 return NULL;
aoqi@0 1065 } else if (num_extra_slots == 0) {
aoqi@0 1066 // The secondary super list is exactly the same as the transitive interfaces.
aoqi@0 1067 // Redefine classes has to be careful not to delete this!
aoqi@0 1068 set_secondary_supers(interfaces);
aoqi@0 1069 return NULL;
aoqi@0 1070 } else {
aoqi@0 1071 // Copy transitive interfaces to a temporary growable array to be constructed
aoqi@0 1072 // into the secondary super list with extra slots.
aoqi@0 1073 GrowableArray<Klass*>* secondaries = new GrowableArray<Klass*>(interfaces->length());
aoqi@0 1074 for (int i = 0; i < interfaces->length(); i++) {
aoqi@0 1075 secondaries->push(interfaces->at(i));
aoqi@0 1076 }
aoqi@0 1077 return secondaries;
aoqi@0 1078 }
aoqi@0 1079 }
aoqi@0 1080
aoqi@0 1081 bool InstanceKlass::compute_is_subtype_of(Klass* k) {
aoqi@0 1082 if (k->is_interface()) {
aoqi@0 1083 return implements_interface(k);
aoqi@0 1084 } else {
aoqi@0 1085 return Klass::compute_is_subtype_of(k);
aoqi@0 1086 }
aoqi@0 1087 }
aoqi@0 1088
aoqi@0 1089 bool InstanceKlass::implements_interface(Klass* k) const {
aoqi@0 1090 if (this == k) return true;
aoqi@0 1091 assert(k->is_interface(), "should be an interface class");
aoqi@0 1092 for (int i = 0; i < transitive_interfaces()->length(); i++) {
aoqi@0 1093 if (transitive_interfaces()->at(i) == k) {
aoqi@0 1094 return true;
aoqi@0 1095 }
aoqi@0 1096 }
aoqi@0 1097 return false;
aoqi@0 1098 }
aoqi@0 1099
aoqi@0 1100 bool InstanceKlass::is_same_or_direct_interface(Klass *k) const {
aoqi@0 1101 // Verify direct super interface
aoqi@0 1102 if (this == k) return true;
aoqi@0 1103 assert(k->is_interface(), "should be an interface class");
aoqi@0 1104 for (int i = 0; i < local_interfaces()->length(); i++) {
aoqi@0 1105 if (local_interfaces()->at(i) == k) {
aoqi@0 1106 return true;
aoqi@0 1107 }
aoqi@0 1108 }
aoqi@0 1109 return false;
aoqi@0 1110 }
aoqi@0 1111
aoqi@0 1112 objArrayOop InstanceKlass::allocate_objArray(int n, int length, TRAPS) {
aoqi@0 1113 if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
aoqi@0 1114 if (length > arrayOopDesc::max_array_length(T_OBJECT)) {
aoqi@0 1115 report_java_out_of_memory("Requested array size exceeds VM limit");
aoqi@0 1116 JvmtiExport::post_array_size_exhausted();
aoqi@0 1117 THROW_OOP_0(Universe::out_of_memory_error_array_size());
aoqi@0 1118 }
aoqi@0 1119 int size = objArrayOopDesc::object_size(length);
aoqi@0 1120 Klass* ak = array_klass(n, CHECK_NULL);
aoqi@0 1121 KlassHandle h_ak (THREAD, ak);
aoqi@0 1122 objArrayOop o =
aoqi@0 1123 (objArrayOop)CollectedHeap::array_allocate(h_ak, size, length, CHECK_NULL);
aoqi@0 1124 return o;
aoqi@0 1125 }
aoqi@0 1126
aoqi@0 1127 instanceOop InstanceKlass::register_finalizer(instanceOop i, TRAPS) {
aoqi@0 1128 if (TraceFinalizerRegistration) {
aoqi@0 1129 tty->print("Registered ");
aoqi@0 1130 i->print_value_on(tty);
aoqi@0 1131 tty->print_cr(" (" INTPTR_FORMAT ") as finalizable", (address)i);
aoqi@0 1132 }
aoqi@0 1133 instanceHandle h_i(THREAD, i);
aoqi@0 1134 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
aoqi@0 1135 JavaValue result(T_VOID);
aoqi@0 1136 JavaCallArguments args(h_i);
aoqi@0 1137 methodHandle mh (THREAD, Universe::finalizer_register_method());
aoqi@0 1138 JavaCalls::call(&result, mh, &args, CHECK_NULL);
aoqi@0 1139 return h_i();
aoqi@0 1140 }
aoqi@0 1141
aoqi@0 1142 instanceOop InstanceKlass::allocate_instance(TRAPS) {
aoqi@0 1143 bool has_finalizer_flag = has_finalizer(); // Query before possible GC
aoqi@0 1144 int size = size_helper(); // Query before forming handle.
aoqi@0 1145
aoqi@0 1146 KlassHandle h_k(THREAD, this);
aoqi@0 1147
aoqi@0 1148 instanceOop i;
aoqi@0 1149
aoqi@0 1150 i = (instanceOop)CollectedHeap::obj_allocate(h_k, size, CHECK_NULL);
aoqi@0 1151 if (has_finalizer_flag && !RegisterFinalizersAtInit) {
aoqi@0 1152 i = register_finalizer(i, CHECK_NULL);
aoqi@0 1153 }
aoqi@0 1154 return i;
aoqi@0 1155 }
aoqi@0 1156
aoqi@0 1157 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
aoqi@0 1158 if (is_interface() || is_abstract()) {
aoqi@0 1159 ResourceMark rm(THREAD);
aoqi@0 1160 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
aoqi@0 1161 : vmSymbols::java_lang_InstantiationException(), external_name());
aoqi@0 1162 }
aoqi@0 1163 if (this == SystemDictionary::Class_klass()) {
aoqi@0 1164 ResourceMark rm(THREAD);
aoqi@0 1165 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
aoqi@0 1166 : vmSymbols::java_lang_IllegalAccessException(), external_name());
aoqi@0 1167 }
aoqi@0 1168 }
aoqi@0 1169
aoqi@0 1170 Klass* InstanceKlass::array_klass_impl(bool or_null, int n, TRAPS) {
aoqi@0 1171 instanceKlassHandle this_oop(THREAD, this);
aoqi@0 1172 return array_klass_impl(this_oop, or_null, n, THREAD);
aoqi@0 1173 }
aoqi@0 1174
aoqi@0 1175 Klass* InstanceKlass::array_klass_impl(instanceKlassHandle this_oop, bool or_null, int n, TRAPS) {
aoqi@0 1176 if (this_oop->array_klasses() == NULL) {
aoqi@0 1177 if (or_null) return NULL;
aoqi@0 1178
aoqi@0 1179 ResourceMark rm;
aoqi@0 1180 JavaThread *jt = (JavaThread *)THREAD;
aoqi@0 1181 {
aoqi@0 1182 // Atomic creation of array_klasses
aoqi@0 1183 MutexLocker mc(Compile_lock, THREAD); // for vtables
aoqi@0 1184 MutexLocker ma(MultiArray_lock, THREAD);
aoqi@0 1185
aoqi@0 1186 // Check if update has already taken place
aoqi@0 1187 if (this_oop->array_klasses() == NULL) {
aoqi@0 1188 Klass* k = ObjArrayKlass::allocate_objArray_klass(this_oop->class_loader_data(), 1, this_oop, CHECK_NULL);
aoqi@0 1189 this_oop->set_array_klasses(k);
aoqi@0 1190 }
aoqi@0 1191 }
aoqi@0 1192 }
aoqi@0 1193 // _this will always be set at this point
aoqi@0 1194 ObjArrayKlass* oak = (ObjArrayKlass*)this_oop->array_klasses();
aoqi@0 1195 if (or_null) {
aoqi@0 1196 return oak->array_klass_or_null(n);
aoqi@0 1197 }
aoqi@0 1198 return oak->array_klass(n, CHECK_NULL);
aoqi@0 1199 }
aoqi@0 1200
aoqi@0 1201 Klass* InstanceKlass::array_klass_impl(bool or_null, TRAPS) {
aoqi@0 1202 return array_klass_impl(or_null, 1, THREAD);
aoqi@0 1203 }
aoqi@0 1204
aoqi@0 1205 void InstanceKlass::call_class_initializer(TRAPS) {
aoqi@0 1206 instanceKlassHandle ik (THREAD, this);
aoqi@0 1207 call_class_initializer_impl(ik, THREAD);
aoqi@0 1208 }
aoqi@0 1209
aoqi@0 1210 static int call_class_initializer_impl_counter = 0; // for debugging
aoqi@0 1211
aoqi@0 1212 Method* InstanceKlass::class_initializer() {
aoqi@0 1213 Method* clinit = find_method(
aoqi@0 1214 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
aoqi@0 1215 if (clinit != NULL && clinit->has_valid_initializer_flags()) {
aoqi@0 1216 return clinit;
aoqi@0 1217 }
aoqi@0 1218 return NULL;
aoqi@0 1219 }
aoqi@0 1220
aoqi@0 1221 void InstanceKlass::call_class_initializer_impl(instanceKlassHandle this_oop, TRAPS) {
aoqi@0 1222 if (ReplayCompiles &&
aoqi@0 1223 (ReplaySuppressInitializers == 1 ||
aoqi@0 1224 ReplaySuppressInitializers >= 2 && this_oop->class_loader() != NULL)) {
aoqi@0 1225 // Hide the existence of the initializer for the purpose of replaying the compile
aoqi@0 1226 return;
aoqi@0 1227 }
aoqi@0 1228
aoqi@0 1229 methodHandle h_method(THREAD, this_oop->class_initializer());
aoqi@0 1230 assert(!this_oop->is_initialized(), "we cannot initialize twice");
aoqi@0 1231 if (TraceClassInitialization) {
aoqi@0 1232 tty->print("%d Initializing ", call_class_initializer_impl_counter++);
aoqi@0 1233 this_oop->name()->print_value();
aoqi@0 1234 tty->print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", (address)this_oop());
aoqi@0 1235 }
aoqi@0 1236 if (h_method() != NULL) {
aoqi@0 1237 JavaCallArguments args; // No arguments
aoqi@0 1238 JavaValue result(T_VOID);
aoqi@0 1239 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
aoqi@0 1240 }
aoqi@0 1241 }
aoqi@0 1242
aoqi@0 1243
aoqi@0 1244 void InstanceKlass::mask_for(methodHandle method, int bci,
aoqi@0 1245 InterpreterOopMap* entry_for) {
aoqi@0 1246 // Dirty read, then double-check under a lock.
aoqi@0 1247 if (_oop_map_cache == NULL) {
aoqi@0 1248 // Otherwise, allocate a new one.
aoqi@0 1249 MutexLocker x(OopMapCacheAlloc_lock);
aoqi@0 1250 // First time use. Allocate a cache in C heap
aoqi@0 1251 if (_oop_map_cache == NULL) {
aoqi@0 1252 // Release stores from OopMapCache constructor before assignment
aoqi@0 1253 // to _oop_map_cache. C++ compilers on ppc do not emit the
aoqi@0 1254 // required memory barrier only because of the volatile
aoqi@0 1255 // qualifier of _oop_map_cache.
aoqi@0 1256 OrderAccess::release_store_ptr(&_oop_map_cache, new OopMapCache());
aoqi@0 1257 }
aoqi@0 1258 }
aoqi@0 1259 // _oop_map_cache is constant after init; lookup below does is own locking.
aoqi@0 1260 _oop_map_cache->lookup(method, bci, entry_for);
aoqi@0 1261 }
aoqi@0 1262
aoqi@0 1263
aoqi@0 1264 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
aoqi@0 1265 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
aoqi@0 1266 Symbol* f_name = fs.name();
aoqi@0 1267 Symbol* f_sig = fs.signature();
aoqi@0 1268 if (f_name == name && f_sig == sig) {
aoqi@0 1269 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
aoqi@0 1270 return true;
aoqi@0 1271 }
aoqi@0 1272 }
aoqi@0 1273 return false;
aoqi@0 1274 }
aoqi@0 1275
aoqi@0 1276
aoqi@0 1277 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
aoqi@0 1278 const int n = local_interfaces()->length();
aoqi@0 1279 for (int i = 0; i < n; i++) {
aoqi@0 1280 Klass* intf1 = local_interfaces()->at(i);
aoqi@0 1281 assert(intf1->is_interface(), "just checking type");
aoqi@0 1282 // search for field in current interface
aoqi@0 1283 if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {
aoqi@0 1284 assert(fd->is_static(), "interface field must be static");
aoqi@0 1285 return intf1;
aoqi@0 1286 }
aoqi@0 1287 // search for field in direct superinterfaces
aoqi@0 1288 Klass* intf2 = InstanceKlass::cast(intf1)->find_interface_field(name, sig, fd);
aoqi@0 1289 if (intf2 != NULL) return intf2;
aoqi@0 1290 }
aoqi@0 1291 // otherwise field lookup fails
aoqi@0 1292 return NULL;
aoqi@0 1293 }
aoqi@0 1294
aoqi@0 1295
aoqi@0 1296 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
aoqi@0 1297 // search order according to newest JVM spec (5.4.3.2, p.167).
aoqi@0 1298 // 1) search for field in current klass
aoqi@0 1299 if (find_local_field(name, sig, fd)) {
aoqi@0 1300 return const_cast<InstanceKlass*>(this);
aoqi@0 1301 }
aoqi@0 1302 // 2) search for field recursively in direct superinterfaces
aoqi@0 1303 { Klass* intf = find_interface_field(name, sig, fd);
aoqi@0 1304 if (intf != NULL) return intf;
aoqi@0 1305 }
aoqi@0 1306 // 3) apply field lookup recursively if superclass exists
aoqi@0 1307 { Klass* supr = super();
aoqi@0 1308 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, fd);
aoqi@0 1309 }
aoqi@0 1310 // 4) otherwise field lookup fails
aoqi@0 1311 return NULL;
aoqi@0 1312 }
aoqi@0 1313
aoqi@0 1314
aoqi@0 1315 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
aoqi@0 1316 // search order according to newest JVM spec (5.4.3.2, p.167).
aoqi@0 1317 // 1) search for field in current klass
aoqi@0 1318 if (find_local_field(name, sig, fd)) {
aoqi@0 1319 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
aoqi@0 1320 }
aoqi@0 1321 // 2) search for field recursively in direct superinterfaces
aoqi@0 1322 if (is_static) {
aoqi@0 1323 Klass* intf = find_interface_field(name, sig, fd);
aoqi@0 1324 if (intf != NULL) return intf;
aoqi@0 1325 }
aoqi@0 1326 // 3) apply field lookup recursively if superclass exists
aoqi@0 1327 { Klass* supr = super();
aoqi@0 1328 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
aoqi@0 1329 }
aoqi@0 1330 // 4) otherwise field lookup fails
aoqi@0 1331 return NULL;
aoqi@0 1332 }
aoqi@0 1333
aoqi@0 1334
aoqi@0 1335 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
aoqi@0 1336 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
aoqi@0 1337 if (fs.offset() == offset) {
aoqi@0 1338 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
aoqi@0 1339 if (fd->is_static() == is_static) return true;
aoqi@0 1340 }
aoqi@0 1341 }
aoqi@0 1342 return false;
aoqi@0 1343 }
aoqi@0 1344
aoqi@0 1345
aoqi@0 1346 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
aoqi@0 1347 Klass* klass = const_cast<InstanceKlass*>(this);
aoqi@0 1348 while (klass != NULL) {
aoqi@0 1349 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
aoqi@0 1350 return true;
aoqi@0 1351 }
aoqi@0 1352 klass = klass->super();
aoqi@0 1353 }
aoqi@0 1354 return false;
aoqi@0 1355 }
aoqi@0 1356
aoqi@0 1357
aoqi@0 1358 void InstanceKlass::methods_do(void f(Method* method)) {
aoqi@0 1359 int len = methods()->length();
aoqi@0 1360 for (int index = 0; index < len; index++) {
aoqi@0 1361 Method* m = methods()->at(index);
aoqi@0 1362 assert(m->is_method(), "must be method");
aoqi@0 1363 f(m);
aoqi@0 1364 }
aoqi@0 1365 }
aoqi@0 1366
aoqi@0 1367
aoqi@0 1368 void InstanceKlass::do_local_static_fields(FieldClosure* cl) {
aoqi@0 1369 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
aoqi@0 1370 if (fs.access_flags().is_static()) {
aoqi@0 1371 fieldDescriptor& fd = fs.field_descriptor();
aoqi@0 1372 cl->do_field(&fd);
aoqi@0 1373 }
aoqi@0 1374 }
aoqi@0 1375 }
aoqi@0 1376
aoqi@0 1377
aoqi@0 1378 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) {
aoqi@0 1379 instanceKlassHandle h_this(THREAD, this);
aoqi@0 1380 do_local_static_fields_impl(h_this, f, mirror, CHECK);
aoqi@0 1381 }
aoqi@0 1382
aoqi@0 1383
aoqi@0 1384 void InstanceKlass::do_local_static_fields_impl(instanceKlassHandle this_k,
aoqi@0 1385 void f(fieldDescriptor* fd, Handle mirror, TRAPS), Handle mirror, TRAPS) {
aoqi@0 1386 for (JavaFieldStream fs(this_k()); !fs.done(); fs.next()) {
aoqi@0 1387 if (fs.access_flags().is_static()) {
aoqi@0 1388 fieldDescriptor& fd = fs.field_descriptor();
aoqi@0 1389 f(&fd, mirror, CHECK);
aoqi@0 1390 }
aoqi@0 1391 }
aoqi@0 1392 }
aoqi@0 1393
aoqi@0 1394
aoqi@0 1395 static int compare_fields_by_offset(int* a, int* b) {
aoqi@0 1396 return a[0] - b[0];
aoqi@0 1397 }
aoqi@0 1398
aoqi@0 1399 void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) {
aoqi@0 1400 InstanceKlass* super = superklass();
aoqi@0 1401 if (super != NULL) {
aoqi@0 1402 super->do_nonstatic_fields(cl);
aoqi@0 1403 }
aoqi@0 1404 fieldDescriptor fd;
aoqi@0 1405 int length = java_fields_count();
aoqi@0 1406 // In DebugInfo nonstatic fields are sorted by offset.
aoqi@0 1407 int* fields_sorted = NEW_C_HEAP_ARRAY(int, 2*(length+1), mtClass);
aoqi@0 1408 int j = 0;
aoqi@0 1409 for (int i = 0; i < length; i += 1) {
aoqi@0 1410 fd.reinitialize(this, i);
aoqi@0 1411 if (!fd.is_static()) {
aoqi@0 1412 fields_sorted[j + 0] = fd.offset();
aoqi@0 1413 fields_sorted[j + 1] = i;
aoqi@0 1414 j += 2;
aoqi@0 1415 }
aoqi@0 1416 }
aoqi@0 1417 if (j > 0) {
aoqi@0 1418 length = j;
aoqi@0 1419 // _sort_Fn is defined in growableArray.hpp.
aoqi@0 1420 qsort(fields_sorted, length/2, 2*sizeof(int), (_sort_Fn)compare_fields_by_offset);
aoqi@0 1421 for (int i = 0; i < length; i += 2) {
aoqi@0 1422 fd.reinitialize(this, fields_sorted[i + 1]);
aoqi@0 1423 assert(!fd.is_static() && fd.offset() == fields_sorted[i], "only nonstatic fields");
aoqi@0 1424 cl->do_field(&fd);
aoqi@0 1425 }
aoqi@0 1426 }
aoqi@0 1427 FREE_C_HEAP_ARRAY(int, fields_sorted, mtClass);
aoqi@0 1428 }
aoqi@0 1429
aoqi@0 1430
aoqi@0 1431 void InstanceKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {
aoqi@0 1432 if (array_klasses() != NULL)
aoqi@0 1433 ArrayKlass::cast(array_klasses())->array_klasses_do(f, THREAD);
aoqi@0 1434 }
aoqi@0 1435
aoqi@0 1436 void InstanceKlass::array_klasses_do(void f(Klass* k)) {
aoqi@0 1437 if (array_klasses() != NULL)
aoqi@0 1438 ArrayKlass::cast(array_klasses())->array_klasses_do(f);
aoqi@0 1439 }
aoqi@0 1440
aoqi@0 1441 #ifdef ASSERT
aoqi@0 1442 static int linear_search(Array<Method*>* methods, Symbol* name, Symbol* signature) {
aoqi@0 1443 int len = methods->length();
aoqi@0 1444 for (int index = 0; index < len; index++) {
aoqi@0 1445 Method* m = methods->at(index);
aoqi@0 1446 assert(m->is_method(), "must be method");
aoqi@0 1447 if (m->signature() == signature && m->name() == name) {
aoqi@0 1448 return index;
aoqi@0 1449 }
aoqi@0 1450 }
aoqi@0 1451 return -1;
aoqi@0 1452 }
aoqi@0 1453 #endif
aoqi@0 1454
aoqi@0 1455 static int binary_search(Array<Method*>* methods, Symbol* name) {
aoqi@0 1456 int len = methods->length();
aoqi@0 1457 // methods are sorted, so do binary search
aoqi@0 1458 int l = 0;
aoqi@0 1459 int h = len - 1;
aoqi@0 1460 while (l <= h) {
aoqi@0 1461 int mid = (l + h) >> 1;
aoqi@0 1462 Method* m = methods->at(mid);
aoqi@0 1463 assert(m->is_method(), "must be method");
aoqi@0 1464 int res = m->name()->fast_compare(name);
aoqi@0 1465 if (res == 0) {
aoqi@0 1466 return mid;
aoqi@0 1467 } else if (res < 0) {
aoqi@0 1468 l = mid + 1;
aoqi@0 1469 } else {
aoqi@0 1470 h = mid - 1;
aoqi@0 1471 }
aoqi@0 1472 }
aoqi@0 1473 return -1;
aoqi@0 1474 }
aoqi@0 1475
aoqi@0 1476 // find_method looks up the name/signature in the local methods array
aoqi@0 1477 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const {
aoqi@0 1478 return find_method_impl(name, signature, false);
aoqi@0 1479 }
aoqi@0 1480
aoqi@0 1481 Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature, bool skipping_overpass) const {
drchase@7499 1482 return InstanceKlass::find_method_impl(methods(), name, signature, skipping_overpass, false);
aoqi@0 1483 }
aoqi@0 1484
aoqi@0 1485 // find_instance_method looks up the name/signature in the local methods array
aoqi@0 1486 // and skips over static methods
aoqi@0 1487 Method* InstanceKlass::find_instance_method(
aoqi@0 1488 Array<Method*>* methods, Symbol* name, Symbol* signature) {
drchase@7499 1489 Method* meth = InstanceKlass::find_method_impl(methods, name, signature, false, true);
aoqi@0 1490 return meth;
aoqi@0 1491 }
aoqi@0 1492
drchase@7499 1493 // find_instance_method looks up the name/signature in the local methods array
drchase@7499 1494 // and skips over static methods
drchase@7499 1495 Method* InstanceKlass::find_instance_method(Symbol* name, Symbol* signature) {
drchase@7499 1496 return InstanceKlass::find_instance_method(methods(), name, signature);
drchase@7499 1497 }
drchase@7499 1498
aoqi@0 1499 // find_method looks up the name/signature in the local methods array
aoqi@0 1500 Method* InstanceKlass::find_method(
aoqi@0 1501 Array<Method*>* methods, Symbol* name, Symbol* signature) {
drchase@7499 1502 return InstanceKlass::find_method_impl(methods, name, signature, false, false);
aoqi@0 1503 }
aoqi@0 1504
aoqi@0 1505 Method* InstanceKlass::find_method_impl(
drchase@7499 1506 Array<Method*>* methods, Symbol* name, Symbol* signature, bool skipping_overpass, bool skipping_static) {
drchase@7499 1507 int hit = find_method_index(methods, name, signature, skipping_overpass, skipping_static);
aoqi@0 1508 return hit >= 0 ? methods->at(hit): NULL;
aoqi@0 1509 }
aoqi@0 1510
drchase@7499 1511 bool InstanceKlass::method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static) {
drchase@7499 1512 return (m->signature() == signature) &&
drchase@7499 1513 (!skipping_overpass || !m->is_overpass()) &&
drchase@7499 1514 (!skipping_static || !m->is_static());
drchase@7499 1515 }
drchase@7499 1516
aoqi@0 1517 // Used directly for default_methods to find the index into the
aoqi@0 1518 // default_vtable_indices, and indirectly by find_method
aoqi@0 1519 // find_method_index looks in the local methods array to return the index
aoqi@0 1520 // of the matching name/signature. If, overpass methods are being ignored,
aoqi@0 1521 // the search continues to find a potential non-overpass match. This capability
aoqi@0 1522 // is important during method resolution to prefer a static method, for example,
aoqi@0 1523 // over an overpass method.
aoqi@0 1524 int InstanceKlass::find_method_index(
drchase@7499 1525 Array<Method*>* methods, Symbol* name, Symbol* signature, bool skipping_overpass, bool skipping_static) {
aoqi@0 1526 int hit = binary_search(methods, name);
aoqi@0 1527 if (hit != -1) {
aoqi@0 1528 Method* m = methods->at(hit);
drchase@7499 1529
aoqi@0 1530 // Do linear search to find matching signature. First, quick check
aoqi@0 1531 // for common case, ignoring overpasses if requested.
drchase@7499 1532 if (method_matches(m, signature, skipping_overpass, skipping_static)) return hit;
aoqi@0 1533
aoqi@0 1534 // search downwards through overloaded methods
aoqi@0 1535 int i;
aoqi@0 1536 for (i = hit - 1; i >= 0; --i) {
aoqi@0 1537 Method* m = methods->at(i);
aoqi@0 1538 assert(m->is_method(), "must be method");
aoqi@0 1539 if (m->name() != name) break;
drchase@7499 1540 if (method_matches(m, signature, skipping_overpass, skipping_static)) return i;
aoqi@0 1541 }
aoqi@0 1542 // search upwards
aoqi@0 1543 for (i = hit + 1; i < methods->length(); ++i) {
aoqi@0 1544 Method* m = methods->at(i);
aoqi@0 1545 assert(m->is_method(), "must be method");
aoqi@0 1546 if (m->name() != name) break;
drchase@7499 1547 if (method_matches(m, signature, skipping_overpass, skipping_static)) return i;
aoqi@0 1548 }
aoqi@0 1549 // not found
aoqi@0 1550 #ifdef ASSERT
drchase@7499 1551 int index = skipping_overpass || skipping_static ? -1 : linear_search(methods, name, signature);
aoqi@0 1552 assert(index == -1, err_msg("binary search should have found entry %d", index));
aoqi@0 1553 #endif
aoqi@0 1554 }
aoqi@0 1555 return -1;
aoqi@0 1556 }
aoqi@0 1557 int InstanceKlass::find_method_by_name(Symbol* name, int* end) {
aoqi@0 1558 return find_method_by_name(methods(), name, end);
aoqi@0 1559 }
aoqi@0 1560
aoqi@0 1561 int InstanceKlass::find_method_by_name(
aoqi@0 1562 Array<Method*>* methods, Symbol* name, int* end_ptr) {
aoqi@0 1563 assert(end_ptr != NULL, "just checking");
aoqi@0 1564 int start = binary_search(methods, name);
aoqi@0 1565 int end = start + 1;
aoqi@0 1566 if (start != -1) {
aoqi@0 1567 while (start - 1 >= 0 && (methods->at(start - 1))->name() == name) --start;
aoqi@0 1568 while (end < methods->length() && (methods->at(end))->name() == name) ++end;
aoqi@0 1569 *end_ptr = end;
aoqi@0 1570 return start;
aoqi@0 1571 }
aoqi@0 1572 return -1;
aoqi@0 1573 }
aoqi@0 1574
aoqi@0 1575 // uncached_lookup_method searches both the local class methods array and all
aoqi@0 1576 // superclasses methods arrays, skipping any overpass methods in superclasses.
aoqi@0 1577 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature, MethodLookupMode mode) const {
aoqi@0 1578 MethodLookupMode lookup_mode = mode;
aoqi@0 1579 Klass* klass = const_cast<InstanceKlass*>(this);
aoqi@0 1580 while (klass != NULL) {
aoqi@0 1581 Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, (lookup_mode == skip_overpass));
aoqi@0 1582 if (method != NULL) {
aoqi@0 1583 return method;
aoqi@0 1584 }
aoqi@0 1585 klass = InstanceKlass::cast(klass)->super();
aoqi@0 1586 lookup_mode = skip_overpass; // Always ignore overpass methods in superclasses
aoqi@0 1587 }
aoqi@0 1588 return NULL;
aoqi@0 1589 }
aoqi@0 1590
dsamersoff@8049 1591 #ifdef ASSERT
dsamersoff@8049 1592 // search through class hierarchy and return true if this class or
dsamersoff@8049 1593 // one of the superclasses was redefined
dsamersoff@8049 1594 bool InstanceKlass::has_redefined_this_or_super() const {
dsamersoff@8049 1595 const InstanceKlass* klass = this;
dsamersoff@8049 1596 while (klass != NULL) {
dsamersoff@8049 1597 if (klass->has_been_redefined()) {
dsamersoff@8049 1598 return true;
dsamersoff@8049 1599 }
dsamersoff@8049 1600 klass = InstanceKlass::cast(klass->super());
dsamersoff@8049 1601 }
dsamersoff@8049 1602 return false;
dsamersoff@8049 1603 }
dsamersoff@8049 1604 #endif
dsamersoff@8049 1605
aoqi@0 1606 // lookup a method in the default methods list then in all transitive interfaces
aoqi@0 1607 // Do NOT return private or static methods
aoqi@0 1608 Method* InstanceKlass::lookup_method_in_ordered_interfaces(Symbol* name,
aoqi@0 1609 Symbol* signature) const {
aoqi@0 1610 Method* m = NULL;
aoqi@0 1611 if (default_methods() != NULL) {
aoqi@0 1612 m = find_method(default_methods(), name, signature);
aoqi@0 1613 }
aoqi@0 1614 // Look up interfaces
aoqi@0 1615 if (m == NULL) {
aoqi@0 1616 m = lookup_method_in_all_interfaces(name, signature, normal);
aoqi@0 1617 }
aoqi@0 1618 return m;
aoqi@0 1619 }
aoqi@0 1620
aoqi@0 1621 // lookup a method in all the interfaces that this class implements
aoqi@0 1622 // Do NOT return private or static methods, new in JDK8 which are not externally visible
aoqi@0 1623 // They should only be found in the initial InterfaceMethodRef
aoqi@0 1624 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name,
aoqi@0 1625 Symbol* signature,
aoqi@0 1626 MethodLookupMode mode) const {
aoqi@0 1627 Array<Klass*>* all_ifs = transitive_interfaces();
aoqi@0 1628 int num_ifs = all_ifs->length();
aoqi@0 1629 InstanceKlass *ik = NULL;
aoqi@0 1630 for (int i = 0; i < num_ifs; i++) {
aoqi@0 1631 ik = InstanceKlass::cast(all_ifs->at(i));
aoqi@0 1632 Method* m = ik->lookup_method(name, signature);
aoqi@0 1633 if (m != NULL && m->is_public() && !m->is_static() &&
aoqi@0 1634 ((mode != skip_defaults) || !m->is_default_method())) {
aoqi@0 1635 return m;
aoqi@0 1636 }
aoqi@0 1637 }
aoqi@0 1638 return NULL;
aoqi@0 1639 }
aoqi@0 1640
aoqi@0 1641 /* jni_id_for_impl for jfieldIds only */
aoqi@0 1642 JNIid* InstanceKlass::jni_id_for_impl(instanceKlassHandle this_oop, int offset) {
aoqi@0 1643 MutexLocker ml(JfieldIdCreation_lock);
aoqi@0 1644 // Retry lookup after we got the lock
aoqi@0 1645 JNIid* probe = this_oop->jni_ids() == NULL ? NULL : this_oop->jni_ids()->find(offset);
aoqi@0 1646 if (probe == NULL) {
aoqi@0 1647 // Slow case, allocate new static field identifier
aoqi@0 1648 probe = new JNIid(this_oop(), offset, this_oop->jni_ids());
aoqi@0 1649 this_oop->set_jni_ids(probe);
aoqi@0 1650 }
aoqi@0 1651 return probe;
aoqi@0 1652 }
aoqi@0 1653
aoqi@0 1654
aoqi@0 1655 /* jni_id_for for jfieldIds only */
aoqi@0 1656 JNIid* InstanceKlass::jni_id_for(int offset) {
aoqi@0 1657 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset);
aoqi@0 1658 if (probe == NULL) {
aoqi@0 1659 probe = jni_id_for_impl(this, offset);
aoqi@0 1660 }
aoqi@0 1661 return probe;
aoqi@0 1662 }
aoqi@0 1663
aoqi@0 1664 u2 InstanceKlass::enclosing_method_data(int offset) {
aoqi@0 1665 Array<jushort>* inner_class_list = inner_classes();
aoqi@0 1666 if (inner_class_list == NULL) {
aoqi@0 1667 return 0;
aoqi@0 1668 }
aoqi@0 1669 int length = inner_class_list->length();
aoqi@0 1670 if (length % inner_class_next_offset == 0) {
aoqi@0 1671 return 0;
aoqi@0 1672 } else {
aoqi@0 1673 int index = length - enclosing_method_attribute_size;
aoqi@0 1674 assert(offset < enclosing_method_attribute_size, "invalid offset");
aoqi@0 1675 return inner_class_list->at(index + offset);
aoqi@0 1676 }
aoqi@0 1677 }
aoqi@0 1678
aoqi@0 1679 void InstanceKlass::set_enclosing_method_indices(u2 class_index,
aoqi@0 1680 u2 method_index) {
aoqi@0 1681 Array<jushort>* inner_class_list = inner_classes();
aoqi@0 1682 assert (inner_class_list != NULL, "_inner_classes list is not set up");
aoqi@0 1683 int length = inner_class_list->length();
aoqi@0 1684 if (length % inner_class_next_offset == enclosing_method_attribute_size) {
aoqi@0 1685 int index = length - enclosing_method_attribute_size;
aoqi@0 1686 inner_class_list->at_put(
aoqi@0 1687 index + enclosing_method_class_index_offset, class_index);
aoqi@0 1688 inner_class_list->at_put(
aoqi@0 1689 index + enclosing_method_method_index_offset, method_index);
aoqi@0 1690 }
aoqi@0 1691 }
aoqi@0 1692
aoqi@0 1693 // Lookup or create a jmethodID.
aoqi@0 1694 // This code is called by the VMThread and JavaThreads so the
aoqi@0 1695 // locking has to be done very carefully to avoid deadlocks
aoqi@0 1696 // and/or other cache consistency problems.
aoqi@0 1697 //
aoqi@0 1698 jmethodID InstanceKlass::get_jmethod_id(instanceKlassHandle ik_h, methodHandle method_h) {
aoqi@0 1699 size_t idnum = (size_t)method_h->method_idnum();
aoqi@0 1700 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
aoqi@0 1701 size_t length = 0;
aoqi@0 1702 jmethodID id = NULL;
aoqi@0 1703
aoqi@0 1704 // We use a double-check locking idiom here because this cache is
aoqi@0 1705 // performance sensitive. In the normal system, this cache only
aoqi@0 1706 // transitions from NULL to non-NULL which is safe because we use
aoqi@0 1707 // release_set_methods_jmethod_ids() to advertise the new cache.
aoqi@0 1708 // A partially constructed cache should never be seen by a racing
aoqi@0 1709 // thread. We also use release_store_ptr() to save a new jmethodID
aoqi@0 1710 // in the cache so a partially constructed jmethodID should never be
aoqi@0 1711 // seen either. Cache reads of existing jmethodIDs proceed without a
aoqi@0 1712 // lock, but cache writes of a new jmethodID requires uniqueness and
aoqi@0 1713 // creation of the cache itself requires no leaks so a lock is
aoqi@0 1714 // generally acquired in those two cases.
aoqi@0 1715 //
aoqi@0 1716 // If the RedefineClasses() API has been used, then this cache can
aoqi@0 1717 // grow and we'll have transitions from non-NULL to bigger non-NULL.
aoqi@0 1718 // Cache creation requires no leaks and we require safety between all
aoqi@0 1719 // cache accesses and freeing of the old cache so a lock is generally
aoqi@0 1720 // acquired when the RedefineClasses() API has been used.
aoqi@0 1721
aoqi@0 1722 if (jmeths != NULL) {
aoqi@0 1723 // the cache already exists
aoqi@0 1724 if (!ik_h->idnum_can_increment()) {
aoqi@0 1725 // the cache can't grow so we can just get the current values
aoqi@0 1726 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
aoqi@0 1727 } else {
aoqi@0 1728 // cache can grow so we have to be more careful
aoqi@0 1729 if (Threads::number_of_threads() == 0 ||
aoqi@0 1730 SafepointSynchronize::is_at_safepoint()) {
aoqi@0 1731 // we're single threaded or at a safepoint - no locking needed
aoqi@0 1732 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
aoqi@0 1733 } else {
aoqi@0 1734 MutexLocker ml(JmethodIdCreation_lock);
aoqi@0 1735 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
aoqi@0 1736 }
aoqi@0 1737 }
aoqi@0 1738 }
aoqi@0 1739 // implied else:
aoqi@0 1740 // we need to allocate a cache so default length and id values are good
aoqi@0 1741
aoqi@0 1742 if (jmeths == NULL || // no cache yet
aoqi@0 1743 length <= idnum || // cache is too short
aoqi@0 1744 id == NULL) { // cache doesn't contain entry
aoqi@0 1745
aoqi@0 1746 // This function can be called by the VMThread so we have to do all
aoqi@0 1747 // things that might block on a safepoint before grabbing the lock.
aoqi@0 1748 // Otherwise, we can deadlock with the VMThread or have a cache
aoqi@0 1749 // consistency issue. These vars keep track of what we might have
aoqi@0 1750 // to free after the lock is dropped.
aoqi@0 1751 jmethodID to_dealloc_id = NULL;
aoqi@0 1752 jmethodID* to_dealloc_jmeths = NULL;
aoqi@0 1753
aoqi@0 1754 // may not allocate new_jmeths or use it if we allocate it
aoqi@0 1755 jmethodID* new_jmeths = NULL;
aoqi@0 1756 if (length <= idnum) {
aoqi@0 1757 // allocate a new cache that might be used
aoqi@0 1758 size_t size = MAX2(idnum+1, (size_t)ik_h->idnum_allocated_count());
aoqi@0 1759 new_jmeths = NEW_C_HEAP_ARRAY(jmethodID, size+1, mtClass);
aoqi@0 1760 memset(new_jmeths, 0, (size+1)*sizeof(jmethodID));
aoqi@0 1761 // cache size is stored in element[0], other elements offset by one
aoqi@0 1762 new_jmeths[0] = (jmethodID)size;
aoqi@0 1763 }
aoqi@0 1764
aoqi@0 1765 // allocate a new jmethodID that might be used
aoqi@0 1766 jmethodID new_id = NULL;
aoqi@0 1767 if (method_h->is_old() && !method_h->is_obsolete()) {
aoqi@0 1768 // The method passed in is old (but not obsolete), we need to use the current version
aoqi@0 1769 Method* current_method = ik_h->method_with_idnum((int)idnum);
aoqi@0 1770 assert(current_method != NULL, "old and but not obsolete, so should exist");
aoqi@0 1771 new_id = Method::make_jmethod_id(ik_h->class_loader_data(), current_method);
aoqi@0 1772 } else {
aoqi@0 1773 // It is the current version of the method or an obsolete method,
aoqi@0 1774 // use the version passed in
aoqi@0 1775 new_id = Method::make_jmethod_id(ik_h->class_loader_data(), method_h());
aoqi@0 1776 }
aoqi@0 1777
aoqi@0 1778 if (Threads::number_of_threads() == 0 ||
aoqi@0 1779 SafepointSynchronize::is_at_safepoint()) {
aoqi@0 1780 // we're single threaded or at a safepoint - no locking needed
aoqi@0 1781 id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths,
aoqi@0 1782 &to_dealloc_id, &to_dealloc_jmeths);
aoqi@0 1783 } else {
aoqi@0 1784 MutexLocker ml(JmethodIdCreation_lock);
aoqi@0 1785 id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths,
aoqi@0 1786 &to_dealloc_id, &to_dealloc_jmeths);
aoqi@0 1787 }
aoqi@0 1788
aoqi@0 1789 // The lock has been dropped so we can free resources.
aoqi@0 1790 // Free up either the old cache or the new cache if we allocated one.
aoqi@0 1791 if (to_dealloc_jmeths != NULL) {
aoqi@0 1792 FreeHeap(to_dealloc_jmeths);
aoqi@0 1793 }
aoqi@0 1794 // free up the new ID since it wasn't needed
aoqi@0 1795 if (to_dealloc_id != NULL) {
aoqi@0 1796 Method::destroy_jmethod_id(ik_h->class_loader_data(), to_dealloc_id);
aoqi@0 1797 }
aoqi@0 1798 }
aoqi@0 1799 return id;
aoqi@0 1800 }
aoqi@0 1801
aoqi@0 1802
aoqi@0 1803 // Common code to fetch the jmethodID from the cache or update the
aoqi@0 1804 // cache with the new jmethodID. This function should never do anything
aoqi@0 1805 // that causes the caller to go to a safepoint or we can deadlock with
aoqi@0 1806 // the VMThread or have cache consistency issues.
aoqi@0 1807 //
aoqi@0 1808 jmethodID InstanceKlass::get_jmethod_id_fetch_or_update(
aoqi@0 1809 instanceKlassHandle ik_h, size_t idnum, jmethodID new_id,
aoqi@0 1810 jmethodID* new_jmeths, jmethodID* to_dealloc_id_p,
aoqi@0 1811 jmethodID** to_dealloc_jmeths_p) {
aoqi@0 1812 assert(new_id != NULL, "sanity check");
aoqi@0 1813 assert(to_dealloc_id_p != NULL, "sanity check");
aoqi@0 1814 assert(to_dealloc_jmeths_p != NULL, "sanity check");
aoqi@0 1815 assert(Threads::number_of_threads() == 0 ||
aoqi@0 1816 SafepointSynchronize::is_at_safepoint() ||
aoqi@0 1817 JmethodIdCreation_lock->owned_by_self(), "sanity check");
aoqi@0 1818
aoqi@0 1819 // reacquire the cache - we are locked, single threaded or at a safepoint
aoqi@0 1820 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
aoqi@0 1821 jmethodID id = NULL;
aoqi@0 1822 size_t length = 0;
aoqi@0 1823
aoqi@0 1824 if (jmeths == NULL || // no cache yet
aoqi@0 1825 (length = (size_t)jmeths[0]) <= idnum) { // cache is too short
aoqi@0 1826 if (jmeths != NULL) {
aoqi@0 1827 // copy any existing entries from the old cache
aoqi@0 1828 for (size_t index = 0; index < length; index++) {
aoqi@0 1829 new_jmeths[index+1] = jmeths[index+1];
aoqi@0 1830 }
aoqi@0 1831 *to_dealloc_jmeths_p = jmeths; // save old cache for later delete
aoqi@0 1832 }
aoqi@0 1833 ik_h->release_set_methods_jmethod_ids(jmeths = new_jmeths);
aoqi@0 1834 } else {
aoqi@0 1835 // fetch jmethodID (if any) from the existing cache
aoqi@0 1836 id = jmeths[idnum+1];
aoqi@0 1837 *to_dealloc_jmeths_p = new_jmeths; // save new cache for later delete
aoqi@0 1838 }
aoqi@0 1839 if (id == NULL) {
aoqi@0 1840 // No matching jmethodID in the existing cache or we have a new
aoqi@0 1841 // cache or we just grew the cache. This cache write is done here
aoqi@0 1842 // by the first thread to win the foot race because a jmethodID
aoqi@0 1843 // needs to be unique once it is generally available.
aoqi@0 1844 id = new_id;
aoqi@0 1845
aoqi@0 1846 // The jmethodID cache can be read while unlocked so we have to
aoqi@0 1847 // make sure the new jmethodID is complete before installing it
aoqi@0 1848 // in the cache.
aoqi@0 1849 OrderAccess::release_store_ptr(&jmeths[idnum+1], id);
aoqi@0 1850 } else {
aoqi@0 1851 *to_dealloc_id_p = new_id; // save new id for later delete
aoqi@0 1852 }
aoqi@0 1853 return id;
aoqi@0 1854 }
aoqi@0 1855
aoqi@0 1856
aoqi@0 1857 // Common code to get the jmethodID cache length and the jmethodID
aoqi@0 1858 // value at index idnum if there is one.
aoqi@0 1859 //
aoqi@0 1860 void InstanceKlass::get_jmethod_id_length_value(jmethodID* cache,
aoqi@0 1861 size_t idnum, size_t *length_p, jmethodID* id_p) {
aoqi@0 1862 assert(cache != NULL, "sanity check");
aoqi@0 1863 assert(length_p != NULL, "sanity check");
aoqi@0 1864 assert(id_p != NULL, "sanity check");
aoqi@0 1865
aoqi@0 1866 // cache size is stored in element[0], other elements offset by one
aoqi@0 1867 *length_p = (size_t)cache[0];
aoqi@0 1868 if (*length_p <= idnum) { // cache is too short
aoqi@0 1869 *id_p = NULL;
aoqi@0 1870 } else {
aoqi@0 1871 *id_p = cache[idnum+1]; // fetch jmethodID (if any)
aoqi@0 1872 }
aoqi@0 1873 }
aoqi@0 1874
aoqi@0 1875
aoqi@0 1876 // Lookup a jmethodID, NULL if not found. Do no blocking, no allocations, no handles
aoqi@0 1877 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
aoqi@0 1878 size_t idnum = (size_t)method->method_idnum();
aoqi@0 1879 jmethodID* jmeths = methods_jmethod_ids_acquire();
aoqi@0 1880 size_t length; // length assigned as debugging crumb
aoqi@0 1881 jmethodID id = NULL;
aoqi@0 1882 if (jmeths != NULL && // If there is a cache
aoqi@0 1883 (length = (size_t)jmeths[0]) > idnum) { // and if it is long enough,
aoqi@0 1884 id = jmeths[idnum+1]; // Look up the id (may be NULL)
aoqi@0 1885 }
aoqi@0 1886 return id;
aoqi@0 1887 }
aoqi@0 1888
stefank@6992 1889 int nmethodBucket::decrement() {
stefank@6992 1890 return Atomic::add(-1, (volatile int *)&_count);
stefank@6992 1891 }
aoqi@0 1892
aoqi@0 1893 //
aoqi@0 1894 // Walk the list of dependent nmethods searching for nmethods which
aoqi@0 1895 // are dependent on the changes that were passed in and mark them for
aoqi@0 1896 // deoptimization. Returns the number of nmethods found.
aoqi@0 1897 //
aoqi@0 1898 int InstanceKlass::mark_dependent_nmethods(DepChange& changes) {
aoqi@0 1899 assert_locked_or_safepoint(CodeCache_lock);
aoqi@0 1900 int found = 0;
aoqi@0 1901 nmethodBucket* b = _dependencies;
aoqi@0 1902 while (b != NULL) {
aoqi@0 1903 nmethod* nm = b->get_nmethod();
aoqi@0 1904 // since dependencies aren't removed until an nmethod becomes a zombie,
aoqi@0 1905 // the dependency list may contain nmethods which aren't alive.
stefank@6992 1906 if (b->count() > 0 && nm->is_alive() && !nm->is_marked_for_deoptimization() && nm->check_dependency_on(changes)) {
aoqi@0 1907 if (TraceDependencies) {
aoqi@0 1908 ResourceMark rm;
aoqi@0 1909 tty->print_cr("Marked for deoptimization");
aoqi@0 1910 tty->print_cr(" context = %s", this->external_name());
aoqi@0 1911 changes.print();
aoqi@0 1912 nm->print();
aoqi@0 1913 nm->print_dependencies();
aoqi@0 1914 }
aoqi@0 1915 nm->mark_for_deoptimization();
aoqi@0 1916 found++;
aoqi@0 1917 }
aoqi@0 1918 b = b->next();
aoqi@0 1919 }
aoqi@0 1920 return found;
aoqi@0 1921 }
aoqi@0 1922
stefank@6992 1923 void InstanceKlass::clean_dependent_nmethods() {
stefank@6992 1924 assert_locked_or_safepoint(CodeCache_lock);
stefank@6992 1925
stefank@6992 1926 if (has_unloaded_dependent()) {
stefank@6992 1927 nmethodBucket* b = _dependencies;
stefank@6992 1928 nmethodBucket* last = NULL;
stefank@6992 1929 while (b != NULL) {
stefank@6992 1930 assert(b->count() >= 0, err_msg("bucket count: %d", b->count()));
stefank@6992 1931
stefank@6992 1932 nmethodBucket* next = b->next();
stefank@6992 1933
stefank@6992 1934 if (b->count() == 0) {
stefank@6992 1935 if (last == NULL) {
stefank@6992 1936 _dependencies = next;
stefank@6992 1937 } else {
stefank@6992 1938 last->set_next(next);
stefank@6992 1939 }
stefank@6992 1940 delete b;
stefank@6992 1941 // last stays the same.
stefank@6992 1942 } else {
stefank@6992 1943 last = b;
stefank@6992 1944 }
stefank@6992 1945
stefank@6992 1946 b = next;
stefank@6992 1947 }
stefank@6992 1948 set_has_unloaded_dependent(false);
stefank@6992 1949 }
stefank@6992 1950 #ifdef ASSERT
stefank@6992 1951 else {
stefank@6992 1952 // Verification
stefank@6992 1953 for (nmethodBucket* b = _dependencies; b != NULL; b = b->next()) {
stefank@6992 1954 assert(b->count() >= 0, err_msg("bucket count: %d", b->count()));
stefank@6992 1955 assert(b->count() != 0, "empty buckets need to be cleaned");
stefank@6992 1956 }
stefank@6992 1957 }
stefank@6992 1958 #endif
stefank@6992 1959 }
aoqi@0 1960
aoqi@0 1961 //
aoqi@0 1962 // Add an nmethodBucket to the list of dependencies for this nmethod.
aoqi@0 1963 // It's possible that an nmethod has multiple dependencies on this klass
aoqi@0 1964 // so a count is kept for each bucket to guarantee that creation and
aoqi@0 1965 // deletion of dependencies is consistent.
aoqi@0 1966 //
aoqi@0 1967 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
aoqi@0 1968 assert_locked_or_safepoint(CodeCache_lock);
aoqi@0 1969 nmethodBucket* b = _dependencies;
aoqi@0 1970 nmethodBucket* last = NULL;
aoqi@0 1971 while (b != NULL) {
aoqi@0 1972 if (nm == b->get_nmethod()) {
aoqi@0 1973 b->increment();
aoqi@0 1974 return;
aoqi@0 1975 }
aoqi@0 1976 b = b->next();
aoqi@0 1977 }
aoqi@0 1978 _dependencies = new nmethodBucket(nm, _dependencies);
aoqi@0 1979 }
aoqi@0 1980
aoqi@0 1981
aoqi@0 1982 //
aoqi@0 1983 // Decrement count of the nmethod in the dependency list and remove
aoqi@0 1984 // the bucket competely when the count goes to 0. This method must
aoqi@0 1985 // find a corresponding bucket otherwise there's a bug in the
aoqi@0 1986 // recording of dependecies.
aoqi@0 1987 //
stefank@8185 1988 void InstanceKlass::remove_dependent_nmethod(nmethod* nm, bool delete_immediately) {
aoqi@0 1989 assert_locked_or_safepoint(CodeCache_lock);
aoqi@0 1990 nmethodBucket* b = _dependencies;
aoqi@0 1991 nmethodBucket* last = NULL;
aoqi@0 1992 while (b != NULL) {
aoqi@0 1993 if (nm == b->get_nmethod()) {
stefank@6992 1994 int val = b->decrement();
stefank@6992 1995 guarantee(val >= 0, err_msg("Underflow: %d", val));
stefank@6992 1996 if (val == 0) {
stefank@8185 1997 if (delete_immediately) {
stefank@8185 1998 if (last == NULL) {
stefank@8185 1999 _dependencies = b->next();
stefank@8185 2000 } else {
stefank@8185 2001 last->set_next(b->next());
stefank@8185 2002 }
stefank@8185 2003 delete b;
stefank@8185 2004 } else {
stefank@8185 2005 // The deletion of this entry is deferred until a later, potentially parallel GC phase.
stefank@8185 2006 set_has_unloaded_dependent(true);
stefank@8185 2007 }
aoqi@0 2008 }
aoqi@0 2009 return;
aoqi@0 2010 }
aoqi@0 2011 last = b;
aoqi@0 2012 b = b->next();
aoqi@0 2013 }
aoqi@0 2014 #ifdef ASSERT
aoqi@0 2015 tty->print_cr("### %s can't find dependent nmethod:", this->external_name());
aoqi@0 2016 nm->print();
aoqi@0 2017 #endif // ASSERT
aoqi@0 2018 ShouldNotReachHere();
aoqi@0 2019 }
aoqi@0 2020
aoqi@0 2021
aoqi@0 2022 #ifndef PRODUCT
aoqi@0 2023 void InstanceKlass::print_dependent_nmethods(bool verbose) {
aoqi@0 2024 nmethodBucket* b = _dependencies;
aoqi@0 2025 int idx = 0;
aoqi@0 2026 while (b != NULL) {
aoqi@0 2027 nmethod* nm = b->get_nmethod();
aoqi@0 2028 tty->print("[%d] count=%d { ", idx++, b->count());
aoqi@0 2029 if (!verbose) {
aoqi@0 2030 nm->print_on(tty, "nmethod");
aoqi@0 2031 tty->print_cr(" } ");
aoqi@0 2032 } else {
aoqi@0 2033 nm->print();
aoqi@0 2034 nm->print_dependencies();
aoqi@0 2035 tty->print_cr("--- } ");
aoqi@0 2036 }
aoqi@0 2037 b = b->next();
aoqi@0 2038 }
aoqi@0 2039 }
aoqi@0 2040
aoqi@0 2041
aoqi@0 2042 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
aoqi@0 2043 nmethodBucket* b = _dependencies;
aoqi@0 2044 while (b != NULL) {
aoqi@0 2045 if (nm == b->get_nmethod()) {
stefank@6992 2046 #ifdef ASSERT
stefank@6992 2047 int count = b->count();
ehelin@6993 2048 assert(count >= 0, err_msg("count shouldn't be negative: %d", count));
stefank@6992 2049 #endif
aoqi@0 2050 return true;
aoqi@0 2051 }
aoqi@0 2052 b = b->next();
aoqi@0 2053 }
aoqi@0 2054 return false;
aoqi@0 2055 }
aoqi@0 2056 #endif //PRODUCT
aoqi@0 2057
aoqi@0 2058
aoqi@0 2059 // Garbage collection
aoqi@0 2060
aoqi@0 2061 #ifdef ASSERT
aoqi@0 2062 template <class T> void assert_is_in(T *p) {
aoqi@0 2063 T heap_oop = oopDesc::load_heap_oop(p);
aoqi@0 2064 if (!oopDesc::is_null(heap_oop)) {
aoqi@0 2065 oop o = oopDesc::decode_heap_oop_not_null(heap_oop);
aoqi@0 2066 assert(Universe::heap()->is_in(o), "should be in heap");
aoqi@0 2067 }
aoqi@0 2068 }
aoqi@0 2069 template <class T> void assert_is_in_closed_subset(T *p) {
aoqi@0 2070 T heap_oop = oopDesc::load_heap_oop(p);
aoqi@0 2071 if (!oopDesc::is_null(heap_oop)) {
aoqi@0 2072 oop o = oopDesc::decode_heap_oop_not_null(heap_oop);
aoqi@0 2073 assert(Universe::heap()->is_in_closed_subset(o),
aoqi@0 2074 err_msg("should be in closed *p " INTPTR_FORMAT " " INTPTR_FORMAT, (address)p, (address)o));
aoqi@0 2075 }
aoqi@0 2076 }
aoqi@0 2077 template <class T> void assert_is_in_reserved(T *p) {
aoqi@0 2078 T heap_oop = oopDesc::load_heap_oop(p);
aoqi@0 2079 if (!oopDesc::is_null(heap_oop)) {
aoqi@0 2080 oop o = oopDesc::decode_heap_oop_not_null(heap_oop);
aoqi@0 2081 assert(Universe::heap()->is_in_reserved(o), "should be in reserved");
aoqi@0 2082 }
aoqi@0 2083 }
aoqi@0 2084 template <class T> void assert_nothing(T *p) {}
aoqi@0 2085
aoqi@0 2086 #else
aoqi@0 2087 template <class T> void assert_is_in(T *p) {}
aoqi@0 2088 template <class T> void assert_is_in_closed_subset(T *p) {}
aoqi@0 2089 template <class T> void assert_is_in_reserved(T *p) {}
aoqi@0 2090 template <class T> void assert_nothing(T *p) {}
aoqi@0 2091 #endif // ASSERT
aoqi@0 2092
aoqi@0 2093 //
aoqi@0 2094 // Macros that iterate over areas of oops which are specialized on type of
aoqi@0 2095 // oop pointer either narrow or wide, depending on UseCompressedOops
aoqi@0 2096 //
aoqi@0 2097 // Parameters are:
aoqi@0 2098 // T - type of oop to point to (either oop or narrowOop)
aoqi@0 2099 // start_p - starting pointer for region to iterate over
aoqi@0 2100 // count - number of oops or narrowOops to iterate over
aoqi@0 2101 // do_oop - action to perform on each oop (it's arbitrary C code which
aoqi@0 2102 // makes it more efficient to put in a macro rather than making
aoqi@0 2103 // it a template function)
aoqi@0 2104 // assert_fn - assert function which is template function because performance
aoqi@0 2105 // doesn't matter when enabled.
aoqi@0 2106 #define InstanceKlass_SPECIALIZED_OOP_ITERATE( \
aoqi@0 2107 T, start_p, count, do_oop, \
aoqi@0 2108 assert_fn) \
aoqi@0 2109 { \
aoqi@0 2110 T* p = (T*)(start_p); \
aoqi@0 2111 T* const end = p + (count); \
aoqi@0 2112 while (p < end) { \
aoqi@0 2113 (assert_fn)(p); \
aoqi@0 2114 do_oop; \
aoqi@0 2115 ++p; \
aoqi@0 2116 } \
aoqi@0 2117 }
aoqi@0 2118
aoqi@0 2119 #define InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE( \
aoqi@0 2120 T, start_p, count, do_oop, \
aoqi@0 2121 assert_fn) \
aoqi@0 2122 { \
aoqi@0 2123 T* const start = (T*)(start_p); \
aoqi@0 2124 T* p = start + (count); \
aoqi@0 2125 while (start < p) { \
aoqi@0 2126 --p; \
aoqi@0 2127 (assert_fn)(p); \
aoqi@0 2128 do_oop; \
aoqi@0 2129 } \
aoqi@0 2130 }
aoqi@0 2131
aoqi@0 2132 #define InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE( \
aoqi@0 2133 T, start_p, count, low, high, \
aoqi@0 2134 do_oop, assert_fn) \
aoqi@0 2135 { \
aoqi@0 2136 T* const l = (T*)(low); \
aoqi@0 2137 T* const h = (T*)(high); \
aoqi@0 2138 assert(mask_bits((intptr_t)l, sizeof(T)-1) == 0 && \
aoqi@0 2139 mask_bits((intptr_t)h, sizeof(T)-1) == 0, \
aoqi@0 2140 "bounded region must be properly aligned"); \
aoqi@0 2141 T* p = (T*)(start_p); \
aoqi@0 2142 T* end = p + (count); \
aoqi@0 2143 if (p < l) p = l; \
aoqi@0 2144 if (end > h) end = h; \
aoqi@0 2145 while (p < end) { \
aoqi@0 2146 (assert_fn)(p); \
aoqi@0 2147 do_oop; \
aoqi@0 2148 ++p; \
aoqi@0 2149 } \
aoqi@0 2150 }
aoqi@0 2151
aoqi@0 2152
aoqi@0 2153 // The following macros call specialized macros, passing either oop or
aoqi@0 2154 // narrowOop as the specialization type. These test the UseCompressedOops
aoqi@0 2155 // flag.
aoqi@0 2156 #define InstanceKlass_OOP_MAP_ITERATE(obj, do_oop, assert_fn) \
aoqi@0 2157 { \
aoqi@0 2158 /* Compute oopmap block range. The common case \
aoqi@0 2159 is nonstatic_oop_map_size == 1. */ \
aoqi@0 2160 OopMapBlock* map = start_of_nonstatic_oop_maps(); \
aoqi@0 2161 OopMapBlock* const end_map = map + nonstatic_oop_map_count(); \
aoqi@0 2162 if (UseCompressedOops) { \
aoqi@0 2163 while (map < end_map) { \
aoqi@0 2164 InstanceKlass_SPECIALIZED_OOP_ITERATE(narrowOop, \
aoqi@0 2165 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \
aoqi@0 2166 do_oop, assert_fn) \
aoqi@0 2167 ++map; \
aoqi@0 2168 } \
aoqi@0 2169 } else { \
aoqi@0 2170 while (map < end_map) { \
aoqi@0 2171 InstanceKlass_SPECIALIZED_OOP_ITERATE(oop, \
aoqi@0 2172 obj->obj_field_addr<oop>(map->offset()), map->count(), \
aoqi@0 2173 do_oop, assert_fn) \
aoqi@0 2174 ++map; \
aoqi@0 2175 } \
aoqi@0 2176 } \
aoqi@0 2177 }
aoqi@0 2178
aoqi@0 2179 #define InstanceKlass_OOP_MAP_REVERSE_ITERATE(obj, do_oop, assert_fn) \
aoqi@0 2180 { \
aoqi@0 2181 OopMapBlock* const start_map = start_of_nonstatic_oop_maps(); \
aoqi@0 2182 OopMapBlock* map = start_map + nonstatic_oop_map_count(); \
aoqi@0 2183 if (UseCompressedOops) { \
aoqi@0 2184 while (start_map < map) { \
aoqi@0 2185 --map; \
aoqi@0 2186 InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE(narrowOop, \
aoqi@0 2187 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \
aoqi@0 2188 do_oop, assert_fn) \
aoqi@0 2189 } \
aoqi@0 2190 } else { \
aoqi@0 2191 while (start_map < map) { \
aoqi@0 2192 --map; \
aoqi@0 2193 InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE(oop, \
aoqi@0 2194 obj->obj_field_addr<oop>(map->offset()), map->count(), \
aoqi@0 2195 do_oop, assert_fn) \
aoqi@0 2196 } \
aoqi@0 2197 } \
aoqi@0 2198 }
aoqi@0 2199
aoqi@0 2200 #define InstanceKlass_BOUNDED_OOP_MAP_ITERATE(obj, low, high, do_oop, \
aoqi@0 2201 assert_fn) \
aoqi@0 2202 { \
aoqi@0 2203 /* Compute oopmap block range. The common case is \
aoqi@0 2204 nonstatic_oop_map_size == 1, so we accept the \
aoqi@0 2205 usually non-existent extra overhead of examining \
aoqi@0 2206 all the maps. */ \
aoqi@0 2207 OopMapBlock* map = start_of_nonstatic_oop_maps(); \
aoqi@0 2208 OopMapBlock* const end_map = map + nonstatic_oop_map_count(); \
aoqi@0 2209 if (UseCompressedOops) { \
aoqi@0 2210 while (map < end_map) { \
aoqi@0 2211 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(narrowOop, \
aoqi@0 2212 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \
aoqi@0 2213 low, high, \
aoqi@0 2214 do_oop, assert_fn) \
aoqi@0 2215 ++map; \
aoqi@0 2216 } \
aoqi@0 2217 } else { \
aoqi@0 2218 while (map < end_map) { \
aoqi@0 2219 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(oop, \
aoqi@0 2220 obj->obj_field_addr<oop>(map->offset()), map->count(), \
aoqi@0 2221 low, high, \
aoqi@0 2222 do_oop, assert_fn) \
aoqi@0 2223 ++map; \
aoqi@0 2224 } \
aoqi@0 2225 } \
aoqi@0 2226 }
aoqi@0 2227
aoqi@0 2228 void InstanceKlass::oop_follow_contents(oop obj) {
aoqi@0 2229 assert(obj != NULL, "can't follow the content of NULL object");
aoqi@0 2230 MarkSweep::follow_klass(obj->klass());
aoqi@0 2231 InstanceKlass_OOP_MAP_ITERATE( \
aoqi@0 2232 obj, \
aoqi@0 2233 MarkSweep::mark_and_push(p), \
aoqi@0 2234 assert_is_in_closed_subset)
aoqi@0 2235 }
aoqi@0 2236
aoqi@0 2237 #if INCLUDE_ALL_GCS
aoqi@0 2238 void InstanceKlass::oop_follow_contents(ParCompactionManager* cm,
aoqi@0 2239 oop obj) {
aoqi@0 2240 assert(obj != NULL, "can't follow the content of NULL object");
aoqi@0 2241 PSParallelCompact::follow_klass(cm, obj->klass());
aoqi@0 2242 // Only mark the header and let the scan of the meta-data mark
aoqi@0 2243 // everything else.
aoqi@0 2244 InstanceKlass_OOP_MAP_ITERATE( \
aoqi@0 2245 obj, \
aoqi@0 2246 PSParallelCompact::mark_and_push(cm, p), \
aoqi@0 2247 assert_is_in)
aoqi@0 2248 }
aoqi@0 2249 #endif // INCLUDE_ALL_GCS
aoqi@0 2250
aoqi@0 2251 // closure's do_metadata() method dictates whether the given closure should be
aoqi@0 2252 // applied to the klass ptr in the object header.
aoqi@0 2253
aoqi@0 2254 #define InstanceKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \
aoqi@0 2255 \
aoqi@0 2256 int InstanceKlass::oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) { \
aoqi@0 2257 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\
aoqi@0 2258 /* header */ \
aoqi@0 2259 if_do_metadata_checked(closure, nv_suffix) { \
aoqi@0 2260 closure->do_klass##nv_suffix(obj->klass()); \
aoqi@0 2261 } \
aoqi@0 2262 InstanceKlass_OOP_MAP_ITERATE( \
aoqi@0 2263 obj, \
aoqi@0 2264 SpecializationStats:: \
aoqi@0 2265 record_do_oop_call##nv_suffix(SpecializationStats::ik); \
aoqi@0 2266 (closure)->do_oop##nv_suffix(p), \
aoqi@0 2267 assert_is_in_closed_subset) \
aoqi@0 2268 return size_helper(); \
aoqi@0 2269 }
aoqi@0 2270
aoqi@0 2271 #if INCLUDE_ALL_GCS
aoqi@0 2272 #define InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) \
aoqi@0 2273 \
aoqi@0 2274 int InstanceKlass::oop_oop_iterate_backwards##nv_suffix(oop obj, \
aoqi@0 2275 OopClosureType* closure) { \
aoqi@0 2276 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik); \
stefank@6982 2277 \
stefank@6982 2278 assert_should_ignore_metadata(closure, nv_suffix); \
stefank@6982 2279 \
aoqi@0 2280 /* instance variables */ \
aoqi@0 2281 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \
aoqi@0 2282 obj, \
aoqi@0 2283 SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::ik);\
aoqi@0 2284 (closure)->do_oop##nv_suffix(p), \
aoqi@0 2285 assert_is_in_closed_subset) \
aoqi@0 2286 return size_helper(); \
aoqi@0 2287 }
aoqi@0 2288 #endif // INCLUDE_ALL_GCS
aoqi@0 2289
aoqi@0 2290 #define InstanceKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix) \
aoqi@0 2291 \
aoqi@0 2292 int InstanceKlass::oop_oop_iterate##nv_suffix##_m(oop obj, \
aoqi@0 2293 OopClosureType* closure, \
aoqi@0 2294 MemRegion mr) { \
aoqi@0 2295 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\
aoqi@0 2296 if_do_metadata_checked(closure, nv_suffix) { \
aoqi@0 2297 if (mr.contains(obj)) { \
aoqi@0 2298 closure->do_klass##nv_suffix(obj->klass()); \
aoqi@0 2299 } \
aoqi@0 2300 } \
aoqi@0 2301 InstanceKlass_BOUNDED_OOP_MAP_ITERATE( \
aoqi@0 2302 obj, mr.start(), mr.end(), \
aoqi@0 2303 (closure)->do_oop##nv_suffix(p), \
aoqi@0 2304 assert_is_in_closed_subset) \
aoqi@0 2305 return size_helper(); \
aoqi@0 2306 }
aoqi@0 2307
aoqi@0 2308 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DEFN)
aoqi@0 2309 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_DEFN)
aoqi@0 2310 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DEFN_m)
aoqi@0 2311 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_DEFN_m)
aoqi@0 2312 #if INCLUDE_ALL_GCS
aoqi@0 2313 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)
aoqi@0 2314 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)
aoqi@0 2315 #endif // INCLUDE_ALL_GCS
aoqi@0 2316
aoqi@0 2317 int InstanceKlass::oop_adjust_pointers(oop obj) {
aoqi@0 2318 int size = size_helper();
aoqi@0 2319 InstanceKlass_OOP_MAP_ITERATE( \
aoqi@0 2320 obj, \
aoqi@0 2321 MarkSweep::adjust_pointer(p), \
aoqi@0 2322 assert_is_in)
aoqi@0 2323 return size;
aoqi@0 2324 }
aoqi@0 2325
aoqi@0 2326 #if INCLUDE_ALL_GCS
aoqi@0 2327 void InstanceKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
aoqi@0 2328 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \
aoqi@0 2329 obj, \
aoqi@0 2330 if (PSScavenge::should_scavenge(p)) { \
aoqi@0 2331 pm->claim_or_forward_depth(p); \
aoqi@0 2332 }, \
aoqi@0 2333 assert_nothing )
aoqi@0 2334 }
aoqi@0 2335
aoqi@0 2336 int InstanceKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
aoqi@0 2337 int size = size_helper();
aoqi@0 2338 InstanceKlass_OOP_MAP_ITERATE( \
aoqi@0 2339 obj, \
aoqi@0 2340 PSParallelCompact::adjust_pointer(p), \
aoqi@0 2341 assert_is_in)
aoqi@0 2342 return size;
aoqi@0 2343 }
aoqi@0 2344
aoqi@0 2345 #endif // INCLUDE_ALL_GCS
aoqi@0 2346
stefank@8185 2347 void InstanceKlass::clean_weak_instanceklass_links(BoolObjectClosure* is_alive) {
stefank@8185 2348 clean_implementors_list(is_alive);
stefank@8185 2349 clean_method_data(is_alive);
stefank@8185 2350
stefank@8185 2351 clean_dependent_nmethods();
stefank@8185 2352 }
stefank@8185 2353
aoqi@0 2354 void InstanceKlass::clean_implementors_list(BoolObjectClosure* is_alive) {
stefank@6992 2355 assert(class_loader_data()->is_alive(is_alive), "this klass should be live");
aoqi@0 2356 if (is_interface()) {
aoqi@0 2357 if (ClassUnloading) {
aoqi@0 2358 Klass* impl = implementor();
aoqi@0 2359 if (impl != NULL) {
aoqi@0 2360 if (!impl->is_loader_alive(is_alive)) {
aoqi@0 2361 // remove this guy
aoqi@0 2362 Klass** klass = adr_implementor();
aoqi@0 2363 assert(klass != NULL, "null klass");
aoqi@0 2364 if (klass != NULL) {
aoqi@0 2365 *klass = NULL;
aoqi@0 2366 }
aoqi@0 2367 }
aoqi@0 2368 }
aoqi@0 2369 }
aoqi@0 2370 }
aoqi@0 2371 }
aoqi@0 2372
aoqi@0 2373 void InstanceKlass::clean_method_data(BoolObjectClosure* is_alive) {
aoqi@0 2374 for (int m = 0; m < methods()->length(); m++) {
aoqi@0 2375 MethodData* mdo = methods()->at(m)->method_data();
aoqi@0 2376 if (mdo != NULL) {
aoqi@0 2377 mdo->clean_method_data(is_alive);
aoqi@0 2378 }
aoqi@0 2379 }
aoqi@0 2380 }
aoqi@0 2381
aoqi@0 2382
aoqi@0 2383 static void remove_unshareable_in_class(Klass* k) {
aoqi@0 2384 // remove klass's unshareable info
aoqi@0 2385 k->remove_unshareable_info();
aoqi@0 2386 }
aoqi@0 2387
aoqi@0 2388 void InstanceKlass::remove_unshareable_info() {
aoqi@0 2389 Klass::remove_unshareable_info();
aoqi@0 2390 // Unlink the class
aoqi@0 2391 if (is_linked()) {
aoqi@0 2392 unlink_class();
aoqi@0 2393 }
aoqi@0 2394 init_implementor();
aoqi@0 2395
aoqi@0 2396 constants()->remove_unshareable_info();
aoqi@0 2397
aoqi@0 2398 for (int i = 0; i < methods()->length(); i++) {
aoqi@0 2399 Method* m = methods()->at(i);
aoqi@0 2400 m->remove_unshareable_info();
aoqi@0 2401 }
aoqi@0 2402
aoqi@0 2403 // do array classes also.
aoqi@0 2404 array_klasses_do(remove_unshareable_in_class);
aoqi@0 2405 }
aoqi@0 2406
iklam@7089 2407 static void restore_unshareable_in_class(Klass* k, TRAPS) {
iklam@7089 2408 // Array classes have null protection domain.
iklam@7089 2409 // --> see ArrayKlass::complete_create_array_klass()
iklam@7089 2410 k->restore_unshareable_info(ClassLoaderData::the_null_class_loader_data(), Handle(), CHECK);
aoqi@0 2411 }
aoqi@0 2412
iklam@7089 2413 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
iklam@7089 2414 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
aoqi@0 2415 instanceKlassHandle ik(THREAD, this);
aoqi@0 2416
aoqi@0 2417 Array<Method*>* methods = ik->methods();
aoqi@0 2418 int num_methods = methods->length();
aoqi@0 2419 for (int index2 = 0; index2 < num_methods; ++index2) {
aoqi@0 2420 methodHandle m(THREAD, methods->at(index2));
aoqi@0 2421 m->restore_unshareable_info(CHECK);
aoqi@0 2422 }
aoqi@0 2423 if (JvmtiExport::has_redefined_a_class()) {
aoqi@0 2424 // Reinitialize vtable because RedefineClasses may have changed some
aoqi@0 2425 // entries in this vtable for super classes so the CDS vtable might
aoqi@0 2426 // point to old or obsolete entries. RedefineClasses doesn't fix up
aoqi@0 2427 // vtables in the shared system dictionary, only the main one.
aoqi@0 2428 // It also redefines the itable too so fix that too.
aoqi@0 2429 ResourceMark rm(THREAD);
aoqi@0 2430 ik->vtable()->initialize_vtable(false, CHECK);
aoqi@0 2431 ik->itable()->initialize_itable(false, CHECK);
aoqi@0 2432 }
aoqi@0 2433
aoqi@0 2434 // restore constant pool resolved references
aoqi@0 2435 ik->constants()->restore_unshareable_info(CHECK);
aoqi@0 2436
aoqi@0 2437 ik->array_klasses_do(restore_unshareable_in_class, CHECK);
aoqi@0 2438 }
aoqi@0 2439
iklam@7089 2440 // returns true IFF is_in_error_state() has been changed as a result of this call.
iklam@7089 2441 bool InstanceKlass::check_sharing_error_state() {
iklam@7089 2442 assert(DumpSharedSpaces, "should only be called during dumping");
iklam@7089 2443 bool old_state = is_in_error_state();
iklam@7089 2444
iklam@7089 2445 if (!is_in_error_state()) {
iklam@7089 2446 bool bad = false;
iklam@7089 2447 for (InstanceKlass* sup = java_super(); sup; sup = sup->java_super()) {
iklam@7089 2448 if (sup->is_in_error_state()) {
iklam@7089 2449 bad = true;
iklam@7089 2450 break;
iklam@7089 2451 }
iklam@7089 2452 }
iklam@7089 2453 if (!bad) {
iklam@7089 2454 Array<Klass*>* interfaces = transitive_interfaces();
iklam@7089 2455 for (int i = 0; i < interfaces->length(); i++) {
iklam@7089 2456 Klass* iface = interfaces->at(i);
iklam@7089 2457 if (InstanceKlass::cast(iface)->is_in_error_state()) {
iklam@7089 2458 bad = true;
iklam@7089 2459 break;
iklam@7089 2460 }
iklam@7089 2461 }
iklam@7089 2462 }
iklam@7089 2463
iklam@7089 2464 if (bad) {
iklam@7089 2465 set_in_error_state();
iklam@7089 2466 }
iklam@7089 2467 }
iklam@7089 2468
iklam@7089 2469 return (old_state != is_in_error_state());
iklam@7089 2470 }
iklam@7089 2471
aoqi@0 2472 static void clear_all_breakpoints(Method* m) {
aoqi@0 2473 m->clear_all_breakpoints();
aoqi@0 2474 }
aoqi@0 2475
aoqi@0 2476
aoqi@0 2477 void InstanceKlass::notify_unload_class(InstanceKlass* ik) {
aoqi@0 2478 // notify the debugger
aoqi@0 2479 if (JvmtiExport::should_post_class_unload()) {
aoqi@0 2480 JvmtiExport::post_class_unload(ik);
aoqi@0 2481 }
aoqi@0 2482
aoqi@0 2483 // notify ClassLoadingService of class unload
aoqi@0 2484 ClassLoadingService::notify_class_unloaded(ik);
aoqi@0 2485 }
aoqi@0 2486
aoqi@0 2487 void InstanceKlass::release_C_heap_structures(InstanceKlass* ik) {
aoqi@0 2488 // Clean up C heap
aoqi@0 2489 ik->release_C_heap_structures();
aoqi@0 2490 ik->constants()->release_C_heap_structures();
aoqi@0 2491 }
aoqi@0 2492
aoqi@0 2493 void InstanceKlass::release_C_heap_structures() {
aoqi@0 2494
aoqi@0 2495 // Can't release the constant pool here because the constant pool can be
aoqi@0 2496 // deallocated separately from the InstanceKlass for default methods and
aoqi@0 2497 // redefine classes.
aoqi@0 2498
aoqi@0 2499 // Deallocate oop map cache
aoqi@0 2500 if (_oop_map_cache != NULL) {
aoqi@0 2501 delete _oop_map_cache;
aoqi@0 2502 _oop_map_cache = NULL;
aoqi@0 2503 }
aoqi@0 2504
aoqi@0 2505 // Deallocate JNI identifiers for jfieldIDs
aoqi@0 2506 JNIid::deallocate(jni_ids());
aoqi@0 2507 set_jni_ids(NULL);
aoqi@0 2508
aoqi@0 2509 jmethodID* jmeths = methods_jmethod_ids_acquire();
aoqi@0 2510 if (jmeths != (jmethodID*)NULL) {
aoqi@0 2511 release_set_methods_jmethod_ids(NULL);
aoqi@0 2512 FreeHeap(jmeths);
aoqi@0 2513 }
aoqi@0 2514
aoqi@0 2515 // Deallocate MemberNameTable
aoqi@0 2516 {
aoqi@0 2517 Mutex* lock_or_null = SafepointSynchronize::is_at_safepoint() ? NULL : MemberNameTable_lock;
aoqi@0 2518 MutexLockerEx ml(lock_or_null, Mutex::_no_safepoint_check_flag);
aoqi@0 2519 MemberNameTable* mnt = member_names();
aoqi@0 2520 if (mnt != NULL) {
aoqi@0 2521 delete mnt;
aoqi@0 2522 set_member_names(NULL);
aoqi@0 2523 }
aoqi@0 2524 }
aoqi@0 2525
aoqi@0 2526 // release dependencies
aoqi@0 2527 nmethodBucket* b = _dependencies;
aoqi@0 2528 _dependencies = NULL;
aoqi@0 2529 while (b != NULL) {
aoqi@0 2530 nmethodBucket* next = b->next();
aoqi@0 2531 delete b;
aoqi@0 2532 b = next;
aoqi@0 2533 }
aoqi@0 2534
aoqi@0 2535 // Deallocate breakpoint records
aoqi@0 2536 if (breakpoints() != 0x0) {
aoqi@0 2537 methods_do(clear_all_breakpoints);
aoqi@0 2538 assert(breakpoints() == 0x0, "should have cleared breakpoints");
aoqi@0 2539 }
aoqi@0 2540
aoqi@0 2541 // deallocate information about previous versions
aoqi@0 2542 if (_previous_versions != NULL) {
aoqi@0 2543 for (int i = _previous_versions->length() - 1; i >= 0; i--) {
aoqi@0 2544 PreviousVersionNode * pv_node = _previous_versions->at(i);
aoqi@0 2545 delete pv_node;
aoqi@0 2546 }
aoqi@0 2547 delete _previous_versions;
aoqi@0 2548 _previous_versions = NULL;
aoqi@0 2549 }
aoqi@0 2550
aoqi@0 2551 // deallocate the cached class file
aoqi@0 2552 if (_cached_class_file != NULL) {
aoqi@0 2553 os::free(_cached_class_file, mtClass);
aoqi@0 2554 _cached_class_file = NULL;
aoqi@0 2555 }
aoqi@0 2556
aoqi@0 2557 // Decrement symbol reference counts associated with the unloaded class.
aoqi@0 2558 if (_name != NULL) _name->decrement_refcount();
aoqi@0 2559 // unreference array name derived from this class name (arrays of an unloaded
aoqi@0 2560 // class can't be referenced anymore).
aoqi@0 2561 if (_array_name != NULL) _array_name->decrement_refcount();
aoqi@0 2562 if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension, mtClass);
aoqi@0 2563
aoqi@0 2564 assert(_total_instanceKlass_count >= 1, "Sanity check");
aoqi@0 2565 Atomic::dec(&_total_instanceKlass_count);
aoqi@0 2566 }
aoqi@0 2567
aoqi@0 2568 void InstanceKlass::set_source_debug_extension(char* array, int length) {
aoqi@0 2569 if (array == NULL) {
aoqi@0 2570 _source_debug_extension = NULL;
aoqi@0 2571 } else {
aoqi@0 2572 // Adding one to the attribute length in order to store a null terminator
aoqi@0 2573 // character could cause an overflow because the attribute length is
aoqi@0 2574 // already coded with an u4 in the classfile, but in practice, it's
aoqi@0 2575 // unlikely to happen.
aoqi@0 2576 assert((length+1) > length, "Overflow checking");
aoqi@0 2577 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
aoqi@0 2578 for (int i = 0; i < length; i++) {
aoqi@0 2579 sde[i] = array[i];
aoqi@0 2580 }
aoqi@0 2581 sde[length] = '\0';
aoqi@0 2582 _source_debug_extension = sde;
aoqi@0 2583 }
aoqi@0 2584 }
aoqi@0 2585
aoqi@0 2586 address InstanceKlass::static_field_addr(int offset) {
aoqi@0 2587 return (address)(offset + InstanceMirrorKlass::offset_of_static_fields() + cast_from_oop<intptr_t>(java_mirror()));
aoqi@0 2588 }
aoqi@0 2589
aoqi@0 2590
aoqi@0 2591 const char* InstanceKlass::signature_name() const {
aoqi@0 2592 int hash_len = 0;
aoqi@0 2593 char hash_buf[40];
aoqi@0 2594
aoqi@0 2595 // If this is an anonymous class, append a hash to make the name unique
aoqi@0 2596 if (is_anonymous()) {
aoqi@0 2597 assert(EnableInvokeDynamic, "EnableInvokeDynamic was not set.");
aoqi@0 2598 intptr_t hash = (java_mirror() != NULL) ? java_mirror()->identity_hash() : 0;
aoqi@0 2599 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash);
aoqi@0 2600 hash_len = (int)strlen(hash_buf);
aoqi@0 2601 }
aoqi@0 2602
aoqi@0 2603 // Get the internal name as a c string
aoqi@0 2604 const char* src = (const char*) (name()->as_C_string());
aoqi@0 2605 const int src_length = (int)strlen(src);
aoqi@0 2606
aoqi@0 2607 char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
aoqi@0 2608
aoqi@0 2609 // Add L as type indicator
aoqi@0 2610 int dest_index = 0;
aoqi@0 2611 dest[dest_index++] = 'L';
aoqi@0 2612
aoqi@0 2613 // Add the actual class name
aoqi@0 2614 for (int src_index = 0; src_index < src_length; ) {
aoqi@0 2615 dest[dest_index++] = src[src_index++];
aoqi@0 2616 }
aoqi@0 2617
aoqi@0 2618 // If we have a hash, append it
aoqi@0 2619 for (int hash_index = 0; hash_index < hash_len; ) {
aoqi@0 2620 dest[dest_index++] = hash_buf[hash_index++];
aoqi@0 2621 }
aoqi@0 2622
aoqi@0 2623 // Add the semicolon and the NULL
aoqi@0 2624 dest[dest_index++] = ';';
aoqi@0 2625 dest[dest_index] = '\0';
aoqi@0 2626 return dest;
aoqi@0 2627 }
aoqi@0 2628
aoqi@0 2629 // different verisons of is_same_class_package
aoqi@0 2630 bool InstanceKlass::is_same_class_package(Klass* class2) {
aoqi@0 2631 Klass* class1 = this;
aoqi@0 2632 oop classloader1 = InstanceKlass::cast(class1)->class_loader();
aoqi@0 2633 Symbol* classname1 = class1->name();
aoqi@0 2634
aoqi@0 2635 if (class2->oop_is_objArray()) {
aoqi@0 2636 class2 = ObjArrayKlass::cast(class2)->bottom_klass();
aoqi@0 2637 }
aoqi@0 2638 oop classloader2;
aoqi@0 2639 if (class2->oop_is_instance()) {
aoqi@0 2640 classloader2 = InstanceKlass::cast(class2)->class_loader();
aoqi@0 2641 } else {
aoqi@0 2642 assert(class2->oop_is_typeArray(), "should be type array");
aoqi@0 2643 classloader2 = NULL;
aoqi@0 2644 }
aoqi@0 2645 Symbol* classname2 = class2->name();
aoqi@0 2646
aoqi@0 2647 return InstanceKlass::is_same_class_package(classloader1, classname1,
aoqi@0 2648 classloader2, classname2);
aoqi@0 2649 }
aoqi@0 2650
aoqi@0 2651 bool InstanceKlass::is_same_class_package(oop classloader2, Symbol* classname2) {
aoqi@0 2652 Klass* class1 = this;
aoqi@0 2653 oop classloader1 = InstanceKlass::cast(class1)->class_loader();
aoqi@0 2654 Symbol* classname1 = class1->name();
aoqi@0 2655
aoqi@0 2656 return InstanceKlass::is_same_class_package(classloader1, classname1,
aoqi@0 2657 classloader2, classname2);
aoqi@0 2658 }
aoqi@0 2659
aoqi@0 2660 // return true if two classes are in the same package, classloader
aoqi@0 2661 // and classname information is enough to determine a class's package
aoqi@0 2662 bool InstanceKlass::is_same_class_package(oop class_loader1, Symbol* class_name1,
aoqi@0 2663 oop class_loader2, Symbol* class_name2) {
aoqi@0 2664 if (class_loader1 != class_loader2) {
aoqi@0 2665 return false;
aoqi@0 2666 } else if (class_name1 == class_name2) {
aoqi@0 2667 return true; // skip painful bytewise comparison
aoqi@0 2668 } else {
aoqi@0 2669 ResourceMark rm;
aoqi@0 2670
aoqi@0 2671 // The Symbol*'s are in UTF8 encoding. Since we only need to check explicitly
aoqi@0 2672 // for ASCII characters ('/', 'L', '['), we can keep them in UTF8 encoding.
aoqi@0 2673 // Otherwise, we just compare jbyte values between the strings.
aoqi@0 2674 const jbyte *name1 = class_name1->base();
aoqi@0 2675 const jbyte *name2 = class_name2->base();
aoqi@0 2676
aoqi@0 2677 const jbyte *last_slash1 = UTF8::strrchr(name1, class_name1->utf8_length(), '/');
aoqi@0 2678 const jbyte *last_slash2 = UTF8::strrchr(name2, class_name2->utf8_length(), '/');
aoqi@0 2679
aoqi@0 2680 if ((last_slash1 == NULL) || (last_slash2 == NULL)) {
aoqi@0 2681 // One of the two doesn't have a package. Only return true
aoqi@0 2682 // if the other one also doesn't have a package.
aoqi@0 2683 return last_slash1 == last_slash2;
aoqi@0 2684 } else {
aoqi@0 2685 // Skip over '['s
aoqi@0 2686 if (*name1 == '[') {
aoqi@0 2687 do {
aoqi@0 2688 name1++;
aoqi@0 2689 } while (*name1 == '[');
aoqi@0 2690 if (*name1 != 'L') {
aoqi@0 2691 // Something is terribly wrong. Shouldn't be here.
aoqi@0 2692 return false;
aoqi@0 2693 }
aoqi@0 2694 }
aoqi@0 2695 if (*name2 == '[') {
aoqi@0 2696 do {
aoqi@0 2697 name2++;
aoqi@0 2698 } while (*name2 == '[');
aoqi@0 2699 if (*name2 != 'L') {
aoqi@0 2700 // Something is terribly wrong. Shouldn't be here.
aoqi@0 2701 return false;
aoqi@0 2702 }
aoqi@0 2703 }
aoqi@0 2704
aoqi@0 2705 // Check that package part is identical
aoqi@0 2706 int length1 = last_slash1 - name1;
aoqi@0 2707 int length2 = last_slash2 - name2;
aoqi@0 2708
aoqi@0 2709 return UTF8::equal(name1, length1, name2, length2);
aoqi@0 2710 }
aoqi@0 2711 }
aoqi@0 2712 }
aoqi@0 2713
aoqi@0 2714 // Returns true iff super_method can be overridden by a method in targetclassname
aoqi@0 2715 // See JSL 3rd edition 8.4.6.1
aoqi@0 2716 // Assumes name-signature match
aoqi@0 2717 // "this" is InstanceKlass of super_method which must exist
aoqi@0 2718 // note that the InstanceKlass of the method in the targetclassname has not always been created yet
aoqi@0 2719 bool InstanceKlass::is_override(methodHandle super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS) {
aoqi@0 2720 // Private methods can not be overridden
aoqi@0 2721 if (super_method->is_private()) {
aoqi@0 2722 return false;
aoqi@0 2723 }
aoqi@0 2724 // If super method is accessible, then override
aoqi@0 2725 if ((super_method->is_protected()) ||
aoqi@0 2726 (super_method->is_public())) {
aoqi@0 2727 return true;
aoqi@0 2728 }
aoqi@0 2729 // Package-private methods are not inherited outside of package
aoqi@0 2730 assert(super_method->is_package_private(), "must be package private");
aoqi@0 2731 return(is_same_class_package(targetclassloader(), targetclassname));
aoqi@0 2732 }
aoqi@0 2733
aoqi@0 2734 /* defined for now in jvm.cpp, for historical reasons *--
aoqi@0 2735 Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle self,
aoqi@0 2736 Symbol*& simple_name_result, TRAPS) {
aoqi@0 2737 ...
aoqi@0 2738 }
aoqi@0 2739 */
aoqi@0 2740
aoqi@0 2741 // tell if two classes have the same enclosing class (at package level)
aoqi@0 2742 bool InstanceKlass::is_same_package_member_impl(instanceKlassHandle class1,
aoqi@0 2743 Klass* class2_oop, TRAPS) {
aoqi@0 2744 if (class2_oop == class1()) return true;
aoqi@0 2745 if (!class2_oop->oop_is_instance()) return false;
aoqi@0 2746 instanceKlassHandle class2(THREAD, class2_oop);
aoqi@0 2747
aoqi@0 2748 // must be in same package before we try anything else
aoqi@0 2749 if (!class1->is_same_class_package(class2->class_loader(), class2->name()))
aoqi@0 2750 return false;
aoqi@0 2751
aoqi@0 2752 // As long as there is an outer1.getEnclosingClass,
aoqi@0 2753 // shift the search outward.
aoqi@0 2754 instanceKlassHandle outer1 = class1;
aoqi@0 2755 for (;;) {
aoqi@0 2756 // As we walk along, look for equalities between outer1 and class2.
aoqi@0 2757 // Eventually, the walks will terminate as outer1 stops
aoqi@0 2758 // at the top-level class around the original class.
aoqi@0 2759 bool ignore_inner_is_member;
aoqi@0 2760 Klass* next = outer1->compute_enclosing_class(&ignore_inner_is_member,
aoqi@0 2761 CHECK_false);
aoqi@0 2762 if (next == NULL) break;
aoqi@0 2763 if (next == class2()) return true;
aoqi@0 2764 outer1 = instanceKlassHandle(THREAD, next);
aoqi@0 2765 }
aoqi@0 2766
aoqi@0 2767 // Now do the same for class2.
aoqi@0 2768 instanceKlassHandle outer2 = class2;
aoqi@0 2769 for (;;) {
aoqi@0 2770 bool ignore_inner_is_member;
aoqi@0 2771 Klass* next = outer2->compute_enclosing_class(&ignore_inner_is_member,
aoqi@0 2772 CHECK_false);
aoqi@0 2773 if (next == NULL) break;
aoqi@0 2774 // Might as well check the new outer against all available values.
aoqi@0 2775 if (next == class1()) return true;
aoqi@0 2776 if (next == outer1()) return true;
aoqi@0 2777 outer2 = instanceKlassHandle(THREAD, next);
aoqi@0 2778 }
aoqi@0 2779
aoqi@0 2780 // If by this point we have not found an equality between the
aoqi@0 2781 // two classes, we know they are in separate package members.
aoqi@0 2782 return false;
aoqi@0 2783 }
aoqi@0 2784
aoqi@0 2785
aoqi@0 2786 jint InstanceKlass::compute_modifier_flags(TRAPS) const {
aoqi@0 2787 jint access = access_flags().as_int();
aoqi@0 2788
aoqi@0 2789 // But check if it happens to be member class.
aoqi@0 2790 instanceKlassHandle ik(THREAD, this);
aoqi@0 2791 InnerClassesIterator iter(ik);
aoqi@0 2792 for (; !iter.done(); iter.next()) {
aoqi@0 2793 int ioff = iter.inner_class_info_index();
aoqi@0 2794 // Inner class attribute can be zero, skip it.
aoqi@0 2795 // Strange but true: JVM spec. allows null inner class refs.
aoqi@0 2796 if (ioff == 0) continue;
aoqi@0 2797
aoqi@0 2798 // only look at classes that are already loaded
aoqi@0 2799 // since we are looking for the flags for our self.
aoqi@0 2800 Symbol* inner_name = ik->constants()->klass_name_at(ioff);
aoqi@0 2801 if ((ik->name() == inner_name)) {
aoqi@0 2802 // This is really a member class.
aoqi@0 2803 access = iter.inner_access_flags();
aoqi@0 2804 break;
aoqi@0 2805 }
aoqi@0 2806 }
aoqi@0 2807 // Remember to strip ACC_SUPER bit
aoqi@0 2808 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS;
aoqi@0 2809 }
aoqi@0 2810
aoqi@0 2811 jint InstanceKlass::jvmti_class_status() const {
aoqi@0 2812 jint result = 0;
aoqi@0 2813
aoqi@0 2814 if (is_linked()) {
aoqi@0 2815 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
aoqi@0 2816 }
aoqi@0 2817
aoqi@0 2818 if (is_initialized()) {
aoqi@0 2819 assert(is_linked(), "Class status is not consistent");
aoqi@0 2820 result |= JVMTI_CLASS_STATUS_INITIALIZED;
aoqi@0 2821 }
aoqi@0 2822 if (is_in_error_state()) {
aoqi@0 2823 result |= JVMTI_CLASS_STATUS_ERROR;
aoqi@0 2824 }
aoqi@0 2825 return result;
aoqi@0 2826 }
aoqi@0 2827
aoqi@0 2828 Method* InstanceKlass::method_at_itable(Klass* holder, int index, TRAPS) {
aoqi@0 2829 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
aoqi@0 2830 int method_table_offset_in_words = ioe->offset()/wordSize;
aoqi@0 2831 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
aoqi@0 2832 / itableOffsetEntry::size();
aoqi@0 2833
aoqi@0 2834 for (int cnt = 0 ; ; cnt ++, ioe ++) {
aoqi@0 2835 // If the interface isn't implemented by the receiver class,
aoqi@0 2836 // the VM should throw IncompatibleClassChangeError.
aoqi@0 2837 if (cnt >= nof_interfaces) {
aoqi@0 2838 THROW_NULL(vmSymbols::java_lang_IncompatibleClassChangeError());
aoqi@0 2839 }
aoqi@0 2840
aoqi@0 2841 Klass* ik = ioe->interface_klass();
aoqi@0 2842 if (ik == holder) break;
aoqi@0 2843 }
aoqi@0 2844
aoqi@0 2845 itableMethodEntry* ime = ioe->first_method_entry(this);
aoqi@0 2846 Method* m = ime[index].method();
aoqi@0 2847 if (m == NULL) {
aoqi@0 2848 THROW_NULL(vmSymbols::java_lang_AbstractMethodError());
aoqi@0 2849 }
aoqi@0 2850 return m;
aoqi@0 2851 }
aoqi@0 2852
aoqi@0 2853
aoqi@0 2854 #if INCLUDE_JVMTI
aoqi@0 2855 // update default_methods for redefineclasses for methods that are
aoqi@0 2856 // not yet in the vtable due to concurrent subclass define and superinterface
aoqi@0 2857 // redefinition
aoqi@0 2858 // Note: those in the vtable, should have been updated via adjust_method_entries
sspitsyn@7636 2859 void InstanceKlass::adjust_default_methods(InstanceKlass* holder, bool* trace_name_printed) {
aoqi@0 2860 // search the default_methods for uses of either obsolete or EMCP methods
aoqi@0 2861 if (default_methods() != NULL) {
sspitsyn@7636 2862 for (int index = 0; index < default_methods()->length(); index ++) {
sspitsyn@7636 2863 Method* old_method = default_methods()->at(index);
sspitsyn@7636 2864 if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) {
sspitsyn@7636 2865 continue; // skip uninteresting entries
sspitsyn@7636 2866 }
sspitsyn@7636 2867 assert(!old_method->is_deleted(), "default methods may not be deleted");
sspitsyn@7636 2868
sspitsyn@7636 2869 Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
sspitsyn@7636 2870
sspitsyn@7636 2871 assert(new_method != NULL, "method_with_idnum() should not be NULL");
sspitsyn@7636 2872 assert(old_method != new_method, "sanity check");
sspitsyn@7636 2873
sspitsyn@7636 2874 default_methods()->at_put(index, new_method);
sspitsyn@7636 2875 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
sspitsyn@7636 2876 if (!(*trace_name_printed)) {
sspitsyn@7636 2877 // RC_TRACE_MESG macro has an embedded ResourceMark
sspitsyn@7636 2878 RC_TRACE_MESG(("adjust: klassname=%s default methods from name=%s",
sspitsyn@7636 2879 external_name(),
sspitsyn@7636 2880 old_method->method_holder()->external_name()));
sspitsyn@7636 2881 *trace_name_printed = true;
aoqi@0 2882 }
sspitsyn@7636 2883 RC_TRACE(0x00100000, ("default method update: %s(%s) ",
sspitsyn@7636 2884 new_method->name()->as_C_string(),
sspitsyn@7636 2885 new_method->signature()->as_C_string()));
aoqi@0 2886 }
aoqi@0 2887 }
aoqi@0 2888 }
aoqi@0 2889 }
aoqi@0 2890 #endif // INCLUDE_JVMTI
aoqi@0 2891
aoqi@0 2892 // On-stack replacement stuff
aoqi@0 2893 void InstanceKlass::add_osr_nmethod(nmethod* n) {
aoqi@0 2894 // only one compilation can be active
aoqi@0 2895 NEEDS_CLEANUP
aoqi@0 2896 // This is a short non-blocking critical region, so the no safepoint check is ok.
aoqi@0 2897 OsrList_lock->lock_without_safepoint_check();
aoqi@0 2898 assert(n->is_osr_method(), "wrong kind of nmethod");
aoqi@0 2899 n->set_osr_link(osr_nmethods_head());
aoqi@0 2900 set_osr_nmethods_head(n);
aoqi@0 2901 // Raise the highest osr level if necessary
aoqi@0 2902 if (TieredCompilation) {
aoqi@0 2903 Method* m = n->method();
aoqi@0 2904 m->set_highest_osr_comp_level(MAX2(m->highest_osr_comp_level(), n->comp_level()));
aoqi@0 2905 }
aoqi@0 2906 // Remember to unlock again
aoqi@0 2907 OsrList_lock->unlock();
aoqi@0 2908
aoqi@0 2909 // Get rid of the osr methods for the same bci that have lower levels.
aoqi@0 2910 if (TieredCompilation) {
aoqi@0 2911 for (int l = CompLevel_limited_profile; l < n->comp_level(); l++) {
aoqi@0 2912 nmethod *inv = lookup_osr_nmethod(n->method(), n->osr_entry_bci(), l, true);
aoqi@0 2913 if (inv != NULL && inv->is_in_use()) {
aoqi@0 2914 inv->make_not_entrant();
aoqi@0 2915 }
aoqi@0 2916 }
aoqi@0 2917 }
aoqi@0 2918 }
aoqi@0 2919
aoqi@0 2920
aoqi@0 2921 void InstanceKlass::remove_osr_nmethod(nmethod* n) {
aoqi@0 2922 // This is a short non-blocking critical region, so the no safepoint check is ok.
aoqi@0 2923 OsrList_lock->lock_without_safepoint_check();
aoqi@0 2924 assert(n->is_osr_method(), "wrong kind of nmethod");
aoqi@0 2925 nmethod* last = NULL;
aoqi@0 2926 nmethod* cur = osr_nmethods_head();
aoqi@0 2927 int max_level = CompLevel_none; // Find the max comp level excluding n
aoqi@0 2928 Method* m = n->method();
aoqi@0 2929 // Search for match
aoqi@0 2930 while(cur != NULL && cur != n) {
aoqi@0 2931 if (TieredCompilation && m == cur->method()) {
aoqi@0 2932 // Find max level before n
aoqi@0 2933 max_level = MAX2(max_level, cur->comp_level());
aoqi@0 2934 }
aoqi@0 2935 last = cur;
aoqi@0 2936 cur = cur->osr_link();
aoqi@0 2937 }
aoqi@0 2938 nmethod* next = NULL;
aoqi@0 2939 if (cur == n) {
aoqi@0 2940 next = cur->osr_link();
aoqi@0 2941 if (last == NULL) {
aoqi@0 2942 // Remove first element
aoqi@0 2943 set_osr_nmethods_head(next);
aoqi@0 2944 } else {
aoqi@0 2945 last->set_osr_link(next);
aoqi@0 2946 }
aoqi@0 2947 }
aoqi@0 2948 n->set_osr_link(NULL);
aoqi@0 2949 if (TieredCompilation) {
aoqi@0 2950 cur = next;
aoqi@0 2951 while (cur != NULL) {
aoqi@0 2952 // Find max level after n
aoqi@0 2953 if (m == cur->method()) {
aoqi@0 2954 max_level = MAX2(max_level, cur->comp_level());
aoqi@0 2955 }
aoqi@0 2956 cur = cur->osr_link();
aoqi@0 2957 }
aoqi@0 2958 m->set_highest_osr_comp_level(max_level);
aoqi@0 2959 }
aoqi@0 2960 // Remember to unlock again
aoqi@0 2961 OsrList_lock->unlock();
aoqi@0 2962 }
aoqi@0 2963
thartmann@7325 2964 int InstanceKlass::mark_osr_nmethods(const Method* m) {
thartmann@7325 2965 // This is a short non-blocking critical region, so the no safepoint check is ok.
thartmann@7325 2966 MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
thartmann@7325 2967 nmethod* osr = osr_nmethods_head();
thartmann@7325 2968 int found = 0;
thartmann@7325 2969 while (osr != NULL) {
thartmann@7325 2970 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
thartmann@7325 2971 if (osr->method() == m) {
thartmann@7325 2972 osr->mark_for_deoptimization();
thartmann@7325 2973 found++;
thartmann@7325 2974 }
thartmann@7325 2975 osr = osr->osr_link();
thartmann@7325 2976 }
thartmann@7325 2977 return found;
thartmann@7325 2978 }
thartmann@7325 2979
aoqi@0 2980 nmethod* InstanceKlass::lookup_osr_nmethod(const Method* m, int bci, int comp_level, bool match_level) const {
aoqi@0 2981 // This is a short non-blocking critical region, so the no safepoint check is ok.
aoqi@0 2982 OsrList_lock->lock_without_safepoint_check();
aoqi@0 2983 nmethod* osr = osr_nmethods_head();
aoqi@0 2984 nmethod* best = NULL;
aoqi@0 2985 while (osr != NULL) {
aoqi@0 2986 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
aoqi@0 2987 // There can be a time when a c1 osr method exists but we are waiting
aoqi@0 2988 // for a c2 version. When c2 completes its osr nmethod we will trash
aoqi@0 2989 // the c1 version and only be able to find the c2 version. However
aoqi@0 2990 // while we overflow in the c1 code at back branches we don't want to
aoqi@0 2991 // try and switch to the same code as we are already running
aoqi@0 2992
aoqi@0 2993 if (osr->method() == m &&
aoqi@0 2994 (bci == InvocationEntryBci || osr->osr_entry_bci() == bci)) {
aoqi@0 2995 if (match_level) {
aoqi@0 2996 if (osr->comp_level() == comp_level) {
aoqi@0 2997 // Found a match - return it.
aoqi@0 2998 OsrList_lock->unlock();
aoqi@0 2999 return osr;
aoqi@0 3000 }
aoqi@0 3001 } else {
aoqi@0 3002 if (best == NULL || (osr->comp_level() > best->comp_level())) {
aoqi@0 3003 if (osr->comp_level() == CompLevel_highest_tier) {
aoqi@0 3004 // Found the best possible - return it.
aoqi@0 3005 OsrList_lock->unlock();
aoqi@0 3006 return osr;
aoqi@0 3007 }
aoqi@0 3008 best = osr;
aoqi@0 3009 }
aoqi@0 3010 }
aoqi@0 3011 }
aoqi@0 3012 osr = osr->osr_link();
aoqi@0 3013 }
aoqi@0 3014 OsrList_lock->unlock();
aoqi@0 3015 if (best != NULL && best->comp_level() >= comp_level && match_level == false) {
aoqi@0 3016 return best;
aoqi@0 3017 }
aoqi@0 3018 return NULL;
aoqi@0 3019 }
aoqi@0 3020
coleenp@7391 3021 bool InstanceKlass::add_member_name(Handle mem_name) {
aoqi@0 3022 jweak mem_name_wref = JNIHandles::make_weak_global(mem_name);
aoqi@0 3023 MutexLocker ml(MemberNameTable_lock);
aoqi@0 3024 DEBUG_ONLY(No_Safepoint_Verifier nsv);
aoqi@0 3025
coleenp@7391 3026 // Check if method has been redefined while taking out MemberNameTable_lock, if so
coleenp@7391 3027 // return false. We cannot cache obsolete methods. They will crash when the function
coleenp@7391 3028 // is called!
coleenp@7391 3029 Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mem_name());
coleenp@7391 3030 if (method->is_obsolete()) {
coleenp@7391 3031 return false;
coleenp@7391 3032 } else if (method->is_old()) {
coleenp@7391 3033 // Replace method with redefined version
coleenp@7391 3034 java_lang_invoke_MemberName::set_vmtarget(mem_name(), method_with_idnum(method->method_idnum()));
coleenp@7391 3035 }
coleenp@7391 3036
aoqi@0 3037 if (_member_names == NULL) {
aoqi@0 3038 _member_names = new (ResourceObj::C_HEAP, mtClass) MemberNameTable(idnum_allocated_count());
aoqi@0 3039 }
coleenp@7391 3040 _member_names->add_member_name(mem_name_wref);
coleenp@7391 3041 return true;
aoqi@0 3042 }
aoqi@0 3043
aoqi@0 3044 // -----------------------------------------------------------------------------------------------------
aoqi@0 3045 // Printing
aoqi@0 3046
aoqi@0 3047 #ifndef PRODUCT
aoqi@0 3048
aoqi@0 3049 #define BULLET " - "
aoqi@0 3050
aoqi@0 3051 static const char* state_names[] = {
aoqi@0 3052 "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
aoqi@0 3053 };
aoqi@0 3054
aoqi@0 3055 static void print_vtable(intptr_t* start, int len, outputStream* st) {
aoqi@0 3056 for (int i = 0; i < len; i++) {
aoqi@0 3057 intptr_t e = start[i];
aoqi@0 3058 st->print("%d : " INTPTR_FORMAT, i, e);
aoqi@0 3059 if (e != 0 && ((Metadata*)e)->is_metaspace_object()) {
aoqi@0 3060 st->print(" ");
aoqi@0 3061 ((Metadata*)e)->print_value_on(st);
aoqi@0 3062 }
aoqi@0 3063 st->cr();
aoqi@0 3064 }
aoqi@0 3065 }
aoqi@0 3066
aoqi@0 3067 void InstanceKlass::print_on(outputStream* st) const {
aoqi@0 3068 assert(is_klass(), "must be klass");
aoqi@0 3069 Klass::print_on(st);
aoqi@0 3070
aoqi@0 3071 st->print(BULLET"instance size: %d", size_helper()); st->cr();
aoqi@0 3072 st->print(BULLET"klass size: %d", size()); st->cr();
aoqi@0 3073 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
aoqi@0 3074 st->print(BULLET"state: "); st->print_cr("%s", state_names[_init_state]);
aoqi@0 3075 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
aoqi@0 3076 st->print(BULLET"super: "); super()->print_value_on_maybe_null(st); st->cr();
aoqi@0 3077 st->print(BULLET"sub: ");
aoqi@0 3078 Klass* sub = subklass();
aoqi@0 3079 int n;
aoqi@0 3080 for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
aoqi@0 3081 if (n < MaxSubklassPrintSize) {
aoqi@0 3082 sub->print_value_on(st);
aoqi@0 3083 st->print(" ");
aoqi@0 3084 }
aoqi@0 3085 }
aoqi@0 3086 if (n >= MaxSubklassPrintSize) st->print("(%d more klasses...)", n - MaxSubklassPrintSize);
aoqi@0 3087 st->cr();
aoqi@0 3088
aoqi@0 3089 if (is_interface()) {
aoqi@0 3090 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
aoqi@0 3091 if (nof_implementors() == 1) {
aoqi@0 3092 st->print_cr(BULLET"implementor: ");
aoqi@0 3093 st->print(" ");
aoqi@0 3094 implementor()->print_value_on(st);
aoqi@0 3095 st->cr();
aoqi@0 3096 }
aoqi@0 3097 }
aoqi@0 3098
aoqi@0 3099 st->print(BULLET"arrays: "); array_klasses()->print_value_on_maybe_null(st); st->cr();
aoqi@0 3100 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr();
aoqi@0 3101 if (Verbose || WizardMode) {
aoqi@0 3102 Array<Method*>* method_array = methods();
aoqi@0 3103 for (int i = 0; i < method_array->length(); i++) {
aoqi@0 3104 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
aoqi@0 3105 }
aoqi@0 3106 }
aoqi@0 3107 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr();
aoqi@0 3108 st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr();
aoqi@0 3109 if (Verbose && default_methods() != NULL) {
aoqi@0 3110 Array<Method*>* method_array = default_methods();
aoqi@0 3111 for (int i = 0; i < method_array->length(); i++) {
aoqi@0 3112 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
aoqi@0 3113 }
aoqi@0 3114 }
aoqi@0 3115 if (default_vtable_indices() != NULL) {
aoqi@0 3116 st->print(BULLET"default vtable indices: "); default_vtable_indices()->print_value_on(st); st->cr();
aoqi@0 3117 }
aoqi@0 3118 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
aoqi@0 3119 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
aoqi@0 3120 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
aoqi@0 3121 if (class_loader_data() != NULL) {
aoqi@0 3122 st->print(BULLET"class loader data: ");
aoqi@0 3123 class_loader_data()->print_value_on(st);
aoqi@0 3124 st->cr();
aoqi@0 3125 }
aoqi@0 3126 st->print(BULLET"host class: "); host_klass()->print_value_on_maybe_null(st); st->cr();
aoqi@0 3127 if (source_file_name() != NULL) {
aoqi@0 3128 st->print(BULLET"source file: ");
aoqi@0 3129 source_file_name()->print_value_on(st);
aoqi@0 3130 st->cr();
aoqi@0 3131 }
aoqi@0 3132 if (source_debug_extension() != NULL) {
aoqi@0 3133 st->print(BULLET"source debug extension: ");
aoqi@0 3134 st->print("%s", source_debug_extension());
aoqi@0 3135 st->cr();
aoqi@0 3136 }
aoqi@0 3137 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr();
aoqi@0 3138 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr();
aoqi@0 3139 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr();
aoqi@0 3140 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr();
aoqi@0 3141 {
aoqi@0 3142 bool have_pv = false;
aoqi@0 3143 PreviousVersionWalker pvw(Thread::current(), (InstanceKlass*)this);
aoqi@0 3144 for (PreviousVersionNode * pv_node = pvw.next_previous_version();
aoqi@0 3145 pv_node != NULL; pv_node = pvw.next_previous_version()) {
aoqi@0 3146 if (!have_pv)
aoqi@0 3147 st->print(BULLET"previous version: ");
aoqi@0 3148 have_pv = true;
aoqi@0 3149 pv_node->prev_constant_pool()->print_value_on(st);
aoqi@0 3150 }
aoqi@0 3151 if (have_pv) st->cr();
aoqi@0 3152 } // pvw is cleaned up
aoqi@0 3153
aoqi@0 3154 if (generic_signature() != NULL) {
aoqi@0 3155 st->print(BULLET"generic signature: ");
aoqi@0 3156 generic_signature()->print_value_on(st);
aoqi@0 3157 st->cr();
aoqi@0 3158 }
aoqi@0 3159 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
aoqi@0 3160 st->print(BULLET"java mirror: "); java_mirror()->print_value_on(st); st->cr();
aoqi@0 3161 st->print(BULLET"vtable length %d (start addr: " INTPTR_FORMAT ")", vtable_length(), start_of_vtable()); st->cr();
aoqi@0 3162 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
aoqi@0 3163 st->print(BULLET"itable length %d (start addr: " INTPTR_FORMAT ")", itable_length(), start_of_itable()); st->cr();
aoqi@0 3164 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st);
aoqi@0 3165 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
aoqi@0 3166 FieldPrinter print_static_field(st);
aoqi@0 3167 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
aoqi@0 3168 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
aoqi@0 3169 FieldPrinter print_nonstatic_field(st);
aoqi@0 3170 ((InstanceKlass*)this)->do_nonstatic_fields(&print_nonstatic_field);
aoqi@0 3171
aoqi@0 3172 st->print(BULLET"non-static oop maps: ");
aoqi@0 3173 OopMapBlock* map = start_of_nonstatic_oop_maps();
aoqi@0 3174 OopMapBlock* end_map = map + nonstatic_oop_map_count();
aoqi@0 3175 while (map < end_map) {
aoqi@0 3176 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
aoqi@0 3177 map++;
aoqi@0 3178 }
aoqi@0 3179 st->cr();
aoqi@0 3180 }
aoqi@0 3181
aoqi@0 3182 #endif //PRODUCT
aoqi@0 3183
aoqi@0 3184 void InstanceKlass::print_value_on(outputStream* st) const {
aoqi@0 3185 assert(is_klass(), "must be klass");
aoqi@0 3186 if (Verbose || WizardMode) access_flags().print_on(st);
aoqi@0 3187 name()->print_value_on(st);
aoqi@0 3188 }
aoqi@0 3189
aoqi@0 3190 #ifndef PRODUCT
aoqi@0 3191
aoqi@0 3192 void FieldPrinter::do_field(fieldDescriptor* fd) {
aoqi@0 3193 _st->print(BULLET);
aoqi@0 3194 if (_obj == NULL) {
aoqi@0 3195 fd->print_on(_st);
aoqi@0 3196 _st->cr();
aoqi@0 3197 } else {
aoqi@0 3198 fd->print_on_for(_st, _obj);
aoqi@0 3199 _st->cr();
aoqi@0 3200 }
aoqi@0 3201 }
aoqi@0 3202
aoqi@0 3203
aoqi@0 3204 void InstanceKlass::oop_print_on(oop obj, outputStream* st) {
aoqi@0 3205 Klass::oop_print_on(obj, st);
aoqi@0 3206
aoqi@0 3207 if (this == SystemDictionary::String_klass()) {
aoqi@0 3208 typeArrayOop value = java_lang_String::value(obj);
aoqi@0 3209 juint offset = java_lang_String::offset(obj);
aoqi@0 3210 juint length = java_lang_String::length(obj);
aoqi@0 3211 if (value != NULL &&
aoqi@0 3212 value->is_typeArray() &&
aoqi@0 3213 offset <= (juint) value->length() &&
aoqi@0 3214 offset + length <= (juint) value->length()) {
aoqi@0 3215 st->print(BULLET"string: ");
stefank@6975 3216 java_lang_String::print(obj, st);
aoqi@0 3217 st->cr();
aoqi@0 3218 if (!WizardMode) return; // that is enough
aoqi@0 3219 }
aoqi@0 3220 }
aoqi@0 3221
aoqi@0 3222 st->print_cr(BULLET"---- fields (total size %d words):", oop_size(obj));
aoqi@0 3223 FieldPrinter print_field(st, obj);
aoqi@0 3224 do_nonstatic_fields(&print_field);
aoqi@0 3225
aoqi@0 3226 if (this == SystemDictionary::Class_klass()) {
aoqi@0 3227 st->print(BULLET"signature: ");
aoqi@0 3228 java_lang_Class::print_signature(obj, st);
aoqi@0 3229 st->cr();
aoqi@0 3230 Klass* mirrored_klass = java_lang_Class::as_Klass(obj);
aoqi@0 3231 st->print(BULLET"fake entry for mirror: ");
aoqi@0 3232 mirrored_klass->print_value_on_maybe_null(st);
aoqi@0 3233 st->cr();
aoqi@0 3234 Klass* array_klass = java_lang_Class::array_klass(obj);
aoqi@0 3235 st->print(BULLET"fake entry for array: ");
aoqi@0 3236 array_klass->print_value_on_maybe_null(st);
aoqi@0 3237 st->cr();
aoqi@0 3238 st->print_cr(BULLET"fake entry for oop_size: %d", java_lang_Class::oop_size(obj));
aoqi@0 3239 st->print_cr(BULLET"fake entry for static_oop_field_count: %d", java_lang_Class::static_oop_field_count(obj));
aoqi@0 3240 Klass* real_klass = java_lang_Class::as_Klass(obj);
aoqi@0 3241 if (real_klass != NULL && real_klass->oop_is_instance()) {
aoqi@0 3242 InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
aoqi@0 3243 }
aoqi@0 3244 } else if (this == SystemDictionary::MethodType_klass()) {
aoqi@0 3245 st->print(BULLET"signature: ");
aoqi@0 3246 java_lang_invoke_MethodType::print_signature(obj, st);
aoqi@0 3247 st->cr();
aoqi@0 3248 }
aoqi@0 3249 }
aoqi@0 3250
aoqi@0 3251 #endif //PRODUCT
aoqi@0 3252
aoqi@0 3253 void InstanceKlass::oop_print_value_on(oop obj, outputStream* st) {
aoqi@0 3254 st->print("a ");
aoqi@0 3255 name()->print_value_on(st);
aoqi@0 3256 obj->print_address_on(st);
aoqi@0 3257 if (this == SystemDictionary::String_klass()
aoqi@0 3258 && java_lang_String::value(obj) != NULL) {
aoqi@0 3259 ResourceMark rm;
aoqi@0 3260 int len = java_lang_String::length(obj);
aoqi@0 3261 int plen = (len < 24 ? len : 12);
aoqi@0 3262 char* str = java_lang_String::as_utf8_string(obj, 0, plen);
aoqi@0 3263 st->print(" = \"%s\"", str);
aoqi@0 3264 if (len > plen)
aoqi@0 3265 st->print("...[%d]", len);
aoqi@0 3266 } else if (this == SystemDictionary::Class_klass()) {
aoqi@0 3267 Klass* k = java_lang_Class::as_Klass(obj);
aoqi@0 3268 st->print(" = ");
aoqi@0 3269 if (k != NULL) {
aoqi@0 3270 k->print_value_on(st);
aoqi@0 3271 } else {
aoqi@0 3272 const char* tname = type2name(java_lang_Class::primitive_type(obj));
aoqi@0 3273 st->print("%s", tname ? tname : "type?");
aoqi@0 3274 }
aoqi@0 3275 } else if (this == SystemDictionary::MethodType_klass()) {
aoqi@0 3276 st->print(" = ");
aoqi@0 3277 java_lang_invoke_MethodType::print_signature(obj, st);
aoqi@0 3278 } else if (java_lang_boxing_object::is_instance(obj)) {
aoqi@0 3279 st->print(" = ");
aoqi@0 3280 java_lang_boxing_object::print(obj, st);
aoqi@0 3281 } else if (this == SystemDictionary::LambdaForm_klass()) {
aoqi@0 3282 oop vmentry = java_lang_invoke_LambdaForm::vmentry(obj);
aoqi@0 3283 if (vmentry != NULL) {
aoqi@0 3284 st->print(" => ");
aoqi@0 3285 vmentry->print_value_on(st);
aoqi@0 3286 }
aoqi@0 3287 } else if (this == SystemDictionary::MemberName_klass()) {
aoqi@0 3288 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(obj);
aoqi@0 3289 if (vmtarget != NULL) {
aoqi@0 3290 st->print(" = ");
aoqi@0 3291 vmtarget->print_value_on(st);
aoqi@0 3292 } else {
aoqi@0 3293 java_lang_invoke_MemberName::clazz(obj)->print_value_on(st);
aoqi@0 3294 st->print(".");
aoqi@0 3295 java_lang_invoke_MemberName::name(obj)->print_value_on(st);
aoqi@0 3296 }
aoqi@0 3297 }
aoqi@0 3298 }
aoqi@0 3299
aoqi@0 3300 const char* InstanceKlass::internal_name() const {
aoqi@0 3301 return external_name();
aoqi@0 3302 }
aoqi@0 3303
aoqi@0 3304 #if INCLUDE_SERVICES
aoqi@0 3305 // Size Statistics
aoqi@0 3306 void InstanceKlass::collect_statistics(KlassSizeStats *sz) const {
aoqi@0 3307 Klass::collect_statistics(sz);
aoqi@0 3308
aoqi@0 3309 sz->_inst_size = HeapWordSize * size_helper();
aoqi@0 3310 sz->_vtab_bytes = HeapWordSize * align_object_offset(vtable_length());
aoqi@0 3311 sz->_itab_bytes = HeapWordSize * align_object_offset(itable_length());
aoqi@0 3312 sz->_nonstatic_oopmap_bytes = HeapWordSize *
aoqi@0 3313 ((is_interface() || is_anonymous()) ?
aoqi@0 3314 align_object_offset(nonstatic_oop_map_size()) :
aoqi@0 3315 nonstatic_oop_map_size());
aoqi@0 3316
aoqi@0 3317 int n = 0;
aoqi@0 3318 n += (sz->_methods_array_bytes = sz->count_array(methods()));
aoqi@0 3319 n += (sz->_method_ordering_bytes = sz->count_array(method_ordering()));
aoqi@0 3320 n += (sz->_local_interfaces_bytes = sz->count_array(local_interfaces()));
aoqi@0 3321 n += (sz->_transitive_interfaces_bytes = sz->count_array(transitive_interfaces()));
aoqi@0 3322 n += (sz->_fields_bytes = sz->count_array(fields()));
aoqi@0 3323 n += (sz->_inner_classes_bytes = sz->count_array(inner_classes()));
aoqi@0 3324 sz->_ro_bytes += n;
aoqi@0 3325
aoqi@0 3326 const ConstantPool* cp = constants();
aoqi@0 3327 if (cp) {
aoqi@0 3328 cp->collect_statistics(sz);
aoqi@0 3329 }
aoqi@0 3330
aoqi@0 3331 const Annotations* anno = annotations();
aoqi@0 3332 if (anno) {
aoqi@0 3333 anno->collect_statistics(sz);
aoqi@0 3334 }
aoqi@0 3335
aoqi@0 3336 const Array<Method*>* methods_array = methods();
aoqi@0 3337 if (methods()) {
aoqi@0 3338 for (int i = 0; i < methods_array->length(); i++) {
aoqi@0 3339 Method* method = methods_array->at(i);
aoqi@0 3340 if (method) {
aoqi@0 3341 sz->_method_count ++;
aoqi@0 3342 method->collect_statistics(sz);
aoqi@0 3343 }
aoqi@0 3344 }
aoqi@0 3345 }
aoqi@0 3346 }
aoqi@0 3347 #endif // INCLUDE_SERVICES
aoqi@0 3348
aoqi@0 3349 // Verification
aoqi@0 3350
aoqi@0 3351 class VerifyFieldClosure: public OopClosure {
aoqi@0 3352 protected:
aoqi@0 3353 template <class T> void do_oop_work(T* p) {
aoqi@0 3354 oop obj = oopDesc::load_decode_heap_oop(p);
aoqi@0 3355 if (!obj->is_oop_or_null()) {
aoqi@0 3356 tty->print_cr("Failed: " PTR_FORMAT " -> " PTR_FORMAT, p, (address)obj);
aoqi@0 3357 Universe::print();
aoqi@0 3358 guarantee(false, "boom");
aoqi@0 3359 }
aoqi@0 3360 }
aoqi@0 3361 public:
aoqi@0 3362 virtual void do_oop(oop* p) { VerifyFieldClosure::do_oop_work(p); }
aoqi@0 3363 virtual void do_oop(narrowOop* p) { VerifyFieldClosure::do_oop_work(p); }
aoqi@0 3364 };
aoqi@0 3365
aoqi@0 3366 void InstanceKlass::verify_on(outputStream* st) {
aoqi@0 3367 #ifndef PRODUCT
aoqi@0 3368 // Avoid redundant verifies, this really should be in product.
aoqi@0 3369 if (_verify_count == Universe::verify_count()) return;
aoqi@0 3370 _verify_count = Universe::verify_count();
aoqi@0 3371 #endif
aoqi@0 3372
aoqi@0 3373 // Verify Klass
aoqi@0 3374 Klass::verify_on(st);
aoqi@0 3375
aoqi@0 3376 // Verify that klass is present in ClassLoaderData
aoqi@0 3377 guarantee(class_loader_data()->contains_klass(this),
aoqi@0 3378 "this class isn't found in class loader data");
aoqi@0 3379
aoqi@0 3380 // Verify vtables
aoqi@0 3381 if (is_linked()) {
aoqi@0 3382 ResourceMark rm;
aoqi@0 3383 // $$$ This used to be done only for m/s collections. Doing it
aoqi@0 3384 // always seemed a valid generalization. (DLD -- 6/00)
aoqi@0 3385 vtable()->verify(st);
aoqi@0 3386 }
aoqi@0 3387
aoqi@0 3388 // Verify first subklass
aoqi@0 3389 if (subklass_oop() != NULL) {
aoqi@0 3390 guarantee(subklass_oop()->is_klass(), "should be klass");
aoqi@0 3391 }
aoqi@0 3392
aoqi@0 3393 // Verify siblings
aoqi@0 3394 Klass* super = this->super();
aoqi@0 3395 Klass* sib = next_sibling();
aoqi@0 3396 if (sib != NULL) {
aoqi@0 3397 if (sib == this) {
aoqi@0 3398 fatal(err_msg("subclass points to itself " PTR_FORMAT, sib));
aoqi@0 3399 }
aoqi@0 3400
aoqi@0 3401 guarantee(sib->is_klass(), "should be klass");
aoqi@0 3402 guarantee(sib->super() == super, "siblings should have same superklass");
aoqi@0 3403 }
aoqi@0 3404
aoqi@0 3405 // Verify implementor fields
aoqi@0 3406 Klass* im = implementor();
aoqi@0 3407 if (im != NULL) {
aoqi@0 3408 guarantee(is_interface(), "only interfaces should have implementor set");
aoqi@0 3409 guarantee(im->is_klass(), "should be klass");
aoqi@0 3410 guarantee(!im->is_interface() || im == this,
aoqi@0 3411 "implementors cannot be interfaces");
aoqi@0 3412 }
aoqi@0 3413
aoqi@0 3414 // Verify local interfaces
aoqi@0 3415 if (local_interfaces()) {
aoqi@0 3416 Array<Klass*>* local_interfaces = this->local_interfaces();
aoqi@0 3417 for (int j = 0; j < local_interfaces->length(); j++) {
aoqi@0 3418 Klass* e = local_interfaces->at(j);
aoqi@0 3419 guarantee(e->is_klass() && e->is_interface(), "invalid local interface");
aoqi@0 3420 }
aoqi@0 3421 }
aoqi@0 3422
aoqi@0 3423 // Verify transitive interfaces
aoqi@0 3424 if (transitive_interfaces() != NULL) {
aoqi@0 3425 Array<Klass*>* transitive_interfaces = this->transitive_interfaces();
aoqi@0 3426 for (int j = 0; j < transitive_interfaces->length(); j++) {
aoqi@0 3427 Klass* e = transitive_interfaces->at(j);
aoqi@0 3428 guarantee(e->is_klass() && e->is_interface(), "invalid transitive interface");
aoqi@0 3429 }
aoqi@0 3430 }
aoqi@0 3431
aoqi@0 3432 // Verify methods
aoqi@0 3433 if (methods() != NULL) {
aoqi@0 3434 Array<Method*>* methods = this->methods();
aoqi@0 3435 for (int j = 0; j < methods->length(); j++) {
aoqi@0 3436 guarantee(methods->at(j)->is_method(), "non-method in methods array");
aoqi@0 3437 }
aoqi@0 3438 for (int j = 0; j < methods->length() - 1; j++) {
aoqi@0 3439 Method* m1 = methods->at(j);
aoqi@0 3440 Method* m2 = methods->at(j + 1);
aoqi@0 3441 guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly");
aoqi@0 3442 }
aoqi@0 3443 }
aoqi@0 3444
aoqi@0 3445 // Verify method ordering
aoqi@0 3446 if (method_ordering() != NULL) {
aoqi@0 3447 Array<int>* method_ordering = this->method_ordering();
aoqi@0 3448 int length = method_ordering->length();
aoqi@0 3449 if (JvmtiExport::can_maintain_original_method_order() ||
aoqi@0 3450 ((UseSharedSpaces || DumpSharedSpaces) && length != 0)) {
aoqi@0 3451 guarantee(length == methods()->length(), "invalid method ordering length");
aoqi@0 3452 jlong sum = 0;
aoqi@0 3453 for (int j = 0; j < length; j++) {
aoqi@0 3454 int original_index = method_ordering->at(j);
aoqi@0 3455 guarantee(original_index >= 0, "invalid method ordering index");
aoqi@0 3456 guarantee(original_index < length, "invalid method ordering index");
aoqi@0 3457 sum += original_index;
aoqi@0 3458 }
aoqi@0 3459 // Verify sum of indices 0,1,...,length-1
aoqi@0 3460 guarantee(sum == ((jlong)length*(length-1))/2, "invalid method ordering sum");
aoqi@0 3461 } else {
aoqi@0 3462 guarantee(length == 0, "invalid method ordering length");
aoqi@0 3463 }
aoqi@0 3464 }
aoqi@0 3465
aoqi@0 3466 // Verify default methods
aoqi@0 3467 if (default_methods() != NULL) {
aoqi@0 3468 Array<Method*>* methods = this->default_methods();
aoqi@0 3469 for (int j = 0; j < methods->length(); j++) {
aoqi@0 3470 guarantee(methods->at(j)->is_method(), "non-method in methods array");
aoqi@0 3471 }
aoqi@0 3472 for (int j = 0; j < methods->length() - 1; j++) {
aoqi@0 3473 Method* m1 = methods->at(j);
aoqi@0 3474 Method* m2 = methods->at(j + 1);
aoqi@0 3475 guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly");
aoqi@0 3476 }
aoqi@0 3477 }
aoqi@0 3478
aoqi@0 3479 // Verify JNI static field identifiers
aoqi@0 3480 if (jni_ids() != NULL) {
aoqi@0 3481 jni_ids()->verify(this);
aoqi@0 3482 }
aoqi@0 3483
aoqi@0 3484 // Verify other fields
aoqi@0 3485 if (array_klasses() != NULL) {
aoqi@0 3486 guarantee(array_klasses()->is_klass(), "should be klass");
aoqi@0 3487 }
aoqi@0 3488 if (constants() != NULL) {
aoqi@0 3489 guarantee(constants()->is_constantPool(), "should be constant pool");
aoqi@0 3490 }
aoqi@0 3491 const Klass* host = host_klass();
aoqi@0 3492 if (host != NULL) {
aoqi@0 3493 guarantee(host->is_klass(), "should be klass");
aoqi@0 3494 }
aoqi@0 3495 }
aoqi@0 3496
aoqi@0 3497 void InstanceKlass::oop_verify_on(oop obj, outputStream* st) {
aoqi@0 3498 Klass::oop_verify_on(obj, st);
aoqi@0 3499 VerifyFieldClosure blk;
aoqi@0 3500 obj->oop_iterate_no_header(&blk);
aoqi@0 3501 }
aoqi@0 3502
aoqi@0 3503
aoqi@0 3504 // JNIid class for jfieldIDs only
aoqi@0 3505 // Note to reviewers:
aoqi@0 3506 // These JNI functions are just moved over to column 1 and not changed
aoqi@0 3507 // in the compressed oops workspace.
aoqi@0 3508 JNIid::JNIid(Klass* holder, int offset, JNIid* next) {
aoqi@0 3509 _holder = holder;
aoqi@0 3510 _offset = offset;
aoqi@0 3511 _next = next;
aoqi@0 3512 debug_only(_is_static_field_id = false;)
aoqi@0 3513 }
aoqi@0 3514
aoqi@0 3515
aoqi@0 3516 JNIid* JNIid::find(int offset) {
aoqi@0 3517 JNIid* current = this;
aoqi@0 3518 while (current != NULL) {
aoqi@0 3519 if (current->offset() == offset) return current;
aoqi@0 3520 current = current->next();
aoqi@0 3521 }
aoqi@0 3522 return NULL;
aoqi@0 3523 }
aoqi@0 3524
aoqi@0 3525 void JNIid::deallocate(JNIid* current) {
aoqi@0 3526 while (current != NULL) {
aoqi@0 3527 JNIid* next = current->next();
aoqi@0 3528 delete current;
aoqi@0 3529 current = next;
aoqi@0 3530 }
aoqi@0 3531 }
aoqi@0 3532
aoqi@0 3533
aoqi@0 3534 void JNIid::verify(Klass* holder) {
aoqi@0 3535 int first_field_offset = InstanceMirrorKlass::offset_of_static_fields();
aoqi@0 3536 int end_field_offset;
aoqi@0 3537 end_field_offset = first_field_offset + (InstanceKlass::cast(holder)->static_field_size() * wordSize);
aoqi@0 3538
aoqi@0 3539 JNIid* current = this;
aoqi@0 3540 while (current != NULL) {
aoqi@0 3541 guarantee(current->holder() == holder, "Invalid klass in JNIid");
aoqi@0 3542 #ifdef ASSERT
aoqi@0 3543 int o = current->offset();
aoqi@0 3544 if (current->is_static_field_id()) {
aoqi@0 3545 guarantee(o >= first_field_offset && o < end_field_offset, "Invalid static field offset in JNIid");
aoqi@0 3546 }
aoqi@0 3547 #endif
aoqi@0 3548 current = current->next();
aoqi@0 3549 }
aoqi@0 3550 }
aoqi@0 3551
aoqi@0 3552
aoqi@0 3553 #ifdef ASSERT
aoqi@0 3554 void InstanceKlass::set_init_state(ClassState state) {
aoqi@0 3555 bool good_state = is_shared() ? (_init_state <= state)
aoqi@0 3556 : (_init_state < state);
aoqi@0 3557 assert(good_state || state == allocated, "illegal state transition");
aoqi@0 3558 _init_state = (u1)state;
aoqi@0 3559 }
aoqi@0 3560 #endif
aoqi@0 3561
aoqi@0 3562
aoqi@0 3563 // RedefineClasses() support for previous versions:
aoqi@0 3564
aoqi@0 3565 // Purge previous versions
aoqi@0 3566 static void purge_previous_versions_internal(InstanceKlass* ik, int emcp_method_count) {
aoqi@0 3567 if (ik->previous_versions() != NULL) {
aoqi@0 3568 // This klass has previous versions so see what we can cleanup
aoqi@0 3569 // while it is safe to do so.
aoqi@0 3570
aoqi@0 3571 int deleted_count = 0; // leave debugging breadcrumbs
aoqi@0 3572 int live_count = 0;
aoqi@0 3573 ClassLoaderData* loader_data = ik->class_loader_data() == NULL ?
aoqi@0 3574 ClassLoaderData::the_null_class_loader_data() :
aoqi@0 3575 ik->class_loader_data();
aoqi@0 3576
aoqi@0 3577 // RC_TRACE macro has an embedded ResourceMark
aoqi@0 3578 RC_TRACE(0x00000200, ("purge: %s: previous version length=%d",
aoqi@0 3579 ik->external_name(), ik->previous_versions()->length()));
aoqi@0 3580
aoqi@0 3581 for (int i = ik->previous_versions()->length() - 1; i >= 0; i--) {
aoqi@0 3582 // check the previous versions array
aoqi@0 3583 PreviousVersionNode * pv_node = ik->previous_versions()->at(i);
aoqi@0 3584 ConstantPool* cp_ref = pv_node->prev_constant_pool();
aoqi@0 3585 assert(cp_ref != NULL, "cp ref was unexpectedly cleared");
aoqi@0 3586
aoqi@0 3587 ConstantPool* pvcp = cp_ref;
aoqi@0 3588 if (!pvcp->on_stack()) {
aoqi@0 3589 // If the constant pool isn't on stack, none of the methods
aoqi@0 3590 // are executing. Delete all the methods, the constant pool and
aoqi@0 3591 // and this previous version node.
aoqi@0 3592 GrowableArray<Method*>* method_refs = pv_node->prev_EMCP_methods();
aoqi@0 3593 if (method_refs != NULL) {
aoqi@0 3594 for (int j = method_refs->length() - 1; j >= 0; j--) {
aoqi@0 3595 Method* method = method_refs->at(j);
aoqi@0 3596 assert(method != NULL, "method ref was unexpectedly cleared");
aoqi@0 3597 method_refs->remove_at(j);
aoqi@0 3598 // method will be freed with associated class.
aoqi@0 3599 }
aoqi@0 3600 }
aoqi@0 3601 // Remove the constant pool
aoqi@0 3602 delete pv_node;
aoqi@0 3603 // Since we are traversing the array backwards, we don't have to
aoqi@0 3604 // do anything special with the index.
aoqi@0 3605 ik->previous_versions()->remove_at(i);
aoqi@0 3606 deleted_count++;
aoqi@0 3607 continue;
aoqi@0 3608 } else {
aoqi@0 3609 RC_TRACE(0x00000200, ("purge: previous version @%d is alive", i));
aoqi@0 3610 assert(pvcp->pool_holder() != NULL, "Constant pool with no holder");
aoqi@0 3611 guarantee (!loader_data->is_unloading(), "unloaded classes can't be on the stack");
aoqi@0 3612 live_count++;
aoqi@0 3613 }
aoqi@0 3614
aoqi@0 3615 // At least one method is live in this previous version, clean out
aoqi@0 3616 // the others or mark them as obsolete.
aoqi@0 3617 GrowableArray<Method*>* method_refs = pv_node->prev_EMCP_methods();
aoqi@0 3618 if (method_refs != NULL) {
aoqi@0 3619 RC_TRACE(0x00000200, ("purge: previous methods length=%d",
aoqi@0 3620 method_refs->length()));
aoqi@0 3621 for (int j = method_refs->length() - 1; j >= 0; j--) {
aoqi@0 3622 Method* method = method_refs->at(j);
aoqi@0 3623 assert(method != NULL, "method ref was unexpectedly cleared");
aoqi@0 3624
aoqi@0 3625 // Remove the emcp method if it's not executing
aoqi@0 3626 // If it's been made obsolete by a redefinition of a non-emcp
aoqi@0 3627 // method, mark it as obsolete but leave it to clean up later.
aoqi@0 3628 if (!method->on_stack()) {
aoqi@0 3629 method_refs->remove_at(j);
aoqi@0 3630 } else if (emcp_method_count == 0) {
aoqi@0 3631 method->set_is_obsolete();
aoqi@0 3632 } else {
aoqi@0 3633 // RC_TRACE macro has an embedded ResourceMark
aoqi@0 3634 RC_TRACE(0x00000200,
aoqi@0 3635 ("purge: %s(%s): prev method @%d in version @%d is alive",
aoqi@0 3636 method->name()->as_C_string(),
aoqi@0 3637 method->signature()->as_C_string(), j, i));
aoqi@0 3638 }
aoqi@0 3639 }
aoqi@0 3640 }
aoqi@0 3641 }
aoqi@0 3642 assert(ik->previous_versions()->length() == live_count, "sanity check");
aoqi@0 3643 RC_TRACE(0x00000200,
aoqi@0 3644 ("purge: previous version stats: live=%d, deleted=%d", live_count,
aoqi@0 3645 deleted_count));
aoqi@0 3646 }
aoqi@0 3647 }
aoqi@0 3648
aoqi@0 3649 // External interface for use during class unloading.
aoqi@0 3650 void InstanceKlass::purge_previous_versions(InstanceKlass* ik) {
aoqi@0 3651 // Call with >0 emcp methods since they are not currently being redefined.
aoqi@0 3652 purge_previous_versions_internal(ik, 1);
aoqi@0 3653 }
aoqi@0 3654
aoqi@0 3655
aoqi@0 3656 // Potentially add an information node that contains pointers to the
aoqi@0 3657 // interesting parts of the previous version of the_class.
aoqi@0 3658 // This is also where we clean out any unused references.
aoqi@0 3659 // Note that while we delete nodes from the _previous_versions
aoqi@0 3660 // array, we never delete the array itself until the klass is
aoqi@0 3661 // unloaded. The has_been_redefined() query depends on that fact.
aoqi@0 3662 //
aoqi@0 3663 void InstanceKlass::add_previous_version(instanceKlassHandle ikh,
aoqi@0 3664 BitMap* emcp_methods, int emcp_method_count) {
aoqi@0 3665 assert(Thread::current()->is_VM_thread(),
aoqi@0 3666 "only VMThread can add previous versions");
aoqi@0 3667
aoqi@0 3668 if (_previous_versions == NULL) {
aoqi@0 3669 // This is the first previous version so make some space.
aoqi@0 3670 // Start with 2 elements under the assumption that the class
aoqi@0 3671 // won't be redefined much.
aoqi@0 3672 _previous_versions = new (ResourceObj::C_HEAP, mtClass)
aoqi@0 3673 GrowableArray<PreviousVersionNode *>(2, true);
aoqi@0 3674 }
aoqi@0 3675
aoqi@0 3676 ConstantPool* cp_ref = ikh->constants();
aoqi@0 3677
aoqi@0 3678 // RC_TRACE macro has an embedded ResourceMark
aoqi@0 3679 RC_TRACE(0x00000400, ("adding previous version ref for %s @%d, EMCP_cnt=%d "
aoqi@0 3680 "on_stack=%d",
aoqi@0 3681 ikh->external_name(), _previous_versions->length(), emcp_method_count,
aoqi@0 3682 cp_ref->on_stack()));
aoqi@0 3683
aoqi@0 3684 // If the constant pool for this previous version of the class
aoqi@0 3685 // is not marked as being on the stack, then none of the methods
aoqi@0 3686 // in this previous version of the class are on the stack so
aoqi@0 3687 // we don't need to create a new PreviousVersionNode. However,
aoqi@0 3688 // we still need to examine older previous versions below.
aoqi@0 3689 Array<Method*>* old_methods = ikh->methods();
aoqi@0 3690
aoqi@0 3691 if (cp_ref->on_stack()) {
aoqi@0 3692 PreviousVersionNode * pv_node = NULL;
aoqi@0 3693 if (emcp_method_count == 0) {
aoqi@0 3694 // non-shared ConstantPool gets a reference
aoqi@0 3695 pv_node = new PreviousVersionNode(cp_ref, NULL);
aoqi@0 3696 RC_TRACE(0x00000400,
aoqi@0 3697 ("add: all methods are obsolete; flushing any EMCP refs"));
aoqi@0 3698 } else {
aoqi@0 3699 int local_count = 0;
aoqi@0 3700 GrowableArray<Method*>* method_refs = new (ResourceObj::C_HEAP, mtClass)
aoqi@0 3701 GrowableArray<Method*>(emcp_method_count, true);
aoqi@0 3702 for (int i = 0; i < old_methods->length(); i++) {
aoqi@0 3703 if (emcp_methods->at(i)) {
aoqi@0 3704 // this old method is EMCP. Save it only if it's on the stack
aoqi@0 3705 Method* old_method = old_methods->at(i);
aoqi@0 3706 if (old_method->on_stack()) {
aoqi@0 3707 method_refs->append(old_method);
aoqi@0 3708 }
aoqi@0 3709 if (++local_count >= emcp_method_count) {
aoqi@0 3710 // no more EMCP methods so bail out now
aoqi@0 3711 break;
aoqi@0 3712 }
aoqi@0 3713 }
aoqi@0 3714 }
aoqi@0 3715 // non-shared ConstantPool gets a reference
aoqi@0 3716 pv_node = new PreviousVersionNode(cp_ref, method_refs);
aoqi@0 3717 }
aoqi@0 3718 // append new previous version.
aoqi@0 3719 _previous_versions->append(pv_node);
aoqi@0 3720 }
aoqi@0 3721
aoqi@0 3722 // Since the caller is the VMThread and we are at a safepoint, this
aoqi@0 3723 // is a good time to clear out unused references.
aoqi@0 3724
aoqi@0 3725 RC_TRACE(0x00000400, ("add: previous version length=%d",
aoqi@0 3726 _previous_versions->length()));
aoqi@0 3727
aoqi@0 3728 // Purge previous versions not executing on the stack
aoqi@0 3729 purge_previous_versions_internal(this, emcp_method_count);
aoqi@0 3730
aoqi@0 3731 int obsolete_method_count = old_methods->length() - emcp_method_count;
aoqi@0 3732
aoqi@0 3733 if (emcp_method_count != 0 && obsolete_method_count != 0 &&
aoqi@0 3734 _previous_versions->length() > 0) {
aoqi@0 3735 // We have a mix of obsolete and EMCP methods so we have to
aoqi@0 3736 // clear out any matching EMCP method entries the hard way.
aoqi@0 3737 int local_count = 0;
aoqi@0 3738 for (int i = 0; i < old_methods->length(); i++) {
aoqi@0 3739 if (!emcp_methods->at(i)) {
aoqi@0 3740 // only obsolete methods are interesting
aoqi@0 3741 Method* old_method = old_methods->at(i);
aoqi@0 3742 Symbol* m_name = old_method->name();
aoqi@0 3743 Symbol* m_signature = old_method->signature();
aoqi@0 3744
aoqi@0 3745 // we might not have added the last entry
aoqi@0 3746 for (int j = _previous_versions->length() - 1; j >= 0; j--) {
aoqi@0 3747 // check the previous versions array for non executing obsolete methods
aoqi@0 3748 PreviousVersionNode * pv_node = _previous_versions->at(j);
aoqi@0 3749
aoqi@0 3750 GrowableArray<Method*>* method_refs = pv_node->prev_EMCP_methods();
aoqi@0 3751 if (method_refs == NULL) {
aoqi@0 3752 // We have run into a PreviousVersion generation where
aoqi@0 3753 // all methods were made obsolete during that generation's
aoqi@0 3754 // RedefineClasses() operation. At the time of that
aoqi@0 3755 // operation, all EMCP methods were flushed so we don't
aoqi@0 3756 // have to go back any further.
aoqi@0 3757 //
aoqi@0 3758 // A NULL method_refs is different than an empty method_refs.
aoqi@0 3759 // We cannot infer any optimizations about older generations
aoqi@0 3760 // from an empty method_refs for the current generation.
aoqi@0 3761 break;
aoqi@0 3762 }
aoqi@0 3763
aoqi@0 3764 for (int k = method_refs->length() - 1; k >= 0; k--) {
aoqi@0 3765 Method* method = method_refs->at(k);
aoqi@0 3766
aoqi@0 3767 if (!method->is_obsolete() &&
aoqi@0 3768 method->name() == m_name &&
aoqi@0 3769 method->signature() == m_signature) {
aoqi@0 3770 // The current RedefineClasses() call has made all EMCP
aoqi@0 3771 // versions of this method obsolete so mark it as obsolete
aoqi@0 3772 // and remove the reference.
aoqi@0 3773 RC_TRACE(0x00000400,
aoqi@0 3774 ("add: %s(%s): flush obsolete method @%d in version @%d",
aoqi@0 3775 m_name->as_C_string(), m_signature->as_C_string(), k, j));
aoqi@0 3776
aoqi@0 3777 method->set_is_obsolete();
aoqi@0 3778 // Leave obsolete methods on the previous version list to
aoqi@0 3779 // clean up later.
aoqi@0 3780 break;
aoqi@0 3781 }
aoqi@0 3782 }
aoqi@0 3783
aoqi@0 3784 // The previous loop may not find a matching EMCP method, but
aoqi@0 3785 // that doesn't mean that we can optimize and not go any
aoqi@0 3786 // further back in the PreviousVersion generations. The EMCP
aoqi@0 3787 // method for this generation could have already been deleted,
aoqi@0 3788 // but there still may be an older EMCP method that has not
aoqi@0 3789 // been deleted.
aoqi@0 3790 }
aoqi@0 3791
aoqi@0 3792 if (++local_count >= obsolete_method_count) {
aoqi@0 3793 // no more obsolete methods so bail out now
aoqi@0 3794 break;
aoqi@0 3795 }
aoqi@0 3796 }
aoqi@0 3797 }
aoqi@0 3798 }
aoqi@0 3799 } // end add_previous_version()
aoqi@0 3800
aoqi@0 3801
aoqi@0 3802 // Determine if InstanceKlass has a previous version.
aoqi@0 3803 bool InstanceKlass::has_previous_version() const {
aoqi@0 3804 return (_previous_versions != NULL && _previous_versions->length() > 0);
aoqi@0 3805 } // end has_previous_version()
aoqi@0 3806
aoqi@0 3807
sspitsyn@7683 3808 InstanceKlass* InstanceKlass::get_klass_version(int version) {
sspitsyn@7683 3809 if (constants()->version() == version) {
sspitsyn@7683 3810 return this;
sspitsyn@7683 3811 }
sspitsyn@7683 3812 PreviousVersionWalker pvw(Thread::current(), (InstanceKlass*)this);
sspitsyn@7683 3813 for (PreviousVersionNode * pv_node = pvw.next_previous_version();
sspitsyn@7683 3814 pv_node != NULL; pv_node = pvw.next_previous_version()) {
sspitsyn@7683 3815 ConstantPool* prev_cp = pv_node->prev_constant_pool();
sspitsyn@7683 3816 if (prev_cp->version() == version) {
sspitsyn@7683 3817 return prev_cp->pool_holder();
sspitsyn@7683 3818 }
sspitsyn@7683 3819 }
sspitsyn@7683 3820 return NULL; // None found
sspitsyn@7683 3821 }
sspitsyn@7683 3822
sspitsyn@7683 3823
aoqi@0 3824 Method* InstanceKlass::method_with_idnum(int idnum) {
aoqi@0 3825 Method* m = NULL;
aoqi@0 3826 if (idnum < methods()->length()) {
aoqi@0 3827 m = methods()->at(idnum);
aoqi@0 3828 }
aoqi@0 3829 if (m == NULL || m->method_idnum() != idnum) {
aoqi@0 3830 for (int index = 0; index < methods()->length(); ++index) {
aoqi@0 3831 m = methods()->at(index);
aoqi@0 3832 if (m->method_idnum() == idnum) {
aoqi@0 3833 return m;
aoqi@0 3834 }
aoqi@0 3835 }
aoqi@0 3836 // None found, return null for the caller to handle.
aoqi@0 3837 return NULL;
aoqi@0 3838 }
aoqi@0 3839 return m;
aoqi@0 3840 }
aoqi@0 3841
sspitsyn@7683 3842
sspitsyn@7683 3843 Method* InstanceKlass::method_with_orig_idnum(int idnum) {
sspitsyn@7683 3844 if (idnum >= methods()->length()) {
sspitsyn@7683 3845 return NULL;
sspitsyn@7683 3846 }
sspitsyn@7683 3847 Method* m = methods()->at(idnum);
sspitsyn@7683 3848 if (m != NULL && m->orig_method_idnum() == idnum) {
sspitsyn@7683 3849 return m;
sspitsyn@7683 3850 }
sspitsyn@7683 3851 // Obsolete method idnum does not match the original idnum
sspitsyn@7683 3852 for (int index = 0; index < methods()->length(); ++index) {
sspitsyn@7683 3853 m = methods()->at(index);
sspitsyn@7683 3854 if (m->orig_method_idnum() == idnum) {
sspitsyn@7683 3855 return m;
sspitsyn@7683 3856 }
sspitsyn@7683 3857 }
sspitsyn@7683 3858 // None found, return null for the caller to handle.
sspitsyn@7683 3859 return NULL;
sspitsyn@7683 3860 }
sspitsyn@7683 3861
sspitsyn@7683 3862
sspitsyn@7683 3863 Method* InstanceKlass::method_with_orig_idnum(int idnum, int version) {
sspitsyn@7683 3864 InstanceKlass* holder = get_klass_version(version);
sspitsyn@7683 3865 if (holder == NULL) {
sspitsyn@7683 3866 return NULL; // The version of klass is gone, no method is found
sspitsyn@7683 3867 }
sspitsyn@7683 3868 Method* method = holder->method_with_orig_idnum(idnum);
sspitsyn@7683 3869 return method;
sspitsyn@7683 3870 }
sspitsyn@7683 3871
sspitsyn@7683 3872
aoqi@0 3873 jint InstanceKlass::get_cached_class_file_len() {
aoqi@0 3874 return VM_RedefineClasses::get_cached_class_file_len(_cached_class_file);
aoqi@0 3875 }
aoqi@0 3876
aoqi@0 3877 unsigned char * InstanceKlass::get_cached_class_file_bytes() {
aoqi@0 3878 return VM_RedefineClasses::get_cached_class_file_bytes(_cached_class_file);
aoqi@0 3879 }
aoqi@0 3880
aoqi@0 3881
aoqi@0 3882 // Construct a PreviousVersionNode entry for the array hung off
aoqi@0 3883 // the InstanceKlass.
aoqi@0 3884 PreviousVersionNode::PreviousVersionNode(ConstantPool* prev_constant_pool,
aoqi@0 3885 GrowableArray<Method*>* prev_EMCP_methods) {
aoqi@0 3886
aoqi@0 3887 _prev_constant_pool = prev_constant_pool;
aoqi@0 3888 _prev_EMCP_methods = prev_EMCP_methods;
aoqi@0 3889 }
aoqi@0 3890
aoqi@0 3891
aoqi@0 3892 // Destroy a PreviousVersionNode
aoqi@0 3893 PreviousVersionNode::~PreviousVersionNode() {
aoqi@0 3894 if (_prev_constant_pool != NULL) {
aoqi@0 3895 _prev_constant_pool = NULL;
aoqi@0 3896 }
aoqi@0 3897
aoqi@0 3898 if (_prev_EMCP_methods != NULL) {
aoqi@0 3899 delete _prev_EMCP_methods;
aoqi@0 3900 }
aoqi@0 3901 }
aoqi@0 3902
aoqi@0 3903 // Construct a helper for walking the previous versions array
aoqi@0 3904 PreviousVersionWalker::PreviousVersionWalker(Thread* thread, InstanceKlass *ik) {
aoqi@0 3905 _thread = thread;
aoqi@0 3906 _previous_versions = ik->previous_versions();
aoqi@0 3907 _current_index = 0;
aoqi@0 3908 _current_p = NULL;
aoqi@0 3909 _current_constant_pool_handle = constantPoolHandle(thread, ik->constants());
aoqi@0 3910 }
aoqi@0 3911
aoqi@0 3912
aoqi@0 3913 // Return the interesting information for the next previous version
aoqi@0 3914 // of the klass. Returns NULL if there are no more previous versions.
aoqi@0 3915 PreviousVersionNode* PreviousVersionWalker::next_previous_version() {
aoqi@0 3916 if (_previous_versions == NULL) {
aoqi@0 3917 // no previous versions so nothing to return
aoqi@0 3918 return NULL;
aoqi@0 3919 }
aoqi@0 3920
aoqi@0 3921 _current_p = NULL; // reset to NULL
aoqi@0 3922 _current_constant_pool_handle = NULL;
aoqi@0 3923
aoqi@0 3924 int length = _previous_versions->length();
aoqi@0 3925
aoqi@0 3926 while (_current_index < length) {
aoqi@0 3927 PreviousVersionNode * pv_node = _previous_versions->at(_current_index++);
aoqi@0 3928
aoqi@0 3929 // Save a handle to the constant pool for this previous version,
aoqi@0 3930 // which keeps all the methods from being deallocated.
aoqi@0 3931 _current_constant_pool_handle = constantPoolHandle(_thread, pv_node->prev_constant_pool());
aoqi@0 3932 _current_p = pv_node;
aoqi@0 3933 return pv_node;
aoqi@0 3934 }
aoqi@0 3935
aoqi@0 3936 return NULL;
aoqi@0 3937 } // end next_previous_version()

mercurial