src/share/vm/oops/instanceKlass.cpp

Fri, 11 Mar 2011 22:34:57 -0800

author
jrose
date
Fri, 11 Mar 2011 22:34:57 -0800
changeset 2639
8033953d67ff
parent 2616
dbad0519a1c4
child 2658
c7f3d0b4570f
permissions
-rw-r--r--

7012648: move JSR 292 to package java.lang.invoke and adjust names
Summary: package and class renaming only; delete unused methods and classes
Reviewed-by: twisti

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

mercurial