annotate hotspot/src/share/vm/oops/instanceKlass.cpp @ 1:489c9b5090e2

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