src/share/vm/oops/instanceKlass.cpp

Sat, 10 Sep 2011 17:29:02 -0700

author
never
date
Sat, 10 Sep 2011 17:29:02 -0700
changeset 3137
e6b1331a51d2
parent 3050
fdb992d83a87
child 3138
f6f3bb0ee072
permissions
-rw-r--r--

7086585: make Java field injection more flexible
Reviewed-by: jrose, twisti, kvn, coleenp

duke@435 1 /*
stefank@2534 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/javaClasses.hpp"
stefank@2314 27 #include "classfile/systemDictionary.hpp"
stefank@2314 28 #include "classfile/verifier.hpp"
stefank@2314 29 #include "classfile/vmSymbols.hpp"
stefank@2314 30 #include "compiler/compileBroker.hpp"
stefank@2314 31 #include "gc_implementation/shared/markSweep.inline.hpp"
stefank@2314 32 #include "gc_interface/collectedHeap.inline.hpp"
stefank@2314 33 #include "interpreter/oopMapCache.hpp"
stefank@2314 34 #include "interpreter/rewriter.hpp"
stefank@2314 35 #include "jvmtifiles/jvmti.h"
stefank@2314 36 #include "memory/genOopClosures.inline.hpp"
stefank@2314 37 #include "memory/oopFactory.hpp"
stefank@2314 38 #include "memory/permGen.hpp"
never@3137 39 #include "oops/fieldStreams.hpp"
stefank@2314 40 #include "oops/instanceKlass.hpp"
never@2658 41 #include "oops/instanceMirrorKlass.hpp"
stefank@2314 42 #include "oops/instanceOop.hpp"
stefank@2314 43 #include "oops/methodOop.hpp"
stefank@2314 44 #include "oops/objArrayKlassKlass.hpp"
stefank@2314 45 #include "oops/oop.inline.hpp"
coleenp@2497 46 #include "oops/symbol.hpp"
stefank@2314 47 #include "prims/jvmtiExport.hpp"
stefank@2314 48 #include "prims/jvmtiRedefineClassesTrace.hpp"
stefank@2314 49 #include "runtime/fieldDescriptor.hpp"
stefank@2314 50 #include "runtime/handles.inline.hpp"
stefank@2314 51 #include "runtime/javaCalls.hpp"
stefank@2314 52 #include "runtime/mutexLocker.hpp"
stefank@2314 53 #include "services/threadService.hpp"
stefank@2314 54 #include "utilities/dtrace.hpp"
stefank@2314 55 #ifdef TARGET_OS_FAMILY_linux
stefank@2314 56 # include "thread_linux.inline.hpp"
stefank@2314 57 #endif
stefank@2314 58 #ifdef TARGET_OS_FAMILY_solaris
stefank@2314 59 # include "thread_solaris.inline.hpp"
stefank@2314 60 #endif
stefank@2314 61 #ifdef TARGET_OS_FAMILY_windows
stefank@2314 62 # include "thread_windows.inline.hpp"
stefank@2314 63 #endif
stefank@2314 64 #ifndef SERIALGC
stefank@2314 65 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
stefank@2314 66 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
stefank@2314 67 #include "gc_implementation/g1/g1RemSet.inline.hpp"
stefank@2314 68 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
stefank@2314 69 #include "gc_implementation/parNew/parOopClosures.inline.hpp"
stefank@2314 70 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
stefank@2314 71 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
stefank@2314 72 #include "oops/oop.pcgc.inline.hpp"
stefank@2314 73 #endif
stefank@2314 74 #ifdef COMPILER1
stefank@2314 75 #include "c1/c1_Compiler.hpp"
stefank@2314 76 #endif
duke@435 77
fparain@1759 78 #ifdef DTRACE_ENABLED
fparain@1759 79
fparain@1759 80 HS_DTRACE_PROBE_DECL4(hotspot, class__initialization__required,
fparain@1759 81 char*, intptr_t, oop, intptr_t);
fparain@1759 82 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__recursive,
fparain@1759 83 char*, intptr_t, oop, intptr_t, int);
fparain@1759 84 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__concurrent,
fparain@1759 85 char*, intptr_t, oop, intptr_t, int);
fparain@1759 86 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__erroneous,
fparain@1759 87 char*, intptr_t, oop, intptr_t, int);
fparain@1759 88 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__super__failed,
fparain@1759 89 char*, intptr_t, oop, intptr_t, int);
fparain@1759 90 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__clinit,
fparain@1759 91 char*, intptr_t, oop, intptr_t, int);
fparain@1759 92 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__error,
fparain@1759 93 char*, intptr_t, oop, intptr_t, int);
fparain@1759 94 HS_DTRACE_PROBE_DECL5(hotspot, class__initialization__end,
fparain@1759 95 char*, intptr_t, oop, intptr_t, int);
fparain@1759 96
fparain@1759 97 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) \
fparain@1759 98 { \
fparain@1759 99 char* data = NULL; \
fparain@1759 100 int len = 0; \
coleenp@2497 101 Symbol* name = (clss)->name(); \
fparain@1759 102 if (name != NULL) { \
fparain@1759 103 data = (char*)name->bytes(); \
fparain@1759 104 len = name->utf8_length(); \
fparain@1759 105 } \
fparain@1759 106 HS_DTRACE_PROBE4(hotspot, class__initialization__##type, \
fparain@1759 107 data, len, (clss)->class_loader(), thread_type); \
fparain@1759 108 }
fparain@1759 109
fparain@1759 110 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) \
fparain@1759 111 { \
fparain@1759 112 char* data = NULL; \
fparain@1759 113 int len = 0; \
coleenp@2497 114 Symbol* name = (clss)->name(); \
fparain@1759 115 if (name != NULL) { \
fparain@1759 116 data = (char*)name->bytes(); \
fparain@1759 117 len = name->utf8_length(); \
fparain@1759 118 } \
fparain@1759 119 HS_DTRACE_PROBE5(hotspot, class__initialization__##type, \
fparain@1759 120 data, len, (clss)->class_loader(), thread_type, wait); \
fparain@1759 121 }
fparain@1759 122
fparain@1759 123 #else // ndef DTRACE_ENABLED
fparain@1759 124
fparain@1759 125 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type)
fparain@1759 126 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait)
fparain@1759 127
fparain@1759 128 #endif // ndef DTRACE_ENABLED
fparain@1759 129
duke@435 130 bool instanceKlass::should_be_initialized() const {
duke@435 131 return !is_initialized();
duke@435 132 }
duke@435 133
duke@435 134 klassVtable* instanceKlass::vtable() const {
duke@435 135 return new klassVtable(as_klassOop(), start_of_vtable(), vtable_length() / vtableEntry::size());
duke@435 136 }
duke@435 137
duke@435 138 klassItable* instanceKlass::itable() const {
duke@435 139 return new klassItable(as_klassOop());
duke@435 140 }
duke@435 141
duke@435 142 void instanceKlass::eager_initialize(Thread *thread) {
duke@435 143 if (!EagerInitialization) return;
duke@435 144
duke@435 145 if (this->is_not_initialized()) {
duke@435 146 // abort if the the class has a class initializer
duke@435 147 if (this->class_initializer() != NULL) return;
duke@435 148
duke@435 149 // abort if it is java.lang.Object (initialization is handled in genesis)
duke@435 150 klassOop super = this->super();
duke@435 151 if (super == NULL) return;
duke@435 152
duke@435 153 // abort if the super class should be initialized
duke@435 154 if (!instanceKlass::cast(super)->is_initialized()) return;
duke@435 155
duke@435 156 // call body to expose the this pointer
duke@435 157 instanceKlassHandle this_oop(thread, this->as_klassOop());
duke@435 158 eager_initialize_impl(this_oop);
duke@435 159 }
duke@435 160 }
duke@435 161
duke@435 162
duke@435 163 void instanceKlass::eager_initialize_impl(instanceKlassHandle this_oop) {
duke@435 164 EXCEPTION_MARK;
duke@435 165 ObjectLocker ol(this_oop, THREAD);
duke@435 166
duke@435 167 // abort if someone beat us to the initialization
duke@435 168 if (!this_oop->is_not_initialized()) return; // note: not equivalent to is_initialized()
duke@435 169
duke@435 170 ClassState old_state = this_oop->_init_state;
duke@435 171 link_class_impl(this_oop, true, THREAD);
duke@435 172 if (HAS_PENDING_EXCEPTION) {
duke@435 173 CLEAR_PENDING_EXCEPTION;
duke@435 174 // Abort if linking the class throws an exception.
duke@435 175
duke@435 176 // Use a test to avoid redundantly resetting the state if there's
duke@435 177 // no change. Set_init_state() asserts that state changes make
duke@435 178 // progress, whereas here we might just be spinning in place.
duke@435 179 if( old_state != this_oop->_init_state )
duke@435 180 this_oop->set_init_state (old_state);
duke@435 181 } else {
duke@435 182 // linking successfull, mark class as initialized
duke@435 183 this_oop->set_init_state (fully_initialized);
duke@435 184 // trace
duke@435 185 if (TraceClassInitialization) {
duke@435 186 ResourceMark rm(THREAD);
duke@435 187 tty->print_cr("[Initialized %s without side effects]", this_oop->external_name());
duke@435 188 }
duke@435 189 }
duke@435 190 }
duke@435 191
duke@435 192
duke@435 193 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
duke@435 194 // process. The step comments refers to the procedure described in that section.
duke@435 195 // Note: implementation moved to static method to expose the this pointer.
duke@435 196 void instanceKlass::initialize(TRAPS) {
duke@435 197 if (this->should_be_initialized()) {
duke@435 198 HandleMark hm(THREAD);
duke@435 199 instanceKlassHandle this_oop(THREAD, this->as_klassOop());
duke@435 200 initialize_impl(this_oop, CHECK);
duke@435 201 // Note: at this point the class may be initialized
duke@435 202 // OR it may be in the state of being initialized
duke@435 203 // in case of recursive initialization!
duke@435 204 } else {
duke@435 205 assert(is_initialized(), "sanity check");
duke@435 206 }
duke@435 207 }
duke@435 208
duke@435 209
duke@435 210 bool instanceKlass::verify_code(
duke@435 211 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) {
duke@435 212 // 1) Verify the bytecodes
duke@435 213 Verifier::Mode mode =
duke@435 214 throw_verifyerror ? Verifier::ThrowException : Verifier::NoException;
acorn@1408 215 return Verifier::verify(this_oop, mode, this_oop->should_verify_class(), CHECK_false);
duke@435 216 }
duke@435 217
duke@435 218
duke@435 219 // Used exclusively by the shared spaces dump mechanism to prevent
duke@435 220 // classes mapped into the shared regions in new VMs from appearing linked.
duke@435 221
duke@435 222 void instanceKlass::unlink_class() {
duke@435 223 assert(is_linked(), "must be linked");
duke@435 224 _init_state = loaded;
duke@435 225 }
duke@435 226
duke@435 227 void instanceKlass::link_class(TRAPS) {
duke@435 228 assert(is_loaded(), "must be loaded");
duke@435 229 if (!is_linked()) {
duke@435 230 instanceKlassHandle this_oop(THREAD, this->as_klassOop());
duke@435 231 link_class_impl(this_oop, true, CHECK);
duke@435 232 }
duke@435 233 }
duke@435 234
duke@435 235 // Called to verify that a class can link during initialization, without
duke@435 236 // throwing a VerifyError.
duke@435 237 bool instanceKlass::link_class_or_fail(TRAPS) {
duke@435 238 assert(is_loaded(), "must be loaded");
duke@435 239 if (!is_linked()) {
duke@435 240 instanceKlassHandle this_oop(THREAD, this->as_klassOop());
duke@435 241 link_class_impl(this_oop, false, CHECK_false);
duke@435 242 }
duke@435 243 return is_linked();
duke@435 244 }
duke@435 245
duke@435 246 bool instanceKlass::link_class_impl(
duke@435 247 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) {
duke@435 248 // check for error state
duke@435 249 if (this_oop->is_in_error_state()) {
duke@435 250 ResourceMark rm(THREAD);
duke@435 251 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(),
duke@435 252 this_oop->external_name(), false);
duke@435 253 }
duke@435 254 // return if already verified
duke@435 255 if (this_oop->is_linked()) {
duke@435 256 return true;
duke@435 257 }
duke@435 258
duke@435 259 // Timing
duke@435 260 // timer handles recursion
duke@435 261 assert(THREAD->is_Java_thread(), "non-JavaThread in link_class_impl");
duke@435 262 JavaThread* jt = (JavaThread*)THREAD;
duke@435 263
duke@435 264 // link super class before linking this class
duke@435 265 instanceKlassHandle super(THREAD, this_oop->super());
duke@435 266 if (super.not_null()) {
duke@435 267 if (super->is_interface()) { // check if super class is an interface
duke@435 268 ResourceMark rm(THREAD);
duke@435 269 Exceptions::fthrow(
duke@435 270 THREAD_AND_LOCATION,
coleenp@2497 271 vmSymbols::java_lang_IncompatibleClassChangeError(),
duke@435 272 "class %s has interface %s as super class",
duke@435 273 this_oop->external_name(),
duke@435 274 super->external_name()
duke@435 275 );
duke@435 276 return false;
duke@435 277 }
duke@435 278
duke@435 279 link_class_impl(super, throw_verifyerror, CHECK_false);
duke@435 280 }
duke@435 281
duke@435 282 // link all interfaces implemented by this class before linking this class
duke@435 283 objArrayHandle interfaces (THREAD, this_oop->local_interfaces());
duke@435 284 int num_interfaces = interfaces->length();
duke@435 285 for (int index = 0; index < num_interfaces; index++) {
duke@435 286 HandleMark hm(THREAD);
duke@435 287 instanceKlassHandle ih(THREAD, klassOop(interfaces->obj_at(index)));
duke@435 288 link_class_impl(ih, throw_verifyerror, CHECK_false);
duke@435 289 }
duke@435 290
duke@435 291 // in case the class is linked in the process of linking its superclasses
duke@435 292 if (this_oop->is_linked()) {
duke@435 293 return true;
duke@435 294 }
duke@435 295
mchung@1310 296 // trace only the link time for this klass that includes
mchung@1310 297 // the verification time
mchung@1310 298 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
mchung@1310 299 ClassLoader::perf_class_link_selftime(),
mchung@1310 300 ClassLoader::perf_classes_linked(),
mchung@1310 301 jt->get_thread_stat()->perf_recursion_counts_addr(),
mchung@1310 302 jt->get_thread_stat()->perf_timers_addr(),
mchung@1310 303 PerfClassTraceTime::CLASS_LINK);
mchung@1310 304
duke@435 305 // verification & rewriting
duke@435 306 {
duke@435 307 ObjectLocker ol(this_oop, THREAD);
duke@435 308 // rewritten will have been set if loader constraint error found
duke@435 309 // on an earlier link attempt
duke@435 310 // don't verify or rewrite if already rewritten
duke@435 311 if (!this_oop->is_linked()) {
duke@435 312 if (!this_oop->is_rewritten()) {
duke@435 313 {
duke@435 314 // Timer includes any side effects of class verification (resolution,
duke@435 315 // etc), but not recursive entry into verify_code().
mchung@1310 316 PerfClassTraceTime timer(ClassLoader::perf_class_verify_time(),
mchung@1310 317 ClassLoader::perf_class_verify_selftime(),
mchung@1310 318 ClassLoader::perf_classes_verified(),
mchung@1310 319 jt->get_thread_stat()->perf_recursion_counts_addr(),
mchung@1310 320 jt->get_thread_stat()->perf_timers_addr(),
mchung@1310 321 PerfClassTraceTime::CLASS_VERIFY);
duke@435 322 bool verify_ok = verify_code(this_oop, throw_verifyerror, THREAD);
duke@435 323 if (!verify_ok) {
duke@435 324 return false;
duke@435 325 }
duke@435 326 }
duke@435 327
duke@435 328 // Just in case a side-effect of verify linked this class already
duke@435 329 // (which can sometimes happen since the verifier loads classes
duke@435 330 // using custom class loaders, which are free to initialize things)
duke@435 331 if (this_oop->is_linked()) {
duke@435 332 return true;
duke@435 333 }
duke@435 334
duke@435 335 // also sets rewritten
duke@435 336 this_oop->rewrite_class(CHECK_false);
duke@435 337 }
duke@435 338
coleenp@2945 339 // relocate jsrs and link methods after they are all rewritten
coleenp@2945 340 this_oop->relocate_and_link_methods(CHECK_false);
coleenp@2945 341
duke@435 342 // Initialize the vtable and interface table after
duke@435 343 // methods have been rewritten since rewrite may
duke@435 344 // fabricate new methodOops.
duke@435 345 // also does loader constraint checking
duke@435 346 if (!this_oop()->is_shared()) {
duke@435 347 ResourceMark rm(THREAD);
duke@435 348 this_oop->vtable()->initialize_vtable(true, CHECK_false);
duke@435 349 this_oop->itable()->initialize_itable(true, CHECK_false);
duke@435 350 }
duke@435 351 #ifdef ASSERT
duke@435 352 else {
duke@435 353 ResourceMark rm(THREAD);
duke@435 354 this_oop->vtable()->verify(tty, true);
duke@435 355 // In case itable verification is ever added.
duke@435 356 // this_oop->itable()->verify(tty, true);
duke@435 357 }
duke@435 358 #endif
duke@435 359 this_oop->set_init_state(linked);
duke@435 360 if (JvmtiExport::should_post_class_prepare()) {
duke@435 361 Thread *thread = THREAD;
duke@435 362 assert(thread->is_Java_thread(), "thread->is_Java_thread()");
duke@435 363 JvmtiExport::post_class_prepare((JavaThread *) thread, this_oop());
duke@435 364 }
duke@435 365 }
duke@435 366 }
duke@435 367 return true;
duke@435 368 }
duke@435 369
duke@435 370
duke@435 371 // Rewrite the byte codes of all of the methods of a class.
duke@435 372 // The rewriter must be called exactly once. Rewriting must happen after
duke@435 373 // verification but before the first method of the class is executed.
duke@435 374 void instanceKlass::rewrite_class(TRAPS) {
duke@435 375 assert(is_loaded(), "must be loaded");
duke@435 376 instanceKlassHandle this_oop(THREAD, this->as_klassOop());
duke@435 377 if (this_oop->is_rewritten()) {
duke@435 378 assert(this_oop()->is_shared(), "rewriting an unshared class?");
duke@435 379 return;
duke@435 380 }
coleenp@2945 381 Rewriter::rewrite(this_oop, CHECK);
duke@435 382 this_oop->set_rewritten();
duke@435 383 }
duke@435 384
coleenp@2945 385 // Now relocate and link method entry points after class is rewritten.
coleenp@2945 386 // This is outside is_rewritten flag. In case of an exception, it can be
coleenp@2945 387 // executed more than once.
coleenp@2945 388 void instanceKlass::relocate_and_link_methods(TRAPS) {
coleenp@2945 389 assert(is_loaded(), "must be loaded");
coleenp@2945 390 instanceKlassHandle this_oop(THREAD, this->as_klassOop());
coleenp@2945 391 Rewriter::relocate_and_link(this_oop, CHECK);
coleenp@2945 392 }
coleenp@2945 393
duke@435 394
duke@435 395 void instanceKlass::initialize_impl(instanceKlassHandle this_oop, TRAPS) {
duke@435 396 // Make sure klass is linked (verified) before initialization
duke@435 397 // A class could already be verified, since it has been reflected upon.
duke@435 398 this_oop->link_class(CHECK);
duke@435 399
fparain@1759 400 DTRACE_CLASSINIT_PROBE(required, instanceKlass::cast(this_oop()), -1);
fparain@1759 401
fparain@1759 402 bool wait = false;
fparain@1759 403
duke@435 404 // refer to the JVM book page 47 for description of steps
duke@435 405 // Step 1
duke@435 406 { ObjectLocker ol(this_oop, THREAD);
duke@435 407
duke@435 408 Thread *self = THREAD; // it's passed the current thread
duke@435 409
duke@435 410 // Step 2
duke@435 411 // If we were to use wait() instead of waitInterruptibly() then
duke@435 412 // we might end up throwing IE from link/symbol resolution sites
duke@435 413 // that aren't expected to throw. This would wreak havoc. See 6320309.
duke@435 414 while(this_oop->is_being_initialized() && !this_oop->is_reentrant_initialization(self)) {
fparain@1759 415 wait = true;
duke@435 416 ol.waitUninterruptibly(CHECK);
duke@435 417 }
duke@435 418
duke@435 419 // Step 3
fparain@1759 420 if (this_oop->is_being_initialized() && this_oop->is_reentrant_initialization(self)) {
fparain@1759 421 DTRACE_CLASSINIT_PROBE_WAIT(recursive, instanceKlass::cast(this_oop()), -1,wait);
duke@435 422 return;
fparain@1759 423 }
duke@435 424
duke@435 425 // Step 4
fparain@1759 426 if (this_oop->is_initialized()) {
fparain@1759 427 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, instanceKlass::cast(this_oop()), -1,wait);
duke@435 428 return;
fparain@1759 429 }
duke@435 430
duke@435 431 // Step 5
duke@435 432 if (this_oop->is_in_error_state()) {
fparain@1759 433 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, instanceKlass::cast(this_oop()), -1,wait);
duke@435 434 ResourceMark rm(THREAD);
duke@435 435 const char* desc = "Could not initialize class ";
duke@435 436 const char* className = this_oop->external_name();
duke@435 437 size_t msglen = strlen(desc) + strlen(className) + 1;
kamg@2106 438 char* message = NEW_RESOURCE_ARRAY(char, msglen);
duke@435 439 if (NULL == message) {
duke@435 440 // Out of memory: can't create detailed error message
duke@435 441 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className);
duke@435 442 } else {
duke@435 443 jio_snprintf(message, msglen, "%s%s", desc, className);
duke@435 444 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message);
duke@435 445 }
duke@435 446 }
duke@435 447
duke@435 448 // Step 6
duke@435 449 this_oop->set_init_state(being_initialized);
duke@435 450 this_oop->set_init_thread(self);
duke@435 451 }
duke@435 452
duke@435 453 // Step 7
duke@435 454 klassOop super_klass = this_oop->super();
duke@435 455 if (super_klass != NULL && !this_oop->is_interface() && Klass::cast(super_klass)->should_be_initialized()) {
duke@435 456 Klass::cast(super_klass)->initialize(THREAD);
duke@435 457
duke@435 458 if (HAS_PENDING_EXCEPTION) {
duke@435 459 Handle e(THREAD, PENDING_EXCEPTION);
duke@435 460 CLEAR_PENDING_EXCEPTION;
duke@435 461 {
duke@435 462 EXCEPTION_MARK;
duke@435 463 this_oop->set_initialization_state_and_notify(initialization_error, THREAD); // Locks object, set state, and notify all waiting threads
duke@435 464 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, superclass initialization error is thrown below
duke@435 465 }
fparain@1759 466 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, instanceKlass::cast(this_oop()), -1,wait);
duke@435 467 THROW_OOP(e());
duke@435 468 }
duke@435 469 }
duke@435 470
duke@435 471 // Step 8
duke@435 472 {
duke@435 473 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
duke@435 474 JavaThread* jt = (JavaThread*)THREAD;
fparain@1759 475 DTRACE_CLASSINIT_PROBE_WAIT(clinit, instanceKlass::cast(this_oop()), -1,wait);
duke@435 476 // Timer includes any side effects of class initialization (resolution,
duke@435 477 // etc), but not recursive entry into call_class_initializer().
mchung@1310 478 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
mchung@1310 479 ClassLoader::perf_class_init_selftime(),
mchung@1310 480 ClassLoader::perf_classes_inited(),
mchung@1310 481 jt->get_thread_stat()->perf_recursion_counts_addr(),
mchung@1310 482 jt->get_thread_stat()->perf_timers_addr(),
mchung@1310 483 PerfClassTraceTime::CLASS_CLINIT);
duke@435 484 this_oop->call_class_initializer(THREAD);
duke@435 485 }
duke@435 486
duke@435 487 // Step 9
duke@435 488 if (!HAS_PENDING_EXCEPTION) {
duke@435 489 this_oop->set_initialization_state_and_notify(fully_initialized, CHECK);
duke@435 490 { ResourceMark rm(THREAD);
duke@435 491 debug_only(this_oop->vtable()->verify(tty, true);)
duke@435 492 }
duke@435 493 }
duke@435 494 else {
duke@435 495 // Step 10 and 11
duke@435 496 Handle e(THREAD, PENDING_EXCEPTION);
duke@435 497 CLEAR_PENDING_EXCEPTION;
duke@435 498 {
duke@435 499 EXCEPTION_MARK;
duke@435 500 this_oop->set_initialization_state_and_notify(initialization_error, THREAD);
duke@435 501 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
duke@435 502 }
fparain@1759 503 DTRACE_CLASSINIT_PROBE_WAIT(error, instanceKlass::cast(this_oop()), -1,wait);
never@1577 504 if (e->is_a(SystemDictionary::Error_klass())) {
duke@435 505 THROW_OOP(e());
duke@435 506 } else {
duke@435 507 JavaCallArguments args(e);
coleenp@2497 508 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
coleenp@2497 509 vmSymbols::throwable_void_signature(),
duke@435 510 &args);
duke@435 511 }
duke@435 512 }
fparain@1759 513 DTRACE_CLASSINIT_PROBE_WAIT(end, instanceKlass::cast(this_oop()), -1,wait);
duke@435 514 }
duke@435 515
duke@435 516
duke@435 517 // Note: implementation moved to static method to expose the this pointer.
duke@435 518 void instanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
duke@435 519 instanceKlassHandle kh(THREAD, this->as_klassOop());
duke@435 520 set_initialization_state_and_notify_impl(kh, state, CHECK);
duke@435 521 }
duke@435 522
duke@435 523 void instanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_oop, ClassState state, TRAPS) {
duke@435 524 ObjectLocker ol(this_oop, THREAD);
duke@435 525 this_oop->set_init_state(state);
duke@435 526 ol.notify_all(CHECK);
duke@435 527 }
duke@435 528
duke@435 529 void instanceKlass::add_implementor(klassOop k) {
duke@435 530 assert(Compile_lock->owned_by_self(), "");
duke@435 531 // Filter out my subinterfaces.
duke@435 532 // (Note: Interfaces are never on the subklass list.)
duke@435 533 if (instanceKlass::cast(k)->is_interface()) return;
duke@435 534
duke@435 535 // Filter out subclasses whose supers already implement me.
duke@435 536 // (Note: CHA must walk subclasses of direct implementors
duke@435 537 // in order to locate indirect implementors.)
duke@435 538 klassOop sk = instanceKlass::cast(k)->super();
duke@435 539 if (sk != NULL && instanceKlass::cast(sk)->implements_interface(as_klassOop()))
duke@435 540 // We only need to check one immediate superclass, since the
duke@435 541 // implements_interface query looks at transitive_interfaces.
duke@435 542 // Any supers of the super have the same (or fewer) transitive_interfaces.
duke@435 543 return;
duke@435 544
duke@435 545 // Update number of implementors
duke@435 546 int i = _nof_implementors++;
duke@435 547
duke@435 548 // Record this implementor, if there are not too many already
duke@435 549 if (i < implementors_limit) {
duke@435 550 assert(_implementors[i] == NULL, "should be exactly one implementor");
duke@435 551 oop_store_without_check((oop*)&_implementors[i], k);
duke@435 552 } else if (i == implementors_limit) {
duke@435 553 // clear out the list on first overflow
duke@435 554 for (int i2 = 0; i2 < implementors_limit; i2++)
duke@435 555 oop_store_without_check((oop*)&_implementors[i2], NULL);
duke@435 556 }
duke@435 557
duke@435 558 // The implementor also implements the transitive_interfaces
duke@435 559 for (int index = 0; index < local_interfaces()->length(); index++) {
duke@435 560 instanceKlass::cast(klassOop(local_interfaces()->obj_at(index)))->add_implementor(k);
duke@435 561 }
duke@435 562 }
duke@435 563
duke@435 564 void instanceKlass::init_implementor() {
duke@435 565 for (int i = 0; i < implementors_limit; i++)
duke@435 566 oop_store_without_check((oop*)&_implementors[i], NULL);
duke@435 567 _nof_implementors = 0;
duke@435 568 }
duke@435 569
duke@435 570
duke@435 571 void instanceKlass::process_interfaces(Thread *thread) {
duke@435 572 // link this class into the implementors list of every interface it implements
duke@435 573 KlassHandle this_as_oop (thread, this->as_klassOop());
duke@435 574 for (int i = local_interfaces()->length() - 1; i >= 0; i--) {
duke@435 575 assert(local_interfaces()->obj_at(i)->is_klass(), "must be a klass");
duke@435 576 instanceKlass* interf = instanceKlass::cast(klassOop(local_interfaces()->obj_at(i)));
duke@435 577 assert(interf->is_interface(), "expected interface");
duke@435 578 interf->add_implementor(this_as_oop());
duke@435 579 }
duke@435 580 }
duke@435 581
duke@435 582 bool instanceKlass::can_be_primary_super_slow() const {
duke@435 583 if (is_interface())
duke@435 584 return false;
duke@435 585 else
duke@435 586 return Klass::can_be_primary_super_slow();
duke@435 587 }
duke@435 588
duke@435 589 objArrayOop instanceKlass::compute_secondary_supers(int num_extra_slots, TRAPS) {
duke@435 590 // The secondaries are the implemented interfaces.
duke@435 591 instanceKlass* ik = instanceKlass::cast(as_klassOop());
duke@435 592 objArrayHandle interfaces (THREAD, ik->transitive_interfaces());
duke@435 593 int num_secondaries = num_extra_slots + interfaces->length();
duke@435 594 if (num_secondaries == 0) {
duke@435 595 return Universe::the_empty_system_obj_array();
duke@435 596 } else if (num_extra_slots == 0) {
duke@435 597 return interfaces();
duke@435 598 } else {
duke@435 599 // a mix of both
duke@435 600 objArrayOop secondaries = oopFactory::new_system_objArray(num_secondaries, CHECK_NULL);
duke@435 601 for (int i = 0; i < interfaces->length(); i++) {
duke@435 602 secondaries->obj_at_put(num_extra_slots+i, interfaces->obj_at(i));
duke@435 603 }
duke@435 604 return secondaries;
duke@435 605 }
duke@435 606 }
duke@435 607
duke@435 608 bool instanceKlass::compute_is_subtype_of(klassOop k) {
duke@435 609 if (Klass::cast(k)->is_interface()) {
duke@435 610 return implements_interface(k);
duke@435 611 } else {
duke@435 612 return Klass::compute_is_subtype_of(k);
duke@435 613 }
duke@435 614 }
duke@435 615
duke@435 616 bool instanceKlass::implements_interface(klassOop k) const {
duke@435 617 if (as_klassOop() == k) return true;
duke@435 618 assert(Klass::cast(k)->is_interface(), "should be an interface class");
duke@435 619 for (int i = 0; i < transitive_interfaces()->length(); i++) {
duke@435 620 if (transitive_interfaces()->obj_at(i) == k) {
duke@435 621 return true;
duke@435 622 }
duke@435 623 }
duke@435 624 return false;
duke@435 625 }
duke@435 626
duke@435 627 objArrayOop instanceKlass::allocate_objArray(int n, int length, TRAPS) {
duke@435 628 if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
duke@435 629 if (length > arrayOopDesc::max_array_length(T_OBJECT)) {
martin@1311 630 report_java_out_of_memory("Requested array size exceeds VM limit");
duke@435 631 THROW_OOP_0(Universe::out_of_memory_error_array_size());
duke@435 632 }
duke@435 633 int size = objArrayOopDesc::object_size(length);
duke@435 634 klassOop ak = array_klass(n, CHECK_NULL);
duke@435 635 KlassHandle h_ak (THREAD, ak);
duke@435 636 objArrayOop o =
duke@435 637 (objArrayOop)CollectedHeap::array_allocate(h_ak, size, length, CHECK_NULL);
duke@435 638 return o;
duke@435 639 }
duke@435 640
duke@435 641 instanceOop instanceKlass::register_finalizer(instanceOop i, TRAPS) {
duke@435 642 if (TraceFinalizerRegistration) {
duke@435 643 tty->print("Registered ");
duke@435 644 i->print_value_on(tty);
duke@435 645 tty->print_cr(" (" INTPTR_FORMAT ") as finalizable", (address)i);
duke@435 646 }
duke@435 647 instanceHandle h_i(THREAD, i);
duke@435 648 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
duke@435 649 JavaValue result(T_VOID);
duke@435 650 JavaCallArguments args(h_i);
duke@435 651 methodHandle mh (THREAD, Universe::finalizer_register_method());
duke@435 652 JavaCalls::call(&result, mh, &args, CHECK_NULL);
duke@435 653 return h_i();
duke@435 654 }
duke@435 655
duke@435 656 instanceOop instanceKlass::allocate_instance(TRAPS) {
never@2658 657 assert(!oop_is_instanceMirror(), "wrong allocation path");
duke@435 658 bool has_finalizer_flag = has_finalizer(); // Query before possible GC
duke@435 659 int size = size_helper(); // Query before forming handle.
duke@435 660
duke@435 661 KlassHandle h_k(THREAD, as_klassOop());
duke@435 662
duke@435 663 instanceOop i;
duke@435 664
duke@435 665 i = (instanceOop)CollectedHeap::obj_allocate(h_k, size, CHECK_NULL);
duke@435 666 if (has_finalizer_flag && !RegisterFinalizersAtInit) {
duke@435 667 i = register_finalizer(i, CHECK_NULL);
duke@435 668 }
duke@435 669 return i;
duke@435 670 }
duke@435 671
duke@435 672 instanceOop instanceKlass::allocate_permanent_instance(TRAPS) {
duke@435 673 // Finalizer registration occurs in the Object.<init> constructor
duke@435 674 // and constructors normally aren't run when allocating perm
duke@435 675 // instances so simply disallow finalizable perm objects. This can
duke@435 676 // be relaxed if a need for it is found.
duke@435 677 assert(!has_finalizer(), "perm objects not allowed to have finalizers");
never@2658 678 assert(!oop_is_instanceMirror(), "wrong allocation path");
duke@435 679 int size = size_helper(); // Query before forming handle.
duke@435 680 KlassHandle h_k(THREAD, as_klassOop());
duke@435 681 instanceOop i = (instanceOop)
duke@435 682 CollectedHeap::permanent_obj_allocate(h_k, size, CHECK_NULL);
duke@435 683 return i;
duke@435 684 }
duke@435 685
duke@435 686 void instanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
duke@435 687 if (is_interface() || is_abstract()) {
duke@435 688 ResourceMark rm(THREAD);
duke@435 689 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
duke@435 690 : vmSymbols::java_lang_InstantiationException(), external_name());
duke@435 691 }
never@1577 692 if (as_klassOop() == SystemDictionary::Class_klass()) {
duke@435 693 ResourceMark rm(THREAD);
duke@435 694 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
duke@435 695 : vmSymbols::java_lang_IllegalAccessException(), external_name());
duke@435 696 }
duke@435 697 }
duke@435 698
duke@435 699 klassOop instanceKlass::array_klass_impl(bool or_null, int n, TRAPS) {
duke@435 700 instanceKlassHandle this_oop(THREAD, as_klassOop());
duke@435 701 return array_klass_impl(this_oop, or_null, n, THREAD);
duke@435 702 }
duke@435 703
duke@435 704 klassOop instanceKlass::array_klass_impl(instanceKlassHandle this_oop, bool or_null, int n, TRAPS) {
duke@435 705 if (this_oop->array_klasses() == NULL) {
duke@435 706 if (or_null) return NULL;
duke@435 707
duke@435 708 ResourceMark rm;
duke@435 709 JavaThread *jt = (JavaThread *)THREAD;
duke@435 710 {
duke@435 711 // Atomic creation of array_klasses
duke@435 712 MutexLocker mc(Compile_lock, THREAD); // for vtables
duke@435 713 MutexLocker ma(MultiArray_lock, THREAD);
duke@435 714
duke@435 715 // Check if update has already taken place
duke@435 716 if (this_oop->array_klasses() == NULL) {
duke@435 717 objArrayKlassKlass* oakk =
duke@435 718 (objArrayKlassKlass*)Universe::objArrayKlassKlassObj()->klass_part();
duke@435 719
duke@435 720 klassOop k = oakk->allocate_objArray_klass(1, this_oop, CHECK_NULL);
duke@435 721 this_oop->set_array_klasses(k);
duke@435 722 }
duke@435 723 }
duke@435 724 }
duke@435 725 // _this will always be set at this point
duke@435 726 objArrayKlass* oak = (objArrayKlass*)this_oop->array_klasses()->klass_part();
duke@435 727 if (or_null) {
duke@435 728 return oak->array_klass_or_null(n);
duke@435 729 }
duke@435 730 return oak->array_klass(n, CHECK_NULL);
duke@435 731 }
duke@435 732
duke@435 733 klassOop instanceKlass::array_klass_impl(bool or_null, TRAPS) {
duke@435 734 return array_klass_impl(or_null, 1, THREAD);
duke@435 735 }
duke@435 736
duke@435 737 void instanceKlass::call_class_initializer(TRAPS) {
duke@435 738 instanceKlassHandle ik (THREAD, as_klassOop());
duke@435 739 call_class_initializer_impl(ik, THREAD);
duke@435 740 }
duke@435 741
duke@435 742 static int call_class_initializer_impl_counter = 0; // for debugging
duke@435 743
duke@435 744 methodOop instanceKlass::class_initializer() {
kamg@2616 745 methodOop clinit = find_method(
kamg@2616 746 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
kamg@2616 747 if (clinit != NULL && clinit->has_valid_initializer_flags()) {
kamg@2616 748 return clinit;
kamg@2616 749 }
kamg@2616 750 return NULL;
duke@435 751 }
duke@435 752
duke@435 753 void instanceKlass::call_class_initializer_impl(instanceKlassHandle this_oop, TRAPS) {
duke@435 754 methodHandle h_method(THREAD, this_oop->class_initializer());
duke@435 755 assert(!this_oop->is_initialized(), "we cannot initialize twice");
duke@435 756 if (TraceClassInitialization) {
duke@435 757 tty->print("%d Initializing ", call_class_initializer_impl_counter++);
duke@435 758 this_oop->name()->print_value();
duke@435 759 tty->print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", (address)this_oop());
duke@435 760 }
duke@435 761 if (h_method() != NULL) {
duke@435 762 JavaCallArguments args; // No arguments
duke@435 763 JavaValue result(T_VOID);
duke@435 764 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
duke@435 765 }
duke@435 766 }
duke@435 767
duke@435 768
duke@435 769 void instanceKlass::mask_for(methodHandle method, int bci,
duke@435 770 InterpreterOopMap* entry_for) {
duke@435 771 // Dirty read, then double-check under a lock.
duke@435 772 if (_oop_map_cache == NULL) {
duke@435 773 // Otherwise, allocate a new one.
duke@435 774 MutexLocker x(OopMapCacheAlloc_lock);
duke@435 775 // First time use. Allocate a cache in C heap
duke@435 776 if (_oop_map_cache == NULL) {
duke@435 777 _oop_map_cache = new OopMapCache();
duke@435 778 }
duke@435 779 }
duke@435 780 // _oop_map_cache is constant after init; lookup below does is own locking.
duke@435 781 _oop_map_cache->lookup(method, bci, entry_for);
duke@435 782 }
duke@435 783
duke@435 784
coleenp@2497 785 bool instanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
never@3137 786 for (JavaFieldStream fs(as_klassOop()); !fs.done(); fs.next()) {
never@3137 787 Symbol* f_name = fs.name();
never@3137 788 Symbol* f_sig = fs.signature();
duke@435 789 if (f_name == name && f_sig == sig) {
never@3137 790 fd->initialize(as_klassOop(), fs.index());
duke@435 791 return true;
duke@435 792 }
duke@435 793 }
duke@435 794 return false;
duke@435 795 }
duke@435 796
duke@435 797
coleenp@2497 798 void instanceKlass::shared_symbols_iterate(SymbolClosure* closure) {
coleenp@2497 799 Klass::shared_symbols_iterate(closure);
coleenp@2497 800 closure->do_symbol(&_generic_signature);
coleenp@2497 801 closure->do_symbol(&_source_file_name);
coleenp@2497 802 closure->do_symbol(&_source_debug_extension);
coleenp@2497 803
never@3137 804 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
never@3137 805 int name_index = fs.name_index();
coleenp@2497 806 closure->do_symbol(constants()->symbol_at_addr(name_index));
never@3137 807 int sig_index = fs.signature_index();
coleenp@2497 808 closure->do_symbol(constants()->symbol_at_addr(sig_index));
duke@435 809 }
duke@435 810 }
duke@435 811
duke@435 812
coleenp@2497 813 klassOop instanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
duke@435 814 const int n = local_interfaces()->length();
duke@435 815 for (int i = 0; i < n; i++) {
duke@435 816 klassOop intf1 = klassOop(local_interfaces()->obj_at(i));
duke@435 817 assert(Klass::cast(intf1)->is_interface(), "just checking type");
duke@435 818 // search for field in current interface
duke@435 819 if (instanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {
duke@435 820 assert(fd->is_static(), "interface field must be static");
duke@435 821 return intf1;
duke@435 822 }
duke@435 823 // search for field in direct superinterfaces
duke@435 824 klassOop intf2 = instanceKlass::cast(intf1)->find_interface_field(name, sig, fd);
duke@435 825 if (intf2 != NULL) return intf2;
duke@435 826 }
duke@435 827 // otherwise field lookup fails
duke@435 828 return NULL;
duke@435 829 }
duke@435 830
duke@435 831
coleenp@2497 832 klassOop instanceKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
duke@435 833 // search order according to newest JVM spec (5.4.3.2, p.167).
duke@435 834 // 1) search for field in current klass
duke@435 835 if (find_local_field(name, sig, fd)) {
duke@435 836 return as_klassOop();
duke@435 837 }
duke@435 838 // 2) search for field recursively in direct superinterfaces
duke@435 839 { klassOop intf = find_interface_field(name, sig, fd);
duke@435 840 if (intf != NULL) return intf;
duke@435 841 }
duke@435 842 // 3) apply field lookup recursively if superclass exists
duke@435 843 { klassOop supr = super();
duke@435 844 if (supr != NULL) return instanceKlass::cast(supr)->find_field(name, sig, fd);
duke@435 845 }
duke@435 846 // 4) otherwise field lookup fails
duke@435 847 return NULL;
duke@435 848 }
duke@435 849
duke@435 850
coleenp@2497 851 klassOop instanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
duke@435 852 // search order according to newest JVM spec (5.4.3.2, p.167).
duke@435 853 // 1) search for field in current klass
duke@435 854 if (find_local_field(name, sig, fd)) {
duke@435 855 if (fd->is_static() == is_static) return as_klassOop();
duke@435 856 }
duke@435 857 // 2) search for field recursively in direct superinterfaces
duke@435 858 if (is_static) {
duke@435 859 klassOop intf = find_interface_field(name, sig, fd);
duke@435 860 if (intf != NULL) return intf;
duke@435 861 }
duke@435 862 // 3) apply field lookup recursively if superclass exists
duke@435 863 { klassOop supr = super();
duke@435 864 if (supr != NULL) return instanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
duke@435 865 }
duke@435 866 // 4) otherwise field lookup fails
duke@435 867 return NULL;
duke@435 868 }
duke@435 869
duke@435 870
duke@435 871 bool instanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
never@3137 872 for (JavaFieldStream fs(as_klassOop()); !fs.done(); fs.next()) {
never@3137 873 if (fs.offset() == offset) {
never@3137 874 fd->initialize(as_klassOop(), fs.index());
duke@435 875 if (fd->is_static() == is_static) return true;
duke@435 876 }
duke@435 877 }
duke@435 878 return false;
duke@435 879 }
duke@435 880
duke@435 881
duke@435 882 bool instanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
duke@435 883 klassOop klass = as_klassOop();
duke@435 884 while (klass != NULL) {
duke@435 885 if (instanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
duke@435 886 return true;
duke@435 887 }
duke@435 888 klass = Klass::cast(klass)->super();
duke@435 889 }
duke@435 890 return false;
duke@435 891 }
duke@435 892
duke@435 893
duke@435 894 void instanceKlass::methods_do(void f(methodOop method)) {
duke@435 895 int len = methods()->length();
duke@435 896 for (int index = 0; index < len; index++) {
duke@435 897 methodOop m = methodOop(methods()->obj_at(index));
duke@435 898 assert(m->is_method(), "must be method");
duke@435 899 f(m);
duke@435 900 }
duke@435 901 }
duke@435 902
never@2658 903
duke@435 904 void instanceKlass::do_local_static_fields(FieldClosure* cl) {
never@3137 905 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
never@3137 906 if (fs.access_flags().is_static()) {
never@3137 907 fieldDescriptor fd;
never@3137 908 fd.initialize(as_klassOop(), fs.index());
never@3137 909 cl->do_field(&fd);
never@3137 910 }
duke@435 911 }
duke@435 912 }
duke@435 913
duke@435 914
duke@435 915 void instanceKlass::do_local_static_fields(void f(fieldDescriptor*, TRAPS), TRAPS) {
duke@435 916 instanceKlassHandle h_this(THREAD, as_klassOop());
duke@435 917 do_local_static_fields_impl(h_this, f, CHECK);
duke@435 918 }
duke@435 919
duke@435 920
duke@435 921 void instanceKlass::do_local_static_fields_impl(instanceKlassHandle this_oop, void f(fieldDescriptor* fd, TRAPS), TRAPS) {
never@3137 922 for (JavaFieldStream fs(this_oop()); !fs.done(); fs.next()) {
never@3137 923 if (fs.access_flags().is_static()) {
never@3137 924 fieldDescriptor fd;
never@3137 925 fd.initialize(this_oop(), fs.index());
never@3137 926 f(&fd, CHECK);
never@3137 927 }
duke@435 928 }
duke@435 929 }
duke@435 930
duke@435 931
kvn@479 932 static int compare_fields_by_offset(int* a, int* b) {
kvn@479 933 return a[0] - b[0];
kvn@479 934 }
kvn@479 935
duke@435 936 void instanceKlass::do_nonstatic_fields(FieldClosure* cl) {
duke@435 937 instanceKlass* super = superklass();
duke@435 938 if (super != NULL) {
duke@435 939 super->do_nonstatic_fields(cl);
duke@435 940 }
kvn@479 941 fieldDescriptor fd;
never@3137 942 int length = java_fields_count();
kvn@479 943 // In DebugInfo nonstatic fields are sorted by offset.
kvn@479 944 int* fields_sorted = NEW_C_HEAP_ARRAY(int, 2*(length+1));
kvn@479 945 int j = 0;
never@3137 946 for (int i = 0; i < length; i += 1) {
duke@435 947 fd.initialize(as_klassOop(), i);
kvn@479 948 if (!fd.is_static()) {
kvn@479 949 fields_sorted[j + 0] = fd.offset();
kvn@479 950 fields_sorted[j + 1] = i;
kvn@479 951 j += 2;
kvn@479 952 }
duke@435 953 }
kvn@479 954 if (j > 0) {
kvn@479 955 length = j;
kvn@479 956 // _sort_Fn is defined in growableArray.hpp.
kvn@479 957 qsort(fields_sorted, length/2, 2*sizeof(int), (_sort_Fn)compare_fields_by_offset);
kvn@479 958 for (int i = 0; i < length; i += 2) {
kvn@479 959 fd.initialize(as_klassOop(), fields_sorted[i + 1]);
kvn@479 960 assert(!fd.is_static() && fd.offset() == fields_sorted[i], "only nonstatic fields");
kvn@479 961 cl->do_field(&fd);
kvn@479 962 }
kvn@479 963 }
kvn@479 964 FREE_C_HEAP_ARRAY(int, fields_sorted);
duke@435 965 }
duke@435 966
duke@435 967
duke@435 968 void instanceKlass::array_klasses_do(void f(klassOop k)) {
duke@435 969 if (array_klasses() != NULL)
duke@435 970 arrayKlass::cast(array_klasses())->array_klasses_do(f);
duke@435 971 }
duke@435 972
duke@435 973
duke@435 974 void instanceKlass::with_array_klasses_do(void f(klassOop k)) {
duke@435 975 f(as_klassOop());
duke@435 976 array_klasses_do(f);
duke@435 977 }
duke@435 978
duke@435 979 #ifdef ASSERT
coleenp@2497 980 static int linear_search(objArrayOop methods, Symbol* name, Symbol* signature) {
duke@435 981 int len = methods->length();
duke@435 982 for (int index = 0; index < len; index++) {
duke@435 983 methodOop m = (methodOop)(methods->obj_at(index));
duke@435 984 assert(m->is_method(), "must be method");
duke@435 985 if (m->signature() == signature && m->name() == name) {
duke@435 986 return index;
duke@435 987 }
duke@435 988 }
duke@435 989 return -1;
duke@435 990 }
duke@435 991 #endif
duke@435 992
coleenp@2497 993 methodOop instanceKlass::find_method(Symbol* name, Symbol* signature) const {
duke@435 994 return instanceKlass::find_method(methods(), name, signature);
duke@435 995 }
duke@435 996
coleenp@2497 997 methodOop instanceKlass::find_method(objArrayOop methods, Symbol* name, Symbol* signature) {
duke@435 998 int len = methods->length();
duke@435 999 // methods are sorted, so do binary search
duke@435 1000 int l = 0;
duke@435 1001 int h = len - 1;
duke@435 1002 while (l <= h) {
duke@435 1003 int mid = (l + h) >> 1;
duke@435 1004 methodOop m = (methodOop)methods->obj_at(mid);
duke@435 1005 assert(m->is_method(), "must be method");
duke@435 1006 int res = m->name()->fast_compare(name);
duke@435 1007 if (res == 0) {
duke@435 1008 // found matching name; do linear search to find matching signature
duke@435 1009 // first, quick check for common case
duke@435 1010 if (m->signature() == signature) return m;
duke@435 1011 // search downwards through overloaded methods
duke@435 1012 int i;
duke@435 1013 for (i = mid - 1; i >= l; i--) {
duke@435 1014 methodOop m = (methodOop)methods->obj_at(i);
duke@435 1015 assert(m->is_method(), "must be method");
duke@435 1016 if (m->name() != name) break;
duke@435 1017 if (m->signature() == signature) return m;
duke@435 1018 }
duke@435 1019 // search upwards
duke@435 1020 for (i = mid + 1; i <= h; i++) {
duke@435 1021 methodOop m = (methodOop)methods->obj_at(i);
duke@435 1022 assert(m->is_method(), "must be method");
duke@435 1023 if (m->name() != name) break;
duke@435 1024 if (m->signature() == signature) return m;
duke@435 1025 }
duke@435 1026 // not found
duke@435 1027 #ifdef ASSERT
duke@435 1028 int index = linear_search(methods, name, signature);
jcoomes@1845 1029 assert(index == -1, err_msg("binary search should have found entry %d", index));
duke@435 1030 #endif
duke@435 1031 return NULL;
duke@435 1032 } else if (res < 0) {
duke@435 1033 l = mid + 1;
duke@435 1034 } else {
duke@435 1035 h = mid - 1;
duke@435 1036 }
duke@435 1037 }
duke@435 1038 #ifdef ASSERT
duke@435 1039 int index = linear_search(methods, name, signature);
jcoomes@1845 1040 assert(index == -1, err_msg("binary search should have found entry %d", index));
duke@435 1041 #endif
duke@435 1042 return NULL;
duke@435 1043 }
duke@435 1044
coleenp@2497 1045 methodOop instanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature) const {
duke@435 1046 klassOop klass = as_klassOop();
duke@435 1047 while (klass != NULL) {
duke@435 1048 methodOop method = instanceKlass::cast(klass)->find_method(name, signature);
duke@435 1049 if (method != NULL) return method;
duke@435 1050 klass = instanceKlass::cast(klass)->super();
duke@435 1051 }
duke@435 1052 return NULL;
duke@435 1053 }
duke@435 1054
duke@435 1055 // lookup a method in all the interfaces that this class implements
coleenp@2497 1056 methodOop instanceKlass::lookup_method_in_all_interfaces(Symbol* name,
coleenp@2497 1057 Symbol* signature) const {
duke@435 1058 objArrayOop all_ifs = instanceKlass::cast(as_klassOop())->transitive_interfaces();
duke@435 1059 int num_ifs = all_ifs->length();
duke@435 1060 instanceKlass *ik = NULL;
duke@435 1061 for (int i = 0; i < num_ifs; i++) {
duke@435 1062 ik = instanceKlass::cast(klassOop(all_ifs->obj_at(i)));
duke@435 1063 methodOop m = ik->lookup_method(name, signature);
duke@435 1064 if (m != NULL) {
duke@435 1065 return m;
duke@435 1066 }
duke@435 1067 }
duke@435 1068 return NULL;
duke@435 1069 }
duke@435 1070
duke@435 1071 /* jni_id_for_impl for jfieldIds only */
duke@435 1072 JNIid* instanceKlass::jni_id_for_impl(instanceKlassHandle this_oop, int offset) {
duke@435 1073 MutexLocker ml(JfieldIdCreation_lock);
duke@435 1074 // Retry lookup after we got the lock
duke@435 1075 JNIid* probe = this_oop->jni_ids() == NULL ? NULL : this_oop->jni_ids()->find(offset);
duke@435 1076 if (probe == NULL) {
duke@435 1077 // Slow case, allocate new static field identifier
duke@435 1078 probe = new JNIid(this_oop->as_klassOop(), offset, this_oop->jni_ids());
duke@435 1079 this_oop->set_jni_ids(probe);
duke@435 1080 }
duke@435 1081 return probe;
duke@435 1082 }
duke@435 1083
duke@435 1084
duke@435 1085 /* jni_id_for for jfieldIds only */
duke@435 1086 JNIid* instanceKlass::jni_id_for(int offset) {
duke@435 1087 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset);
duke@435 1088 if (probe == NULL) {
duke@435 1089 probe = jni_id_for_impl(this->as_klassOop(), offset);
duke@435 1090 }
duke@435 1091 return probe;
duke@435 1092 }
duke@435 1093
duke@435 1094
duke@435 1095 // Lookup or create a jmethodID.
dcubed@1412 1096 // This code is called by the VMThread and JavaThreads so the
dcubed@1412 1097 // locking has to be done very carefully to avoid deadlocks
dcubed@1412 1098 // and/or other cache consistency problems.
dcubed@1412 1099 //
dcubed@1412 1100 jmethodID instanceKlass::get_jmethod_id(instanceKlassHandle ik_h, methodHandle method_h) {
duke@435 1101 size_t idnum = (size_t)method_h->method_idnum();
duke@435 1102 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
duke@435 1103 size_t length = 0;
duke@435 1104 jmethodID id = NULL;
duke@435 1105
dcubed@1412 1106 // We use a double-check locking idiom here because this cache is
dcubed@1412 1107 // performance sensitive. In the normal system, this cache only
dcubed@1412 1108 // transitions from NULL to non-NULL which is safe because we use
dcubed@1412 1109 // release_set_methods_jmethod_ids() to advertise the new cache.
dcubed@1412 1110 // A partially constructed cache should never be seen by a racing
dcubed@1412 1111 // thread. We also use release_store_ptr() to save a new jmethodID
dcubed@1412 1112 // in the cache so a partially constructed jmethodID should never be
dcubed@1412 1113 // seen either. Cache reads of existing jmethodIDs proceed without a
dcubed@1412 1114 // lock, but cache writes of a new jmethodID requires uniqueness and
dcubed@1412 1115 // creation of the cache itself requires no leaks so a lock is
dcubed@1412 1116 // generally acquired in those two cases.
dcubed@1412 1117 //
dcubed@1412 1118 // If the RedefineClasses() API has been used, then this cache can
dcubed@1412 1119 // grow and we'll have transitions from non-NULL to bigger non-NULL.
dcubed@1412 1120 // Cache creation requires no leaks and we require safety between all
dcubed@1412 1121 // cache accesses and freeing of the old cache so a lock is generally
dcubed@1412 1122 // acquired when the RedefineClasses() API has been used.
duke@435 1123
dcubed@1412 1124 if (jmeths != NULL) {
dcubed@1412 1125 // the cache already exists
dcubed@1412 1126 if (!ik_h->idnum_can_increment()) {
dcubed@1412 1127 // the cache can't grow so we can just get the current values
dcubed@1412 1128 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
dcubed@1412 1129 } else {
dcubed@1412 1130 // cache can grow so we have to be more careful
dcubed@1412 1131 if (Threads::number_of_threads() == 0 ||
dcubed@1412 1132 SafepointSynchronize::is_at_safepoint()) {
dcubed@1412 1133 // we're single threaded or at a safepoint - no locking needed
dcubed@1412 1134 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
dcubed@1412 1135 } else {
dcubed@1412 1136 MutexLocker ml(JmethodIdCreation_lock);
dcubed@1412 1137 get_jmethod_id_length_value(jmeths, idnum, &length, &id);
dcubed@1412 1138 }
dcubed@1412 1139 }
dcubed@1412 1140 }
dcubed@1412 1141 // implied else:
dcubed@1412 1142 // we need to allocate a cache so default length and id values are good
dcubed@1412 1143
dcubed@1412 1144 if (jmeths == NULL || // no cache yet
dcubed@1412 1145 length <= idnum || // cache is too short
dcubed@1412 1146 id == NULL) { // cache doesn't contain entry
dcubed@1412 1147
dcubed@1412 1148 // This function can be called by the VMThread so we have to do all
dcubed@1412 1149 // things that might block on a safepoint before grabbing the lock.
dcubed@1412 1150 // Otherwise, we can deadlock with the VMThread or have a cache
dcubed@1412 1151 // consistency issue. These vars keep track of what we might have
dcubed@1412 1152 // to free after the lock is dropped.
dcubed@1412 1153 jmethodID to_dealloc_id = NULL;
dcubed@1412 1154 jmethodID* to_dealloc_jmeths = NULL;
dcubed@1412 1155
dcubed@1412 1156 // may not allocate new_jmeths or use it if we allocate it
duke@435 1157 jmethodID* new_jmeths = NULL;
duke@435 1158 if (length <= idnum) {
dcubed@1412 1159 // allocate a new cache that might be used
duke@435 1160 size_t size = MAX2(idnum+1, (size_t)ik_h->idnum_allocated_count());
duke@435 1161 new_jmeths = NEW_C_HEAP_ARRAY(jmethodID, size+1);
duke@435 1162 memset(new_jmeths, 0, (size+1)*sizeof(jmethodID));
dcubed@1412 1163 // cache size is stored in element[0], other elements offset by one
dcubed@1412 1164 new_jmeths[0] = (jmethodID)size;
duke@435 1165 }
duke@435 1166
dcubed@1412 1167 // allocate a new jmethodID that might be used
duke@435 1168 jmethodID new_id = NULL;
duke@435 1169 if (method_h->is_old() && !method_h->is_obsolete()) {
duke@435 1170 // The method passed in is old (but not obsolete), we need to use the current version
duke@435 1171 methodOop current_method = ik_h->method_with_idnum((int)idnum);
duke@435 1172 assert(current_method != NULL, "old and but not obsolete, so should exist");
duke@435 1173 methodHandle current_method_h(current_method == NULL? method_h() : current_method);
duke@435 1174 new_id = JNIHandles::make_jmethod_id(current_method_h);
duke@435 1175 } else {
duke@435 1176 // It is the current version of the method or an obsolete method,
duke@435 1177 // use the version passed in
duke@435 1178 new_id = JNIHandles::make_jmethod_id(method_h);
duke@435 1179 }
duke@435 1180
dcubed@1412 1181 if (Threads::number_of_threads() == 0 ||
dcubed@1412 1182 SafepointSynchronize::is_at_safepoint()) {
dcubed@1412 1183 // we're single threaded or at a safepoint - no locking needed
dcubed@1412 1184 id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths,
dcubed@1412 1185 &to_dealloc_id, &to_dealloc_jmeths);
dcubed@484 1186 } else {
duke@435 1187 MutexLocker ml(JmethodIdCreation_lock);
dcubed@1412 1188 id = get_jmethod_id_fetch_or_update(ik_h, idnum, new_id, new_jmeths,
dcubed@1412 1189 &to_dealloc_id, &to_dealloc_jmeths);
dcubed@1412 1190 }
dcubed@1412 1191
dcubed@1412 1192 // The lock has been dropped so we can free resources.
dcubed@1412 1193 // Free up either the old cache or the new cache if we allocated one.
dcubed@1412 1194 if (to_dealloc_jmeths != NULL) {
dcubed@1412 1195 FreeHeap(to_dealloc_jmeths);
dcubed@1412 1196 }
dcubed@1412 1197 // free up the new ID since it wasn't needed
dcubed@1412 1198 if (to_dealloc_id != NULL) {
dcubed@1412 1199 JNIHandles::destroy_jmethod_id(to_dealloc_id);
dcubed@484 1200 }
dcubed@484 1201 }
dcubed@484 1202 return id;
dcubed@484 1203 }
duke@435 1204
duke@435 1205
dcubed@1412 1206 // Common code to fetch the jmethodID from the cache or update the
dcubed@1412 1207 // cache with the new jmethodID. This function should never do anything
dcubed@1412 1208 // that causes the caller to go to a safepoint or we can deadlock with
dcubed@1412 1209 // the VMThread or have cache consistency issues.
dcubed@1412 1210 //
dcubed@1412 1211 jmethodID instanceKlass::get_jmethod_id_fetch_or_update(
dcubed@1412 1212 instanceKlassHandle ik_h, size_t idnum, jmethodID new_id,
dcubed@1412 1213 jmethodID* new_jmeths, jmethodID* to_dealloc_id_p,
dcubed@1412 1214 jmethodID** to_dealloc_jmeths_p) {
dcubed@1412 1215 assert(new_id != NULL, "sanity check");
dcubed@1412 1216 assert(to_dealloc_id_p != NULL, "sanity check");
dcubed@1412 1217 assert(to_dealloc_jmeths_p != NULL, "sanity check");
dcubed@1412 1218 assert(Threads::number_of_threads() == 0 ||
dcubed@1412 1219 SafepointSynchronize::is_at_safepoint() ||
dcubed@1412 1220 JmethodIdCreation_lock->owned_by_self(), "sanity check");
dcubed@1412 1221
dcubed@1412 1222 // reacquire the cache - we are locked, single threaded or at a safepoint
dcubed@484 1223 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
dcubed@1412 1224 jmethodID id = NULL;
dcubed@1412 1225 size_t length = 0;
dcubed@484 1226
dcubed@1412 1227 if (jmeths == NULL || // no cache yet
dcubed@1412 1228 (length = (size_t)jmeths[0]) <= idnum) { // cache is too short
dcubed@484 1229 if (jmeths != NULL) {
dcubed@1412 1230 // copy any existing entries from the old cache
dcubed@484 1231 for (size_t index = 0; index < length; index++) {
dcubed@484 1232 new_jmeths[index+1] = jmeths[index+1];
duke@435 1233 }
dcubed@1412 1234 *to_dealloc_jmeths_p = jmeths; // save old cache for later delete
duke@435 1235 }
dcubed@484 1236 ik_h->release_set_methods_jmethod_ids(jmeths = new_jmeths);
dcubed@484 1237 } else {
dcubed@1412 1238 // fetch jmethodID (if any) from the existing cache
dcubed@484 1239 id = jmeths[idnum+1];
dcubed@1412 1240 *to_dealloc_jmeths_p = new_jmeths; // save new cache for later delete
dcubed@484 1241 }
dcubed@484 1242 if (id == NULL) {
dcubed@1412 1243 // No matching jmethodID in the existing cache or we have a new
dcubed@1412 1244 // cache or we just grew the cache. This cache write is done here
dcubed@1412 1245 // by the first thread to win the foot race because a jmethodID
dcubed@1412 1246 // needs to be unique once it is generally available.
dcubed@484 1247 id = new_id;
dcubed@1412 1248
dcubed@1412 1249 // The jmethodID cache can be read while unlocked so we have to
dcubed@1412 1250 // make sure the new jmethodID is complete before installing it
dcubed@1412 1251 // in the cache.
dcubed@1412 1252 OrderAccess::release_store_ptr(&jmeths[idnum+1], id);
dcubed@484 1253 } else {
dcubed@1412 1254 *to_dealloc_id_p = new_id; // save new id for later delete
duke@435 1255 }
duke@435 1256 return id;
duke@435 1257 }
duke@435 1258
duke@435 1259
dcubed@1412 1260 // Common code to get the jmethodID cache length and the jmethodID
dcubed@1412 1261 // value at index idnum if there is one.
dcubed@1412 1262 //
dcubed@1412 1263 void instanceKlass::get_jmethod_id_length_value(jmethodID* cache,
dcubed@1412 1264 size_t idnum, size_t *length_p, jmethodID* id_p) {
dcubed@1412 1265 assert(cache != NULL, "sanity check");
dcubed@1412 1266 assert(length_p != NULL, "sanity check");
dcubed@1412 1267 assert(id_p != NULL, "sanity check");
dcubed@1412 1268
dcubed@1412 1269 // cache size is stored in element[0], other elements offset by one
dcubed@1412 1270 *length_p = (size_t)cache[0];
dcubed@1412 1271 if (*length_p <= idnum) { // cache is too short
dcubed@1412 1272 *id_p = NULL;
dcubed@1412 1273 } else {
dcubed@1412 1274 *id_p = cache[idnum+1]; // fetch jmethodID (if any)
dcubed@1412 1275 }
dcubed@1412 1276 }
dcubed@1412 1277
dcubed@1412 1278
duke@435 1279 // Lookup a jmethodID, NULL if not found. Do no blocking, no allocations, no handles
duke@435 1280 jmethodID instanceKlass::jmethod_id_or_null(methodOop method) {
duke@435 1281 size_t idnum = (size_t)method->method_idnum();
duke@435 1282 jmethodID* jmeths = methods_jmethod_ids_acquire();
duke@435 1283 size_t length; // length assigned as debugging crumb
duke@435 1284 jmethodID id = NULL;
dcubed@1412 1285 if (jmeths != NULL && // If there is a cache
duke@435 1286 (length = (size_t)jmeths[0]) > idnum) { // and if it is long enough,
duke@435 1287 id = jmeths[idnum+1]; // Look up the id (may be NULL)
duke@435 1288 }
duke@435 1289 return id;
duke@435 1290 }
duke@435 1291
duke@435 1292
duke@435 1293 // Cache an itable index
duke@435 1294 void instanceKlass::set_cached_itable_index(size_t idnum, int index) {
duke@435 1295 int* indices = methods_cached_itable_indices_acquire();
dcubed@1412 1296 int* to_dealloc_indices = NULL;
dcubed@1412 1297
dcubed@1412 1298 // We use a double-check locking idiom here because this cache is
dcubed@1412 1299 // performance sensitive. In the normal system, this cache only
dcubed@1412 1300 // transitions from NULL to non-NULL which is safe because we use
dcubed@1412 1301 // release_set_methods_cached_itable_indices() to advertise the
dcubed@1412 1302 // new cache. A partially constructed cache should never be seen
dcubed@1412 1303 // by a racing thread. Cache reads and writes proceed without a
dcubed@1412 1304 // lock, but creation of the cache itself requires no leaks so a
dcubed@1412 1305 // lock is generally acquired in that case.
dcubed@1412 1306 //
dcubed@1412 1307 // If the RedefineClasses() API has been used, then this cache can
dcubed@1412 1308 // grow and we'll have transitions from non-NULL to bigger non-NULL.
dcubed@1412 1309 // Cache creation requires no leaks and we require safety between all
dcubed@1412 1310 // cache accesses and freeing of the old cache so a lock is generally
dcubed@1412 1311 // acquired when the RedefineClasses() API has been used.
dcubed@1412 1312
dcubed@1412 1313 if (indices == NULL || idnum_can_increment()) {
dcubed@1412 1314 // we need a cache or the cache can grow
duke@435 1315 MutexLocker ml(JNICachedItableIndex_lock);
dcubed@1412 1316 // reacquire the cache to see if another thread already did the work
duke@435 1317 indices = methods_cached_itable_indices_acquire();
duke@435 1318 size_t length = 0;
dcubed@1412 1319 // cache size is stored in element[0], other elements offset by one
duke@435 1320 if (indices == NULL || (length = (size_t)indices[0]) <= idnum) {
duke@435 1321 size_t size = MAX2(idnum+1, (size_t)idnum_allocated_count());
duke@435 1322 int* new_indices = NEW_C_HEAP_ARRAY(int, size+1);
dcubed@1412 1323 new_indices[0] = (int)size;
dcubed@1412 1324 // copy any existing entries
duke@435 1325 size_t i;
duke@435 1326 for (i = 0; i < length; i++) {
duke@435 1327 new_indices[i+1] = indices[i+1];
duke@435 1328 }
duke@435 1329 // Set all the rest to -1
duke@435 1330 for (i = length; i < size; i++) {
duke@435 1331 new_indices[i+1] = -1;
duke@435 1332 }
duke@435 1333 if (indices != NULL) {
dcubed@1412 1334 // We have an old cache to delete so save it for after we
dcubed@1412 1335 // drop the lock.
dcubed@1412 1336 to_dealloc_indices = indices;
duke@435 1337 }
duke@435 1338 release_set_methods_cached_itable_indices(indices = new_indices);
duke@435 1339 }
dcubed@1412 1340
dcubed@1412 1341 if (idnum_can_increment()) {
dcubed@1412 1342 // this cache can grow so we have to write to it safely
dcubed@1412 1343 indices[idnum+1] = index;
dcubed@1412 1344 }
duke@435 1345 } else {
duke@435 1346 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
duke@435 1347 }
dcubed@1412 1348
dcubed@1412 1349 if (!idnum_can_increment()) {
dcubed@1412 1350 // The cache cannot grow and this JNI itable index value does not
dcubed@1412 1351 // have to be unique like a jmethodID. If there is a race to set it,
dcubed@1412 1352 // it doesn't matter.
dcubed@1412 1353 indices[idnum+1] = index;
dcubed@1412 1354 }
dcubed@1412 1355
dcubed@1412 1356 if (to_dealloc_indices != NULL) {
dcubed@1412 1357 // we allocated a new cache so free the old one
dcubed@1412 1358 FreeHeap(to_dealloc_indices);
dcubed@1412 1359 }
duke@435 1360 }
duke@435 1361
duke@435 1362
duke@435 1363 // Retrieve a cached itable index
duke@435 1364 int instanceKlass::cached_itable_index(size_t idnum) {
duke@435 1365 int* indices = methods_cached_itable_indices_acquire();
duke@435 1366 if (indices != NULL && ((size_t)indices[0]) > idnum) {
duke@435 1367 // indices exist and are long enough, retrieve possible cached
duke@435 1368 return indices[idnum+1];
duke@435 1369 }
duke@435 1370 return -1;
duke@435 1371 }
duke@435 1372
duke@435 1373
duke@435 1374 //
duke@435 1375 // nmethodBucket is used to record dependent nmethods for
duke@435 1376 // deoptimization. nmethod dependencies are actually <klass, method>
duke@435 1377 // pairs but we really only care about the klass part for purposes of
duke@435 1378 // finding nmethods which might need to be deoptimized. Instead of
duke@435 1379 // recording the method, a count of how many times a particular nmethod
duke@435 1380 // was recorded is kept. This ensures that any recording errors are
duke@435 1381 // noticed since an nmethod should be removed as many times are it's
duke@435 1382 // added.
duke@435 1383 //
duke@435 1384 class nmethodBucket {
duke@435 1385 private:
duke@435 1386 nmethod* _nmethod;
duke@435 1387 int _count;
duke@435 1388 nmethodBucket* _next;
duke@435 1389
duke@435 1390 public:
duke@435 1391 nmethodBucket(nmethod* nmethod, nmethodBucket* next) {
duke@435 1392 _nmethod = nmethod;
duke@435 1393 _next = next;
duke@435 1394 _count = 1;
duke@435 1395 }
duke@435 1396 int count() { return _count; }
duke@435 1397 int increment() { _count += 1; return _count; }
duke@435 1398 int decrement() { _count -= 1; assert(_count >= 0, "don't underflow"); return _count; }
duke@435 1399 nmethodBucket* next() { return _next; }
duke@435 1400 void set_next(nmethodBucket* b) { _next = b; }
duke@435 1401 nmethod* get_nmethod() { return _nmethod; }
duke@435 1402 };
duke@435 1403
duke@435 1404
duke@435 1405 //
duke@435 1406 // Walk the list of dependent nmethods searching for nmethods which
twisti@3050 1407 // are dependent on the changes that were passed in and mark them for
duke@435 1408 // deoptimization. Returns the number of nmethods found.
duke@435 1409 //
duke@435 1410 int instanceKlass::mark_dependent_nmethods(DepChange& changes) {
duke@435 1411 assert_locked_or_safepoint(CodeCache_lock);
duke@435 1412 int found = 0;
duke@435 1413 nmethodBucket* b = _dependencies;
duke@435 1414 while (b != NULL) {
duke@435 1415 nmethod* nm = b->get_nmethod();
duke@435 1416 // since dependencies aren't removed until an nmethod becomes a zombie,
duke@435 1417 // the dependency list may contain nmethods which aren't alive.
duke@435 1418 if (nm->is_alive() && !nm->is_marked_for_deoptimization() && nm->check_dependency_on(changes)) {
duke@435 1419 if (TraceDependencies) {
duke@435 1420 ResourceMark rm;
duke@435 1421 tty->print_cr("Marked for deoptimization");
duke@435 1422 tty->print_cr(" context = %s", this->external_name());
duke@435 1423 changes.print();
duke@435 1424 nm->print();
duke@435 1425 nm->print_dependencies();
duke@435 1426 }
duke@435 1427 nm->mark_for_deoptimization();
duke@435 1428 found++;
duke@435 1429 }
duke@435 1430 b = b->next();
duke@435 1431 }
duke@435 1432 return found;
duke@435 1433 }
duke@435 1434
duke@435 1435
duke@435 1436 //
duke@435 1437 // Add an nmethodBucket to the list of dependencies for this nmethod.
duke@435 1438 // It's possible that an nmethod has multiple dependencies on this klass
duke@435 1439 // so a count is kept for each bucket to guarantee that creation and
duke@435 1440 // deletion of dependencies is consistent.
duke@435 1441 //
duke@435 1442 void instanceKlass::add_dependent_nmethod(nmethod* nm) {
duke@435 1443 assert_locked_or_safepoint(CodeCache_lock);
duke@435 1444 nmethodBucket* b = _dependencies;
duke@435 1445 nmethodBucket* last = NULL;
duke@435 1446 while (b != NULL) {
duke@435 1447 if (nm == b->get_nmethod()) {
duke@435 1448 b->increment();
duke@435 1449 return;
duke@435 1450 }
duke@435 1451 b = b->next();
duke@435 1452 }
duke@435 1453 _dependencies = new nmethodBucket(nm, _dependencies);
duke@435 1454 }
duke@435 1455
duke@435 1456
duke@435 1457 //
duke@435 1458 // Decrement count of the nmethod in the dependency list and remove
duke@435 1459 // the bucket competely when the count goes to 0. This method must
duke@435 1460 // find a corresponding bucket otherwise there's a bug in the
duke@435 1461 // recording of dependecies.
duke@435 1462 //
duke@435 1463 void instanceKlass::remove_dependent_nmethod(nmethod* nm) {
duke@435 1464 assert_locked_or_safepoint(CodeCache_lock);
duke@435 1465 nmethodBucket* b = _dependencies;
duke@435 1466 nmethodBucket* last = NULL;
duke@435 1467 while (b != NULL) {
duke@435 1468 if (nm == b->get_nmethod()) {
duke@435 1469 if (b->decrement() == 0) {
duke@435 1470 if (last == NULL) {
duke@435 1471 _dependencies = b->next();
duke@435 1472 } else {
duke@435 1473 last->set_next(b->next());
duke@435 1474 }
duke@435 1475 delete b;
duke@435 1476 }
duke@435 1477 return;
duke@435 1478 }
duke@435 1479 last = b;
duke@435 1480 b = b->next();
duke@435 1481 }
duke@435 1482 #ifdef ASSERT
duke@435 1483 tty->print_cr("### %s can't find dependent nmethod:", this->external_name());
duke@435 1484 nm->print();
duke@435 1485 #endif // ASSERT
duke@435 1486 ShouldNotReachHere();
duke@435 1487 }
duke@435 1488
duke@435 1489
duke@435 1490 #ifndef PRODUCT
duke@435 1491 void instanceKlass::print_dependent_nmethods(bool verbose) {
duke@435 1492 nmethodBucket* b = _dependencies;
duke@435 1493 int idx = 0;
duke@435 1494 while (b != NULL) {
duke@435 1495 nmethod* nm = b->get_nmethod();
duke@435 1496 tty->print("[%d] count=%d { ", idx++, b->count());
duke@435 1497 if (!verbose) {
duke@435 1498 nm->print_on(tty, "nmethod");
duke@435 1499 tty->print_cr(" } ");
duke@435 1500 } else {
duke@435 1501 nm->print();
duke@435 1502 nm->print_dependencies();
duke@435 1503 tty->print_cr("--- } ");
duke@435 1504 }
duke@435 1505 b = b->next();
duke@435 1506 }
duke@435 1507 }
duke@435 1508
duke@435 1509
duke@435 1510 bool instanceKlass::is_dependent_nmethod(nmethod* nm) {
duke@435 1511 nmethodBucket* b = _dependencies;
duke@435 1512 while (b != NULL) {
duke@435 1513 if (nm == b->get_nmethod()) {
duke@435 1514 return true;
duke@435 1515 }
duke@435 1516 b = b->next();
duke@435 1517 }
duke@435 1518 return false;
duke@435 1519 }
duke@435 1520 #endif //PRODUCT
duke@435 1521
duke@435 1522
coleenp@548 1523 #ifdef ASSERT
coleenp@548 1524 template <class T> void assert_is_in(T *p) {
coleenp@548 1525 T heap_oop = oopDesc::load_heap_oop(p);
coleenp@548 1526 if (!oopDesc::is_null(heap_oop)) {
coleenp@548 1527 oop o = oopDesc::decode_heap_oop_not_null(heap_oop);
coleenp@548 1528 assert(Universe::heap()->is_in(o), "should be in heap");
coleenp@548 1529 }
coleenp@548 1530 }
coleenp@548 1531 template <class T> void assert_is_in_closed_subset(T *p) {
coleenp@548 1532 T heap_oop = oopDesc::load_heap_oop(p);
coleenp@548 1533 if (!oopDesc::is_null(heap_oop)) {
coleenp@548 1534 oop o = oopDesc::decode_heap_oop_not_null(heap_oop);
coleenp@548 1535 assert(Universe::heap()->is_in_closed_subset(o), "should be in closed");
coleenp@548 1536 }
coleenp@548 1537 }
coleenp@548 1538 template <class T> void assert_is_in_reserved(T *p) {
coleenp@548 1539 T heap_oop = oopDesc::load_heap_oop(p);
coleenp@548 1540 if (!oopDesc::is_null(heap_oop)) {
coleenp@548 1541 oop o = oopDesc::decode_heap_oop_not_null(heap_oop);
coleenp@548 1542 assert(Universe::heap()->is_in_reserved(o), "should be in reserved");
coleenp@548 1543 }
coleenp@548 1544 }
coleenp@548 1545 template <class T> void assert_nothing(T *p) {}
coleenp@548 1546
coleenp@548 1547 #else
coleenp@548 1548 template <class T> void assert_is_in(T *p) {}
coleenp@548 1549 template <class T> void assert_is_in_closed_subset(T *p) {}
coleenp@548 1550 template <class T> void assert_is_in_reserved(T *p) {}
coleenp@548 1551 template <class T> void assert_nothing(T *p) {}
coleenp@548 1552 #endif // ASSERT
coleenp@548 1553
coleenp@548 1554 //
coleenp@548 1555 // Macros that iterate over areas of oops which are specialized on type of
coleenp@548 1556 // oop pointer either narrow or wide, depending on UseCompressedOops
coleenp@548 1557 //
coleenp@548 1558 // Parameters are:
coleenp@548 1559 // T - type of oop to point to (either oop or narrowOop)
coleenp@548 1560 // start_p - starting pointer for region to iterate over
coleenp@548 1561 // count - number of oops or narrowOops to iterate over
coleenp@548 1562 // do_oop - action to perform on each oop (it's arbitrary C code which
coleenp@548 1563 // makes it more efficient to put in a macro rather than making
coleenp@548 1564 // it a template function)
coleenp@548 1565 // assert_fn - assert function which is template function because performance
coleenp@548 1566 // doesn't matter when enabled.
coleenp@548 1567 #define InstanceKlass_SPECIALIZED_OOP_ITERATE( \
coleenp@548 1568 T, start_p, count, do_oop, \
coleenp@548 1569 assert_fn) \
coleenp@548 1570 { \
coleenp@548 1571 T* p = (T*)(start_p); \
coleenp@548 1572 T* const end = p + (count); \
coleenp@548 1573 while (p < end) { \
coleenp@548 1574 (assert_fn)(p); \
coleenp@548 1575 do_oop; \
coleenp@548 1576 ++p; \
coleenp@548 1577 } \
coleenp@548 1578 }
coleenp@548 1579
coleenp@548 1580 #define InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE( \
coleenp@548 1581 T, start_p, count, do_oop, \
coleenp@548 1582 assert_fn) \
coleenp@548 1583 { \
coleenp@548 1584 T* const start = (T*)(start_p); \
coleenp@548 1585 T* p = start + (count); \
coleenp@548 1586 while (start < p) { \
coleenp@548 1587 --p; \
coleenp@548 1588 (assert_fn)(p); \
coleenp@548 1589 do_oop; \
coleenp@548 1590 } \
coleenp@548 1591 }
coleenp@548 1592
coleenp@548 1593 #define InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE( \
coleenp@548 1594 T, start_p, count, low, high, \
coleenp@548 1595 do_oop, assert_fn) \
coleenp@548 1596 { \
coleenp@548 1597 T* const l = (T*)(low); \
coleenp@548 1598 T* const h = (T*)(high); \
coleenp@548 1599 assert(mask_bits((intptr_t)l, sizeof(T)-1) == 0 && \
coleenp@548 1600 mask_bits((intptr_t)h, sizeof(T)-1) == 0, \
coleenp@548 1601 "bounded region must be properly aligned"); \
coleenp@548 1602 T* p = (T*)(start_p); \
coleenp@548 1603 T* end = p + (count); \
coleenp@548 1604 if (p < l) p = l; \
coleenp@548 1605 if (end > h) end = h; \
coleenp@548 1606 while (p < end) { \
coleenp@548 1607 (assert_fn)(p); \
coleenp@548 1608 do_oop; \
coleenp@548 1609 ++p; \
coleenp@548 1610 } \
coleenp@548 1611 }
coleenp@548 1612
coleenp@548 1613
coleenp@548 1614 // The following macros call specialized macros, passing either oop or
coleenp@548 1615 // narrowOop as the specialization type. These test the UseCompressedOops
coleenp@548 1616 // flag.
coleenp@548 1617 #define InstanceKlass_OOP_MAP_ITERATE(obj, do_oop, assert_fn) \
coleenp@548 1618 { \
coleenp@548 1619 /* Compute oopmap block range. The common case \
coleenp@548 1620 is nonstatic_oop_map_size == 1. */ \
coleenp@548 1621 OopMapBlock* map = start_of_nonstatic_oop_maps(); \
jcoomes@1373 1622 OopMapBlock* const end_map = map + nonstatic_oop_map_count(); \
coleenp@548 1623 if (UseCompressedOops) { \
coleenp@548 1624 while (map < end_map) { \
coleenp@548 1625 InstanceKlass_SPECIALIZED_OOP_ITERATE(narrowOop, \
jcoomes@1373 1626 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \
coleenp@548 1627 do_oop, assert_fn) \
coleenp@548 1628 ++map; \
coleenp@548 1629 } \
coleenp@548 1630 } else { \
coleenp@548 1631 while (map < end_map) { \
coleenp@548 1632 InstanceKlass_SPECIALIZED_OOP_ITERATE(oop, \
jcoomes@1373 1633 obj->obj_field_addr<oop>(map->offset()), map->count(), \
coleenp@548 1634 do_oop, assert_fn) \
coleenp@548 1635 ++map; \
coleenp@548 1636 } \
coleenp@548 1637 } \
coleenp@548 1638 }
coleenp@548 1639
coleenp@548 1640 #define InstanceKlass_OOP_MAP_REVERSE_ITERATE(obj, do_oop, assert_fn) \
coleenp@548 1641 { \
coleenp@548 1642 OopMapBlock* const start_map = start_of_nonstatic_oop_maps(); \
jcoomes@1373 1643 OopMapBlock* map = start_map + nonstatic_oop_map_count(); \
coleenp@548 1644 if (UseCompressedOops) { \
coleenp@548 1645 while (start_map < map) { \
coleenp@548 1646 --map; \
coleenp@548 1647 InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE(narrowOop, \
jcoomes@1373 1648 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \
coleenp@548 1649 do_oop, assert_fn) \
coleenp@548 1650 } \
coleenp@548 1651 } else { \
coleenp@548 1652 while (start_map < map) { \
coleenp@548 1653 --map; \
coleenp@548 1654 InstanceKlass_SPECIALIZED_OOP_REVERSE_ITERATE(oop, \
jcoomes@1373 1655 obj->obj_field_addr<oop>(map->offset()), map->count(), \
coleenp@548 1656 do_oop, assert_fn) \
coleenp@548 1657 } \
coleenp@548 1658 } \
coleenp@548 1659 }
coleenp@548 1660
coleenp@548 1661 #define InstanceKlass_BOUNDED_OOP_MAP_ITERATE(obj, low, high, do_oop, \
coleenp@548 1662 assert_fn) \
coleenp@548 1663 { \
coleenp@548 1664 /* Compute oopmap block range. The common case is \
coleenp@548 1665 nonstatic_oop_map_size == 1, so we accept the \
coleenp@548 1666 usually non-existent extra overhead of examining \
coleenp@548 1667 all the maps. */ \
coleenp@548 1668 OopMapBlock* map = start_of_nonstatic_oop_maps(); \
jcoomes@1373 1669 OopMapBlock* const end_map = map + nonstatic_oop_map_count(); \
coleenp@548 1670 if (UseCompressedOops) { \
coleenp@548 1671 while (map < end_map) { \
coleenp@548 1672 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(narrowOop, \
jcoomes@1373 1673 obj->obj_field_addr<narrowOop>(map->offset()), map->count(), \
coleenp@548 1674 low, high, \
coleenp@548 1675 do_oop, assert_fn) \
coleenp@548 1676 ++map; \
coleenp@548 1677 } \
coleenp@548 1678 } else { \
coleenp@548 1679 while (map < end_map) { \
coleenp@548 1680 InstanceKlass_SPECIALIZED_BOUNDED_OOP_ITERATE(oop, \
jcoomes@1373 1681 obj->obj_field_addr<oop>(map->offset()), map->count(), \
coleenp@548 1682 low, high, \
coleenp@548 1683 do_oop, assert_fn) \
coleenp@548 1684 ++map; \
coleenp@548 1685 } \
coleenp@548 1686 } \
coleenp@548 1687 }
coleenp@548 1688
duke@435 1689 void instanceKlass::oop_follow_contents(oop obj) {
coleenp@548 1690 assert(obj != NULL, "can't follow the content of NULL object");
duke@435 1691 obj->follow_header();
coleenp@548 1692 InstanceKlass_OOP_MAP_ITERATE( \
coleenp@548 1693 obj, \
coleenp@548 1694 MarkSweep::mark_and_push(p), \
coleenp@548 1695 assert_is_in_closed_subset)
duke@435 1696 }
duke@435 1697
duke@435 1698 #ifndef SERIALGC
duke@435 1699 void instanceKlass::oop_follow_contents(ParCompactionManager* cm,
duke@435 1700 oop obj) {
coleenp@548 1701 assert(obj != NULL, "can't follow the content of NULL object");
duke@435 1702 obj->follow_header(cm);
coleenp@548 1703 InstanceKlass_OOP_MAP_ITERATE( \
coleenp@548 1704 obj, \
coleenp@548 1705 PSParallelCompact::mark_and_push(cm, p), \
coleenp@548 1706 assert_is_in)
duke@435 1707 }
duke@435 1708 #endif // SERIALGC
duke@435 1709
duke@435 1710 // closure's do_header() method dicates whether the given closure should be
duke@435 1711 // applied to the klass ptr in the object header.
duke@435 1712
ysr@777 1713 #define InstanceKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \
ysr@777 1714 \
ysr@777 1715 int instanceKlass::oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) { \
coleenp@548 1716 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\
coleenp@548 1717 /* header */ \
coleenp@548 1718 if (closure->do_header()) { \
coleenp@548 1719 obj->oop_iterate_header(closure); \
coleenp@548 1720 } \
coleenp@548 1721 InstanceKlass_OOP_MAP_ITERATE( \
coleenp@548 1722 obj, \
coleenp@548 1723 SpecializationStats:: \
coleenp@548 1724 record_do_oop_call##nv_suffix(SpecializationStats::ik); \
coleenp@548 1725 (closure)->do_oop##nv_suffix(p), \
coleenp@548 1726 assert_is_in_closed_subset) \
coleenp@548 1727 return size_helper(); \
duke@435 1728 }
duke@435 1729
ysr@777 1730 #ifndef SERIALGC
ysr@777 1731 #define InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) \
ysr@777 1732 \
ysr@777 1733 int instanceKlass::oop_oop_iterate_backwards##nv_suffix(oop obj, \
ysr@777 1734 OopClosureType* closure) { \
ysr@777 1735 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik); \
ysr@777 1736 /* header */ \
ysr@777 1737 if (closure->do_header()) { \
ysr@777 1738 obj->oop_iterate_header(closure); \
ysr@777 1739 } \
ysr@777 1740 /* instance variables */ \
ysr@777 1741 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \
ysr@777 1742 obj, \
ysr@777 1743 SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::ik);\
ysr@777 1744 (closure)->do_oop##nv_suffix(p), \
ysr@777 1745 assert_is_in_closed_subset) \
ysr@777 1746 return size_helper(); \
ysr@777 1747 }
ysr@777 1748 #endif // !SERIALGC
ysr@777 1749
coleenp@548 1750 #define InstanceKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix) \
coleenp@548 1751 \
coleenp@548 1752 int instanceKlass::oop_oop_iterate##nv_suffix##_m(oop obj, \
coleenp@548 1753 OopClosureType* closure, \
coleenp@548 1754 MemRegion mr) { \
coleenp@548 1755 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\
coleenp@548 1756 if (closure->do_header()) { \
coleenp@548 1757 obj->oop_iterate_header(closure, mr); \
coleenp@548 1758 } \
coleenp@548 1759 InstanceKlass_BOUNDED_OOP_MAP_ITERATE( \
coleenp@548 1760 obj, mr.start(), mr.end(), \
coleenp@548 1761 (closure)->do_oop##nv_suffix(p), \
coleenp@548 1762 assert_is_in_closed_subset) \
coleenp@548 1763 return size_helper(); \
duke@435 1764 }
duke@435 1765
duke@435 1766 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DEFN)
ysr@777 1767 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_DEFN)
duke@435 1768 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_DEFN_m)
ysr@777 1769 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_DEFN_m)
ysr@777 1770 #ifndef SERIALGC
ysr@777 1771 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)
ysr@777 1772 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)
ysr@777 1773 #endif // !SERIALGC
duke@435 1774
duke@435 1775 int instanceKlass::oop_adjust_pointers(oop obj) {
duke@435 1776 int size = size_helper();
coleenp@548 1777 InstanceKlass_OOP_MAP_ITERATE( \
coleenp@548 1778 obj, \
coleenp@548 1779 MarkSweep::adjust_pointer(p), \
coleenp@548 1780 assert_is_in)
duke@435 1781 obj->adjust_header();
duke@435 1782 return size;
duke@435 1783 }
duke@435 1784
duke@435 1785 #ifndef SERIALGC
duke@435 1786 void instanceKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
coleenp@548 1787 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \
coleenp@548 1788 obj, \
coleenp@548 1789 if (PSScavenge::should_scavenge(p)) { \
coleenp@548 1790 pm->claim_or_forward_depth(p); \
coleenp@548 1791 }, \
coleenp@548 1792 assert_nothing )
duke@435 1793 }
duke@435 1794
duke@435 1795 int instanceKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
coleenp@548 1796 InstanceKlass_OOP_MAP_ITERATE( \
coleenp@548 1797 obj, \
coleenp@548 1798 PSParallelCompact::adjust_pointer(p), \
coleenp@548 1799 assert_nothing)
duke@435 1800 return size_helper();
duke@435 1801 }
duke@435 1802
duke@435 1803 #endif // SERIALGC
duke@435 1804
duke@435 1805 // This klass is alive but the implementor link is not followed/updated.
duke@435 1806 // Subklass and sibling links are handled by Klass::follow_weak_klass_links
duke@435 1807
duke@435 1808 void instanceKlass::follow_weak_klass_links(
duke@435 1809 BoolObjectClosure* is_alive, OopClosure* keep_alive) {
duke@435 1810 assert(is_alive->do_object_b(as_klassOop()), "this oop should be live");
duke@435 1811 if (ClassUnloading) {
duke@435 1812 for (int i = 0; i < implementors_limit; i++) {
duke@435 1813 klassOop impl = _implementors[i];
duke@435 1814 if (impl == NULL) break; // no more in the list
duke@435 1815 if (!is_alive->do_object_b(impl)) {
duke@435 1816 // remove this guy from the list by overwriting him with the tail
duke@435 1817 int lasti = --_nof_implementors;
duke@435 1818 assert(lasti >= i && lasti < implementors_limit, "just checking");
duke@435 1819 _implementors[i] = _implementors[lasti];
duke@435 1820 _implementors[lasti] = NULL;
duke@435 1821 --i; // rerun the loop at this index
duke@435 1822 }
duke@435 1823 }
duke@435 1824 } else {
duke@435 1825 for (int i = 0; i < implementors_limit; i++) {
duke@435 1826 keep_alive->do_oop(&adr_implementors()[i]);
duke@435 1827 }
duke@435 1828 }
duke@435 1829 Klass::follow_weak_klass_links(is_alive, keep_alive);
duke@435 1830 }
duke@435 1831
duke@435 1832 void instanceKlass::remove_unshareable_info() {
duke@435 1833 Klass::remove_unshareable_info();
duke@435 1834 init_implementor();
duke@435 1835 }
duke@435 1836
duke@435 1837 static void clear_all_breakpoints(methodOop m) {
duke@435 1838 m->clear_all_breakpoints();
duke@435 1839 }
duke@435 1840
duke@435 1841 void instanceKlass::release_C_heap_structures() {
duke@435 1842 // Deallocate oop map cache
duke@435 1843 if (_oop_map_cache != NULL) {
duke@435 1844 delete _oop_map_cache;
duke@435 1845 _oop_map_cache = NULL;
duke@435 1846 }
duke@435 1847
duke@435 1848 // Deallocate JNI identifiers for jfieldIDs
duke@435 1849 JNIid::deallocate(jni_ids());
duke@435 1850 set_jni_ids(NULL);
duke@435 1851
duke@435 1852 jmethodID* jmeths = methods_jmethod_ids_acquire();
duke@435 1853 if (jmeths != (jmethodID*)NULL) {
duke@435 1854 release_set_methods_jmethod_ids(NULL);
duke@435 1855 FreeHeap(jmeths);
duke@435 1856 }
duke@435 1857
duke@435 1858 int* indices = methods_cached_itable_indices_acquire();
duke@435 1859 if (indices != (int*)NULL) {
duke@435 1860 release_set_methods_cached_itable_indices(NULL);
duke@435 1861 FreeHeap(indices);
duke@435 1862 }
duke@435 1863
duke@435 1864 // release dependencies
duke@435 1865 nmethodBucket* b = _dependencies;
duke@435 1866 _dependencies = NULL;
duke@435 1867 while (b != NULL) {
duke@435 1868 nmethodBucket* next = b->next();
duke@435 1869 delete b;
duke@435 1870 b = next;
duke@435 1871 }
duke@435 1872
duke@435 1873 // Deallocate breakpoint records
duke@435 1874 if (breakpoints() != 0x0) {
duke@435 1875 methods_do(clear_all_breakpoints);
duke@435 1876 assert(breakpoints() == 0x0, "should have cleared breakpoints");
duke@435 1877 }
duke@435 1878
duke@435 1879 // deallocate information about previous versions
duke@435 1880 if (_previous_versions != NULL) {
duke@435 1881 for (int i = _previous_versions->length() - 1; i >= 0; i--) {
duke@435 1882 PreviousVersionNode * pv_node = _previous_versions->at(i);
duke@435 1883 delete pv_node;
duke@435 1884 }
duke@435 1885 delete _previous_versions;
duke@435 1886 _previous_versions = NULL;
duke@435 1887 }
duke@435 1888
duke@435 1889 // deallocate the cached class file
duke@435 1890 if (_cached_class_file_bytes != NULL) {
duke@435 1891 os::free(_cached_class_file_bytes);
duke@435 1892 _cached_class_file_bytes = NULL;
duke@435 1893 _cached_class_file_len = 0;
duke@435 1894 }
coleenp@2497 1895
coleenp@2497 1896 // Decrement symbol reference counts associated with the unloaded class.
coleenp@2497 1897 if (_name != NULL) _name->decrement_refcount();
coleenp@2497 1898 // unreference array name derived from this class name (arrays of an unloaded
coleenp@2497 1899 // class can't be referenced anymore).
coleenp@2497 1900 if (_array_name != NULL) _array_name->decrement_refcount();
coleenp@2497 1901 if (_source_file_name != NULL) _source_file_name->decrement_refcount();
coleenp@2497 1902 if (_source_debug_extension != NULL) _source_debug_extension->decrement_refcount();
coleenp@2497 1903 // walk constant pool and decrement symbol reference counts
coleenp@2497 1904 _constants->unreference_symbols();
coleenp@2497 1905 }
coleenp@2497 1906
coleenp@2497 1907 void instanceKlass::set_source_file_name(Symbol* n) {
coleenp@2497 1908 _source_file_name = n;
coleenp@2497 1909 if (_source_file_name != NULL) _source_file_name->increment_refcount();
coleenp@2497 1910 }
coleenp@2497 1911
coleenp@2497 1912 void instanceKlass::set_source_debug_extension(Symbol* n) {
coleenp@2497 1913 _source_debug_extension = n;
coleenp@2497 1914 if (_source_debug_extension != NULL) _source_debug_extension->increment_refcount();
duke@435 1915 }
duke@435 1916
never@2658 1917 address instanceKlass::static_field_addr(int offset) {
never@2658 1918 return (address)(offset + instanceMirrorKlass::offset_of_static_fields() + (intptr_t)java_mirror());
never@2658 1919 }
never@2658 1920
never@2658 1921
jrose@1474 1922 const char* instanceKlass::signature_name() const {
duke@435 1923 const char* src = (const char*) (name()->as_C_string());
duke@435 1924 const int src_length = (int)strlen(src);
duke@435 1925 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
duke@435 1926 int src_index = 0;
duke@435 1927 int dest_index = 0;
duke@435 1928 dest[dest_index++] = 'L';
duke@435 1929 while (src_index < src_length) {
duke@435 1930 dest[dest_index++] = src[src_index++];
duke@435 1931 }
duke@435 1932 dest[dest_index++] = ';';
duke@435 1933 dest[dest_index] = '\0';
duke@435 1934 return dest;
duke@435 1935 }
duke@435 1936
duke@435 1937 // different verisons of is_same_class_package
duke@435 1938 bool instanceKlass::is_same_class_package(klassOop class2) {
duke@435 1939 klassOop class1 = as_klassOop();
duke@435 1940 oop classloader1 = instanceKlass::cast(class1)->class_loader();
coleenp@2497 1941 Symbol* classname1 = Klass::cast(class1)->name();
duke@435 1942
duke@435 1943 if (Klass::cast(class2)->oop_is_objArray()) {
duke@435 1944 class2 = objArrayKlass::cast(class2)->bottom_klass();
duke@435 1945 }
duke@435 1946 oop classloader2;
duke@435 1947 if (Klass::cast(class2)->oop_is_instance()) {
duke@435 1948 classloader2 = instanceKlass::cast(class2)->class_loader();
duke@435 1949 } else {
duke@435 1950 assert(Klass::cast(class2)->oop_is_typeArray(), "should be type array");
duke@435 1951 classloader2 = NULL;
duke@435 1952 }
coleenp@2497 1953 Symbol* classname2 = Klass::cast(class2)->name();
duke@435 1954
duke@435 1955 return instanceKlass::is_same_class_package(classloader1, classname1,
duke@435 1956 classloader2, classname2);
duke@435 1957 }
duke@435 1958
coleenp@2497 1959 bool instanceKlass::is_same_class_package(oop classloader2, Symbol* classname2) {
duke@435 1960 klassOop class1 = as_klassOop();
duke@435 1961 oop classloader1 = instanceKlass::cast(class1)->class_loader();
coleenp@2497 1962 Symbol* classname1 = Klass::cast(class1)->name();
duke@435 1963
duke@435 1964 return instanceKlass::is_same_class_package(classloader1, classname1,
duke@435 1965 classloader2, classname2);
duke@435 1966 }
duke@435 1967
duke@435 1968 // return true if two classes are in the same package, classloader
duke@435 1969 // and classname information is enough to determine a class's package
coleenp@2497 1970 bool instanceKlass::is_same_class_package(oop class_loader1, Symbol* class_name1,
coleenp@2497 1971 oop class_loader2, Symbol* class_name2) {
duke@435 1972 if (class_loader1 != class_loader2) {
duke@435 1973 return false;
jrose@1100 1974 } else if (class_name1 == class_name2) {
jrose@1100 1975 return true; // skip painful bytewise comparison
duke@435 1976 } else {
duke@435 1977 ResourceMark rm;
duke@435 1978
coleenp@2497 1979 // The Symbol*'s are in UTF8 encoding. Since we only need to check explicitly
duke@435 1980 // for ASCII characters ('/', 'L', '['), we can keep them in UTF8 encoding.
duke@435 1981 // Otherwise, we just compare jbyte values between the strings.
coleenp@2497 1982 const jbyte *name1 = class_name1->base();
coleenp@2497 1983 const jbyte *name2 = class_name2->base();
coleenp@2497 1984
coleenp@2497 1985 const jbyte *last_slash1 = UTF8::strrchr(name1, class_name1->utf8_length(), '/');
coleenp@2497 1986 const jbyte *last_slash2 = UTF8::strrchr(name2, class_name2->utf8_length(), '/');
duke@435 1987
duke@435 1988 if ((last_slash1 == NULL) || (last_slash2 == NULL)) {
duke@435 1989 // One of the two doesn't have a package. Only return true
duke@435 1990 // if the other one also doesn't have a package.
duke@435 1991 return last_slash1 == last_slash2;
duke@435 1992 } else {
duke@435 1993 // Skip over '['s
duke@435 1994 if (*name1 == '[') {
duke@435 1995 do {
duke@435 1996 name1++;
duke@435 1997 } while (*name1 == '[');
duke@435 1998 if (*name1 != 'L') {
duke@435 1999 // Something is terribly wrong. Shouldn't be here.
duke@435 2000 return false;
duke@435 2001 }
duke@435 2002 }
duke@435 2003 if (*name2 == '[') {
duke@435 2004 do {
duke@435 2005 name2++;
duke@435 2006 } while (*name2 == '[');
duke@435 2007 if (*name2 != 'L') {
duke@435 2008 // Something is terribly wrong. Shouldn't be here.
duke@435 2009 return false;
duke@435 2010 }
duke@435 2011 }
duke@435 2012
duke@435 2013 // Check that package part is identical
duke@435 2014 int length1 = last_slash1 - name1;
duke@435 2015 int length2 = last_slash2 - name2;
duke@435 2016
duke@435 2017 return UTF8::equal(name1, length1, name2, length2);
duke@435 2018 }
duke@435 2019 }
duke@435 2020 }
duke@435 2021
acorn@1087 2022 // Returns true iff super_method can be overridden by a method in targetclassname
acorn@1087 2023 // See JSL 3rd edition 8.4.6.1
acorn@1087 2024 // Assumes name-signature match
acorn@1087 2025 // "this" is instanceKlass of super_method which must exist
acorn@1087 2026 // note that the instanceKlass of the method in the targetclassname has not always been created yet
coleenp@2497 2027 bool instanceKlass::is_override(methodHandle super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS) {
acorn@1087 2028 // Private methods can not be overridden
acorn@1087 2029 if (super_method->is_private()) {
acorn@1087 2030 return false;
acorn@1087 2031 }
acorn@1087 2032 // If super method is accessible, then override
acorn@1087 2033 if ((super_method->is_protected()) ||
acorn@1087 2034 (super_method->is_public())) {
acorn@1087 2035 return true;
acorn@1087 2036 }
acorn@1087 2037 // Package-private methods are not inherited outside of package
acorn@1087 2038 assert(super_method->is_package_private(), "must be package private");
coleenp@2497 2039 return(is_same_class_package(targetclassloader(), targetclassname));
acorn@1087 2040 }
twisti@1040 2041
jrose@1100 2042 /* defined for now in jvm.cpp, for historical reasons *--
jrose@1100 2043 klassOop instanceKlass::compute_enclosing_class_impl(instanceKlassHandle self,
coleenp@2497 2044 Symbol*& simple_name_result, TRAPS) {
jrose@1100 2045 ...
jrose@1100 2046 }
jrose@1100 2047 */
jrose@1100 2048
jrose@1100 2049 // tell if two classes have the same enclosing class (at package level)
jrose@1100 2050 bool instanceKlass::is_same_package_member_impl(instanceKlassHandle class1,
jrose@1100 2051 klassOop class2_oop, TRAPS) {
jrose@1100 2052 if (class2_oop == class1->as_klassOop()) return true;
jrose@1100 2053 if (!Klass::cast(class2_oop)->oop_is_instance()) return false;
jrose@1100 2054 instanceKlassHandle class2(THREAD, class2_oop);
jrose@1100 2055
jrose@1100 2056 // must be in same package before we try anything else
jrose@1100 2057 if (!class1->is_same_class_package(class2->class_loader(), class2->name()))
jrose@1100 2058 return false;
jrose@1100 2059
jrose@1100 2060 // As long as there is an outer1.getEnclosingClass,
jrose@1100 2061 // shift the search outward.
jrose@1100 2062 instanceKlassHandle outer1 = class1;
jrose@1100 2063 for (;;) {
jrose@1100 2064 // As we walk along, look for equalities between outer1 and class2.
jrose@1100 2065 // Eventually, the walks will terminate as outer1 stops
jrose@1100 2066 // at the top-level class around the original class.
xlu@1561 2067 bool ignore_inner_is_member;
xlu@1561 2068 klassOop next = outer1->compute_enclosing_class(&ignore_inner_is_member,
xlu@1561 2069 CHECK_false);
jrose@1100 2070 if (next == NULL) break;
jrose@1100 2071 if (next == class2()) return true;
jrose@1100 2072 outer1 = instanceKlassHandle(THREAD, next);
jrose@1100 2073 }
jrose@1100 2074
jrose@1100 2075 // Now do the same for class2.
jrose@1100 2076 instanceKlassHandle outer2 = class2;
jrose@1100 2077 for (;;) {
xlu@1561 2078 bool ignore_inner_is_member;
xlu@1561 2079 klassOop next = outer2->compute_enclosing_class(&ignore_inner_is_member,
xlu@1561 2080 CHECK_false);
jrose@1100 2081 if (next == NULL) break;
jrose@1100 2082 // Might as well check the new outer against all available values.
jrose@1100 2083 if (next == class1()) return true;
jrose@1100 2084 if (next == outer1()) return true;
jrose@1100 2085 outer2 = instanceKlassHandle(THREAD, next);
jrose@1100 2086 }
jrose@1100 2087
jrose@1100 2088 // If by this point we have not found an equality between the
jrose@1100 2089 // two classes, we know they are in separate package members.
jrose@1100 2090 return false;
jrose@1100 2091 }
jrose@1100 2092
duke@435 2093
duke@435 2094 jint instanceKlass::compute_modifier_flags(TRAPS) const {
duke@435 2095 klassOop k = as_klassOop();
duke@435 2096 jint access = access_flags().as_int();
duke@435 2097
duke@435 2098 // But check if it happens to be member class.
duke@435 2099 typeArrayOop inner_class_list = inner_classes();
duke@435 2100 int length = (inner_class_list == NULL) ? 0 : inner_class_list->length();
duke@435 2101 assert (length % instanceKlass::inner_class_next_offset == 0, "just checking");
duke@435 2102 if (length > 0) {
duke@435 2103 typeArrayHandle inner_class_list_h(THREAD, inner_class_list);
duke@435 2104 instanceKlassHandle ik(THREAD, k);
duke@435 2105 for (int i = 0; i < length; i += instanceKlass::inner_class_next_offset) {
duke@435 2106 int ioff = inner_class_list_h->ushort_at(
duke@435 2107 i + instanceKlass::inner_class_inner_class_info_offset);
duke@435 2108
duke@435 2109 // Inner class attribute can be zero, skip it.
duke@435 2110 // Strange but true: JVM spec. allows null inner class refs.
duke@435 2111 if (ioff == 0) continue;
duke@435 2112
duke@435 2113 // only look at classes that are already loaded
duke@435 2114 // since we are looking for the flags for our self.
coleenp@2497 2115 Symbol* inner_name = ik->constants()->klass_name_at(ioff);
duke@435 2116 if ((ik->name() == inner_name)) {
duke@435 2117 // This is really a member class.
duke@435 2118 access = inner_class_list_h->ushort_at(i + instanceKlass::inner_class_access_flags_offset);
duke@435 2119 break;
duke@435 2120 }
duke@435 2121 }
duke@435 2122 }
duke@435 2123 // Remember to strip ACC_SUPER bit
duke@435 2124 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS;
duke@435 2125 }
duke@435 2126
duke@435 2127 jint instanceKlass::jvmti_class_status() const {
duke@435 2128 jint result = 0;
duke@435 2129
duke@435 2130 if (is_linked()) {
duke@435 2131 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
duke@435 2132 }
duke@435 2133
duke@435 2134 if (is_initialized()) {
duke@435 2135 assert(is_linked(), "Class status is not consistent");
duke@435 2136 result |= JVMTI_CLASS_STATUS_INITIALIZED;
duke@435 2137 }
duke@435 2138 if (is_in_error_state()) {
duke@435 2139 result |= JVMTI_CLASS_STATUS_ERROR;
duke@435 2140 }
duke@435 2141 return result;
duke@435 2142 }
duke@435 2143
duke@435 2144 methodOop instanceKlass::method_at_itable(klassOop holder, int index, TRAPS) {
duke@435 2145 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
duke@435 2146 int method_table_offset_in_words = ioe->offset()/wordSize;
duke@435 2147 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
duke@435 2148 / itableOffsetEntry::size();
duke@435 2149
duke@435 2150 for (int cnt = 0 ; ; cnt ++, ioe ++) {
twisti@1040 2151 // If the interface isn't implemented by the receiver class,
duke@435 2152 // the VM should throw IncompatibleClassChangeError.
duke@435 2153 if (cnt >= nof_interfaces) {
coleenp@2497 2154 THROW_0(vmSymbols::java_lang_IncompatibleClassChangeError());
duke@435 2155 }
duke@435 2156
duke@435 2157 klassOop ik = ioe->interface_klass();
duke@435 2158 if (ik == holder) break;
duke@435 2159 }
duke@435 2160
duke@435 2161 itableMethodEntry* ime = ioe->first_method_entry(as_klassOop());
duke@435 2162 methodOop m = ime[index].method();
duke@435 2163 if (m == NULL) {
coleenp@2497 2164 THROW_0(vmSymbols::java_lang_AbstractMethodError());
duke@435 2165 }
duke@435 2166 return m;
duke@435 2167 }
duke@435 2168
duke@435 2169 // On-stack replacement stuff
duke@435 2170 void instanceKlass::add_osr_nmethod(nmethod* n) {
duke@435 2171 // only one compilation can be active
duke@435 2172 NEEDS_CLEANUP
duke@435 2173 // This is a short non-blocking critical region, so the no safepoint check is ok.
duke@435 2174 OsrList_lock->lock_without_safepoint_check();
duke@435 2175 assert(n->is_osr_method(), "wrong kind of nmethod");
jrose@1424 2176 n->set_osr_link(osr_nmethods_head());
duke@435 2177 set_osr_nmethods_head(n);
iveresov@2138 2178 // Raise the highest osr level if necessary
iveresov@2138 2179 if (TieredCompilation) {
iveresov@2138 2180 methodOop m = n->method();
iveresov@2138 2181 m->set_highest_osr_comp_level(MAX2(m->highest_osr_comp_level(), n->comp_level()));
iveresov@2138 2182 }
duke@435 2183 // Remember to unlock again
duke@435 2184 OsrList_lock->unlock();
iveresov@2138 2185
iveresov@2138 2186 // Get rid of the osr methods for the same bci that have lower levels.
iveresov@2138 2187 if (TieredCompilation) {
iveresov@2138 2188 for (int l = CompLevel_limited_profile; l < n->comp_level(); l++) {
iveresov@2138 2189 nmethod *inv = lookup_osr_nmethod(n->method(), n->osr_entry_bci(), l, true);
iveresov@2138 2190 if (inv != NULL && inv->is_in_use()) {
iveresov@2138 2191 inv->make_not_entrant();
iveresov@2138 2192 }
iveresov@2138 2193 }
iveresov@2138 2194 }
duke@435 2195 }
duke@435 2196
duke@435 2197
duke@435 2198 void instanceKlass::remove_osr_nmethod(nmethod* n) {
duke@435 2199 // This is a short non-blocking critical region, so the no safepoint check is ok.
duke@435 2200 OsrList_lock->lock_without_safepoint_check();
duke@435 2201 assert(n->is_osr_method(), "wrong kind of nmethod");
duke@435 2202 nmethod* last = NULL;
duke@435 2203 nmethod* cur = osr_nmethods_head();
iveresov@2138 2204 int max_level = CompLevel_none; // Find the max comp level excluding n
iveresov@2138 2205 methodOop m = n->method();
duke@435 2206 // Search for match
duke@435 2207 while(cur != NULL && cur != n) {
iveresov@2138 2208 if (TieredCompilation) {
iveresov@2138 2209 // Find max level before n
iveresov@2138 2210 max_level = MAX2(max_level, cur->comp_level());
iveresov@2138 2211 }
duke@435 2212 last = cur;
jrose@1424 2213 cur = cur->osr_link();
duke@435 2214 }
iveresov@2138 2215 nmethod* next = NULL;
duke@435 2216 if (cur == n) {
iveresov@2138 2217 next = cur->osr_link();
duke@435 2218 if (last == NULL) {
duke@435 2219 // Remove first element
iveresov@2138 2220 set_osr_nmethods_head(next);
duke@435 2221 } else {
iveresov@2138 2222 last->set_osr_link(next);
duke@435 2223 }
duke@435 2224 }
jrose@1424 2225 n->set_osr_link(NULL);
iveresov@2138 2226 if (TieredCompilation) {
iveresov@2138 2227 cur = next;
iveresov@2138 2228 while (cur != NULL) {
iveresov@2138 2229 // Find max level after n
iveresov@2138 2230 max_level = MAX2(max_level, cur->comp_level());
iveresov@2138 2231 cur = cur->osr_link();
iveresov@2138 2232 }
iveresov@2138 2233 m->set_highest_osr_comp_level(max_level);
iveresov@2138 2234 }
duke@435 2235 // Remember to unlock again
duke@435 2236 OsrList_lock->unlock();
duke@435 2237 }
duke@435 2238
iveresov@2138 2239 nmethod* instanceKlass::lookup_osr_nmethod(const methodOop m, int bci, int comp_level, bool match_level) const {
duke@435 2240 // This is a short non-blocking critical region, so the no safepoint check is ok.
duke@435 2241 OsrList_lock->lock_without_safepoint_check();
duke@435 2242 nmethod* osr = osr_nmethods_head();
iveresov@2138 2243 nmethod* best = NULL;
duke@435 2244 while (osr != NULL) {
duke@435 2245 assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
iveresov@2138 2246 // There can be a time when a c1 osr method exists but we are waiting
iveresov@2138 2247 // for a c2 version. When c2 completes its osr nmethod we will trash
iveresov@2138 2248 // the c1 version and only be able to find the c2 version. However
iveresov@2138 2249 // while we overflow in the c1 code at back branches we don't want to
iveresov@2138 2250 // try and switch to the same code as we are already running
iveresov@2138 2251
duke@435 2252 if (osr->method() == m &&
duke@435 2253 (bci == InvocationEntryBci || osr->osr_entry_bci() == bci)) {
iveresov@2138 2254 if (match_level) {
iveresov@2138 2255 if (osr->comp_level() == comp_level) {
iveresov@2138 2256 // Found a match - return it.
iveresov@2138 2257 OsrList_lock->unlock();
iveresov@2138 2258 return osr;
iveresov@2138 2259 }
iveresov@2138 2260 } else {
iveresov@2138 2261 if (best == NULL || (osr->comp_level() > best->comp_level())) {
iveresov@2138 2262 if (osr->comp_level() == CompLevel_highest_tier) {
iveresov@2138 2263 // Found the best possible - return it.
iveresov@2138 2264 OsrList_lock->unlock();
iveresov@2138 2265 return osr;
iveresov@2138 2266 }
iveresov@2138 2267 best = osr;
iveresov@2138 2268 }
iveresov@2138 2269 }
duke@435 2270 }
jrose@1424 2271 osr = osr->osr_link();
duke@435 2272 }
duke@435 2273 OsrList_lock->unlock();
iveresov@2138 2274 if (best != NULL && best->comp_level() >= comp_level && match_level == false) {
iveresov@2138 2275 return best;
iveresov@2138 2276 }
duke@435 2277 return NULL;
duke@435 2278 }
duke@435 2279
duke@435 2280 // -----------------------------------------------------------------------------------------------------
duke@435 2281 #ifndef PRODUCT
duke@435 2282
duke@435 2283 // Printing
duke@435 2284
jrose@1100 2285 #define BULLET " - "
jrose@1100 2286
duke@435 2287 void FieldPrinter::do_field(fieldDescriptor* fd) {
jrose@1100 2288 _st->print(BULLET);
never@2658 2289 if (_obj == NULL) {
duke@435 2290 fd->print_on(_st);
duke@435 2291 _st->cr();
duke@435 2292 } else {
duke@435 2293 fd->print_on_for(_st, _obj);
duke@435 2294 _st->cr();
duke@435 2295 }
duke@435 2296 }
duke@435 2297
duke@435 2298
duke@435 2299 void instanceKlass::oop_print_on(oop obj, outputStream* st) {
duke@435 2300 Klass::oop_print_on(obj, st);
duke@435 2301
never@1577 2302 if (as_klassOop() == SystemDictionary::String_klass()) {
duke@435 2303 typeArrayOop value = java_lang_String::value(obj);
duke@435 2304 juint offset = java_lang_String::offset(obj);
duke@435 2305 juint length = java_lang_String::length(obj);
duke@435 2306 if (value != NULL &&
duke@435 2307 value->is_typeArray() &&
duke@435 2308 offset <= (juint) value->length() &&
duke@435 2309 offset + length <= (juint) value->length()) {
jrose@1100 2310 st->print(BULLET"string: ");
duke@435 2311 Handle h_obj(obj);
duke@435 2312 java_lang_String::print(h_obj, st);
duke@435 2313 st->cr();
duke@435 2314 if (!WizardMode) return; // that is enough
duke@435 2315 }
duke@435 2316 }
duke@435 2317
jrose@1100 2318 st->print_cr(BULLET"---- fields (total size %d words):", oop_size(obj));
never@2658 2319 FieldPrinter print_field(st, obj);
never@2658 2320 do_nonstatic_fields(&print_field);
duke@435 2321
never@1577 2322 if (as_klassOop() == SystemDictionary::Class_klass()) {
jrose@1100 2323 st->print(BULLET"signature: ");
jrose@1100 2324 java_lang_Class::print_signature(obj, st);
jrose@1100 2325 st->cr();
duke@435 2326 klassOop mirrored_klass = java_lang_Class::as_klassOop(obj);
jrose@1100 2327 st->print(BULLET"fake entry for mirror: ");
duke@435 2328 mirrored_klass->print_value_on(st);
duke@435 2329 st->cr();
jrose@1100 2330 st->print(BULLET"fake entry resolved_constructor: ");
duke@435 2331 methodOop ctor = java_lang_Class::resolved_constructor(obj);
duke@435 2332 ctor->print_value_on(st);
duke@435 2333 klassOop array_klass = java_lang_Class::array_klass(obj);
jrose@1100 2334 st->cr();
jrose@1100 2335 st->print(BULLET"fake entry for array: ");
duke@435 2336 array_klass->print_value_on(st);
duke@435 2337 st->cr();
never@2658 2338 st->print_cr(BULLET"fake entry for oop_size: %d", java_lang_Class::oop_size(obj));
never@2658 2339 st->print_cr(BULLET"fake entry for static_oop_field_count: %d", java_lang_Class::static_oop_field_count(obj));
never@2658 2340 klassOop real_klass = java_lang_Class::as_klassOop(obj);
never@2690 2341 if (real_klass != NULL && real_klass->klass_part()->oop_is_instance()) {
never@2658 2342 instanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
never@2658 2343 }
jrose@1474 2344 } else if (as_klassOop() == SystemDictionary::MethodType_klass()) {
jrose@1474 2345 st->print(BULLET"signature: ");
jrose@2639 2346 java_lang_invoke_MethodType::print_signature(obj, st);
jrose@1474 2347 st->cr();
duke@435 2348 }
duke@435 2349 }
duke@435 2350
jrose@1590 2351 #endif //PRODUCT
jrose@1590 2352
duke@435 2353 void instanceKlass::oop_print_value_on(oop obj, outputStream* st) {
duke@435 2354 st->print("a ");
duke@435 2355 name()->print_value_on(st);
duke@435 2356 obj->print_address_on(st);
never@1577 2357 if (as_klassOop() == SystemDictionary::String_klass()
jrose@1100 2358 && java_lang_String::value(obj) != NULL) {
jrose@1100 2359 ResourceMark rm;
jrose@1100 2360 int len = java_lang_String::length(obj);
jrose@1100 2361 int plen = (len < 24 ? len : 12);
jrose@1100 2362 char* str = java_lang_String::as_utf8_string(obj, 0, plen);
jrose@1100 2363 st->print(" = \"%s\"", str);
jrose@1100 2364 if (len > plen)
jrose@1100 2365 st->print("...[%d]", len);
never@1577 2366 } else if (as_klassOop() == SystemDictionary::Class_klass()) {
jrose@1100 2367 klassOop k = java_lang_Class::as_klassOop(obj);
jrose@1100 2368 st->print(" = ");
jrose@1100 2369 if (k != NULL) {
jrose@1100 2370 k->print_value_on(st);
jrose@1100 2371 } else {
jrose@1100 2372 const char* tname = type2name(java_lang_Class::primitive_type(obj));
jrose@1100 2373 st->print("%s", tname ? tname : "type?");
jrose@1100 2374 }
jrose@1474 2375 } else if (as_klassOop() == SystemDictionary::MethodType_klass()) {
jrose@1474 2376 st->print(" = ");
jrose@2639 2377 java_lang_invoke_MethodType::print_signature(obj, st);
jrose@1100 2378 } else if (java_lang_boxing_object::is_instance(obj)) {
jrose@1100 2379 st->print(" = ");
jrose@1100 2380 java_lang_boxing_object::print(obj, st);
jrose@1100 2381 }
duke@435 2382 }
duke@435 2383
duke@435 2384 const char* instanceKlass::internal_name() const {
duke@435 2385 return external_name();
duke@435 2386 }
duke@435 2387
duke@435 2388 // Verification
duke@435 2389
duke@435 2390 class VerifyFieldClosure: public OopClosure {
coleenp@548 2391 protected:
coleenp@548 2392 template <class T> void do_oop_work(T* p) {
duke@435 2393 guarantee(Universe::heap()->is_in_closed_subset(p), "should be in heap");
coleenp@548 2394 oop obj = oopDesc::load_decode_heap_oop(p);
coleenp@548 2395 if (!obj->is_oop_or_null()) {
coleenp@548 2396 tty->print_cr("Failed: " PTR_FORMAT " -> " PTR_FORMAT, p, (address)obj);
duke@435 2397 Universe::print();
duke@435 2398 guarantee(false, "boom");
duke@435 2399 }
duke@435 2400 }
coleenp@548 2401 public:
coleenp@548 2402 virtual void do_oop(oop* p) { VerifyFieldClosure::do_oop_work(p); }
coleenp@548 2403 virtual void do_oop(narrowOop* p) { VerifyFieldClosure::do_oop_work(p); }
duke@435 2404 };
duke@435 2405
duke@435 2406 void instanceKlass::oop_verify_on(oop obj, outputStream* st) {
duke@435 2407 Klass::oop_verify_on(obj, st);
duke@435 2408 VerifyFieldClosure blk;
duke@435 2409 oop_oop_iterate(obj, &blk);
duke@435 2410 }
duke@435 2411
coleenp@548 2412 // JNIid class for jfieldIDs only
coleenp@548 2413 // Note to reviewers:
coleenp@548 2414 // These JNI functions are just moved over to column 1 and not changed
coleenp@548 2415 // in the compressed oops workspace.
coleenp@548 2416 JNIid::JNIid(klassOop holder, int offset, JNIid* next) {
coleenp@548 2417 _holder = holder;
coleenp@548 2418 _offset = offset;
coleenp@548 2419 _next = next;
coleenp@548 2420 debug_only(_is_static_field_id = false;)
coleenp@548 2421 }
duke@435 2422
duke@435 2423
coleenp@548 2424 JNIid* JNIid::find(int offset) {
coleenp@548 2425 JNIid* current = this;
coleenp@548 2426 while (current != NULL) {
coleenp@548 2427 if (current->offset() == offset) return current;
coleenp@548 2428 current = current->next();
coleenp@548 2429 }
coleenp@548 2430 return NULL;
coleenp@548 2431 }
duke@435 2432
duke@435 2433 void JNIid::oops_do(OopClosure* f) {
duke@435 2434 for (JNIid* cur = this; cur != NULL; cur = cur->next()) {
duke@435 2435 f->do_oop(cur->holder_addr());
duke@435 2436 }
duke@435 2437 }
duke@435 2438
duke@435 2439 void JNIid::deallocate(JNIid* current) {
coleenp@548 2440 while (current != NULL) {
coleenp@548 2441 JNIid* next = current->next();
coleenp@548 2442 delete current;
coleenp@548 2443 current = next;
coleenp@548 2444 }
coleenp@548 2445 }
duke@435 2446
duke@435 2447
coleenp@548 2448 void JNIid::verify(klassOop holder) {
never@2658 2449 int first_field_offset = instanceMirrorKlass::offset_of_static_fields();
coleenp@548 2450 int end_field_offset;
coleenp@548 2451 end_field_offset = first_field_offset + (instanceKlass::cast(holder)->static_field_size() * wordSize);
duke@435 2452
coleenp@548 2453 JNIid* current = this;
coleenp@548 2454 while (current != NULL) {
coleenp@548 2455 guarantee(current->holder() == holder, "Invalid klass in JNIid");
coleenp@548 2456 #ifdef ASSERT
coleenp@548 2457 int o = current->offset();
coleenp@548 2458 if (current->is_static_field_id()) {
coleenp@548 2459 guarantee(o >= first_field_offset && o < end_field_offset, "Invalid static field offset in JNIid");
coleenp@548 2460 }
coleenp@548 2461 #endif
coleenp@548 2462 current = current->next();
coleenp@548 2463 }
coleenp@548 2464 }
duke@435 2465
duke@435 2466
duke@435 2467 #ifdef ASSERT
coleenp@548 2468 void instanceKlass::set_init_state(ClassState state) {
coleenp@548 2469 bool good_state = as_klassOop()->is_shared() ? (_init_state <= state)
coleenp@548 2470 : (_init_state < state);
coleenp@548 2471 assert(good_state || state == allocated, "illegal state transition");
coleenp@548 2472 _init_state = state;
coleenp@548 2473 }
duke@435 2474 #endif
duke@435 2475
duke@435 2476
duke@435 2477 // RedefineClasses() support for previous versions:
duke@435 2478
duke@435 2479 // Add an information node that contains weak references to the
duke@435 2480 // interesting parts of the previous version of the_class.
dcubed@1412 2481 // This is also where we clean out any unused weak references.
dcubed@1412 2482 // Note that while we delete nodes from the _previous_versions
dcubed@1412 2483 // array, we never delete the array itself until the klass is
dcubed@1412 2484 // unloaded. The has_been_redefined() query depends on that fact.
dcubed@1412 2485 //
duke@435 2486 void instanceKlass::add_previous_version(instanceKlassHandle ikh,
coleenp@548 2487 BitMap* emcp_methods, int emcp_method_count) {
duke@435 2488 assert(Thread::current()->is_VM_thread(),
coleenp@548 2489 "only VMThread can add previous versions");
duke@435 2490
duke@435 2491 if (_previous_versions == NULL) {
duke@435 2492 // This is the first previous version so make some space.
duke@435 2493 // Start with 2 elements under the assumption that the class
duke@435 2494 // won't be redefined much.
duke@435 2495 _previous_versions = new (ResourceObj::C_HEAP)
duke@435 2496 GrowableArray<PreviousVersionNode *>(2, true);
duke@435 2497 }
duke@435 2498
duke@435 2499 // RC_TRACE macro has an embedded ResourceMark
duke@435 2500 RC_TRACE(0x00000100, ("adding previous version ref for %s @%d, EMCP_cnt=%d",
duke@435 2501 ikh->external_name(), _previous_versions->length(), emcp_method_count));
duke@435 2502 constantPoolHandle cp_h(ikh->constants());
dcubed@482 2503 jobject cp_ref;
dcubed@482 2504 if (cp_h->is_shared()) {
dcubed@482 2505 // a shared ConstantPool requires a regular reference; a weak
dcubed@482 2506 // reference would be collectible
dcubed@482 2507 cp_ref = JNIHandles::make_global(cp_h);
dcubed@482 2508 } else {
dcubed@482 2509 cp_ref = JNIHandles::make_weak_global(cp_h);
dcubed@482 2510 }
duke@435 2511 PreviousVersionNode * pv_node = NULL;
duke@435 2512 objArrayOop old_methods = ikh->methods();
duke@435 2513
duke@435 2514 if (emcp_method_count == 0) {
dcubed@482 2515 // non-shared ConstantPool gets a weak reference
dcubed@482 2516 pv_node = new PreviousVersionNode(cp_ref, !cp_h->is_shared(), NULL);
duke@435 2517 RC_TRACE(0x00000400,
duke@435 2518 ("add: all methods are obsolete; flushing any EMCP weak refs"));
duke@435 2519 } else {
duke@435 2520 int local_count = 0;
duke@435 2521 GrowableArray<jweak>* method_refs = new (ResourceObj::C_HEAP)
duke@435 2522 GrowableArray<jweak>(emcp_method_count, true);
duke@435 2523 for (int i = 0; i < old_methods->length(); i++) {
duke@435 2524 if (emcp_methods->at(i)) {
duke@435 2525 // this old method is EMCP so save a weak ref
duke@435 2526 methodOop old_method = (methodOop) old_methods->obj_at(i);
duke@435 2527 methodHandle old_method_h(old_method);
duke@435 2528 jweak method_ref = JNIHandles::make_weak_global(old_method_h);
duke@435 2529 method_refs->append(method_ref);
duke@435 2530 if (++local_count >= emcp_method_count) {
duke@435 2531 // no more EMCP methods so bail out now
duke@435 2532 break;
duke@435 2533 }
duke@435 2534 }
duke@435 2535 }
dcubed@482 2536 // non-shared ConstantPool gets a weak reference
dcubed@482 2537 pv_node = new PreviousVersionNode(cp_ref, !cp_h->is_shared(), method_refs);
duke@435 2538 }
duke@435 2539
duke@435 2540 _previous_versions->append(pv_node);
duke@435 2541
duke@435 2542 // Using weak references allows the interesting parts of previous
duke@435 2543 // classes to be GC'ed when they are no longer needed. Since the
duke@435 2544 // caller is the VMThread and we are at a safepoint, this is a good
duke@435 2545 // time to clear out unused weak references.
duke@435 2546
duke@435 2547 RC_TRACE(0x00000400, ("add: previous version length=%d",
duke@435 2548 _previous_versions->length()));
duke@435 2549
duke@435 2550 // skip the last entry since we just added it
duke@435 2551 for (int i = _previous_versions->length() - 2; i >= 0; i--) {
duke@435 2552 // check the previous versions array for a GC'ed weak refs
duke@435 2553 pv_node = _previous_versions->at(i);
duke@435 2554 cp_ref = pv_node->prev_constant_pool();
dcubed@482 2555 assert(cp_ref != NULL, "cp ref was unexpectedly cleared");
duke@435 2556 if (cp_ref == NULL) {
duke@435 2557 delete pv_node;
duke@435 2558 _previous_versions->remove_at(i);
duke@435 2559 // Since we are traversing the array backwards, we don't have to
duke@435 2560 // do anything special with the index.
duke@435 2561 continue; // robustness
duke@435 2562 }
duke@435 2563
duke@435 2564 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref);
duke@435 2565 if (cp == NULL) {
duke@435 2566 // this entry has been GC'ed so remove it
duke@435 2567 delete pv_node;
duke@435 2568 _previous_versions->remove_at(i);
duke@435 2569 // Since we are traversing the array backwards, we don't have to
duke@435 2570 // do anything special with the index.
duke@435 2571 continue;
duke@435 2572 } else {
duke@435 2573 RC_TRACE(0x00000400, ("add: previous version @%d is alive", i));
duke@435 2574 }
duke@435 2575
duke@435 2576 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods();
duke@435 2577 if (method_refs != NULL) {
duke@435 2578 RC_TRACE(0x00000400, ("add: previous methods length=%d",
duke@435 2579 method_refs->length()));
duke@435 2580 for (int j = method_refs->length() - 1; j >= 0; j--) {
duke@435 2581 jweak method_ref = method_refs->at(j);
duke@435 2582 assert(method_ref != NULL, "weak method ref was unexpectedly cleared");
duke@435 2583 if (method_ref == NULL) {
duke@435 2584 method_refs->remove_at(j);
duke@435 2585 // Since we are traversing the array backwards, we don't have to
duke@435 2586 // do anything special with the index.
duke@435 2587 continue; // robustness
duke@435 2588 }
duke@435 2589
duke@435 2590 methodOop method = (methodOop)JNIHandles::resolve(method_ref);
duke@435 2591 if (method == NULL || emcp_method_count == 0) {
duke@435 2592 // This method entry has been GC'ed or the current
duke@435 2593 // RedefineClasses() call has made all methods obsolete
duke@435 2594 // so remove it.
duke@435 2595 JNIHandles::destroy_weak_global(method_ref);
duke@435 2596 method_refs->remove_at(j);
duke@435 2597 } else {
duke@435 2598 // RC_TRACE macro has an embedded ResourceMark
duke@435 2599 RC_TRACE(0x00000400,
duke@435 2600 ("add: %s(%s): previous method @%d in version @%d is alive",
duke@435 2601 method->name()->as_C_string(), method->signature()->as_C_string(),
duke@435 2602 j, i));
duke@435 2603 }
duke@435 2604 }
duke@435 2605 }
duke@435 2606 }
duke@435 2607
duke@435 2608 int obsolete_method_count = old_methods->length() - emcp_method_count;
duke@435 2609
duke@435 2610 if (emcp_method_count != 0 && obsolete_method_count != 0 &&
duke@435 2611 _previous_versions->length() > 1) {
duke@435 2612 // We have a mix of obsolete and EMCP methods. If there is more
duke@435 2613 // than the previous version that we just added, then we have to
duke@435 2614 // clear out any matching EMCP method entries the hard way.
duke@435 2615 int local_count = 0;
duke@435 2616 for (int i = 0; i < old_methods->length(); i++) {
duke@435 2617 if (!emcp_methods->at(i)) {
duke@435 2618 // only obsolete methods are interesting
duke@435 2619 methodOop old_method = (methodOop) old_methods->obj_at(i);
coleenp@2497 2620 Symbol* m_name = old_method->name();
coleenp@2497 2621 Symbol* m_signature = old_method->signature();
duke@435 2622
duke@435 2623 // skip the last entry since we just added it
duke@435 2624 for (int j = _previous_versions->length() - 2; j >= 0; j--) {
duke@435 2625 // check the previous versions array for a GC'ed weak refs
duke@435 2626 pv_node = _previous_versions->at(j);
duke@435 2627 cp_ref = pv_node->prev_constant_pool();
dcubed@482 2628 assert(cp_ref != NULL, "cp ref was unexpectedly cleared");
duke@435 2629 if (cp_ref == NULL) {
duke@435 2630 delete pv_node;
duke@435 2631 _previous_versions->remove_at(j);
duke@435 2632 // Since we are traversing the array backwards, we don't have to
duke@435 2633 // do anything special with the index.
duke@435 2634 continue; // robustness
duke@435 2635 }
duke@435 2636
duke@435 2637 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref);
duke@435 2638 if (cp == NULL) {
duke@435 2639 // this entry has been GC'ed so remove it
duke@435 2640 delete pv_node;
duke@435 2641 _previous_versions->remove_at(j);
duke@435 2642 // Since we are traversing the array backwards, we don't have to
duke@435 2643 // do anything special with the index.
duke@435 2644 continue;
duke@435 2645 }
duke@435 2646
duke@435 2647 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods();
duke@435 2648 if (method_refs == NULL) {
duke@435 2649 // We have run into a PreviousVersion generation where
duke@435 2650 // all methods were made obsolete during that generation's
duke@435 2651 // RedefineClasses() operation. At the time of that
duke@435 2652 // operation, all EMCP methods were flushed so we don't
duke@435 2653 // have to go back any further.
duke@435 2654 //
duke@435 2655 // A NULL method_refs is different than an empty method_refs.
duke@435 2656 // We cannot infer any optimizations about older generations
duke@435 2657 // from an empty method_refs for the current generation.
duke@435 2658 break;
duke@435 2659 }
duke@435 2660
duke@435 2661 for (int k = method_refs->length() - 1; k >= 0; k--) {
duke@435 2662 jweak method_ref = method_refs->at(k);
duke@435 2663 assert(method_ref != NULL,
duke@435 2664 "weak method ref was unexpectedly cleared");
duke@435 2665 if (method_ref == NULL) {
duke@435 2666 method_refs->remove_at(k);
duke@435 2667 // Since we are traversing the array backwards, we don't
duke@435 2668 // have to do anything special with the index.
duke@435 2669 continue; // robustness
duke@435 2670 }
duke@435 2671
duke@435 2672 methodOop method = (methodOop)JNIHandles::resolve(method_ref);
duke@435 2673 if (method == NULL) {
duke@435 2674 // this method entry has been GC'ed so skip it
duke@435 2675 JNIHandles::destroy_weak_global(method_ref);
duke@435 2676 method_refs->remove_at(k);
duke@435 2677 continue;
duke@435 2678 }
duke@435 2679
duke@435 2680 if (method->name() == m_name &&
duke@435 2681 method->signature() == m_signature) {
duke@435 2682 // The current RedefineClasses() call has made all EMCP
duke@435 2683 // versions of this method obsolete so mark it as obsolete
duke@435 2684 // and remove the weak ref.
duke@435 2685 RC_TRACE(0x00000400,
duke@435 2686 ("add: %s(%s): flush obsolete method @%d in version @%d",
duke@435 2687 m_name->as_C_string(), m_signature->as_C_string(), k, j));
duke@435 2688
duke@435 2689 method->set_is_obsolete();
duke@435 2690 JNIHandles::destroy_weak_global(method_ref);
duke@435 2691 method_refs->remove_at(k);
duke@435 2692 break;
duke@435 2693 }
duke@435 2694 }
duke@435 2695
duke@435 2696 // The previous loop may not find a matching EMCP method, but
duke@435 2697 // that doesn't mean that we can optimize and not go any
duke@435 2698 // further back in the PreviousVersion generations. The EMCP
duke@435 2699 // method for this generation could have already been GC'ed,
duke@435 2700 // but there still may be an older EMCP method that has not
duke@435 2701 // been GC'ed.
duke@435 2702 }
duke@435 2703
duke@435 2704 if (++local_count >= obsolete_method_count) {
duke@435 2705 // no more obsolete methods so bail out now
duke@435 2706 break;
duke@435 2707 }
duke@435 2708 }
duke@435 2709 }
duke@435 2710 }
duke@435 2711 } // end add_previous_version()
duke@435 2712
duke@435 2713
duke@435 2714 // Determine if instanceKlass has a previous version.
duke@435 2715 bool instanceKlass::has_previous_version() const {
duke@435 2716 if (_previous_versions == NULL) {
duke@435 2717 // no previous versions array so answer is easy
duke@435 2718 return false;
duke@435 2719 }
duke@435 2720
duke@435 2721 for (int i = _previous_versions->length() - 1; i >= 0; i--) {
duke@435 2722 // Check the previous versions array for an info node that hasn't
duke@435 2723 // been GC'ed
duke@435 2724 PreviousVersionNode * pv_node = _previous_versions->at(i);
duke@435 2725
dcubed@482 2726 jobject cp_ref = pv_node->prev_constant_pool();
dcubed@482 2727 assert(cp_ref != NULL, "cp reference was unexpectedly cleared");
duke@435 2728 if (cp_ref == NULL) {
duke@435 2729 continue; // robustness
duke@435 2730 }
duke@435 2731
duke@435 2732 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref);
duke@435 2733 if (cp != NULL) {
duke@435 2734 // we have at least one previous version
duke@435 2735 return true;
duke@435 2736 }
duke@435 2737
duke@435 2738 // We don't have to check the method refs. If the constant pool has
duke@435 2739 // been GC'ed then so have the methods.
duke@435 2740 }
duke@435 2741
duke@435 2742 // all of the underlying nodes' info has been GC'ed
duke@435 2743 return false;
duke@435 2744 } // end has_previous_version()
duke@435 2745
duke@435 2746 methodOop instanceKlass::method_with_idnum(int idnum) {
duke@435 2747 methodOop m = NULL;
duke@435 2748 if (idnum < methods()->length()) {
duke@435 2749 m = (methodOop) methods()->obj_at(idnum);
duke@435 2750 }
duke@435 2751 if (m == NULL || m->method_idnum() != idnum) {
duke@435 2752 for (int index = 0; index < methods()->length(); ++index) {
duke@435 2753 m = (methodOop) methods()->obj_at(index);
duke@435 2754 if (m->method_idnum() == idnum) {
duke@435 2755 return m;
duke@435 2756 }
duke@435 2757 }
duke@435 2758 }
duke@435 2759 return m;
duke@435 2760 }
duke@435 2761
duke@435 2762
duke@435 2763 // Set the annotation at 'idnum' to 'anno'.
duke@435 2764 // We don't want to create or extend the array if 'anno' is NULL, since that is the
duke@435 2765 // default value. However, if the array exists and is long enough, we must set NULL values.
duke@435 2766 void instanceKlass::set_methods_annotations_of(int idnum, typeArrayOop anno, objArrayOop* md_p) {
duke@435 2767 objArrayOop md = *md_p;
duke@435 2768 if (md != NULL && md->length() > idnum) {
duke@435 2769 md->obj_at_put(idnum, anno);
duke@435 2770 } else if (anno != NULL) {
duke@435 2771 // create the array
duke@435 2772 int length = MAX2(idnum+1, (int)_idnum_allocated_count);
duke@435 2773 md = oopFactory::new_system_objArray(length, Thread::current());
duke@435 2774 if (*md_p != NULL) {
duke@435 2775 // copy the existing entries
duke@435 2776 for (int index = 0; index < (*md_p)->length(); index++) {
duke@435 2777 md->obj_at_put(index, (*md_p)->obj_at(index));
duke@435 2778 }
duke@435 2779 }
duke@435 2780 set_annotations(md, md_p);
duke@435 2781 md->obj_at_put(idnum, anno);
duke@435 2782 } // if no array and idnum isn't included there is nothing to do
duke@435 2783 }
duke@435 2784
duke@435 2785 // Construct a PreviousVersionNode entry for the array hung off
duke@435 2786 // the instanceKlass.
dcubed@482 2787 PreviousVersionNode::PreviousVersionNode(jobject prev_constant_pool,
dcubed@482 2788 bool prev_cp_is_weak, GrowableArray<jweak>* prev_EMCP_methods) {
duke@435 2789
duke@435 2790 _prev_constant_pool = prev_constant_pool;
dcubed@482 2791 _prev_cp_is_weak = prev_cp_is_weak;
duke@435 2792 _prev_EMCP_methods = prev_EMCP_methods;
duke@435 2793 }
duke@435 2794
duke@435 2795
duke@435 2796 // Destroy a PreviousVersionNode
duke@435 2797 PreviousVersionNode::~PreviousVersionNode() {
duke@435 2798 if (_prev_constant_pool != NULL) {
dcubed@482 2799 if (_prev_cp_is_weak) {
dcubed@482 2800 JNIHandles::destroy_weak_global(_prev_constant_pool);
dcubed@482 2801 } else {
dcubed@482 2802 JNIHandles::destroy_global(_prev_constant_pool);
dcubed@482 2803 }
duke@435 2804 }
duke@435 2805
duke@435 2806 if (_prev_EMCP_methods != NULL) {
duke@435 2807 for (int i = _prev_EMCP_methods->length() - 1; i >= 0; i--) {
duke@435 2808 jweak method_ref = _prev_EMCP_methods->at(i);
duke@435 2809 if (method_ref != NULL) {
duke@435 2810 JNIHandles::destroy_weak_global(method_ref);
duke@435 2811 }
duke@435 2812 }
duke@435 2813 delete _prev_EMCP_methods;
duke@435 2814 }
duke@435 2815 }
duke@435 2816
duke@435 2817
duke@435 2818 // Construct a PreviousVersionInfo entry
duke@435 2819 PreviousVersionInfo::PreviousVersionInfo(PreviousVersionNode *pv_node) {
duke@435 2820 _prev_constant_pool_handle = constantPoolHandle(); // NULL handle
duke@435 2821 _prev_EMCP_method_handles = NULL;
duke@435 2822
dcubed@482 2823 jobject cp_ref = pv_node->prev_constant_pool();
dcubed@482 2824 assert(cp_ref != NULL, "constant pool ref was unexpectedly cleared");
duke@435 2825 if (cp_ref == NULL) {
duke@435 2826 return; // robustness
duke@435 2827 }
duke@435 2828
duke@435 2829 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref);
duke@435 2830 if (cp == NULL) {
duke@435 2831 // Weak reference has been GC'ed. Since the constant pool has been
duke@435 2832 // GC'ed, the methods have also been GC'ed.
duke@435 2833 return;
duke@435 2834 }
duke@435 2835
duke@435 2836 // make the constantPoolOop safe to return
duke@435 2837 _prev_constant_pool_handle = constantPoolHandle(cp);
duke@435 2838
duke@435 2839 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods();
duke@435 2840 if (method_refs == NULL) {
duke@435 2841 // the instanceKlass did not have any EMCP methods
duke@435 2842 return;
duke@435 2843 }
duke@435 2844
duke@435 2845 _prev_EMCP_method_handles = new GrowableArray<methodHandle>(10);
duke@435 2846
duke@435 2847 int n_methods = method_refs->length();
duke@435 2848 for (int i = 0; i < n_methods; i++) {
duke@435 2849 jweak method_ref = method_refs->at(i);
duke@435 2850 assert(method_ref != NULL, "weak method ref was unexpectedly cleared");
duke@435 2851 if (method_ref == NULL) {
duke@435 2852 continue; // robustness
duke@435 2853 }
duke@435 2854
duke@435 2855 methodOop method = (methodOop)JNIHandles::resolve(method_ref);
duke@435 2856 if (method == NULL) {
duke@435 2857 // this entry has been GC'ed so skip it
duke@435 2858 continue;
duke@435 2859 }
duke@435 2860
duke@435 2861 // make the methodOop safe to return
duke@435 2862 _prev_EMCP_method_handles->append(methodHandle(method));
duke@435 2863 }
duke@435 2864 }
duke@435 2865
duke@435 2866
duke@435 2867 // Destroy a PreviousVersionInfo
duke@435 2868 PreviousVersionInfo::~PreviousVersionInfo() {
duke@435 2869 // Since _prev_EMCP_method_handles is not C-heap allocated, we
duke@435 2870 // don't have to delete it.
duke@435 2871 }
duke@435 2872
duke@435 2873
duke@435 2874 // Construct a helper for walking the previous versions array
duke@435 2875 PreviousVersionWalker::PreviousVersionWalker(instanceKlass *ik) {
duke@435 2876 _previous_versions = ik->previous_versions();
duke@435 2877 _current_index = 0;
duke@435 2878 // _hm needs no initialization
duke@435 2879 _current_p = NULL;
duke@435 2880 }
duke@435 2881
duke@435 2882
duke@435 2883 // Destroy a PreviousVersionWalker
duke@435 2884 PreviousVersionWalker::~PreviousVersionWalker() {
duke@435 2885 // Delete the current info just in case the caller didn't walk to
duke@435 2886 // the end of the previous versions list. No harm if _current_p is
duke@435 2887 // already NULL.
duke@435 2888 delete _current_p;
duke@435 2889
duke@435 2890 // When _hm is destroyed, all the Handles returned in
duke@435 2891 // PreviousVersionInfo objects will be destroyed.
duke@435 2892 // Also, after this destructor is finished it will be
duke@435 2893 // safe to delete the GrowableArray allocated in the
duke@435 2894 // PreviousVersionInfo objects.
duke@435 2895 }
duke@435 2896
duke@435 2897
duke@435 2898 // Return the interesting information for the next previous version
duke@435 2899 // of the klass. Returns NULL if there are no more previous versions.
duke@435 2900 PreviousVersionInfo* PreviousVersionWalker::next_previous_version() {
duke@435 2901 if (_previous_versions == NULL) {
duke@435 2902 // no previous versions so nothing to return
duke@435 2903 return NULL;
duke@435 2904 }
duke@435 2905
duke@435 2906 delete _current_p; // cleanup the previous info for the caller
duke@435 2907 _current_p = NULL; // reset to NULL so we don't delete same object twice
duke@435 2908
duke@435 2909 int length = _previous_versions->length();
duke@435 2910
duke@435 2911 while (_current_index < length) {
duke@435 2912 PreviousVersionNode * pv_node = _previous_versions->at(_current_index++);
duke@435 2913 PreviousVersionInfo * pv_info = new (ResourceObj::C_HEAP)
duke@435 2914 PreviousVersionInfo(pv_node);
duke@435 2915
duke@435 2916 constantPoolHandle cp_h = pv_info->prev_constant_pool_handle();
duke@435 2917 if (cp_h.is_null()) {
duke@435 2918 delete pv_info;
duke@435 2919
duke@435 2920 // The underlying node's info has been GC'ed so try the next one.
duke@435 2921 // We don't have to check the methods. If the constant pool has
duke@435 2922 // GC'ed then so have the methods.
duke@435 2923 continue;
duke@435 2924 }
duke@435 2925
duke@435 2926 // Found a node with non GC'ed info so return it. The caller will
duke@435 2927 // need to delete pv_info when they are done with it.
duke@435 2928 _current_p = pv_info;
duke@435 2929 return pv_info;
duke@435 2930 }
duke@435 2931
duke@435 2932 // all of the underlying nodes' info has been GC'ed
duke@435 2933 return NULL;
duke@435 2934 } // end next_previous_version()

mercurial