src/share/vm/oops/instanceKlass.cpp

Fri, 27 Aug 2010 15:05:28 -0400

author
kamg
date
Fri, 27 Aug 2010 15:05:28 -0400
changeset 2106
2528b5bd749c
parent 2061
9d7a8ab3736b
child 2138
d5d065957597
permissions
-rw-r--r--

6980262: Memory leak when exception is thrown in static initializer
Summary: Use resource memory instead of c-heap for the exception message
Reviewed-by: phh, jmasa

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

mercurial