src/share/vm/oops/instanceKlass.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7812
3c8b53552a43
parent 7535
7ae4e26cb1e0
child 8604
04d83ba48607
permissions
-rw-r--r--

merge

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

mercurial