comparison src/share/vm/oops/instanceKlass.cpp @ 6948:e522a00b91aa

Merge with http://hg.openjdk.java.net/hsx/hsx25/hotspot/ after NPG - C++ build works
author Doug Simon <doug.simon@oracle.com>
date Mon, 12 Nov 2012 23:14:12 +0100
parents 957c266d8bc5 4735d2c84362
children 41938af2b3d8
comparison
equal deleted inserted replaced
6711:ae13cc658b80 6948:e522a00b91aa
32 #include "gc_interface/collectedHeap.inline.hpp" 32 #include "gc_interface/collectedHeap.inline.hpp"
33 #include "interpreter/oopMapCache.hpp" 33 #include "interpreter/oopMapCache.hpp"
34 #include "interpreter/rewriter.hpp" 34 #include "interpreter/rewriter.hpp"
35 #include "jvmtifiles/jvmti.h" 35 #include "jvmtifiles/jvmti.h"
36 #include "memory/genOopClosures.inline.hpp" 36 #include "memory/genOopClosures.inline.hpp"
37 #include "memory/metadataFactory.hpp"
37 #include "memory/oopFactory.hpp" 38 #include "memory/oopFactory.hpp"
38 #include "memory/permGen.hpp"
39 #include "oops/fieldStreams.hpp" 39 #include "oops/fieldStreams.hpp"
40 #include "oops/instanceClassLoaderKlass.hpp"
40 #include "oops/instanceKlass.hpp" 41 #include "oops/instanceKlass.hpp"
41 #include "oops/instanceMirrorKlass.hpp" 42 #include "oops/instanceMirrorKlass.hpp"
42 #include "oops/instanceOop.hpp" 43 #include "oops/instanceOop.hpp"
43 #include "oops/methodOop.hpp" 44 #include "oops/klass.inline.hpp"
44 #include "oops/objArrayKlassKlass.hpp" 45 #include "oops/method.hpp"
45 #include "oops/oop.inline.hpp" 46 #include "oops/oop.inline.hpp"
46 #include "oops/symbol.hpp" 47 #include "oops/symbol.hpp"
47 #include "prims/jvmtiExport.hpp" 48 #include "prims/jvmtiExport.hpp"
48 #include "prims/jvmtiRedefineClassesTrace.hpp" 49 #include "prims/jvmtiRedefineClassesTrace.hpp"
49 #include "runtime/fieldDescriptor.hpp" 50 #include "runtime/fieldDescriptor.hpp"
63 #endif 64 #endif
64 #ifdef TARGET_OS_FAMILY_bsd 65 #ifdef TARGET_OS_FAMILY_bsd
65 # include "thread_bsd.inline.hpp" 66 # include "thread_bsd.inline.hpp"
66 #endif 67 #endif
67 #ifndef SERIALGC 68 #ifndef SERIALGC
69 #include "gc_implementation/concurrentMarkSweep/cmsOopClosures.inline.hpp"
68 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" 70 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
69 #include "gc_implementation/g1/g1OopClosures.inline.hpp" 71 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
70 #include "gc_implementation/g1/g1RemSet.inline.hpp" 72 #include "gc_implementation/g1/g1RemSet.inline.hpp"
71 #include "gc_implementation/g1/heapRegionSeq.inline.hpp" 73 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
72 #include "gc_implementation/parNew/parOopClosures.inline.hpp" 74 #include "gc_implementation/parNew/parOopClosures.inline.hpp"
75 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.inline.hpp"
73 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp" 76 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
74 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp" 77 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
75 #include "oops/oop.pcgc.inline.hpp" 78 #include "oops/oop.pcgc.inline.hpp"
76 #endif 79 #endif
77 #ifdef COMPILER1 80 #ifdef COMPILER1
166 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type) 169 #define DTRACE_CLASSINIT_PROBE(type, clss, thread_type)
167 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait) 170 #define DTRACE_CLASSINIT_PROBE_WAIT(type, clss, thread_type, wait)
168 171
169 #endif // ndef DTRACE_ENABLED 172 #endif // ndef DTRACE_ENABLED
170 173
171 bool instanceKlass::should_be_initialized() const { 174 Klass* InstanceKlass::allocate_instance_klass(ClassLoaderData* loader_data,
175 int vtable_len,
176 int itable_len,
177 int static_field_size,
178 int nonstatic_oop_map_size,
179 ReferenceType rt,
180 AccessFlags access_flags,
181 Symbol* name,
182 Klass* super_klass,
183 KlassHandle host_klass,
184 TRAPS) {
185
186 int size = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size,
187 access_flags.is_interface(),
188 !host_klass.is_null());
189
190 // Allocation
191 InstanceKlass* ik;
192 if (rt == REF_NONE) {
193 if (name == vmSymbols::java_lang_Class()) {
194 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(
195 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
196 access_flags, !host_klass.is_null());
197 } else if (name == vmSymbols::java_lang_ClassLoader() ||
198 (SystemDictionary::ClassLoader_klass_loaded() &&
199 super_klass != NULL &&
200 super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass()))) {
201 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(
202 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
203 access_flags, !host_klass.is_null());
204 } else {
205 // normal class
206 ik = new (loader_data, size, THREAD) InstanceKlass(
207 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
208 access_flags, !host_klass.is_null());
209 }
210 } else {
211 // reference klass
212 ik = new (loader_data, size, THREAD) InstanceRefKlass(
213 vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt,
214 access_flags, !host_klass.is_null());
215 }
216
217 return ik;
218 }
219
220 InstanceKlass::InstanceKlass(int vtable_len,
221 int itable_len,
222 int static_field_size,
223 int nonstatic_oop_map_size,
224 ReferenceType rt,
225 AccessFlags access_flags,
226 bool is_anonymous) {
227 No_Safepoint_Verifier no_safepoint; // until k becomes parsable
228
229 int size = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size,
230 access_flags.is_interface(), is_anonymous);
231
232 // The sizes of these these three variables are used for determining the
233 // size of the instanceKlassOop. It is critical that these are set to the right
234 // sizes before the first GC, i.e., when we allocate the mirror.
235 this->set_vtable_length(vtable_len);
236 this->set_itable_length(itable_len);
237 this->set_static_field_size(static_field_size);
238 this->set_nonstatic_oop_map_size(nonstatic_oop_map_size);
239 this->set_access_flags(access_flags);
240 this->set_is_anonymous(is_anonymous);
241 assert(this->size() == size, "wrong size for object");
242
243 this->set_array_klasses(NULL);
244 this->set_methods(NULL);
245 this->set_method_ordering(NULL);
246 this->set_local_interfaces(NULL);
247 this->set_transitive_interfaces(NULL);
248 this->init_implementor();
249 this->set_fields(NULL, 0);
250 this->set_constants(NULL);
251 this->set_class_loader_data(NULL);
252 this->set_protection_domain(NULL);
253 this->set_signers(NULL);
254 this->set_source_file_name(NULL);
255 this->set_source_debug_extension(NULL, 0);
256 this->set_array_name(NULL);
257 this->set_inner_classes(NULL);
258 this->set_static_oop_field_count(0);
259 this->set_nonstatic_field_size(0);
260 this->set_is_marked_dependent(false);
261 this->set_init_state(InstanceKlass::allocated);
262 this->set_init_thread(NULL);
263 this->set_init_lock(NULL);
264 this->set_reference_type(rt);
265 this->set_oop_map_cache(NULL);
266 this->set_jni_ids(NULL);
267 this->set_osr_nmethods_head(NULL);
268 this->set_breakpoints(NULL);
269 this->init_previous_versions();
270 this->set_generic_signature(NULL);
271 this->release_set_methods_jmethod_ids(NULL);
272 this->release_set_methods_cached_itable_indices(NULL);
273 this->set_annotations(NULL);
274 this->set_jvmti_cached_class_field_map(NULL);
275 this->set_initial_method_idnum(0);
276
277 // initialize the non-header words to zero
278 intptr_t* p = (intptr_t*)this;
279 for (int index = InstanceKlass::header_size(); index < size; index++) {
280 p[index] = NULL_WORD;
281 }
282
283 // Set temporary value until parseClassFile updates it with the real instance
284 // size.
285 this->set_layout_helper(Klass::instance_layout_helper(0, true));
286 }
287
288
289 // This function deallocates the metadata and C heap pointers that the
290 // InstanceKlass points to.
291 void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
292
293 // Orphan the mirror first, CMS thinks it's still live.
294 java_lang_Class::set_klass(java_mirror(), NULL);
295
296 // Need to take this class off the class loader data list.
297 loader_data->remove_class(this);
298
299 // The array_klass for this class is created later, after error handling.
300 // For class redefinition, we keep the original class so this scratch class
301 // doesn't have an array class. Either way, assert that there is nothing
302 // to deallocate.
303 assert(array_klasses() == NULL, "array classes shouldn't be created for this class yet");
304
305 // Release C heap allocated data that this might point to, which includes
306 // reference counting symbol names.
307 release_C_heap_structures();
308
309 Array<Method*>* ms = methods();
310 if (ms != Universe::the_empty_method_array()) {
311 for (int i = 0; i <= methods()->length() -1 ; i++) {
312 Method* method = methods()->at(i);
313 // Only want to delete methods that are not executing for RedefineClasses.
314 // The previous version will point to them so they're not totally dangling
315 assert (!method->on_stack(), "shouldn't be called with methods on stack");
316 MetadataFactory::free_metadata(loader_data, method);
317 }
318 MetadataFactory::free_array<Method*>(loader_data, methods());
319 }
320 set_methods(NULL);
321
322 if (method_ordering() != Universe::the_empty_int_array()) {
323 MetadataFactory::free_array<int>(loader_data, method_ordering());
324 }
325 set_method_ordering(NULL);
326
327 // This array is in Klass, but remove it with the InstanceKlass since
328 // this place would be the only caller and it can share memory with transitive
329 // interfaces.
330 if (secondary_supers() != Universe::the_empty_klass_array() &&
331 secondary_supers() != transitive_interfaces()) {
332 MetadataFactory::free_array<Klass*>(loader_data, secondary_supers());
333 }
334 set_secondary_supers(NULL);
335
336 // Only deallocate transitive interfaces if not empty, same as super class
337 // or same as local interfaces. See code in parseClassFile.
338 Array<Klass*>* ti = transitive_interfaces();
339 if (ti != Universe::the_empty_klass_array() && ti != local_interfaces()) {
340 // check that the interfaces don't come from super class
341 Array<Klass*>* sti = (super() == NULL) ? NULL :
342 InstanceKlass::cast(super())->transitive_interfaces();
343 if (ti != sti) {
344 MetadataFactory::free_array<Klass*>(loader_data, ti);
345 }
346 }
347 set_transitive_interfaces(NULL);
348
349 // local interfaces can be empty
350 Array<Klass*>* li = local_interfaces();
351 if (li != Universe::the_empty_klass_array()) {
352 MetadataFactory::free_array<Klass*>(loader_data, li);
353 }
354 set_local_interfaces(NULL);
355
356 MetadataFactory::free_array<jushort>(loader_data, fields());
357 set_fields(NULL, 0);
358
359 // If a method from a redefined class is using this constant pool, don't
360 // delete it, yet. The new class's previous version will point to this.
361 assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
362 MetadataFactory::free_metadata(loader_data, constants());
363 set_constants(NULL);
364
365 if (inner_classes() != Universe::the_empty_short_array()) {
366 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
367 }
368 set_inner_classes(NULL);
369
370 // Null out Java heap objects, although these won't be walked to keep
371 // alive once this InstanceKlass is deallocated.
372 set_protection_domain(NULL);
373 set_signers(NULL);
374 set_init_lock(NULL);
375 set_annotations(NULL);
376 }
377
378 volatile oop InstanceKlass::init_lock() const {
379 volatile oop lock = _init_lock; // read once
380 assert((oop)lock != NULL || !is_not_initialized(), // initialized or in_error state
381 "only fully initialized state can have a null lock");
382 return lock;
383 }
384
385 // Set the initialization lock to null so the object can be GC'ed. Any racing
386 // threads to get this lock will see a null lock and will not lock.
387 // That's okay because they all check for initialized state after getting
388 // the lock and return.
389 void InstanceKlass::fence_and_clear_init_lock() {
390 // make sure previous stores are all done, notably the init_state.
391 OrderAccess::storestore();
392 klass_oop_store(&_init_lock, NULL);
393 assert(!is_not_initialized(), "class must be initialized now");
394 }
395
396
397 bool InstanceKlass::should_be_initialized() const {
172 return !is_initialized(); 398 return !is_initialized();
173 } 399 }
174 400
175 klassVtable* instanceKlass::vtable() const { 401 klassVtable* InstanceKlass::vtable() const {
176 return new klassVtable(as_klassOop(), start_of_vtable(), vtable_length() / vtableEntry::size()); 402 return new klassVtable(this, start_of_vtable(), vtable_length() / vtableEntry::size());
177 } 403 }
178 404
179 klassItable* instanceKlass::itable() const { 405 klassItable* InstanceKlass::itable() const {
180 return new klassItable(as_klassOop()); 406 return new klassItable(instanceKlassHandle(this));
181 } 407 }
182 408
183 void instanceKlass::eager_initialize(Thread *thread) { 409 void InstanceKlass::eager_initialize(Thread *thread) {
184 if (!EagerInitialization) return; 410 if (!EagerInitialization) return;
185 411
186 if (this->is_not_initialized()) { 412 if (this->is_not_initialized()) {
187 // abort if the the class has a class initializer 413 // abort if the the class has a class initializer
188 if (this->class_initializer() != NULL) return; 414 if (this->class_initializer() != NULL) return;
189 415
190 // abort if it is java.lang.Object (initialization is handled in genesis) 416 // abort if it is java.lang.Object (initialization is handled in genesis)
191 klassOop super = this->super(); 417 Klass* super = this->super();
192 if (super == NULL) return; 418 if (super == NULL) return;
193 419
194 // abort if the super class should be initialized 420 // abort if the super class should be initialized
195 if (!instanceKlass::cast(super)->is_initialized()) return; 421 if (!InstanceKlass::cast(super)->is_initialized()) return;
196 422
197 // call body to expose the this pointer 423 // call body to expose the this pointer
198 instanceKlassHandle this_oop(thread, this->as_klassOop()); 424 instanceKlassHandle this_oop(thread, this);
199 eager_initialize_impl(this_oop); 425 eager_initialize_impl(this_oop);
200 } 426 }
201 } 427 }
202 428
203 429
204 void instanceKlass::eager_initialize_impl(instanceKlassHandle this_oop) { 430 void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_oop) {
205 EXCEPTION_MARK; 431 EXCEPTION_MARK;
206 ObjectLocker ol(this_oop, THREAD); 432 volatile oop init_lock = this_oop->init_lock();
433 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
207 434
208 // abort if someone beat us to the initialization 435 // abort if someone beat us to the initialization
209 if (!this_oop->is_not_initialized()) return; // note: not equivalent to is_initialized() 436 if (!this_oop->is_not_initialized()) return; // note: not equivalent to is_initialized()
210 437
211 ClassState old_state = this_oop->init_state(); 438 ClassState old_state = this_oop->init_state();
220 if( old_state != this_oop->_init_state ) 447 if( old_state != this_oop->_init_state )
221 this_oop->set_init_state (old_state); 448 this_oop->set_init_state (old_state);
222 } else { 449 } else {
223 // linking successfull, mark class as initialized 450 // linking successfull, mark class as initialized
224 this_oop->set_init_state (fully_initialized); 451 this_oop->set_init_state (fully_initialized);
452 this_oop->fence_and_clear_init_lock();
225 // trace 453 // trace
226 if (TraceClassInitialization) { 454 if (TraceClassInitialization) {
227 ResourceMark rm(THREAD); 455 ResourceMark rm(THREAD);
228 tty->print_cr("[Initialized %s without side effects]", this_oop->external_name()); 456 tty->print_cr("[Initialized %s without side effects]", this_oop->external_name());
229 } 457 }
232 460
233 461
234 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization 462 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
235 // process. The step comments refers to the procedure described in that section. 463 // process. The step comments refers to the procedure described in that section.
236 // Note: implementation moved to static method to expose the this pointer. 464 // Note: implementation moved to static method to expose the this pointer.
237 void instanceKlass::initialize(TRAPS) { 465 void InstanceKlass::initialize(TRAPS) {
238 if (this->should_be_initialized()) { 466 if (this->should_be_initialized()) {
239 HandleMark hm(THREAD); 467 HandleMark hm(THREAD);
240 instanceKlassHandle this_oop(THREAD, this->as_klassOop()); 468 instanceKlassHandle this_oop(THREAD, this);
241 initialize_impl(this_oop, CHECK); 469 initialize_impl(this_oop, CHECK);
242 // Note: at this point the class may be initialized 470 // Note: at this point the class may be initialized
243 // OR it may be in the state of being initialized 471 // OR it may be in the state of being initialized
244 // in case of recursive initialization! 472 // in case of recursive initialization!
245 } else { 473 } else {
246 assert(is_initialized(), "sanity check"); 474 assert(is_initialized(), "sanity check");
247 } 475 }
248 } 476 }
249 477
250 478
251 bool instanceKlass::verify_code( 479 bool InstanceKlass::verify_code(
252 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) { 480 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) {
253 // 1) Verify the bytecodes 481 // 1) Verify the bytecodes
254 Verifier::Mode mode = 482 Verifier::Mode mode =
255 throw_verifyerror ? Verifier::ThrowException : Verifier::NoException; 483 throw_verifyerror ? Verifier::ThrowException : Verifier::NoException;
256 return Verifier::verify(this_oop, mode, this_oop->should_verify_class(), CHECK_false); 484 return Verifier::verify(this_oop, mode, this_oop->should_verify_class(), CHECK_false);
258 486
259 487
260 // Used exclusively by the shared spaces dump mechanism to prevent 488 // Used exclusively by the shared spaces dump mechanism to prevent
261 // classes mapped into the shared regions in new VMs from appearing linked. 489 // classes mapped into the shared regions in new VMs from appearing linked.
262 490
263 void instanceKlass::unlink_class() { 491 void InstanceKlass::unlink_class() {
264 assert(is_linked(), "must be linked"); 492 assert(is_linked(), "must be linked");
265 _init_state = loaded; 493 _init_state = loaded;
266 } 494 }
267 495
268 void instanceKlass::link_class(TRAPS) { 496 void InstanceKlass::link_class(TRAPS) {
269 assert(is_loaded(), "must be loaded"); 497 assert(is_loaded(), "must be loaded");
270 if (!is_linked()) { 498 if (!is_linked()) {
271 instanceKlassHandle this_oop(THREAD, this->as_klassOop()); 499 HandleMark hm(THREAD);
500 instanceKlassHandle this_oop(THREAD, this);
272 link_class_impl(this_oop, true, CHECK); 501 link_class_impl(this_oop, true, CHECK);
273 } 502 }
274 } 503 }
275 504
276 // Called to verify that a class can link during initialization, without 505 // Called to verify that a class can link during initialization, without
277 // throwing a VerifyError. 506 // throwing a VerifyError.
278 bool instanceKlass::link_class_or_fail(TRAPS) { 507 bool InstanceKlass::link_class_or_fail(TRAPS) {
279 assert(is_loaded(), "must be loaded"); 508 assert(is_loaded(), "must be loaded");
280 if (!is_linked()) { 509 if (!is_linked()) {
281 instanceKlassHandle this_oop(THREAD, this->as_klassOop()); 510 HandleMark hm(THREAD);
511 instanceKlassHandle this_oop(THREAD, this);
282 link_class_impl(this_oop, false, CHECK_false); 512 link_class_impl(this_oop, false, CHECK_false);
283 } 513 }
284 return is_linked(); 514 return is_linked();
285 } 515 }
286 516
287 bool instanceKlass::link_class_impl( 517 bool InstanceKlass::link_class_impl(
288 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) { 518 instanceKlassHandle this_oop, bool throw_verifyerror, TRAPS) {
289 // check for error state 519 // check for error state
290 if (this_oop->is_in_error_state()) { 520 if (this_oop->is_in_error_state()) {
291 ResourceMark rm(THREAD); 521 ResourceMark rm(THREAD);
292 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(), 522 THROW_MSG_(vmSymbols::java_lang_NoClassDefFoundError(),
319 549
320 link_class_impl(super, throw_verifyerror, CHECK_false); 550 link_class_impl(super, throw_verifyerror, CHECK_false);
321 } 551 }
322 552
323 // link all interfaces implemented by this class before linking this class 553 // link all interfaces implemented by this class before linking this class
324 objArrayHandle interfaces (THREAD, this_oop->local_interfaces()); 554 Array<Klass*>* interfaces = this_oop->local_interfaces();
325 int num_interfaces = interfaces->length(); 555 int num_interfaces = interfaces->length();
326 for (int index = 0; index < num_interfaces; index++) { 556 for (int index = 0; index < num_interfaces; index++) {
327 HandleMark hm(THREAD); 557 HandleMark hm(THREAD);
328 instanceKlassHandle ih(THREAD, klassOop(interfaces->obj_at(index))); 558 instanceKlassHandle ih(THREAD, interfaces->at(index));
329 link_class_impl(ih, throw_verifyerror, CHECK_false); 559 link_class_impl(ih, throw_verifyerror, CHECK_false);
330 } 560 }
331 561
332 // in case the class is linked in the process of linking its superclasses 562 // in case the class is linked in the process of linking its superclasses
333 if (this_oop->is_linked()) { 563 if (this_oop->is_linked()) {
343 jt->get_thread_stat()->perf_timers_addr(), 573 jt->get_thread_stat()->perf_timers_addr(),
344 PerfClassTraceTime::CLASS_LINK); 574 PerfClassTraceTime::CLASS_LINK);
345 575
346 // verification & rewriting 576 // verification & rewriting
347 { 577 {
348 ObjectLocker ol(this_oop, THREAD); 578 volatile oop init_lock = this_oop->init_lock();
579 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
349 // rewritten will have been set if loader constraint error found 580 // rewritten will have been set if loader constraint error found
350 // on an earlier link attempt 581 // on an earlier link attempt
351 // don't verify or rewrite if already rewritten 582 // don't verify or rewrite if already rewritten
583
352 if (!this_oop->is_linked()) { 584 if (!this_oop->is_linked()) {
353 if (!this_oop->is_rewritten()) { 585 if (!this_oop->is_rewritten()) {
354 { 586 {
355 // Timer includes any side effects of class verification (resolution, 587 // Timer includes any side effects of class verification (resolution,
356 // etc), but not recursive entry into verify_code(). 588 // etc), but not recursive entry into verify_code().
380 // relocate jsrs and link methods after they are all rewritten 612 // relocate jsrs and link methods after they are all rewritten
381 this_oop->relocate_and_link_methods(CHECK_false); 613 this_oop->relocate_and_link_methods(CHECK_false);
382 614
383 // Initialize the vtable and interface table after 615 // Initialize the vtable and interface table after
384 // methods have been rewritten since rewrite may 616 // methods have been rewritten since rewrite may
385 // fabricate new methodOops. 617 // fabricate new Method*s.
386 // also does loader constraint checking 618 // also does loader constraint checking
387 if (!this_oop()->is_shared()) { 619 if (!this_oop()->is_shared()) {
388 ResourceMark rm(THREAD); 620 ResourceMark rm(THREAD);
389 this_oop->vtable()->initialize_vtable(true, CHECK_false); 621 this_oop->vtable()->initialize_vtable(true, CHECK_false);
390 this_oop->itable()->initialize_itable(true, CHECK_false); 622 this_oop->itable()->initialize_itable(true, CHECK_false);
410 642
411 643
412 // Rewrite the byte codes of all of the methods of a class. 644 // Rewrite the byte codes of all of the methods of a class.
413 // The rewriter must be called exactly once. Rewriting must happen after 645 // The rewriter must be called exactly once. Rewriting must happen after
414 // verification but before the first method of the class is executed. 646 // verification but before the first method of the class is executed.
415 void instanceKlass::rewrite_class(TRAPS) { 647 void InstanceKlass::rewrite_class(TRAPS) {
416 assert(is_loaded(), "must be loaded"); 648 assert(is_loaded(), "must be loaded");
417 instanceKlassHandle this_oop(THREAD, this->as_klassOop()); 649 instanceKlassHandle this_oop(THREAD, this);
418 if (this_oop->is_rewritten()) { 650 if (this_oop->is_rewritten()) {
419 assert(this_oop()->is_shared(), "rewriting an unshared class?"); 651 assert(this_oop()->is_shared(), "rewriting an unshared class?");
420 return; 652 return;
421 } 653 }
422 Rewriter::rewrite(this_oop, CHECK); 654 Rewriter::rewrite(this_oop, CHECK);
424 } 656 }
425 657
426 // Now relocate and link method entry points after class is rewritten. 658 // Now relocate and link method entry points after class is rewritten.
427 // This is outside is_rewritten flag. In case of an exception, it can be 659 // This is outside is_rewritten flag. In case of an exception, it can be
428 // executed more than once. 660 // executed more than once.
429 void instanceKlass::relocate_and_link_methods(TRAPS) { 661 void InstanceKlass::relocate_and_link_methods(TRAPS) {
430 assert(is_loaded(), "must be loaded"); 662 assert(is_loaded(), "must be loaded");
431 instanceKlassHandle this_oop(THREAD, this->as_klassOop()); 663 instanceKlassHandle this_oop(THREAD, this);
432 Rewriter::relocate_and_link(this_oop, CHECK); 664 Rewriter::relocate_and_link(this_oop, CHECK);
433 } 665 }
434 666
435 667
436 void instanceKlass::initialize_impl(instanceKlassHandle this_oop, TRAPS) { 668 void InstanceKlass::initialize_impl(instanceKlassHandle this_oop, TRAPS) {
437 // Make sure klass is linked (verified) before initialization 669 // Make sure klass is linked (verified) before initialization
438 // A class could already be verified, since it has been reflected upon. 670 // A class could already be verified, since it has been reflected upon.
439 this_oop->link_class(CHECK); 671 this_oop->link_class(CHECK);
440 672
441 DTRACE_CLASSINIT_PROBE(required, instanceKlass::cast(this_oop()), -1); 673 DTRACE_CLASSINIT_PROBE(required, InstanceKlass::cast(this_oop()), -1);
442 674
443 bool wait = false; 675 bool wait = false;
444 676
445 // refer to the JVM book page 47 for description of steps 677 // refer to the JVM book page 47 for description of steps
446 // Step 1 678 // Step 1
447 { ObjectLocker ol(this_oop, THREAD); 679 {
680 volatile oop init_lock = this_oop->init_lock();
681 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
448 682
449 Thread *self = THREAD; // it's passed the current thread 683 Thread *self = THREAD; // it's passed the current thread
450 684
451 // Step 2 685 // Step 2
452 // If we were to use wait() instead of waitInterruptibly() then 686 // If we were to use wait() instead of waitInterruptibly() then
457 ol.waitUninterruptibly(CHECK); 691 ol.waitUninterruptibly(CHECK);
458 } 692 }
459 693
460 // Step 3 694 // Step 3
461 if (this_oop->is_being_initialized() && this_oop->is_reentrant_initialization(self)) { 695 if (this_oop->is_being_initialized() && this_oop->is_reentrant_initialization(self)) {
462 DTRACE_CLASSINIT_PROBE_WAIT(recursive, instanceKlass::cast(this_oop()), -1,wait); 696 DTRACE_CLASSINIT_PROBE_WAIT(recursive, InstanceKlass::cast(this_oop()), -1,wait);
463 return; 697 return;
464 } 698 }
465 699
466 // Step 4 700 // Step 4
467 if (this_oop->is_initialized()) { 701 if (this_oop->is_initialized()) {
468 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, instanceKlass::cast(this_oop()), -1,wait); 702 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, InstanceKlass::cast(this_oop()), -1,wait);
469 return; 703 return;
470 } 704 }
471 705
472 // Step 5 706 // Step 5
473 if (this_oop->is_in_error_state()) { 707 if (this_oop->is_in_error_state()) {
474 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, instanceKlass::cast(this_oop()), -1,wait); 708 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, InstanceKlass::cast(this_oop()), -1,wait);
475 ResourceMark rm(THREAD); 709 ResourceMark rm(THREAD);
476 const char* desc = "Could not initialize class "; 710 const char* desc = "Could not initialize class ";
477 const char* className = this_oop->external_name(); 711 const char* className = this_oop->external_name();
478 size_t msglen = strlen(desc) + strlen(className) + 1; 712 size_t msglen = strlen(desc) + strlen(className) + 1;
479 char* message = NEW_RESOURCE_ARRAY(char, msglen); 713 char* message = NEW_RESOURCE_ARRAY(char, msglen);
490 this_oop->set_init_state(being_initialized); 724 this_oop->set_init_state(being_initialized);
491 this_oop->set_init_thread(self); 725 this_oop->set_init_thread(self);
492 } 726 }
493 727
494 // Step 7 728 // Step 7
495 klassOop super_klass = this_oop->super(); 729 Klass* super_klass = this_oop->super();
496 if (super_klass != NULL && !this_oop->is_interface() && Klass::cast(super_klass)->should_be_initialized()) { 730 if (super_klass != NULL && !this_oop->is_interface() && Klass::cast(super_klass)->should_be_initialized()) {
497 Klass::cast(super_klass)->initialize(THREAD); 731 Klass::cast(super_klass)->initialize(THREAD);
498 732
499 if (HAS_PENDING_EXCEPTION) { 733 if (HAS_PENDING_EXCEPTION) {
500 Handle e(THREAD, PENDING_EXCEPTION); 734 Handle e(THREAD, PENDING_EXCEPTION);
502 { 736 {
503 EXCEPTION_MARK; 737 EXCEPTION_MARK;
504 this_oop->set_initialization_state_and_notify(initialization_error, THREAD); // Locks object, set state, and notify all waiting threads 738 this_oop->set_initialization_state_and_notify(initialization_error, THREAD); // Locks object, set state, and notify all waiting threads
505 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, superclass initialization error is thrown below 739 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, superclass initialization error is thrown below
506 } 740 }
507 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, instanceKlass::cast(this_oop()), -1,wait); 741 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, InstanceKlass::cast(this_oop()), -1,wait);
508 THROW_OOP(e()); 742 THROW_OOP(e());
743 }
744 }
745
746 if (this_oop->has_default_methods()) {
747 // Step 7.5: initialize any interfaces which have default methods
748 for (int i = 0; i < this_oop->local_interfaces()->length(); ++i) {
749 Klass* iface = this_oop->local_interfaces()->at(i);
750 InstanceKlass* ik = InstanceKlass::cast(iface);
751 if (ik->has_default_methods() && ik->should_be_initialized()) {
752 ik->initialize(THREAD);
753
754 if (HAS_PENDING_EXCEPTION) {
755 Handle e(THREAD, PENDING_EXCEPTION);
756 CLEAR_PENDING_EXCEPTION;
757 {
758 EXCEPTION_MARK;
759 // Locks object, set state, and notify all waiting threads
760 this_oop->set_initialization_state_and_notify(
761 initialization_error, THREAD);
762
763 // ignore any exception thrown, superclass initialization error is
764 // thrown below
765 CLEAR_PENDING_EXCEPTION;
766 }
767 DTRACE_CLASSINIT_PROBE_WAIT(
768 super__failed, InstanceKlass::cast(this_oop()), -1, wait);
769 THROW_OOP(e());
770 }
771 }
509 } 772 }
510 } 773 }
511 774
512 // Step 8 775 // Step 8
513 { 776 {
514 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl"); 777 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
515 JavaThread* jt = (JavaThread*)THREAD; 778 JavaThread* jt = (JavaThread*)THREAD;
516 DTRACE_CLASSINIT_PROBE_WAIT(clinit, instanceKlass::cast(this_oop()), -1,wait); 779 DTRACE_CLASSINIT_PROBE_WAIT(clinit, InstanceKlass::cast(this_oop()), -1,wait);
517 // Timer includes any side effects of class initialization (resolution, 780 // Timer includes any side effects of class initialization (resolution,
518 // etc), but not recursive entry into call_class_initializer(). 781 // etc), but not recursive entry into call_class_initializer().
519 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(), 782 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
520 ClassLoader::perf_class_init_selftime(), 783 ClassLoader::perf_class_init_selftime(),
521 ClassLoader::perf_classes_inited(), 784 ClassLoader::perf_classes_inited(),
539 { 802 {
540 EXCEPTION_MARK; 803 EXCEPTION_MARK;
541 this_oop->set_initialization_state_and_notify(initialization_error, THREAD); 804 this_oop->set_initialization_state_and_notify(initialization_error, THREAD);
542 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below 805 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
543 } 806 }
544 DTRACE_CLASSINIT_PROBE_WAIT(error, instanceKlass::cast(this_oop()), -1,wait); 807 DTRACE_CLASSINIT_PROBE_WAIT(error, InstanceKlass::cast(this_oop()), -1,wait);
545 if (e->is_a(SystemDictionary::Error_klass())) { 808 if (e->is_a(SystemDictionary::Error_klass())) {
546 THROW_OOP(e()); 809 THROW_OOP(e());
547 } else { 810 } else {
548 JavaCallArguments args(e); 811 JavaCallArguments args(e);
549 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(), 812 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
550 vmSymbols::throwable_void_signature(), 813 vmSymbols::throwable_void_signature(),
551 &args); 814 &args);
552 } 815 }
553 } 816 }
554 DTRACE_CLASSINIT_PROBE_WAIT(end, instanceKlass::cast(this_oop()), -1,wait); 817 DTRACE_CLASSINIT_PROBE_WAIT(end, InstanceKlass::cast(this_oop()), -1,wait);
555 } 818 }
556 819
557 820
558 // Note: implementation moved to static method to expose the this pointer. 821 // Note: implementation moved to static method to expose the this pointer.
559 void instanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) { 822 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
560 instanceKlassHandle kh(THREAD, this->as_klassOop()); 823 instanceKlassHandle kh(THREAD, this);
561 set_initialization_state_and_notify_impl(kh, state, CHECK); 824 set_initialization_state_and_notify_impl(kh, state, CHECK);
562 } 825 }
563 826
564 void instanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_oop, ClassState state, TRAPS) { 827 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_oop, ClassState state, TRAPS) {
565 ObjectLocker ol(this_oop, THREAD); 828 volatile oop init_lock = this_oop->init_lock();
829 ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
566 this_oop->set_init_state(state); 830 this_oop->set_init_state(state);
831 this_oop->fence_and_clear_init_lock();
567 ol.notify_all(CHECK); 832 ol.notify_all(CHECK);
568 } 833 }
569 834
570 // The embedded _implementor field can only record one implementor. 835 // The embedded _implementor field can only record one implementor.
571 // When there are more than one implementors, the _implementor field 836 // When there are more than one implementors, the _implementor field
572 // is set to the interface klassOop itself. Following are the possible 837 // is set to the interface Klass* itself. Following are the possible
573 // values for the _implementor field: 838 // values for the _implementor field:
574 // NULL - no implementor 839 // NULL - no implementor
575 // implementor klassOop - one implementor 840 // implementor Klass* - one implementor
576 // self - more than one implementor 841 // self - more than one implementor
577 // 842 //
578 // The _implementor field only exists for interfaces. 843 // The _implementor field only exists for interfaces.
579 void instanceKlass::add_implementor(klassOop k) { 844 void InstanceKlass::add_implementor(Klass* k) {
580 assert(Compile_lock->owned_by_self(), ""); 845 assert(Compile_lock->owned_by_self(), "");
581 assert(is_interface(), "not interface"); 846 assert(is_interface(), "not interface");
582 // Filter out my subinterfaces. 847 // Filter out my subinterfaces.
583 // (Note: Interfaces are never on the subklass list.) 848 // (Note: Interfaces are never on the subklass list.)
584 if (instanceKlass::cast(k)->is_interface()) return; 849 if (InstanceKlass::cast(k)->is_interface()) return;
585 850
586 // Filter out subclasses whose supers already implement me. 851 // Filter out subclasses whose supers already implement me.
587 // (Note: CHA must walk subclasses of direct implementors 852 // (Note: CHA must walk subclasses of direct implementors
588 // in order to locate indirect implementors.) 853 // in order to locate indirect implementors.)
589 klassOop sk = instanceKlass::cast(k)->super(); 854 Klass* sk = InstanceKlass::cast(k)->super();
590 if (sk != NULL && instanceKlass::cast(sk)->implements_interface(as_klassOop())) 855 if (sk != NULL && InstanceKlass::cast(sk)->implements_interface(this))
591 // We only need to check one immediate superclass, since the 856 // We only need to check one immediate superclass, since the
592 // implements_interface query looks at transitive_interfaces. 857 // implements_interface query looks at transitive_interfaces.
593 // Any supers of the super have the same (or fewer) transitive_interfaces. 858 // Any supers of the super have the same (or fewer) transitive_interfaces.
594 return; 859 return;
595 860
596 klassOop ik = implementor(); 861 Klass* ik = implementor();
597 if (ik == NULL) { 862 if (ik == NULL) {
598 set_implementor(k); 863 set_implementor(k);
599 } else if (ik != this->as_klassOop()) { 864 } else if (ik != this) {
600 // There is already an implementor. Use itself as an indicator of 865 // There is already an implementor. Use itself as an indicator of
601 // more than one implementors. 866 // more than one implementors.
602 set_implementor(this->as_klassOop()); 867 set_implementor(this);
603 } 868 }
604 869
605 // The implementor also implements the transitive_interfaces 870 // The implementor also implements the transitive_interfaces
606 for (int index = 0; index < local_interfaces()->length(); index++) { 871 for (int index = 0; index < local_interfaces()->length(); index++) {
607 instanceKlass::cast(klassOop(local_interfaces()->obj_at(index)))->add_implementor(k); 872 InstanceKlass::cast(local_interfaces()->at(index))->add_implementor(k);
608 } 873 }
609 } 874 }
610 875
611 void instanceKlass::init_implementor() { 876 void InstanceKlass::init_implementor() {
612 if (is_interface()) { 877 if (is_interface()) {
613 set_implementor(NULL); 878 set_implementor(NULL);
614 } 879 }
615 } 880 }
616 881
617 882
618 void instanceKlass::process_interfaces(Thread *thread) { 883 void InstanceKlass::process_interfaces(Thread *thread) {
619 // link this class into the implementors list of every interface it implements 884 // link this class into the implementors list of every interface it implements
620 KlassHandle this_as_oop (thread, this->as_klassOop()); 885 Klass* this_as_klass_oop = this;
621 for (int i = local_interfaces()->length() - 1; i >= 0; i--) { 886 for (int i = local_interfaces()->length() - 1; i >= 0; i--) {
622 assert(local_interfaces()->obj_at(i)->is_klass(), "must be a klass"); 887 assert(local_interfaces()->at(i)->is_klass(), "must be a klass");
623 instanceKlass* interf = instanceKlass::cast(klassOop(local_interfaces()->obj_at(i))); 888 InstanceKlass* interf = InstanceKlass::cast(local_interfaces()->at(i));
624 assert(interf->is_interface(), "expected interface"); 889 assert(interf->is_interface(), "expected interface");
625 interf->add_implementor(this_as_oop()); 890 interf->add_implementor(this_as_klass_oop);
626 } 891 }
627 } 892 }
628 893
629 bool instanceKlass::can_be_primary_super_slow() const { 894 bool InstanceKlass::can_be_primary_super_slow() const {
630 if (is_interface()) 895 if (is_interface())
631 return false; 896 return false;
632 else 897 else
633 return Klass::can_be_primary_super_slow(); 898 return Klass::can_be_primary_super_slow();
634 } 899 }
635 900
636 objArrayOop instanceKlass::compute_secondary_supers(int num_extra_slots, TRAPS) { 901 GrowableArray<Klass*>* InstanceKlass::compute_secondary_supers(int num_extra_slots) {
637 // The secondaries are the implemented interfaces. 902 // The secondaries are the implemented interfaces.
638 instanceKlass* ik = instanceKlass::cast(as_klassOop()); 903 InstanceKlass* ik = InstanceKlass::cast(this);
639 objArrayHandle interfaces (THREAD, ik->transitive_interfaces()); 904 Array<Klass*>* interfaces = ik->transitive_interfaces();
640 int num_secondaries = num_extra_slots + interfaces->length(); 905 int num_secondaries = num_extra_slots + interfaces->length();
641 if (num_secondaries == 0) { 906 if (num_secondaries == 0) {
642 return Universe::the_empty_system_obj_array(); 907 // Must share this for correct bootstrapping!
908 set_secondary_supers(Universe::the_empty_klass_array());
909 return NULL;
643 } else if (num_extra_slots == 0) { 910 } else if (num_extra_slots == 0) {
644 return interfaces(); 911 // The secondary super list is exactly the same as the transitive interfaces.
912 // Redefine classes has to be careful not to delete this!
913 set_secondary_supers(interfaces);
914 return NULL;
645 } else { 915 } else {
646 // a mix of both 916 // Copy transitive interfaces to a temporary growable array to be constructed
647 objArrayOop secondaries = oopFactory::new_system_objArray(num_secondaries, CHECK_NULL); 917 // into the secondary super list with extra slots.
918 GrowableArray<Klass*>* secondaries = new GrowableArray<Klass*>(interfaces->length());
648 for (int i = 0; i < interfaces->length(); i++) { 919 for (int i = 0; i < interfaces->length(); i++) {
649 secondaries->obj_at_put(num_extra_slots+i, interfaces->obj_at(i)); 920 secondaries->push(interfaces->at(i));
650 } 921 }
651 return secondaries; 922 return secondaries;
652 } 923 }
653 } 924 }
654 925
655 bool instanceKlass::compute_is_subtype_of(klassOop k) { 926 bool InstanceKlass::compute_is_subtype_of(Klass* k) {
656 if (Klass::cast(k)->is_interface()) { 927 if (Klass::cast(k)->is_interface()) {
657 return implements_interface(k); 928 return implements_interface(k);
658 } else { 929 } else {
659 return Klass::compute_is_subtype_of(k); 930 return Klass::compute_is_subtype_of(k);
660 } 931 }
661 } 932 }
662 933
663 bool instanceKlass::implements_interface(klassOop k) const { 934 bool InstanceKlass::implements_interface(Klass* k) const {
664 if (as_klassOop() == k) return true; 935 if (this == k) return true;
665 assert(Klass::cast(k)->is_interface(), "should be an interface class"); 936 assert(Klass::cast(k)->is_interface(), "should be an interface class");
666 for (int i = 0; i < transitive_interfaces()->length(); i++) { 937 for (int i = 0; i < transitive_interfaces()->length(); i++) {
667 if (transitive_interfaces()->obj_at(i) == k) { 938 if (transitive_interfaces()->at(i) == k) {
668 return true; 939 return true;
669 } 940 }
670 } 941 }
671 return false; 942 return false;
672 } 943 }
673 944
674 objArrayOop instanceKlass::allocate_objArray(int n, int length, TRAPS) { 945 objArrayOop InstanceKlass::allocate_objArray(int n, int length, TRAPS) {
675 if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException()); 946 if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
676 if (length > arrayOopDesc::max_array_length(T_OBJECT)) { 947 if (length > arrayOopDesc::max_array_length(T_OBJECT)) {
677 report_java_out_of_memory("Requested array size exceeds VM limit"); 948 report_java_out_of_memory("Requested array size exceeds VM limit");
678 JvmtiExport::post_array_size_exhausted(); 949 JvmtiExport::post_array_size_exhausted();
679 THROW_OOP_0(Universe::out_of_memory_error_array_size()); 950 THROW_OOP_0(Universe::out_of_memory_error_array_size());
680 } 951 }
681 int size = objArrayOopDesc::object_size(length); 952 int size = objArrayOopDesc::object_size(length);
682 klassOop ak = array_klass(n, CHECK_NULL); 953 Klass* ak = array_klass(n, CHECK_NULL);
683 KlassHandle h_ak (THREAD, ak); 954 KlassHandle h_ak (THREAD, ak);
684 objArrayOop o = 955 objArrayOop o =
685 (objArrayOop)CollectedHeap::array_allocate(h_ak, size, length, CHECK_NULL); 956 (objArrayOop)CollectedHeap::array_allocate(h_ak, size, length, CHECK_NULL);
686 return o; 957 return o;
687 } 958 }
688 959
689 instanceOop instanceKlass::register_finalizer(instanceOop i, TRAPS) { 960 instanceOop InstanceKlass::register_finalizer(instanceOop i, TRAPS) {
690 if (TraceFinalizerRegistration) { 961 if (TraceFinalizerRegistration) {
691 tty->print("Registered "); 962 tty->print("Registered ");
692 i->print_value_on(tty); 963 i->print_value_on(tty);
693 tty->print_cr(" (" INTPTR_FORMAT ") as finalizable", (address)i); 964 tty->print_cr(" (" INTPTR_FORMAT ") as finalizable", (address)i);
694 } 965 }
699 methodHandle mh (THREAD, Universe::finalizer_register_method()); 970 methodHandle mh (THREAD, Universe::finalizer_register_method());
700 JavaCalls::call(&result, mh, &args, CHECK_NULL); 971 JavaCalls::call(&result, mh, &args, CHECK_NULL);
701 return h_i(); 972 return h_i();
702 } 973 }
703 974
704 instanceOop instanceKlass::allocate_instance(TRAPS) { 975 instanceOop InstanceKlass::allocate_instance(TRAPS) {
705 assert(!oop_is_instanceMirror(), "wrong allocation path");
706 bool has_finalizer_flag = has_finalizer(); // Query before possible GC 976 bool has_finalizer_flag = has_finalizer(); // Query before possible GC
707 int size = size_helper(); // Query before forming handle. 977 int size = size_helper(); // Query before forming handle.
708 978
709 KlassHandle h_k(THREAD, as_klassOop()); 979 KlassHandle h_k(THREAD, this);
710 980
711 instanceOop i; 981 instanceOop i;
712 982
713 i = (instanceOop)CollectedHeap::obj_allocate(h_k, size, CHECK_NULL); 983 i = (instanceOop)CollectedHeap::obj_allocate(h_k, size, CHECK_NULL);
714 if (has_finalizer_flag && !RegisterFinalizersAtInit) { 984 if (has_finalizer_flag && !RegisterFinalizersAtInit) {
715 i = register_finalizer(i, CHECK_NULL); 985 i = register_finalizer(i, CHECK_NULL);
716 } 986 }
717 return i; 987 return i;
718 } 988 }
719 989
720 instanceOop instanceKlass::allocate_permanent_instance(TRAPS) { 990 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
721 // Finalizer registration occurs in the Object.<init> constructor
722 // and constructors normally aren't run when allocating perm
723 // instances so simply disallow finalizable perm objects. This can
724 // be relaxed if a need for it is found.
725 assert(!has_finalizer(), "perm objects not allowed to have finalizers");
726 assert(!oop_is_instanceMirror(), "wrong allocation path");
727 int size = size_helper(); // Query before forming handle.
728 KlassHandle h_k(THREAD, as_klassOop());
729 instanceOop i = (instanceOop)
730 CollectedHeap::permanent_obj_allocate(h_k, size, CHECK_NULL);
731 return i;
732 }
733
734 void instanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
735 if (is_interface() || is_abstract()) { 991 if (is_interface() || is_abstract()) {
736 ResourceMark rm(THREAD); 992 ResourceMark rm(THREAD);
737 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError() 993 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
738 : vmSymbols::java_lang_InstantiationException(), external_name()); 994 : vmSymbols::java_lang_InstantiationException(), external_name());
739 } 995 }
740 if (as_klassOop() == SystemDictionary::Class_klass()) { 996 if (this == SystemDictionary::Class_klass()) {
741 ResourceMark rm(THREAD); 997 ResourceMark rm(THREAD);
742 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError() 998 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
743 : vmSymbols::java_lang_IllegalAccessException(), external_name()); 999 : vmSymbols::java_lang_IllegalAccessException(), external_name());
744 } 1000 }
745 } 1001 }
746 1002
747 klassOop instanceKlass::array_klass_impl(bool or_null, int n, TRAPS) { 1003 Klass* InstanceKlass::array_klass_impl(bool or_null, int n, TRAPS) {
748 instanceKlassHandle this_oop(THREAD, as_klassOop()); 1004 instanceKlassHandle this_oop(THREAD, this);
749 return array_klass_impl(this_oop, or_null, n, THREAD); 1005 return array_klass_impl(this_oop, or_null, n, THREAD);
750 } 1006 }
751 1007
752 klassOop instanceKlass::array_klass_impl(instanceKlassHandle this_oop, bool or_null, int n, TRAPS) { 1008 Klass* InstanceKlass::array_klass_impl(instanceKlassHandle this_oop, bool or_null, int n, TRAPS) {
753 if (this_oop->array_klasses() == NULL) { 1009 if (this_oop->array_klasses() == NULL) {
754 if (or_null) return NULL; 1010 if (or_null) return NULL;
755 1011
756 ResourceMark rm; 1012 ResourceMark rm;
757 JavaThread *jt = (JavaThread *)THREAD; 1013 JavaThread *jt = (JavaThread *)THREAD;
760 MutexLocker mc(Compile_lock, THREAD); // for vtables 1016 MutexLocker mc(Compile_lock, THREAD); // for vtables
761 MutexLocker ma(MultiArray_lock, THREAD); 1017 MutexLocker ma(MultiArray_lock, THREAD);
762 1018
763 // Check if update has already taken place 1019 // Check if update has already taken place
764 if (this_oop->array_klasses() == NULL) { 1020 if (this_oop->array_klasses() == NULL) {
765 objArrayKlassKlass* oakk = 1021 Klass* k = ObjArrayKlass::allocate_objArray_klass(this_oop->class_loader_data(), 1, this_oop, CHECK_NULL);
766 (objArrayKlassKlass*)Universe::objArrayKlassKlassObj()->klass_part();
767
768 klassOop k = oakk->allocate_objArray_klass(1, this_oop, CHECK_NULL);
769 this_oop->set_array_klasses(k); 1022 this_oop->set_array_klasses(k);
770 } 1023 }
771 } 1024 }
772 } 1025 }
773 // _this will always be set at this point 1026 // _this will always be set at this point
774 objArrayKlass* oak = (objArrayKlass*)this_oop->array_klasses()->klass_part(); 1027 ObjArrayKlass* oak = (ObjArrayKlass*)this_oop->array_klasses();
775 if (or_null) { 1028 if (or_null) {
776 return oak->array_klass_or_null(n); 1029 return oak->array_klass_or_null(n);
777 } 1030 }
778 return oak->array_klass(n, CHECK_NULL); 1031 return oak->array_klass(n, CHECK_NULL);
779 } 1032 }
780 1033
781 klassOop instanceKlass::array_klass_impl(bool or_null, TRAPS) { 1034 Klass* InstanceKlass::array_klass_impl(bool or_null, TRAPS) {
782 return array_klass_impl(or_null, 1, THREAD); 1035 return array_klass_impl(or_null, 1, THREAD);
783 } 1036 }
784 1037
785 void instanceKlass::call_class_initializer(TRAPS) { 1038 void InstanceKlass::call_class_initializer(TRAPS) {
786 instanceKlassHandle ik (THREAD, as_klassOop()); 1039 instanceKlassHandle ik (THREAD, this);
787 call_class_initializer_impl(ik, THREAD); 1040 call_class_initializer_impl(ik, THREAD);
788 } 1041 }
789 1042
790 static int call_class_initializer_impl_counter = 0; // for debugging 1043 static int call_class_initializer_impl_counter = 0; // for debugging
791 1044
792 methodOop instanceKlass::class_initializer() { 1045 Method* InstanceKlass::class_initializer() {
793 methodOop clinit = find_method( 1046 Method* clinit = find_method(
794 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature()); 1047 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
795 if (clinit != NULL && clinit->has_valid_initializer_flags()) { 1048 if (clinit != NULL && clinit->has_valid_initializer_flags()) {
796 return clinit; 1049 return clinit;
797 } 1050 }
798 return NULL; 1051 return NULL;
799 } 1052 }
800 1053
801 void instanceKlass::call_class_initializer_impl(instanceKlassHandle this_oop, TRAPS) { 1054 void InstanceKlass::call_class_initializer_impl(instanceKlassHandle this_oop, TRAPS) {
802 methodHandle h_method(THREAD, this_oop->class_initializer()); 1055 methodHandle h_method(THREAD, this_oop->class_initializer());
803 assert(!this_oop->is_initialized(), "we cannot initialize twice"); 1056 assert(!this_oop->is_initialized(), "we cannot initialize twice");
804 if (TraceClassInitialization) { 1057 if (TraceClassInitialization) {
805 tty->print("%d Initializing ", call_class_initializer_impl_counter++); 1058 tty->print("%d Initializing ", call_class_initializer_impl_counter++);
806 this_oop->name()->print_value(); 1059 this_oop->name()->print_value();
812 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args) 1065 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
813 } 1066 }
814 } 1067 }
815 1068
816 1069
817 void instanceKlass::mask_for(methodHandle method, int bci, 1070 void InstanceKlass::mask_for(methodHandle method, int bci,
818 InterpreterOopMap* entry_for) { 1071 InterpreterOopMap* entry_for) {
819 // Dirty read, then double-check under a lock. 1072 // Dirty read, then double-check under a lock.
820 if (_oop_map_cache == NULL) { 1073 if (_oop_map_cache == NULL) {
821 // Otherwise, allocate a new one. 1074 // Otherwise, allocate a new one.
822 MutexLocker x(OopMapCacheAlloc_lock); 1075 MutexLocker x(OopMapCacheAlloc_lock);
828 // _oop_map_cache is constant after init; lookup below does is own locking. 1081 // _oop_map_cache is constant after init; lookup below does is own locking.
829 _oop_map_cache->lookup(method, bci, entry_for); 1082 _oop_map_cache->lookup(method, bci, entry_for);
830 } 1083 }
831 1084
832 1085
833 bool instanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const { 1086 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
834 for (JavaFieldStream fs(as_klassOop()); !fs.done(); fs.next()) { 1087 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
835 Symbol* f_name = fs.name(); 1088 Symbol* f_name = fs.name();
836 Symbol* f_sig = fs.signature(); 1089 Symbol* f_sig = fs.signature();
837 if (f_name == name && f_sig == sig) { 1090 if (f_name == name && f_sig == sig) {
838 fd->initialize(as_klassOop(), fs.index()); 1091 fd->initialize(const_cast<InstanceKlass*>(this), fs.index());
839 return true; 1092 return true;
840 } 1093 }
841 } 1094 }
842 return false; 1095 return false;
843 } 1096 }
844 1097
845 1098
846 void instanceKlass::shared_symbols_iterate(SymbolClosure* closure) { 1099 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
847 Klass::shared_symbols_iterate(closure);
848 closure->do_symbol(&_generic_signature);
849 closure->do_symbol(&_source_file_name);
850
851 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
852 int name_index = fs.name_index();
853 closure->do_symbol(constants()->symbol_at_addr(name_index));
854 int sig_index = fs.signature_index();
855 closure->do_symbol(constants()->symbol_at_addr(sig_index));
856 }
857 }
858
859
860 klassOop instanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
861 const int n = local_interfaces()->length(); 1100 const int n = local_interfaces()->length();
862 for (int i = 0; i < n; i++) { 1101 for (int i = 0; i < n; i++) {
863 klassOop intf1 = klassOop(local_interfaces()->obj_at(i)); 1102 Klass* intf1 = local_interfaces()->at(i);
864 assert(Klass::cast(intf1)->is_interface(), "just checking type"); 1103 assert(Klass::cast(intf1)->is_interface(), "just checking type");
865 // search for field in current interface 1104 // search for field in current interface
866 if (instanceKlass::cast(intf1)->find_local_field(name, sig, fd)) { 1105 if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {
867 assert(fd->is_static(), "interface field must be static"); 1106 assert(fd->is_static(), "interface field must be static");
868 return intf1; 1107 return intf1;
869 } 1108 }
870 // search for field in direct superinterfaces 1109 // search for field in direct superinterfaces
871 klassOop intf2 = instanceKlass::cast(intf1)->find_interface_field(name, sig, fd); 1110 Klass* intf2 = InstanceKlass::cast(intf1)->find_interface_field(name, sig, fd);
872 if (intf2 != NULL) return intf2; 1111 if (intf2 != NULL) return intf2;
873 } 1112 }
874 // otherwise field lookup fails 1113 // otherwise field lookup fails
875 return NULL; 1114 return NULL;
876 } 1115 }
877 1116
878 1117
879 klassOop instanceKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const { 1118 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
880 // search order according to newest JVM spec (5.4.3.2, p.167). 1119 // search order according to newest JVM spec (5.4.3.2, p.167).
881 // 1) search for field in current klass 1120 // 1) search for field in current klass
882 if (find_local_field(name, sig, fd)) { 1121 if (find_local_field(name, sig, fd)) {
883 return as_klassOop(); 1122 return const_cast<InstanceKlass*>(this);
884 } 1123 }
885 // 2) search for field recursively in direct superinterfaces 1124 // 2) search for field recursively in direct superinterfaces
886 { klassOop intf = find_interface_field(name, sig, fd); 1125 { Klass* intf = find_interface_field(name, sig, fd);
887 if (intf != NULL) return intf; 1126 if (intf != NULL) return intf;
888 } 1127 }
889 // 3) apply field lookup recursively if superclass exists 1128 // 3) apply field lookup recursively if superclass exists
890 { klassOop supr = super(); 1129 { Klass* supr = super();
891 if (supr != NULL) return instanceKlass::cast(supr)->find_field(name, sig, fd); 1130 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, fd);
892 } 1131 }
893 // 4) otherwise field lookup fails 1132 // 4) otherwise field lookup fails
894 return NULL; 1133 return NULL;
895 } 1134 }
896 1135
897 1136
898 klassOop instanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const { 1137 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
899 // search order according to newest JVM spec (5.4.3.2, p.167). 1138 // search order according to newest JVM spec (5.4.3.2, p.167).
900 // 1) search for field in current klass 1139 // 1) search for field in current klass
901 if (find_local_field(name, sig, fd)) { 1140 if (find_local_field(name, sig, fd)) {
902 if (fd->is_static() == is_static) return as_klassOop(); 1141 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
903 } 1142 }
904 // 2) search for field recursively in direct superinterfaces 1143 // 2) search for field recursively in direct superinterfaces
905 if (is_static) { 1144 if (is_static) {
906 klassOop intf = find_interface_field(name, sig, fd); 1145 Klass* intf = find_interface_field(name, sig, fd);
907 if (intf != NULL) return intf; 1146 if (intf != NULL) return intf;
908 } 1147 }
909 // 3) apply field lookup recursively if superclass exists 1148 // 3) apply field lookup recursively if superclass exists
910 { klassOop supr = super(); 1149 { Klass* supr = super();
911 if (supr != NULL) return instanceKlass::cast(supr)->find_field(name, sig, is_static, fd); 1150 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
912 } 1151 }
913 // 4) otherwise field lookup fails 1152 // 4) otherwise field lookup fails
914 return NULL; 1153 return NULL;
915 } 1154 }
916 1155
917 1156
918 bool instanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const { 1157 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
919 for (JavaFieldStream fs(as_klassOop()); !fs.done(); fs.next()) { 1158 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
920 if (fs.offset() == offset) { 1159 if (fs.offset() == offset) {
921 fd->initialize(as_klassOop(), fs.index()); 1160 fd->initialize(const_cast<InstanceKlass*>(this), fs.index());
922 if (fd->is_static() == is_static) return true; 1161 if (fd->is_static() == is_static) return true;
923 } 1162 }
924 } 1163 }
925 return false; 1164 return false;
926 } 1165 }
927 1166
928 1167
929 bool instanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const { 1168 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
930 klassOop klass = as_klassOop(); 1169 Klass* klass = const_cast<InstanceKlass*>(this);
931 while (klass != NULL) { 1170 while (klass != NULL) {
932 if (instanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) { 1171 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
933 return true; 1172 return true;
934 } 1173 }
935 klass = Klass::cast(klass)->super(); 1174 klass = Klass::cast(klass)->super();
936 } 1175 }
937 return false; 1176 return false;
938 } 1177 }
939 1178
940 1179
941 void instanceKlass::methods_do(void f(methodOop method)) { 1180 void InstanceKlass::methods_do(void f(Method* method)) {
942 int len = methods()->length(); 1181 int len = methods()->length();
943 for (int index = 0; index < len; index++) { 1182 for (int index = 0; index < len; index++) {
944 methodOop m = methodOop(methods()->obj_at(index)); 1183 Method* m = methods()->at(index);
945 assert(m->is_method(), "must be method"); 1184 assert(m->is_method(), "must be method");
946 f(m); 1185 f(m);
947 } 1186 }
948 } 1187 }
949 1188
950 1189
951 void instanceKlass::do_local_static_fields(FieldClosure* cl) { 1190 void InstanceKlass::do_local_static_fields(FieldClosure* cl) {
952 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 1191 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
953 if (fs.access_flags().is_static()) { 1192 if (fs.access_flags().is_static()) {
954 fieldDescriptor fd; 1193 fieldDescriptor fd;
955 fd.initialize(as_klassOop(), fs.index()); 1194 fd.initialize(this, fs.index());
956 cl->do_field(&fd); 1195 cl->do_field(&fd);
957 } 1196 }
958 } 1197 }
959 } 1198 }
960 1199
961 1200
962 void instanceKlass::do_local_static_fields(void f(fieldDescriptor*, TRAPS), TRAPS) { 1201 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, TRAPS), TRAPS) {
963 instanceKlassHandle h_this(THREAD, as_klassOop()); 1202 instanceKlassHandle h_this(THREAD, this);
964 do_local_static_fields_impl(h_this, f, CHECK); 1203 do_local_static_fields_impl(h_this, f, CHECK);
965 } 1204 }
966 1205
967 1206
968 void instanceKlass::do_local_static_fields_impl(instanceKlassHandle this_oop, void f(fieldDescriptor* fd, TRAPS), TRAPS) { 1207 void InstanceKlass::do_local_static_fields_impl(instanceKlassHandle this_oop, void f(fieldDescriptor* fd, TRAPS), TRAPS) {
969 for (JavaFieldStream fs(this_oop()); !fs.done(); fs.next()) { 1208 for (JavaFieldStream fs(this_oop()); !fs.done(); fs.next()) {
970 if (fs.access_flags().is_static()) { 1209 if (fs.access_flags().is_static()) {
971 fieldDescriptor fd; 1210 fieldDescriptor fd;
972 fd.initialize(this_oop(), fs.index()); 1211 fd.initialize(this_oop(), fs.index());
973 f(&fd, CHECK); 1212 f(&fd, CHECK);
978 1217
979 static int compare_fields_by_offset(int* a, int* b) { 1218 static int compare_fields_by_offset(int* a, int* b) {
980 return a[0] - b[0]; 1219 return a[0] - b[0];
981 } 1220 }
982 1221
983 void instanceKlass::do_nonstatic_fields(FieldClosure* cl) { 1222 void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) {
984 instanceKlass* super = superklass(); 1223 InstanceKlass* super = superklass();
985 if (super != NULL) { 1224 if (super != NULL) {
986 super->do_nonstatic_fields(cl); 1225 super->do_nonstatic_fields(cl);
987 } 1226 }
988 fieldDescriptor fd; 1227 fieldDescriptor fd;
989 int length = java_fields_count(); 1228 int length = java_fields_count();
990 // In DebugInfo nonstatic fields are sorted by offset. 1229 // In DebugInfo nonstatic fields are sorted by offset.
991 int* fields_sorted = NEW_C_HEAP_ARRAY(int, 2*(length+1), mtClass); 1230 int* fields_sorted = NEW_C_HEAP_ARRAY(int, 2*(length+1), mtClass);
992 int j = 0; 1231 int j = 0;
993 for (int i = 0; i < length; i += 1) { 1232 for (int i = 0; i < length; i += 1) {
994 fd.initialize(as_klassOop(), i); 1233 fd.initialize(this, i);
995 if (!fd.is_static()) { 1234 if (!fd.is_static()) {
996 fields_sorted[j + 0] = fd.offset(); 1235 fields_sorted[j + 0] = fd.offset();
997 fields_sorted[j + 1] = i; 1236 fields_sorted[j + 1] = i;
998 j += 2; 1237 j += 2;
999 } 1238 }
1001 if (j > 0) { 1240 if (j > 0) {
1002 length = j; 1241 length = j;
1003 // _sort_Fn is defined in growableArray.hpp. 1242 // _sort_Fn is defined in growableArray.hpp.
1004 qsort(fields_sorted, length/2, 2*sizeof(int), (_sort_Fn)compare_fields_by_offset); 1243 qsort(fields_sorted, length/2, 2*sizeof(int), (_sort_Fn)compare_fields_by_offset);
1005 for (int i = 0; i < length; i += 2) { 1244 for (int i = 0; i < length; i += 2) {
1006 fd.initialize(as_klassOop(), fields_sorted[i + 1]); 1245 fd.initialize(this, fields_sorted[i + 1]);
1007 assert(!fd.is_static() && fd.offset() == fields_sorted[i], "only nonstatic fields"); 1246 assert(!fd.is_static() && fd.offset() == fields_sorted[i], "only nonstatic fields");
1008 cl->do_field(&fd); 1247 cl->do_field(&fd);
1009 } 1248 }
1010 } 1249 }
1011 FREE_C_HEAP_ARRAY(int, fields_sorted, mtClass); 1250 FREE_C_HEAP_ARRAY(int, fields_sorted, mtClass);
1012 } 1251 }
1013 1252
1014 1253
1015 void instanceKlass::array_klasses_do(void f(klassOop k)) { 1254 void InstanceKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {
1016 if (array_klasses() != NULL) 1255 if (array_klasses() != NULL)
1017 arrayKlass::cast(array_klasses())->array_klasses_do(f); 1256 ArrayKlass::cast(array_klasses())->array_klasses_do(f, THREAD);
1018 } 1257 }
1019 1258
1020 1259 void InstanceKlass::array_klasses_do(void f(Klass* k)) {
1021 void instanceKlass::with_array_klasses_do(void f(klassOop k)) { 1260 if (array_klasses() != NULL)
1022 f(as_klassOop()); 1261 ArrayKlass::cast(array_klasses())->array_klasses_do(f);
1262 }
1263
1264
1265 void InstanceKlass::with_array_klasses_do(void f(Klass* k)) {
1266 f(this);
1023 array_klasses_do(f); 1267 array_klasses_do(f);
1024 } 1268 }
1025 1269
1026 #ifdef ASSERT 1270 #ifdef ASSERT
1027 static int linear_search(objArrayOop methods, Symbol* name, Symbol* signature) { 1271 static int linear_search(Array<Method*>* methods, Symbol* name, Symbol* signature) {
1028 int len = methods->length(); 1272 int len = methods->length();
1029 for (int index = 0; index < len; index++) { 1273 for (int index = 0; index < len; index++) {
1030 methodOop m = (methodOop)(methods->obj_at(index)); 1274 Method* m = methods->at(index);
1031 assert(m->is_method(), "must be method"); 1275 assert(m->is_method(), "must be method");
1032 if (m->signature() == signature && m->name() == name) { 1276 if (m->signature() == signature && m->name() == name) {
1033 return index; 1277 return index;
1034 } 1278 }
1035 } 1279 }
1036 return -1; 1280 return -1;
1037 } 1281 }
1038 #endif 1282 #endif
1039 1283
1040 methodOop instanceKlass::find_method(Symbol* name, Symbol* signature) const { 1284 static int binary_search(Array<Method*>* methods, Symbol* name) {
1041 return instanceKlass::find_method(methods(), name, signature);
1042 }
1043
1044 methodOop instanceKlass::find_method(objArrayOop methods, Symbol* name, Symbol* signature) {
1045 int len = methods->length(); 1285 int len = methods->length();
1046 // methods are sorted, so do binary search 1286 // methods are sorted, so do binary search
1047 int l = 0; 1287 int l = 0;
1048 int h = len - 1; 1288 int h = len - 1;
1049 while (l <= h) { 1289 while (l <= h) {
1050 int mid = (l + h) >> 1; 1290 int mid = (l + h) >> 1;
1051 methodOop m = (methodOop)methods->obj_at(mid); 1291 Method* m = methods->at(mid);
1052 assert(m->is_method(), "must be method"); 1292 assert(m->is_method(), "must be method");
1053 int res = m->name()->fast_compare(name); 1293 int res = m->name()->fast_compare(name);
1054 if (res == 0) { 1294 if (res == 0) {
1055 // found matching name; do linear search to find matching signature 1295 return mid;
1056 // first, quick check for common case
1057 if (m->signature() == signature) return m;
1058 // search downwards through overloaded methods
1059 int i;
1060 for (i = mid - 1; i >= l; i--) {
1061 methodOop m = (methodOop)methods->obj_at(i);
1062 assert(m->is_method(), "must be method");
1063 if (m->name() != name) break;
1064 if (m->signature() == signature) return m;
1065 }
1066 // search upwards
1067 for (i = mid + 1; i <= h; i++) {
1068 methodOop m = (methodOop)methods->obj_at(i);
1069 assert(m->is_method(), "must be method");
1070 if (m->name() != name) break;
1071 if (m->signature() == signature) return m;
1072 }
1073 // not found
1074 #ifdef ASSERT
1075 int index = linear_search(methods, name, signature);
1076 assert(index == -1, err_msg("binary search should have found entry %d", index));
1077 #endif
1078 return NULL;
1079 } else if (res < 0) { 1296 } else if (res < 0) {
1080 l = mid + 1; 1297 l = mid + 1;
1081 } else { 1298 } else {
1082 h = mid - 1; 1299 h = mid - 1;
1083 } 1300 }
1084 } 1301 }
1302 return -1;
1303 }
1304
1305 Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const {
1306 return InstanceKlass::find_method(methods(), name, signature);
1307 }
1308
1309 Method* InstanceKlass::find_method(
1310 Array<Method*>* methods, Symbol* name, Symbol* signature) {
1311 int hit = binary_search(methods, name);
1312 if (hit != -1) {
1313 Method* m = methods->at(hit);
1314 // Do linear search to find matching signature. First, quick check
1315 // for common case
1316 if (m->signature() == signature) return m;
1317 // search downwards through overloaded methods
1318 int i;
1319 for (i = hit - 1; i >= 0; --i) {
1320 Method* m = methods->at(i);
1321 assert(m->is_method(), "must be method");
1322 if (m->name() != name) break;
1323 if (m->signature() == signature) return m;
1324 }
1325 // search upwards
1326 for (i = hit + 1; i < methods->length(); ++i) {
1327 Method* m = methods->at(i);
1328 assert(m->is_method(), "must be method");
1329 if (m->name() != name) break;
1330 if (m->signature() == signature) return m;
1331 }
1332 // not found
1085 #ifdef ASSERT 1333 #ifdef ASSERT
1086 int index = linear_search(methods, name, signature); 1334 int index = linear_search(methods, name, signature);
1087 assert(index == -1, err_msg("binary search should have found entry %d", index)); 1335 assert(index == -1, err_msg("binary search should have found entry %d", index));
1088 #endif 1336 #endif
1337 }
1089 return NULL; 1338 return NULL;
1090 } 1339 }
1091 1340
1092 methodOop instanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature) const { 1341 int InstanceKlass::find_method_by_name(Symbol* name, int* end) {
1093 klassOop klass = as_klassOop(); 1342 return find_method_by_name(methods(), name, end);
1343 }
1344
1345 int InstanceKlass::find_method_by_name(
1346 Array<Method*>* methods, Symbol* name, int* end_ptr) {
1347 assert(end_ptr != NULL, "just checking");
1348 int start = binary_search(methods, name);
1349 int end = start + 1;
1350 if (start != -1) {
1351 while (start - 1 >= 0 && (methods->at(start - 1))->name() == name) --start;
1352 while (end < methods->length() && (methods->at(end))->name() == name) ++end;
1353 *end_ptr = end;
1354 return start;
1355 }
1356 return -1;
1357 }
1358
1359 Method* InstanceKlass::uncached_lookup_method(Symbol* name, Symbol* signature) const {
1360 Klass* klass = const_cast<InstanceKlass*>(this);
1094 while (klass != NULL) { 1361 while (klass != NULL) {
1095 methodOop method = instanceKlass::cast(klass)->find_method(name, signature); 1362 Method* method = InstanceKlass::cast(klass)->find_method(name, signature);
1096 if (method != NULL) return method; 1363 if (method != NULL) return method;
1097 klass = instanceKlass::cast(klass)->super(); 1364 klass = InstanceKlass::cast(klass)->super();
1098 } 1365 }
1099 return NULL; 1366 return NULL;
1100 } 1367 }
1101 1368
1102 // lookup a method in all the interfaces that this class implements 1369 // lookup a method in all the interfaces that this class implements
1103 methodOop instanceKlass::lookup_method_in_all_interfaces(Symbol* name, 1370 Method* InstanceKlass::lookup_method_in_all_interfaces(Symbol* name,
1104 Symbol* signature) const { 1371 Symbol* signature) const {
1105 objArrayOop all_ifs = instanceKlass::cast(as_klassOop())->transitive_interfaces(); 1372 Array<Klass*>* all_ifs = transitive_interfaces();
1106 int num_ifs = all_ifs->length(); 1373 int num_ifs = all_ifs->length();
1107 instanceKlass *ik = NULL; 1374 InstanceKlass *ik = NULL;
1108 for (int i = 0; i < num_ifs; i++) { 1375 for (int i = 0; i < num_ifs; i++) {
1109 ik = instanceKlass::cast(klassOop(all_ifs->obj_at(i))); 1376 ik = InstanceKlass::cast(all_ifs->at(i));
1110 methodOop m = ik->lookup_method(name, signature); 1377 Method* m = ik->lookup_method(name, signature);
1111 if (m != NULL) { 1378 if (m != NULL) {
1112 return m; 1379 return m;
1113 } 1380 }
1114 } 1381 }
1115 return NULL; 1382 return NULL;
1116 } 1383 }
1117 1384
1118 /* jni_id_for_impl for jfieldIds only */ 1385 /* jni_id_for_impl for jfieldIds only */
1119 JNIid* instanceKlass::jni_id_for_impl(instanceKlassHandle this_oop, int offset) { 1386 JNIid* InstanceKlass::jni_id_for_impl(instanceKlassHandle this_oop, int offset) {
1120 MutexLocker ml(JfieldIdCreation_lock); 1387 MutexLocker ml(JfieldIdCreation_lock);
1121 // Retry lookup after we got the lock 1388 // Retry lookup after we got the lock
1122 JNIid* probe = this_oop->jni_ids() == NULL ? NULL : this_oop->jni_ids()->find(offset); 1389 JNIid* probe = this_oop->jni_ids() == NULL ? NULL : this_oop->jni_ids()->find(offset);
1123 if (probe == NULL) { 1390 if (probe == NULL) {
1124 // Slow case, allocate new static field identifier 1391 // Slow case, allocate new static field identifier
1125 probe = new JNIid(this_oop->as_klassOop(), offset, this_oop->jni_ids()); 1392 probe = new JNIid(this_oop(), offset, this_oop->jni_ids());
1126 this_oop->set_jni_ids(probe); 1393 this_oop->set_jni_ids(probe);
1127 } 1394 }
1128 return probe; 1395 return probe;
1129 } 1396 }
1130 1397
1131 1398
1132 /* jni_id_for for jfieldIds only */ 1399 /* jni_id_for for jfieldIds only */
1133 JNIid* instanceKlass::jni_id_for(int offset) { 1400 JNIid* InstanceKlass::jni_id_for(int offset) {
1134 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset); 1401 JNIid* probe = jni_ids() == NULL ? NULL : jni_ids()->find(offset);
1135 if (probe == NULL) { 1402 if (probe == NULL) {
1136 probe = jni_id_for_impl(this->as_klassOop(), offset); 1403 probe = jni_id_for_impl(this, offset);
1137 } 1404 }
1138 return probe; 1405 return probe;
1139 } 1406 }
1140 1407
1141 u2 instanceKlass::enclosing_method_data(int offset) { 1408 u2 InstanceKlass::enclosing_method_data(int offset) {
1142 typeArrayOop inner_class_list = inner_classes(); 1409 Array<jushort>* inner_class_list = inner_classes();
1143 if (inner_class_list == NULL) { 1410 if (inner_class_list == NULL) {
1144 return 0; 1411 return 0;
1145 } 1412 }
1146 int length = inner_class_list->length(); 1413 int length = inner_class_list->length();
1147 if (length % inner_class_next_offset == 0) { 1414 if (length % inner_class_next_offset == 0) {
1148 return 0; 1415 return 0;
1149 } else { 1416 } else {
1150 int index = length - enclosing_method_attribute_size; 1417 int index = length - enclosing_method_attribute_size;
1151 typeArrayHandle inner_class_list_h(inner_class_list);
1152 assert(offset < enclosing_method_attribute_size, "invalid offset"); 1418 assert(offset < enclosing_method_attribute_size, "invalid offset");
1153 return inner_class_list_h->ushort_at(index + offset); 1419 return inner_class_list->at(index + offset);
1154 } 1420 }
1155 } 1421 }
1156 1422
1157 void instanceKlass::set_enclosing_method_indices(u2 class_index, 1423 void InstanceKlass::set_enclosing_method_indices(u2 class_index,
1158 u2 method_index) { 1424 u2 method_index) {
1159 typeArrayOop inner_class_list = inner_classes(); 1425 Array<jushort>* inner_class_list = inner_classes();
1160 assert (inner_class_list != NULL, "_inner_classes list is not set up"); 1426 assert (inner_class_list != NULL, "_inner_classes list is not set up");
1161 int length = inner_class_list->length(); 1427 int length = inner_class_list->length();
1162 if (length % inner_class_next_offset == enclosing_method_attribute_size) { 1428 if (length % inner_class_next_offset == enclosing_method_attribute_size) {
1163 int index = length - enclosing_method_attribute_size; 1429 int index = length - enclosing_method_attribute_size;
1164 typeArrayHandle inner_class_list_h(inner_class_list); 1430 inner_class_list->at_put(
1165 inner_class_list_h->ushort_at_put(
1166 index + enclosing_method_class_index_offset, class_index); 1431 index + enclosing_method_class_index_offset, class_index);
1167 inner_class_list_h->ushort_at_put( 1432 inner_class_list->at_put(
1168 index + enclosing_method_method_index_offset, method_index); 1433 index + enclosing_method_method_index_offset, method_index);
1169 } 1434 }
1170 } 1435 }
1171 1436
1172 // Lookup or create a jmethodID. 1437 // Lookup or create a jmethodID.
1173 // This code is called by the VMThread and JavaThreads so the 1438 // This code is called by the VMThread and JavaThreads so the
1174 // locking has to be done very carefully to avoid deadlocks 1439 // locking has to be done very carefully to avoid deadlocks
1175 // and/or other cache consistency problems. 1440 // and/or other cache consistency problems.
1176 // 1441 //
1177 jmethodID instanceKlass::get_jmethod_id(instanceKlassHandle ik_h, methodHandle method_h) { 1442 jmethodID InstanceKlass::get_jmethod_id(instanceKlassHandle ik_h, methodHandle method_h) {
1178 size_t idnum = (size_t)method_h->method_idnum(); 1443 size_t idnum = (size_t)method_h->method_idnum();
1179 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire(); 1444 jmethodID* jmeths = ik_h->methods_jmethod_ids_acquire();
1180 size_t length = 0; 1445 size_t length = 0;
1181 jmethodID id = NULL; 1446 jmethodID id = NULL;
1182 1447
1243 1508
1244 // allocate a new jmethodID that might be used 1509 // allocate a new jmethodID that might be used
1245 jmethodID new_id = NULL; 1510 jmethodID new_id = NULL;
1246 if (method_h->is_old() && !method_h->is_obsolete()) { 1511 if (method_h->is_old() && !method_h->is_obsolete()) {
1247 // The method passed in is old (but not obsolete), we need to use the current version 1512 // The method passed in is old (but not obsolete), we need to use the current version
1248 methodOop current_method = ik_h->method_with_idnum((int)idnum); 1513 Method* current_method = ik_h->method_with_idnum((int)idnum);
1249 assert(current_method != NULL, "old and but not obsolete, so should exist"); 1514 assert(current_method != NULL, "old and but not obsolete, so should exist");
1250 methodHandle current_method_h(current_method == NULL? method_h() : current_method); 1515 new_id = Method::make_jmethod_id(ik_h->class_loader_data(), current_method);
1251 new_id = JNIHandles::make_jmethod_id(current_method_h);
1252 } else { 1516 } else {
1253 // It is the current version of the method or an obsolete method, 1517 // It is the current version of the method or an obsolete method,
1254 // use the version passed in 1518 // use the version passed in
1255 new_id = JNIHandles::make_jmethod_id(method_h); 1519 new_id = Method::make_jmethod_id(ik_h->class_loader_data(), method_h());
1256 } 1520 }
1257 1521
1258 if (Threads::number_of_threads() == 0 || 1522 if (Threads::number_of_threads() == 0 ||
1259 SafepointSynchronize::is_at_safepoint()) { 1523 SafepointSynchronize::is_at_safepoint()) {
1260 // we're single threaded or at a safepoint - no locking needed 1524 // we're single threaded or at a safepoint - no locking needed
1271 if (to_dealloc_jmeths != NULL) { 1535 if (to_dealloc_jmeths != NULL) {
1272 FreeHeap(to_dealloc_jmeths); 1536 FreeHeap(to_dealloc_jmeths);
1273 } 1537 }
1274 // free up the new ID since it wasn't needed 1538 // free up the new ID since it wasn't needed
1275 if (to_dealloc_id != NULL) { 1539 if (to_dealloc_id != NULL) {
1276 JNIHandles::destroy_jmethod_id(to_dealloc_id); 1540 Method::destroy_jmethod_id(ik_h->class_loader_data(), to_dealloc_id);
1277 } 1541 }
1278 } 1542 }
1279 return id; 1543 return id;
1280 } 1544 }
1281 1545
1283 // Common code to fetch the jmethodID from the cache or update the 1547 // Common code to fetch the jmethodID from the cache or update the
1284 // cache with the new jmethodID. This function should never do anything 1548 // cache with the new jmethodID. This function should never do anything
1285 // that causes the caller to go to a safepoint or we can deadlock with 1549 // that causes the caller to go to a safepoint or we can deadlock with
1286 // the VMThread or have cache consistency issues. 1550 // the VMThread or have cache consistency issues.
1287 // 1551 //
1288 jmethodID instanceKlass::get_jmethod_id_fetch_or_update( 1552 jmethodID InstanceKlass::get_jmethod_id_fetch_or_update(
1289 instanceKlassHandle ik_h, size_t idnum, jmethodID new_id, 1553 instanceKlassHandle ik_h, size_t idnum, jmethodID new_id,
1290 jmethodID* new_jmeths, jmethodID* to_dealloc_id_p, 1554 jmethodID* new_jmeths, jmethodID* to_dealloc_id_p,
1291 jmethodID** to_dealloc_jmeths_p) { 1555 jmethodID** to_dealloc_jmeths_p) {
1292 assert(new_id != NULL, "sanity check"); 1556 assert(new_id != NULL, "sanity check");
1293 assert(to_dealloc_id_p != NULL, "sanity check"); 1557 assert(to_dealloc_id_p != NULL, "sanity check");
1335 1599
1336 1600
1337 // Common code to get the jmethodID cache length and the jmethodID 1601 // Common code to get the jmethodID cache length and the jmethodID
1338 // value at index idnum if there is one. 1602 // value at index idnum if there is one.
1339 // 1603 //
1340 void instanceKlass::get_jmethod_id_length_value(jmethodID* cache, 1604 void InstanceKlass::get_jmethod_id_length_value(jmethodID* cache,
1341 size_t idnum, size_t *length_p, jmethodID* id_p) { 1605 size_t idnum, size_t *length_p, jmethodID* id_p) {
1342 assert(cache != NULL, "sanity check"); 1606 assert(cache != NULL, "sanity check");
1343 assert(length_p != NULL, "sanity check"); 1607 assert(length_p != NULL, "sanity check");
1344 assert(id_p != NULL, "sanity check"); 1608 assert(id_p != NULL, "sanity check");
1345 1609
1352 } 1616 }
1353 } 1617 }
1354 1618
1355 1619
1356 // Lookup a jmethodID, NULL if not found. Do no blocking, no allocations, no handles 1620 // Lookup a jmethodID, NULL if not found. Do no blocking, no allocations, no handles
1357 jmethodID instanceKlass::jmethod_id_or_null(methodOop method) { 1621 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
1358 size_t idnum = (size_t)method->method_idnum(); 1622 size_t idnum = (size_t)method->method_idnum();
1359 jmethodID* jmeths = methods_jmethod_ids_acquire(); 1623 jmethodID* jmeths = methods_jmethod_ids_acquire();
1360 size_t length; // length assigned as debugging crumb 1624 size_t length; // length assigned as debugging crumb
1361 jmethodID id = NULL; 1625 jmethodID id = NULL;
1362 if (jmeths != NULL && // If there is a cache 1626 if (jmeths != NULL && // If there is a cache
1366 return id; 1630 return id;
1367 } 1631 }
1368 1632
1369 1633
1370 // Cache an itable index 1634 // Cache an itable index
1371 void instanceKlass::set_cached_itable_index(size_t idnum, int index) { 1635 void InstanceKlass::set_cached_itable_index(size_t idnum, int index) {
1372 int* indices = methods_cached_itable_indices_acquire(); 1636 int* indices = methods_cached_itable_indices_acquire();
1373 int* to_dealloc_indices = NULL; 1637 int* to_dealloc_indices = NULL;
1374 1638
1375 // We use a double-check locking idiom here because this cache is 1639 // We use a double-check locking idiom here because this cache is
1376 // performance sensitive. In the normal system, this cache only 1640 // performance sensitive. In the normal system, this cache only
1436 } 1700 }
1437 } 1701 }
1438 1702
1439 1703
1440 // Retrieve a cached itable index 1704 // Retrieve a cached itable index
1441 int instanceKlass::cached_itable_index(size_t idnum) { 1705 int InstanceKlass::cached_itable_index(size_t idnum) {
1442 int* indices = methods_cached_itable_indices_acquire(); 1706 int* indices = methods_cached_itable_indices_acquire();
1443 if (indices != NULL && ((size_t)indices[0]) > idnum) { 1707 if (indices != NULL && ((size_t)indices[0]) > idnum) {
1444 // indices exist and are long enough, retrieve possible cached 1708 // indices exist and are long enough, retrieve possible cached
1445 return indices[idnum+1]; 1709 return indices[idnum+1];
1446 } 1710 }
1451 // 1715 //
1452 // Walk the list of dependent nmethods searching for nmethods which 1716 // Walk the list of dependent nmethods searching for nmethods which
1453 // are dependent on the changes that were passed in and mark them for 1717 // are dependent on the changes that were passed in and mark them for
1454 // deoptimization. Returns the number of nmethods found. 1718 // deoptimization. Returns the number of nmethods found.
1455 // 1719 //
1456 int instanceKlass::mark_dependent_nmethods(DepChange& changes) { 1720 int InstanceKlass::mark_dependent_nmethods(DepChange& changes) {
1457 assert_locked_or_safepoint(CodeCache_lock); 1721 assert_locked_or_safepoint(CodeCache_lock);
1458 int found = 0; 1722 int found = 0;
1459 nmethodBucket* b = _dependencies; 1723 nmethodBucket* b = _dependencies;
1460 while (b != NULL) { 1724 while (b != NULL) {
1461 nmethod* nm = b->get_nmethod(); 1725 nmethod* nm = b->get_nmethod();
1483 // Add an nmethodBucket to the list of dependencies for this nmethod. 1747 // Add an nmethodBucket to the list of dependencies for this nmethod.
1484 // It's possible that an nmethod has multiple dependencies on this klass 1748 // It's possible that an nmethod has multiple dependencies on this klass
1485 // so a count is kept for each bucket to guarantee that creation and 1749 // so a count is kept for each bucket to guarantee that creation and
1486 // deletion of dependencies is consistent. 1750 // deletion of dependencies is consistent.
1487 // 1751 //
1488 void instanceKlass::add_dependent_nmethod(nmethod* nm) { 1752 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
1489 assert_locked_or_safepoint(CodeCache_lock); 1753 assert_locked_or_safepoint(CodeCache_lock);
1490 nmethodBucket* b = _dependencies; 1754 nmethodBucket* b = _dependencies;
1491 nmethodBucket* last = NULL; 1755 nmethodBucket* last = NULL;
1492 while (b != NULL) { 1756 while (b != NULL) {
1493 if (nm == b->get_nmethod()) { 1757 if (nm == b->get_nmethod()) {
1504 // Decrement count of the nmethod in the dependency list and remove 1768 // Decrement count of the nmethod in the dependency list and remove
1505 // the bucket competely when the count goes to 0. This method must 1769 // the bucket competely when the count goes to 0. This method must
1506 // find a corresponding bucket otherwise there's a bug in the 1770 // find a corresponding bucket otherwise there's a bug in the
1507 // recording of dependecies. 1771 // recording of dependecies.
1508 // 1772 //
1509 void instanceKlass::remove_dependent_nmethod(nmethod* nm) { 1773 void InstanceKlass::remove_dependent_nmethod(nmethod* nm) {
1510 assert_locked_or_safepoint(CodeCache_lock); 1774 assert_locked_or_safepoint(CodeCache_lock);
1511 nmethodBucket* b = _dependencies; 1775 nmethodBucket* b = _dependencies;
1512 nmethodBucket* last = NULL; 1776 nmethodBucket* last = NULL;
1513 while (b != NULL) { 1777 while (b != NULL) {
1514 if (nm == b->get_nmethod()) { 1778 if (nm == b->get_nmethod()) {
1532 ShouldNotReachHere(); 1796 ShouldNotReachHere();
1533 } 1797 }
1534 1798
1535 1799
1536 #ifndef PRODUCT 1800 #ifndef PRODUCT
1537 void instanceKlass::print_dependent_nmethods(bool verbose) { 1801 void InstanceKlass::print_dependent_nmethods(bool verbose) {
1538 nmethodBucket* b = _dependencies; 1802 nmethodBucket* b = _dependencies;
1539 int idx = 0; 1803 int idx = 0;
1540 while (b != NULL) { 1804 while (b != NULL) {
1541 nmethod* nm = b->get_nmethod(); 1805 nmethod* nm = b->get_nmethod();
1542 tty->print("[%d] count=%d { ", idx++, b->count()); 1806 tty->print("[%d] count=%d { ", idx++, b->count());
1551 b = b->next(); 1815 b = b->next();
1552 } 1816 }
1553 } 1817 }
1554 1818
1555 1819
1556 bool instanceKlass::is_dependent_nmethod(nmethod* nm) { 1820 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
1557 nmethodBucket* b = _dependencies; 1821 nmethodBucket* b = _dependencies;
1558 while (b != NULL) { 1822 while (b != NULL) {
1559 if (nm == b->get_nmethod()) { 1823 if (nm == b->get_nmethod()) {
1560 return true; 1824 return true;
1561 } 1825 }
1563 } 1827 }
1564 return false; 1828 return false;
1565 } 1829 }
1566 #endif //PRODUCT 1830 #endif //PRODUCT
1567 1831
1832
1833 // Garbage collection
1834
1835 void InstanceKlass::oops_do(OopClosure* cl) {
1836 Klass::oops_do(cl);
1837
1838 cl->do_oop(adr_protection_domain());
1839 cl->do_oop(adr_signers());
1840 cl->do_oop(adr_init_lock());
1841
1842 // Don't walk the arrays since they are walked from the ClassLoaderData objects.
1843 }
1568 1844
1569 #ifdef ASSERT 1845 #ifdef ASSERT
1570 template <class T> void assert_is_in(T *p) { 1846 template <class T> void assert_is_in(T *p) {
1571 T heap_oop = oopDesc::load_heap_oop(p); 1847 T heap_oop = oopDesc::load_heap_oop(p);
1572 if (!oopDesc::is_null(heap_oop)) { 1848 if (!oopDesc::is_null(heap_oop)) {
1576 } 1852 }
1577 template <class T> void assert_is_in_closed_subset(T *p) { 1853 template <class T> void assert_is_in_closed_subset(T *p) {
1578 T heap_oop = oopDesc::load_heap_oop(p); 1854 T heap_oop = oopDesc::load_heap_oop(p);
1579 if (!oopDesc::is_null(heap_oop)) { 1855 if (!oopDesc::is_null(heap_oop)) {
1580 oop o = oopDesc::decode_heap_oop_not_null(heap_oop); 1856 oop o = oopDesc::decode_heap_oop_not_null(heap_oop);
1581 assert(Universe::heap()->is_in_closed_subset(o), "should be in closed"); 1857 assert(Universe::heap()->is_in_closed_subset(o),
1858 err_msg("should be in closed *p " INTPTR_FORMAT " " INTPTR_FORMAT, (address)p, (address)o));
1582 } 1859 }
1583 } 1860 }
1584 template <class T> void assert_is_in_reserved(T *p) { 1861 template <class T> void assert_is_in_reserved(T *p) {
1585 T heap_oop = oopDesc::load_heap_oop(p); 1862 T heap_oop = oopDesc::load_heap_oop(p);
1586 if (!oopDesc::is_null(heap_oop)) { 1863 if (!oopDesc::is_null(heap_oop)) {
1730 ++map; \ 2007 ++map; \
1731 } \ 2008 } \
1732 } \ 2009 } \
1733 } 2010 }
1734 2011
1735 void instanceKlass::oop_follow_contents(oop obj) { 2012 void InstanceKlass::oop_follow_contents(oop obj) {
1736 assert(obj != NULL, "can't follow the content of NULL object"); 2013 assert(obj != NULL, "can't follow the content of NULL object");
1737 obj->follow_header(); 2014 MarkSweep::follow_klass(obj->klass());
1738 InstanceKlass_OOP_MAP_ITERATE( \ 2015 InstanceKlass_OOP_MAP_ITERATE( \
1739 obj, \ 2016 obj, \
1740 MarkSweep::mark_and_push(p), \ 2017 MarkSweep::mark_and_push(p), \
1741 assert_is_in_closed_subset) 2018 assert_is_in_closed_subset)
1742 } 2019 }
1743 2020
1744 #ifndef SERIALGC 2021 #ifndef SERIALGC
1745 void instanceKlass::oop_follow_contents(ParCompactionManager* cm, 2022 void InstanceKlass::oop_follow_contents(ParCompactionManager* cm,
1746 oop obj) { 2023 oop obj) {
1747 assert(obj != NULL, "can't follow the content of NULL object"); 2024 assert(obj != NULL, "can't follow the content of NULL object");
1748 obj->follow_header(cm); 2025 PSParallelCompact::follow_klass(cm, obj->klass());
2026 // Only mark the header and let the scan of the meta-data mark
2027 // everything else.
1749 InstanceKlass_OOP_MAP_ITERATE( \ 2028 InstanceKlass_OOP_MAP_ITERATE( \
1750 obj, \ 2029 obj, \
1751 PSParallelCompact::mark_and_push(cm, p), \ 2030 PSParallelCompact::mark_and_push(cm, p), \
1752 assert_is_in) 2031 assert_is_in)
1753 } 2032 }
1754 #endif // SERIALGC 2033 #endif // SERIALGC
1755 2034
1756 // closure's do_header() method dicates whether the given closure should be 2035 // closure's do_metadata() method dictates whether the given closure should be
1757 // applied to the klass ptr in the object header. 2036 // applied to the klass ptr in the object header.
2037
2038 #define if_do_metadata_checked(closure, nv_suffix) \
2039 /* Make sure the non-virtual and the virtual versions match. */ \
2040 assert(closure->do_metadata##nv_suffix() == closure->do_metadata(), \
2041 "Inconsistency in do_metadata"); \
2042 if (closure->do_metadata##nv_suffix())
1758 2043
1759 #define InstanceKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \ 2044 #define InstanceKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \
1760 \ 2045 \
1761 int instanceKlass::oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) { \ 2046 int InstanceKlass::oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) { \
1762 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\ 2047 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\
1763 /* header */ \ 2048 /* header */ \
1764 if (closure->do_header()) { \ 2049 if_do_metadata_checked(closure, nv_suffix) { \
1765 obj->oop_iterate_header(closure); \ 2050 closure->do_klass##nv_suffix(obj->klass()); \
1766 } \ 2051 } \
1767 InstanceKlass_OOP_MAP_ITERATE( \ 2052 InstanceKlass_OOP_MAP_ITERATE( \
1768 obj, \ 2053 obj, \
1769 SpecializationStats:: \ 2054 SpecializationStats:: \
1770 record_do_oop_call##nv_suffix(SpecializationStats::ik); \ 2055 record_do_oop_call##nv_suffix(SpecializationStats::ik); \
1774 } 2059 }
1775 2060
1776 #ifndef SERIALGC 2061 #ifndef SERIALGC
1777 #define InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) \ 2062 #define InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) \
1778 \ 2063 \
1779 int instanceKlass::oop_oop_iterate_backwards##nv_suffix(oop obj, \ 2064 int InstanceKlass::oop_oop_iterate_backwards##nv_suffix(oop obj, \
1780 OopClosureType* closure) { \ 2065 OopClosureType* closure) { \
1781 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik); \ 2066 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik); \
1782 /* header */ \ 2067 /* header */ \
1783 if (closure->do_header()) { \ 2068 if_do_metadata_checked(closure, nv_suffix) { \
1784 obj->oop_iterate_header(closure); \ 2069 closure->do_klass##nv_suffix(obj->klass()); \
1785 } \ 2070 } \
1786 /* instance variables */ \ 2071 /* instance variables */ \
1787 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \ 2072 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \
1788 obj, \ 2073 obj, \
1789 SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::ik);\ 2074 SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::ik);\
1793 } 2078 }
1794 #endif // !SERIALGC 2079 #endif // !SERIALGC
1795 2080
1796 #define InstanceKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix) \ 2081 #define InstanceKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix) \
1797 \ 2082 \
1798 int instanceKlass::oop_oop_iterate##nv_suffix##_m(oop obj, \ 2083 int InstanceKlass::oop_oop_iterate##nv_suffix##_m(oop obj, \
1799 OopClosureType* closure, \ 2084 OopClosureType* closure, \
1800 MemRegion mr) { \ 2085 MemRegion mr) { \
1801 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\ 2086 SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::ik);\
1802 if (closure->do_header()) { \ 2087 if_do_metadata_checked(closure, nv_suffix) { \
1803 obj->oop_iterate_header(closure, mr); \ 2088 if (mr.contains(obj)) { \
2089 closure->do_klass##nv_suffix(obj->klass()); \
2090 } \
1804 } \ 2091 } \
1805 InstanceKlass_BOUNDED_OOP_MAP_ITERATE( \ 2092 InstanceKlass_BOUNDED_OOP_MAP_ITERATE( \
1806 obj, mr.start(), mr.end(), \ 2093 obj, mr.start(), mr.end(), \
1807 (closure)->do_oop##nv_suffix(p), \ 2094 (closure)->do_oop##nv_suffix(p), \
1808 assert_is_in_closed_subset) \ 2095 assert_is_in_closed_subset) \
1816 #ifndef SERIALGC 2103 #ifndef SERIALGC
1817 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN) 2104 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)
1818 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN) 2105 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)
1819 #endif // !SERIALGC 2106 #endif // !SERIALGC
1820 2107
1821 int instanceKlass::oop_adjust_pointers(oop obj) { 2108 int InstanceKlass::oop_adjust_pointers(oop obj) {
1822 int size = size_helper(); 2109 int size = size_helper();
1823 InstanceKlass_OOP_MAP_ITERATE( \ 2110 InstanceKlass_OOP_MAP_ITERATE( \
1824 obj, \ 2111 obj, \
1825 MarkSweep::adjust_pointer(p), \ 2112 MarkSweep::adjust_pointer(p), \
1826 assert_is_in) 2113 assert_is_in)
1827 obj->adjust_header(); 2114 MarkSweep::adjust_klass(obj->klass());
1828 return size; 2115 return size;
1829 } 2116 }
1830 2117
1831 #ifndef SERIALGC 2118 #ifndef SERIALGC
1832 void instanceKlass::oop_push_contents(PSPromotionManager* pm, oop obj) { 2119 void InstanceKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
1833 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \ 2120 InstanceKlass_OOP_MAP_REVERSE_ITERATE( \
1834 obj, \ 2121 obj, \
1835 if (PSScavenge::should_scavenge(p)) { \ 2122 if (PSScavenge::should_scavenge(p)) { \
1836 pm->claim_or_forward_depth(p); \ 2123 pm->claim_or_forward_depth(p); \
1837 }, \ 2124 }, \
1838 assert_nothing ) 2125 assert_nothing )
1839 } 2126 }
1840 2127
1841 int instanceKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) { 2128 int InstanceKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
2129 int size = size_helper();
1842 InstanceKlass_OOP_MAP_ITERATE( \ 2130 InstanceKlass_OOP_MAP_ITERATE( \
1843 obj, \ 2131 obj, \
1844 PSParallelCompact::adjust_pointer(p), \ 2132 PSParallelCompact::adjust_pointer(p), \
1845 assert_nothing) 2133 assert_is_in)
1846 return size_helper(); 2134 obj->update_header(cm);
2135 return size;
1847 } 2136 }
1848 2137
1849 #endif // SERIALGC 2138 #endif // SERIALGC
1850 2139
1851 // This klass is alive but the implementor link is not followed/updated. 2140 void InstanceKlass::clean_implementors_list(BoolObjectClosure* is_alive) {
1852 // Subklass and sibling links are handled by Klass::follow_weak_klass_links 2141 assert(is_loader_alive(is_alive), "this klass should be live");
1853
1854 void instanceKlass::follow_weak_klass_links(
1855 BoolObjectClosure* is_alive, OopClosure* keep_alive) {
1856 assert(is_alive->do_object_b(as_klassOop()), "this oop should be live");
1857
1858 if (is_interface()) { 2142 if (is_interface()) {
1859 if (ClassUnloading) { 2143 if (ClassUnloading) {
1860 klassOop impl = implementor(); 2144 Klass* impl = implementor();
1861 if (impl != NULL) { 2145 if (impl != NULL) {
1862 if (!is_alive->do_object_b(impl)) { 2146 if (!impl->is_loader_alive(is_alive)) {
1863 // remove this guy 2147 // remove this guy
1864 *adr_implementor() = NULL; 2148 *adr_implementor() = NULL;
1865 } 2149 }
1866 } 2150 }
1867 } else { 2151 }
1868 assert(adr_implementor() != NULL, "just checking"); 2152 }
1869 keep_alive->do_oop(adr_implementor()); 2153 }
1870 } 2154
1871 } 2155 void InstanceKlass::clean_method_data(BoolObjectClosure* is_alive) {
1872 2156 #ifdef COMPILER2
1873 Klass::follow_weak_klass_links(is_alive, keep_alive); 2157 // Currently only used by C2.
1874 } 2158 for (int m = 0; m < methods()->length(); m++) {
1875 2159 MethodData* mdo = methods()->at(m)->method_data();
1876 void instanceKlass::remove_unshareable_info() { 2160 if (mdo != NULL) {
2161 for (ProfileData* data = mdo->first_data();
2162 mdo->is_valid(data);
2163 data = mdo->next_data(data)) {
2164 data->clean_weak_klass_links(is_alive);
2165 }
2166 }
2167 }
2168 #else
2169 #ifdef ASSERT
2170 // Verify that we haven't started to use MDOs for C1.
2171 for (int m = 0; m < methods()->length(); m++) {
2172 MethodData* mdo = methods()->at(m)->method_data();
2173 assert(mdo == NULL, "Didn't expect C1 to use MDOs");
2174 }
2175 #endif // ASSERT
2176 #endif // !COMPILER2
2177 }
2178
2179
2180 static void remove_unshareable_in_class(Klass* k) {
2181 // remove klass's unshareable info
2182 k->remove_unshareable_info();
2183 }
2184
2185 void InstanceKlass::remove_unshareable_info() {
1877 Klass::remove_unshareable_info(); 2186 Klass::remove_unshareable_info();
2187 // Unlink the class
2188 if (is_linked()) {
2189 unlink_class();
2190 }
1878 init_implementor(); 2191 init_implementor();
1879 } 2192
1880 2193 constants()->remove_unshareable_info();
1881 static void clear_all_breakpoints(methodOop m) { 2194
2195 for (int i = 0; i < methods()->length(); i++) {
2196 Method* m = methods()->at(i);
2197 m->remove_unshareable_info();
2198 }
2199
2200 // Need to reinstate when reading back the class.
2201 set_init_lock(NULL);
2202
2203 // do array classes also.
2204 array_klasses_do(remove_unshareable_in_class);
2205 }
2206
2207 void restore_unshareable_in_class(Klass* k, TRAPS) {
2208 k->restore_unshareable_info(CHECK);
2209 }
2210
2211 void InstanceKlass::restore_unshareable_info(TRAPS) {
2212 Klass::restore_unshareable_info(CHECK);
2213 instanceKlassHandle ik(THREAD, this);
2214
2215 Array<Method*>* methods = ik->methods();
2216 int num_methods = methods->length();
2217 for (int index2 = 0; index2 < num_methods; ++index2) {
2218 methodHandle m(THREAD, methods->at(index2));
2219 m()->link_method(m, CHECK);
2220 // restore method's vtable by calling a virtual function
2221 m->restore_vtable();
2222 }
2223 if (JvmtiExport::has_redefined_a_class()) {
2224 // Reinitialize vtable because RedefineClasses may have changed some
2225 // entries in this vtable for super classes so the CDS vtable might
2226 // point to old or obsolete entries. RedefineClasses doesn't fix up
2227 // vtables in the shared system dictionary, only the main one.
2228 // It also redefines the itable too so fix that too.
2229 ResourceMark rm(THREAD);
2230 ik->vtable()->initialize_vtable(false, CHECK);
2231 ik->itable()->initialize_itable(false, CHECK);
2232 }
2233
2234 // Allocate a simple java object for a lock.
2235 // This needs to be a java object because during class initialization
2236 // it can be held across a java call.
2237 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
2238 Handle h(THREAD, (oop)r);
2239 ik->set_init_lock(h());
2240
2241 // restore constant pool resolved references
2242 ik->constants()->restore_unshareable_info(CHECK);
2243
2244 ik->array_klasses_do(restore_unshareable_in_class, CHECK);
2245 }
2246
2247 static void clear_all_breakpoints(Method* m) {
1882 m->clear_all_breakpoints(); 2248 m->clear_all_breakpoints();
1883 } 2249 }
1884 2250
1885 void instanceKlass::release_C_heap_structures() { 2251 void InstanceKlass::release_C_heap_structures() {
1886 // Deallocate oop map cache 2252 // Deallocate oop map cache
1887 if (_oop_map_cache != NULL) { 2253 if (_oop_map_cache != NULL) {
1888 delete _oop_map_cache; 2254 delete _oop_map_cache;
1889 _oop_map_cache = NULL; 2255 _oop_map_cache = NULL;
1890 } 2256 }
1941 if (_name != NULL) _name->decrement_refcount(); 2307 if (_name != NULL) _name->decrement_refcount();
1942 // unreference array name derived from this class name (arrays of an unloaded 2308 // unreference array name derived from this class name (arrays of an unloaded
1943 // class can't be referenced anymore). 2309 // class can't be referenced anymore).
1944 if (_array_name != NULL) _array_name->decrement_refcount(); 2310 if (_array_name != NULL) _array_name->decrement_refcount();
1945 if (_source_file_name != NULL) _source_file_name->decrement_refcount(); 2311 if (_source_file_name != NULL) _source_file_name->decrement_refcount();
1946 // walk constant pool and decrement symbol reference counts
1947 _constants->unreference_symbols();
1948
1949 if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension, mtClass); 2312 if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension, mtClass);
1950 } 2313 }
1951 2314
1952 void instanceKlass::set_source_file_name(Symbol* n) { 2315 void InstanceKlass::set_source_file_name(Symbol* n) {
1953 _source_file_name = n; 2316 _source_file_name = n;
1954 if (_source_file_name != NULL) _source_file_name->increment_refcount(); 2317 if (_source_file_name != NULL) _source_file_name->increment_refcount();
1955 } 2318 }
1956 2319
1957 void instanceKlass::set_source_debug_extension(char* array, int length) { 2320 void InstanceKlass::set_source_debug_extension(char* array, int length) {
1958 if (array == NULL) { 2321 if (array == NULL) {
1959 _source_debug_extension = NULL; 2322 _source_debug_extension = NULL;
1960 } else { 2323 } else {
1961 // Adding one to the attribute length in order to store a null terminator 2324 // Adding one to the attribute length in order to store a null terminator
1962 // character could cause an overflow because the attribute length is 2325 // character could cause an overflow because the attribute length is
1970 sde[length] = '\0'; 2333 sde[length] = '\0';
1971 _source_debug_extension = sde; 2334 _source_debug_extension = sde;
1972 } 2335 }
1973 } 2336 }
1974 2337
1975 address instanceKlass::static_field_addr(int offset) { 2338 address InstanceKlass::static_field_addr(int offset) {
1976 return (address)(offset + instanceMirrorKlass::offset_of_static_fields() + (intptr_t)java_mirror()); 2339 return (address)(offset + InstanceMirrorKlass::offset_of_static_fields() + (intptr_t)java_mirror());
1977 } 2340 }
1978 2341
1979 2342
1980 const char* instanceKlass::signature_name() const { 2343 const char* InstanceKlass::signature_name() const {
1981 const char* src = (const char*) (name()->as_C_string()); 2344 const char* src = (const char*) (name()->as_C_string());
1982 const int src_length = (int)strlen(src); 2345 const int src_length = (int)strlen(src);
1983 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3); 2346 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
1984 int src_index = 0; 2347 int src_index = 0;
1985 int dest_index = 0; 2348 int dest_index = 0;
1991 dest[dest_index] = '\0'; 2354 dest[dest_index] = '\0';
1992 return dest; 2355 return dest;
1993 } 2356 }
1994 2357
1995 // different verisons of is_same_class_package 2358 // different verisons of is_same_class_package
1996 bool instanceKlass::is_same_class_package(klassOop class2) { 2359 bool InstanceKlass::is_same_class_package(Klass* class2) {
1997 klassOop class1 = as_klassOop(); 2360 Klass* class1 = this;
1998 oop classloader1 = instanceKlass::cast(class1)->class_loader(); 2361 oop classloader1 = InstanceKlass::cast(class1)->class_loader();
1999 Symbol* classname1 = Klass::cast(class1)->name(); 2362 Symbol* classname1 = Klass::cast(class1)->name();
2000 2363
2001 if (Klass::cast(class2)->oop_is_objArray()) { 2364 if (Klass::cast(class2)->oop_is_objArray()) {
2002 class2 = objArrayKlass::cast(class2)->bottom_klass(); 2365 class2 = ObjArrayKlass::cast(class2)->bottom_klass();
2003 } 2366 }
2004 oop classloader2; 2367 oop classloader2;
2005 if (Klass::cast(class2)->oop_is_instance()) { 2368 if (Klass::cast(class2)->oop_is_instance()) {
2006 classloader2 = instanceKlass::cast(class2)->class_loader(); 2369 classloader2 = InstanceKlass::cast(class2)->class_loader();
2007 } else { 2370 } else {
2008 assert(Klass::cast(class2)->oop_is_typeArray(), "should be type array"); 2371 assert(Klass::cast(class2)->oop_is_typeArray(), "should be type array");
2009 classloader2 = NULL; 2372 classloader2 = NULL;
2010 } 2373 }
2011 Symbol* classname2 = Klass::cast(class2)->name(); 2374 Symbol* classname2 = Klass::cast(class2)->name();
2012 2375
2013 return instanceKlass::is_same_class_package(classloader1, classname1, 2376 return InstanceKlass::is_same_class_package(classloader1, classname1,
2014 classloader2, classname2); 2377 classloader2, classname2);
2015 } 2378 }
2016 2379
2017 bool instanceKlass::is_same_class_package(oop classloader2, Symbol* classname2) { 2380 bool InstanceKlass::is_same_class_package(oop classloader2, Symbol* classname2) {
2018 klassOop class1 = as_klassOop(); 2381 Klass* class1 = this;
2019 oop classloader1 = instanceKlass::cast(class1)->class_loader(); 2382 oop classloader1 = InstanceKlass::cast(class1)->class_loader();
2020 Symbol* classname1 = Klass::cast(class1)->name(); 2383 Symbol* classname1 = Klass::cast(class1)->name();
2021 2384
2022 return instanceKlass::is_same_class_package(classloader1, classname1, 2385 return InstanceKlass::is_same_class_package(classloader1, classname1,
2023 classloader2, classname2); 2386 classloader2, classname2);
2024 } 2387 }
2025 2388
2026 // return true if two classes are in the same package, classloader 2389 // return true if two classes are in the same package, classloader
2027 // and classname information is enough to determine a class's package 2390 // and classname information is enough to determine a class's package
2028 bool instanceKlass::is_same_class_package(oop class_loader1, Symbol* class_name1, 2391 bool InstanceKlass::is_same_class_package(oop class_loader1, Symbol* class_name1,
2029 oop class_loader2, Symbol* class_name2) { 2392 oop class_loader2, Symbol* class_name2) {
2030 if (class_loader1 != class_loader2) { 2393 if (class_loader1 != class_loader2) {
2031 return false; 2394 return false;
2032 } else if (class_name1 == class_name2) { 2395 } else if (class_name1 == class_name2) {
2033 return true; // skip painful bytewise comparison 2396 return true; // skip painful bytewise comparison
2078 } 2441 }
2079 2442
2080 // Returns true iff super_method can be overridden by a method in targetclassname 2443 // Returns true iff super_method can be overridden by a method in targetclassname
2081 // See JSL 3rd edition 8.4.6.1 2444 // See JSL 3rd edition 8.4.6.1
2082 // Assumes name-signature match 2445 // Assumes name-signature match
2083 // "this" is instanceKlass of super_method which must exist 2446 // "this" is InstanceKlass of super_method which must exist
2084 // note that the instanceKlass of the method in the targetclassname has not always been created yet 2447 // note that the InstanceKlass of the method in the targetclassname has not always been created yet
2085 bool instanceKlass::is_override(methodHandle super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS) { 2448 bool InstanceKlass::is_override(methodHandle super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS) {
2086 // Private methods can not be overridden 2449 // Private methods can not be overridden
2087 if (super_method->is_private()) { 2450 if (super_method->is_private()) {
2088 return false; 2451 return false;
2089 } 2452 }
2090 // If super method is accessible, then override 2453 // If super method is accessible, then override
2096 assert(super_method->is_package_private(), "must be package private"); 2459 assert(super_method->is_package_private(), "must be package private");
2097 return(is_same_class_package(targetclassloader(), targetclassname)); 2460 return(is_same_class_package(targetclassloader(), targetclassname));
2098 } 2461 }
2099 2462
2100 /* defined for now in jvm.cpp, for historical reasons *-- 2463 /* defined for now in jvm.cpp, for historical reasons *--
2101 klassOop instanceKlass::compute_enclosing_class_impl(instanceKlassHandle self, 2464 Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle self,
2102 Symbol*& simple_name_result, TRAPS) { 2465 Symbol*& simple_name_result, TRAPS) {
2103 ... 2466 ...
2104 } 2467 }
2105 */ 2468 */
2106 2469
2107 // tell if two classes have the same enclosing class (at package level) 2470 // tell if two classes have the same enclosing class (at package level)
2108 bool instanceKlass::is_same_package_member_impl(instanceKlassHandle class1, 2471 bool InstanceKlass::is_same_package_member_impl(instanceKlassHandle class1,
2109 klassOop class2_oop, TRAPS) { 2472 Klass* class2_oop, TRAPS) {
2110 if (class2_oop == class1->as_klassOop()) return true; 2473 if (class2_oop == class1()) return true;
2111 if (!Klass::cast(class2_oop)->oop_is_instance()) return false; 2474 if (!Klass::cast(class2_oop)->oop_is_instance()) return false;
2112 instanceKlassHandle class2(THREAD, class2_oop); 2475 instanceKlassHandle class2(THREAD, class2_oop);
2113 2476
2114 // must be in same package before we try anything else 2477 // must be in same package before we try anything else
2115 if (!class1->is_same_class_package(class2->class_loader(), class2->name())) 2478 if (!class1->is_same_class_package(class2->class_loader(), class2->name()))
2121 for (;;) { 2484 for (;;) {
2122 // As we walk along, look for equalities between outer1 and class2. 2485 // As we walk along, look for equalities between outer1 and class2.
2123 // Eventually, the walks will terminate as outer1 stops 2486 // Eventually, the walks will terminate as outer1 stops
2124 // at the top-level class around the original class. 2487 // at the top-level class around the original class.
2125 bool ignore_inner_is_member; 2488 bool ignore_inner_is_member;
2126 klassOop next = outer1->compute_enclosing_class(&ignore_inner_is_member, 2489 Klass* next = outer1->compute_enclosing_class(&ignore_inner_is_member,
2127 CHECK_false); 2490 CHECK_false);
2128 if (next == NULL) break; 2491 if (next == NULL) break;
2129 if (next == class2()) return true; 2492 if (next == class2()) return true;
2130 outer1 = instanceKlassHandle(THREAD, next); 2493 outer1 = instanceKlassHandle(THREAD, next);
2131 } 2494 }
2132 2495
2133 // Now do the same for class2. 2496 // Now do the same for class2.
2134 instanceKlassHandle outer2 = class2; 2497 instanceKlassHandle outer2 = class2;
2135 for (;;) { 2498 for (;;) {
2136 bool ignore_inner_is_member; 2499 bool ignore_inner_is_member;
2137 klassOop next = outer2->compute_enclosing_class(&ignore_inner_is_member, 2500 Klass* next = outer2->compute_enclosing_class(&ignore_inner_is_member,
2138 CHECK_false); 2501 CHECK_false);
2139 if (next == NULL) break; 2502 if (next == NULL) break;
2140 // Might as well check the new outer against all available values. 2503 // Might as well check the new outer against all available values.
2141 if (next == class1()) return true; 2504 if (next == class1()) return true;
2142 if (next == outer1()) return true; 2505 if (next == outer1()) return true;
2147 // two classes, we know they are in separate package members. 2510 // two classes, we know they are in separate package members.
2148 return false; 2511 return false;
2149 } 2512 }
2150 2513
2151 2514
2152 jint instanceKlass::compute_modifier_flags(TRAPS) const { 2515 jint InstanceKlass::compute_modifier_flags(TRAPS) const {
2153 klassOop k = as_klassOop();
2154 jint access = access_flags().as_int(); 2516 jint access = access_flags().as_int();
2155 2517
2156 // But check if it happens to be member class. 2518 // But check if it happens to be member class.
2157 instanceKlassHandle ik(THREAD, k); 2519 instanceKlassHandle ik(THREAD, this);
2158 InnerClassesIterator iter(ik); 2520 InnerClassesIterator iter(ik);
2159 for (; !iter.done(); iter.next()) { 2521 for (; !iter.done(); iter.next()) {
2160 int ioff = iter.inner_class_info_index(); 2522 int ioff = iter.inner_class_info_index();
2161 // Inner class attribute can be zero, skip it. 2523 // Inner class attribute can be zero, skip it.
2162 // Strange but true: JVM spec. allows null inner class refs. 2524 // Strange but true: JVM spec. allows null inner class refs.
2173 } 2535 }
2174 // Remember to strip ACC_SUPER bit 2536 // Remember to strip ACC_SUPER bit
2175 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS; 2537 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS;
2176 } 2538 }
2177 2539
2178 jint instanceKlass::jvmti_class_status() const { 2540 jint InstanceKlass::jvmti_class_status() const {
2179 jint result = 0; 2541 jint result = 0;
2180 2542
2181 if (is_linked()) { 2543 if (is_linked()) {
2182 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED; 2544 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
2183 } 2545 }
2190 result |= JVMTI_CLASS_STATUS_ERROR; 2552 result |= JVMTI_CLASS_STATUS_ERROR;
2191 } 2553 }
2192 return result; 2554 return result;
2193 } 2555 }
2194 2556
2195 methodOop instanceKlass::method_at_itable(klassOop holder, int index, TRAPS) { 2557 Method* InstanceKlass::method_at_itable(Klass* holder, int index, TRAPS) {
2196 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable(); 2558 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2197 int method_table_offset_in_words = ioe->offset()/wordSize; 2559 int method_table_offset_in_words = ioe->offset()/wordSize;
2198 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words()) 2560 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2199 / itableOffsetEntry::size(); 2561 / itableOffsetEntry::size();
2200 2562
2201 for (int cnt = 0 ; ; cnt ++, ioe ++) { 2563 for (int cnt = 0 ; ; cnt ++, ioe ++) {
2202 // If the interface isn't implemented by the receiver class, 2564 // If the interface isn't implemented by the receiver class,
2203 // the VM should throw IncompatibleClassChangeError. 2565 // the VM should throw IncompatibleClassChangeError.
2204 if (cnt >= nof_interfaces) { 2566 if (cnt >= nof_interfaces) {
2205 THROW_0(vmSymbols::java_lang_IncompatibleClassChangeError()); 2567 THROW_NULL(vmSymbols::java_lang_IncompatibleClassChangeError());
2206 } 2568 }
2207 2569
2208 klassOop ik = ioe->interface_klass(); 2570 Klass* ik = ioe->interface_klass();
2209 if (ik == holder) break; 2571 if (ik == holder) break;
2210 } 2572 }
2211 2573
2212 itableMethodEntry* ime = ioe->first_method_entry(as_klassOop()); 2574 itableMethodEntry* ime = ioe->first_method_entry(this);
2213 methodOop m = ime[index].method(); 2575 Method* m = ime[index].method();
2214 if (m == NULL) { 2576 if (m == NULL) {
2215 THROW_0(vmSymbols::java_lang_AbstractMethodError()); 2577 THROW_NULL(vmSymbols::java_lang_AbstractMethodError());
2216 } 2578 }
2217 return m; 2579 return m;
2218 } 2580 }
2219 2581
2220 // On-stack replacement stuff 2582 // On-stack replacement stuff
2221 void instanceKlass::add_osr_nmethod(nmethod* n) { 2583 void InstanceKlass::add_osr_nmethod(nmethod* n) {
2222 // only one compilation can be active 2584 // only one compilation can be active
2223 NEEDS_CLEANUP 2585 NEEDS_CLEANUP
2224 // This is a short non-blocking critical region, so the no safepoint check is ok. 2586 // This is a short non-blocking critical region, so the no safepoint check is ok.
2225 OsrList_lock->lock_without_safepoint_check(); 2587 OsrList_lock->lock_without_safepoint_check();
2226 assert(n->is_osr_method(), "wrong kind of nmethod"); 2588 assert(n->is_osr_method(), "wrong kind of nmethod");
2227 n->set_osr_link(osr_nmethods_head()); 2589 n->set_osr_link(osr_nmethods_head());
2228 set_osr_nmethods_head(n); 2590 set_osr_nmethods_head(n);
2229 // Raise the highest osr level if necessary 2591 // Raise the highest osr level if necessary
2230 if (TieredCompilation) { 2592 if (TieredCompilation) {
2231 methodOop m = n->method(); 2593 Method* m = n->method();
2232 m->set_highest_osr_comp_level(MAX2(m->highest_osr_comp_level(), n->comp_level())); 2594 m->set_highest_osr_comp_level(MAX2(m->highest_osr_comp_level(), n->comp_level()));
2233 } 2595 }
2234 // Remember to unlock again 2596 // Remember to unlock again
2235 OsrList_lock->unlock(); 2597 OsrList_lock->unlock();
2236 2598
2244 } 2606 }
2245 } 2607 }
2246 } 2608 }
2247 2609
2248 2610
2249 void instanceKlass::remove_osr_nmethod(nmethod* n) { 2611 void InstanceKlass::remove_osr_nmethod(nmethod* n) {
2250 // This is a short non-blocking critical region, so the no safepoint check is ok. 2612 // This is a short non-blocking critical region, so the no safepoint check is ok.
2251 OsrList_lock->lock_without_safepoint_check(); 2613 OsrList_lock->lock_without_safepoint_check();
2252 assert(n->is_osr_method(), "wrong kind of nmethod"); 2614 assert(n->is_osr_method(), "wrong kind of nmethod");
2253 nmethod* last = NULL; 2615 nmethod* last = NULL;
2254 nmethod* cur = osr_nmethods_head(); 2616 nmethod* cur = osr_nmethods_head();
2255 int max_level = CompLevel_none; // Find the max comp level excluding n 2617 int max_level = CompLevel_none; // Find the max comp level excluding n
2256 methodOop m = n->method(); 2618 Method* m = n->method();
2257 // Search for match 2619 // Search for match
2258 while(cur != NULL && cur != n) { 2620 while(cur != NULL && cur != n) {
2259 if (TieredCompilation) { 2621 if (TieredCompilation) {
2260 // Find max level before n 2622 // Find max level before n
2261 max_level = MAX2(max_level, cur->comp_level()); 2623 max_level = MAX2(max_level, cur->comp_level());
2285 } 2647 }
2286 // Remember to unlock again 2648 // Remember to unlock again
2287 OsrList_lock->unlock(); 2649 OsrList_lock->unlock();
2288 } 2650 }
2289 2651
2290 nmethod* instanceKlass::lookup_osr_nmethod(const methodOop m, int bci, int comp_level, bool match_level) const { 2652 nmethod* InstanceKlass::lookup_osr_nmethod(Method* const m, int bci, int comp_level, bool match_level) const {
2291 // This is a short non-blocking critical region, so the no safepoint check is ok. 2653 // This is a short non-blocking critical region, so the no safepoint check is ok.
2292 OsrList_lock->lock_without_safepoint_check(); 2654 OsrList_lock->lock_without_safepoint_check();
2293 nmethod* osr = osr_nmethods_head(); 2655 nmethod* osr = osr_nmethods_head();
2294 nmethod* best = NULL; 2656 nmethod* best = NULL;
2295 while (osr != NULL) { 2657 while (osr != NULL) {
2327 } 2689 }
2328 return NULL; 2690 return NULL;
2329 } 2691 }
2330 2692
2331 // ----------------------------------------------------------------------------------------------------- 2693 // -----------------------------------------------------------------------------------------------------
2332
2333
2334 // Printing 2694 // Printing
2335 2695
2696 #ifndef PRODUCT
2697
2336 #define BULLET " - " 2698 #define BULLET " - "
2699
2700 static const char* state_names[] = {
2701 "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
2702 };
2703
2704 void InstanceKlass::print_on(outputStream* st) const {
2705 assert(is_klass(), "must be klass");
2706 Klass::print_on(st);
2707
2708 st->print(BULLET"instance size: %d", size_helper()); st->cr();
2709 st->print(BULLET"klass size: %d", size()); st->cr();
2710 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
2711 st->print(BULLET"state: "); st->print_cr(state_names[_init_state]);
2712 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
2713 st->print(BULLET"super: "); super()->print_value_on_maybe_null(st); st->cr();
2714 st->print(BULLET"sub: ");
2715 Klass* sub = subklass();
2716 int n;
2717 for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
2718 if (n < MaxSubklassPrintSize) {
2719 sub->print_value_on(st);
2720 st->print(" ");
2721 }
2722 }
2723 if (n >= MaxSubklassPrintSize) st->print("(%d more klasses...)", n - MaxSubklassPrintSize);
2724 st->cr();
2725
2726 if (is_interface()) {
2727 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
2728 if (nof_implementors() == 1) {
2729 st->print_cr(BULLET"implementor: ");
2730 st->print(" ");
2731 implementor()->print_value_on(st);
2732 st->cr();
2733 }
2734 }
2735
2736 st->print(BULLET"arrays: "); array_klasses()->print_value_on_maybe_null(st); st->cr();
2737 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr();
2738 if (Verbose) {
2739 Array<Method*>* method_array = methods();
2740 for(int i = 0; i < method_array->length(); i++) {
2741 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
2742 }
2743 }
2744 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr();
2745 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
2746 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
2747 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
2748 if (class_loader_data() != NULL) {
2749 st->print(BULLET"class loader data: ");
2750 class_loader_data()->print_value_on(st);
2751 st->cr();
2752 }
2753 st->print(BULLET"protection domain: "); ((InstanceKlass*)this)->protection_domain()->print_value_on(st); st->cr();
2754 st->print(BULLET"host class: "); host_klass()->print_value_on_maybe_null(st); st->cr();
2755 st->print(BULLET"signers: "); signers()->print_value_on(st); st->cr();
2756 st->print(BULLET"init_lock: "); ((oop)init_lock())->print_value_on(st); st->cr();
2757 if (source_file_name() != NULL) {
2758 st->print(BULLET"source file: ");
2759 source_file_name()->print_value_on(st);
2760 st->cr();
2761 }
2762 if (source_debug_extension() != NULL) {
2763 st->print(BULLET"source debug extension: ");
2764 st->print("%s", source_debug_extension());
2765 st->cr();
2766 }
2767 st->print(BULLET"annotations: "); annotations()->print_value_on(st); st->cr();
2768 {
2769 ResourceMark rm;
2770 // PreviousVersionInfo objects returned via PreviousVersionWalker
2771 // contain a GrowableArray of handles. We have to clean up the
2772 // GrowableArray _after_ the PreviousVersionWalker destructor
2773 // has destroyed the handles.
2774 {
2775 bool have_pv = false;
2776 PreviousVersionWalker pvw((InstanceKlass*)this);
2777 for (PreviousVersionInfo * pv_info = pvw.next_previous_version();
2778 pv_info != NULL; pv_info = pvw.next_previous_version()) {
2779 if (!have_pv)
2780 st->print(BULLET"previous version: ");
2781 have_pv = true;
2782 pv_info->prev_constant_pool_handle()()->print_value_on(st);
2783 }
2784 if (have_pv) st->cr();
2785 } // pvw is cleaned up
2786 } // rm is cleaned up
2787
2788 if (generic_signature() != NULL) {
2789 st->print(BULLET"generic signature: ");
2790 generic_signature()->print_value_on(st);
2791 st->cr();
2792 }
2793 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
2794 st->print(BULLET"java mirror: "); java_mirror()->print_value_on(st); st->cr();
2795 st->print(BULLET"vtable length %d (start addr: " INTPTR_FORMAT ")", vtable_length(), start_of_vtable()); st->cr();
2796 st->print(BULLET"itable length %d (start addr: " INTPTR_FORMAT ")", itable_length(), start_of_itable()); st->cr();
2797 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
2798 FieldPrinter print_static_field(st);
2799 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
2800 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
2801 FieldPrinter print_nonstatic_field(st);
2802 ((InstanceKlass*)this)->do_nonstatic_fields(&print_nonstatic_field);
2803
2804 st->print(BULLET"non-static oop maps: ");
2805 OopMapBlock* map = start_of_nonstatic_oop_maps();
2806 OopMapBlock* end_map = map + nonstatic_oop_map_count();
2807 while (map < end_map) {
2808 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
2809 map++;
2810 }
2811 st->cr();
2812 }
2813
2814 #endif //PRODUCT
2815
2816 void InstanceKlass::print_value_on(outputStream* st) const {
2817 assert(is_klass(), "must be klass");
2818 name()->print_value_on(st);
2819 }
2820
2821 #ifndef PRODUCT
2337 2822
2338 void FieldPrinter::do_field(fieldDescriptor* fd) { 2823 void FieldPrinter::do_field(fieldDescriptor* fd) {
2339 _st->print(BULLET); 2824 _st->print(BULLET);
2340 if (_obj == NULL) { 2825 if (_obj == NULL) {
2341 fd->print_on(_st); 2826 fd->print_on(_st);
2345 _st->cr(); 2830 _st->cr();
2346 } 2831 }
2347 } 2832 }
2348 2833
2349 2834
2350 void instanceKlass::oop_print_on(oop obj, outputStream* st) { 2835 void InstanceKlass::oop_print_on(oop obj, outputStream* st) {
2351 Klass::oop_print_on(obj, st); 2836 Klass::oop_print_on(obj, st);
2352 2837
2353 if (as_klassOop() == SystemDictionary::String_klass()) { 2838 if (this == SystemDictionary::String_klass()) {
2354 typeArrayOop value = java_lang_String::value(obj); 2839 typeArrayOop value = java_lang_String::value(obj);
2355 juint offset = java_lang_String::offset(obj); 2840 juint offset = java_lang_String::offset(obj);
2356 juint length = java_lang_String::length(obj); 2841 juint length = java_lang_String::length(obj);
2357 if (value != NULL && 2842 if (value != NULL &&
2358 value->is_typeArray() && 2843 value->is_typeArray() &&
2368 2853
2369 st->print_cr(BULLET"---- fields (total size %d words):", oop_size(obj)); 2854 st->print_cr(BULLET"---- fields (total size %d words):", oop_size(obj));
2370 FieldPrinter print_field(st, obj); 2855 FieldPrinter print_field(st, obj);
2371 do_nonstatic_fields(&print_field); 2856 do_nonstatic_fields(&print_field);
2372 2857
2373 if (as_klassOop() == SystemDictionary::Class_klass()) { 2858 if (this == SystemDictionary::Class_klass()) {
2374 st->print(BULLET"signature: "); 2859 st->print(BULLET"signature: ");
2375 java_lang_Class::print_signature(obj, st); 2860 java_lang_Class::print_signature(obj, st);
2376 st->cr(); 2861 st->cr();
2377 klassOop mirrored_klass = java_lang_Class::as_klassOop(obj); 2862 Klass* mirrored_klass = java_lang_Class::as_Klass(obj);
2378 st->print(BULLET"fake entry for mirror: "); 2863 st->print(BULLET"fake entry for mirror: ");
2379 mirrored_klass->print_value_on(st); 2864 mirrored_klass->print_value_on_maybe_null(st);
2380 st->cr(); 2865 st->cr();
2381 st->print(BULLET"fake entry resolved_constructor: "); 2866 st->print(BULLET"fake entry resolved_constructor: ");
2382 methodOop ctor = java_lang_Class::resolved_constructor(obj); 2867 Method* ctor = java_lang_Class::resolved_constructor(obj);
2383 ctor->print_value_on(st); 2868 ctor->print_value_on_maybe_null(st);
2384 klassOop array_klass = java_lang_Class::array_klass(obj); 2869 Klass* array_klass = java_lang_Class::array_klass(obj);
2385 st->cr(); 2870 st->cr();
2386 st->print(BULLET"fake entry for array: "); 2871 st->print(BULLET"fake entry for array: ");
2387 array_klass->print_value_on(st); 2872 array_klass->print_value_on_maybe_null(st);
2388 st->cr(); 2873 st->cr();
2389 st->print_cr(BULLET"fake entry for oop_size: %d", java_lang_Class::oop_size(obj)); 2874 st->print_cr(BULLET"fake entry for oop_size: %d", java_lang_Class::oop_size(obj));
2390 st->print_cr(BULLET"fake entry for static_oop_field_count: %d", java_lang_Class::static_oop_field_count(obj)); 2875 st->print_cr(BULLET"fake entry for static_oop_field_count: %d", java_lang_Class::static_oop_field_count(obj));
2391 klassOop real_klass = java_lang_Class::as_klassOop(obj); 2876 Klass* real_klass = java_lang_Class::as_Klass(obj);
2392 if (real_klass != NULL && real_klass->klass_part()->oop_is_instance()) { 2877 if (real_klass != NULL && real_klass->oop_is_instance()) {
2393 instanceKlass::cast(real_klass)->do_local_static_fields(&print_field); 2878 InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
2394 } 2879 }
2395 } else if (as_klassOop() == SystemDictionary::MethodType_klass()) { 2880 } else if (this == SystemDictionary::MethodType_klass()) {
2396 st->print(BULLET"signature: "); 2881 st->print(BULLET"signature: ");
2397 java_lang_invoke_MethodType::print_signature(obj, st); 2882 java_lang_invoke_MethodType::print_signature(obj, st);
2398 st->cr(); 2883 st->cr();
2399 } 2884 }
2400 } 2885 }
2401 2886
2402 2887 #endif
2403 2888
2404 void instanceKlass::oop_print_value_on(oop obj, outputStream* st) { 2889 void InstanceKlass::oop_print_value_on(oop obj, outputStream* st) {
2405 st->print("a "); 2890 st->print("a ");
2406 name()->print_value_on(st); 2891 name()->print_value_on(st);
2407 obj->print_address_on(st); 2892 obj->print_address_on(st);
2408 if (as_klassOop() == SystemDictionary::String_klass() 2893 if (this == SystemDictionary::String_klass()
2409 && java_lang_String::value(obj) != NULL) { 2894 && java_lang_String::value(obj) != NULL) {
2410 ResourceMark rm; 2895 ResourceMark rm;
2411 int len = java_lang_String::length(obj); 2896 int len = java_lang_String::length(obj);
2412 int plen = (len < 24 ? len : 12); 2897 int plen = (len < 24 ? len : 12);
2413 char* str = java_lang_String::as_utf8_string(obj, 0, plen); 2898 char* str = java_lang_String::as_utf8_string(obj, 0, plen);
2414 st->print(" = \"%s\"", str); 2899 st->print(" = \"%s\"", str);
2415 if (len > plen) 2900 if (len > plen)
2416 st->print("...[%d]", len); 2901 st->print("...[%d]", len);
2417 } else if (as_klassOop() == SystemDictionary::Class_klass()) { 2902 } else if (this == SystemDictionary::Class_klass()) {
2418 klassOop k = java_lang_Class::as_klassOop(obj); 2903 Klass* k = java_lang_Class::as_Klass(obj);
2419 st->print(" = "); 2904 st->print(" = ");
2420 if (k != NULL) { 2905 if (k != NULL) {
2421 k->print_value_on(st); 2906 k->print_value_on(st);
2422 } else { 2907 } else {
2423 const char* tname = type2name(java_lang_Class::primitive_type(obj)); 2908 const char* tname = type2name(java_lang_Class::primitive_type(obj));
2424 st->print("%s", tname ? tname : "type?"); 2909 st->print("%s", tname ? tname : "type?");
2425 } 2910 }
2426 } else if (as_klassOop() == SystemDictionary::MethodType_klass()) { 2911 } else if (this == SystemDictionary::MethodType_klass()) {
2427 st->print(" = "); 2912 st->print(" = ");
2428 java_lang_invoke_MethodType::print_signature(obj, st); 2913 java_lang_invoke_MethodType::print_signature(obj, st);
2429 } else if (java_lang_boxing_object::is_instance(obj)) { 2914 } else if (java_lang_boxing_object::is_instance(obj)) {
2430 st->print(" = "); 2915 st->print(" = ");
2431 java_lang_boxing_object::print(obj, st); 2916 java_lang_boxing_object::print(obj, st);
2432 } else if (as_klassOop() == SystemDictionary::LambdaForm_klass()) { 2917 } else if (this == SystemDictionary::LambdaForm_klass()) {
2433 oop vmentry = java_lang_invoke_LambdaForm::vmentry(obj); 2918 oop vmentry = java_lang_invoke_LambdaForm::vmentry(obj);
2434 if (vmentry != NULL) { 2919 if (vmentry != NULL) {
2435 st->print(" => "); 2920 st->print(" => ");
2436 vmentry->print_value_on(st); 2921 vmentry->print_value_on(st);
2437 } 2922 }
2438 } else if (as_klassOop() == SystemDictionary::MemberName_klass()) { 2923 } else if (this == SystemDictionary::MemberName_klass()) {
2439 oop vmtarget = java_lang_invoke_MemberName::vmtarget(obj); 2924 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(obj);
2440 if (vmtarget != NULL) { 2925 if (vmtarget != NULL) {
2441 st->print(" = "); 2926 st->print(" = ");
2442 vmtarget->print_value_on(st); 2927 vmtarget->print_value_on(st);
2443 } else { 2928 } else {
2444 java_lang_invoke_MemberName::clazz(obj)->print_value_on(st); 2929 java_lang_invoke_MemberName::clazz(obj)->print_value_on(st);
2446 java_lang_invoke_MemberName::name(obj)->print_value_on(st); 2931 java_lang_invoke_MemberName::name(obj)->print_value_on(st);
2447 } 2932 }
2448 } 2933 }
2449 } 2934 }
2450 2935
2451 const char* instanceKlass::internal_name() const { 2936 const char* InstanceKlass::internal_name() const {
2452 return external_name(); 2937 return external_name();
2453 } 2938 }
2454 2939
2455 // Verification 2940 // Verification
2456 2941
2457 class VerifyFieldClosure: public OopClosure { 2942 class VerifyFieldClosure: public OopClosure {
2458 protected: 2943 protected:
2459 template <class T> void do_oop_work(T* p) { 2944 template <class T> void do_oop_work(T* p) {
2460 guarantee(Universe::heap()->is_in_closed_subset(p), "should be in heap");
2461 oop obj = oopDesc::load_decode_heap_oop(p); 2945 oop obj = oopDesc::load_decode_heap_oop(p);
2462 if (!obj->is_oop_or_null()) { 2946 if (!obj->is_oop_or_null()) {
2463 tty->print_cr("Failed: " PTR_FORMAT " -> " PTR_FORMAT, p, (address)obj); 2947 tty->print_cr("Failed: " PTR_FORMAT " -> " PTR_FORMAT, p, (address)obj);
2464 Universe::print(); 2948 Universe::print();
2465 guarantee(false, "boom"); 2949 guarantee(false, "boom");
2468 public: 2952 public:
2469 virtual void do_oop(oop* p) { VerifyFieldClosure::do_oop_work(p); } 2953 virtual void do_oop(oop* p) { VerifyFieldClosure::do_oop_work(p); }
2470 virtual void do_oop(narrowOop* p) { VerifyFieldClosure::do_oop_work(p); } 2954 virtual void do_oop(narrowOop* p) { VerifyFieldClosure::do_oop_work(p); }
2471 }; 2955 };
2472 2956
2473 void instanceKlass::oop_verify_on(oop obj, outputStream* st) { 2957 void InstanceKlass::verify_on(outputStream* st) {
2958 Klass::verify_on(st);
2959 Thread *thread = Thread::current();
2960
2961 #ifndef PRODUCT
2962 // Avoid redundant verifies
2963 if (_verify_count == Universe::verify_count()) return;
2964 _verify_count = Universe::verify_count();
2965 #endif
2966 // Verify that klass is present in SystemDictionary
2967 if (is_loaded() && !is_anonymous()) {
2968 Symbol* h_name = name();
2969 SystemDictionary::verify_obj_klass_present(h_name, class_loader_data());
2970 }
2971
2972 // Verify static fields
2973 VerifyFieldClosure blk;
2974
2975 // Verify vtables
2976 if (is_linked()) {
2977 ResourceMark rm(thread);
2978 // $$$ This used to be done only for m/s collections. Doing it
2979 // always seemed a valid generalization. (DLD -- 6/00)
2980 vtable()->verify(st);
2981 }
2982
2983 // Verify first subklass
2984 if (subklass_oop() != NULL) {
2985 guarantee(subklass_oop()->is_metadata(), "should be in metaspace");
2986 guarantee(subklass_oop()->is_klass(), "should be klass");
2987 }
2988
2989 // Verify siblings
2990 Klass* super = this->super();
2991 Klass* sib = next_sibling();
2992 if (sib != NULL) {
2993 if (sib == this) {
2994 fatal(err_msg("subclass points to itself " PTR_FORMAT, sib));
2995 }
2996
2997 guarantee(sib->is_metadata(), "should be in metaspace");
2998 guarantee(sib->is_klass(), "should be klass");
2999 guarantee(sib->super() == super, "siblings should have same superklass");
3000 }
3001
3002 // Verify implementor fields
3003 Klass* im = implementor();
3004 if (im != NULL) {
3005 guarantee(is_interface(), "only interfaces should have implementor set");
3006 guarantee(im->is_klass(), "should be klass");
3007 guarantee(!Klass::cast(im)->is_interface() || im == this,
3008 "implementors cannot be interfaces");
3009 }
3010
3011 // Verify local interfaces
3012 if (local_interfaces()) {
3013 Array<Klass*>* local_interfaces = this->local_interfaces();
3014 for (int j = 0; j < local_interfaces->length(); j++) {
3015 Klass* e = local_interfaces->at(j);
3016 guarantee(e->is_klass() && Klass::cast(e)->is_interface(), "invalid local interface");
3017 }
3018 }
3019
3020 // Verify transitive interfaces
3021 if (transitive_interfaces() != NULL) {
3022 Array<Klass*>* transitive_interfaces = this->transitive_interfaces();
3023 for (int j = 0; j < transitive_interfaces->length(); j++) {
3024 Klass* e = transitive_interfaces->at(j);
3025 guarantee(e->is_klass() && Klass::cast(e)->is_interface(), "invalid transitive interface");
3026 }
3027 }
3028
3029 // Verify methods
3030 if (methods() != NULL) {
3031 Array<Method*>* methods = this->methods();
3032 for (int j = 0; j < methods->length(); j++) {
3033 guarantee(methods->at(j)->is_metadata(), "should be in metaspace");
3034 guarantee(methods->at(j)->is_method(), "non-method in methods array");
3035 }
3036 for (int j = 0; j < methods->length() - 1; j++) {
3037 Method* m1 = methods->at(j);
3038 Method* m2 = methods->at(j + 1);
3039 guarantee(m1->name()->fast_compare(m2->name()) <= 0, "methods not sorted correctly");
3040 }
3041 }
3042
3043 // Verify method ordering
3044 if (method_ordering() != NULL) {
3045 Array<int>* method_ordering = this->method_ordering();
3046 int length = method_ordering->length();
3047 if (JvmtiExport::can_maintain_original_method_order() ||
3048 (UseSharedSpaces && length != 0)) {
3049 guarantee(length == methods()->length(), "invalid method ordering length");
3050 jlong sum = 0;
3051 for (int j = 0; j < length; j++) {
3052 int original_index = method_ordering->at(j);
3053 guarantee(original_index >= 0, "invalid method ordering index");
3054 guarantee(original_index < length, "invalid method ordering index");
3055 sum += original_index;
3056 }
3057 // Verify sum of indices 0,1,...,length-1
3058 guarantee(sum == ((jlong)length*(length-1))/2, "invalid method ordering sum");
3059 } else {
3060 guarantee(length == 0, "invalid method ordering length");
3061 }
3062 }
3063
3064 // Verify JNI static field identifiers
3065 if (jni_ids() != NULL) {
3066 jni_ids()->verify(this);
3067 }
3068
3069 // Verify other fields
3070 if (array_klasses() != NULL) {
3071 guarantee(array_klasses()->is_metadata(), "should be in metaspace");
3072 guarantee(array_klasses()->is_klass(), "should be klass");
3073 }
3074 if (constants() != NULL) {
3075 guarantee(constants()->is_metadata(), "should be in metaspace");
3076 guarantee(constants()->is_constantPool(), "should be constant pool");
3077 }
3078 if (protection_domain() != NULL) {
3079 guarantee(protection_domain()->is_oop(), "should be oop");
3080 }
3081 if (host_klass() != NULL) {
3082 guarantee(host_klass()->is_metadata(), "should be in metaspace");
3083 guarantee(host_klass()->is_klass(), "should be klass");
3084 }
3085 if (signers() != NULL) {
3086 guarantee(signers()->is_objArray(), "should be obj array");
3087 }
3088 }
3089
3090 void InstanceKlass::oop_verify_on(oop obj, outputStream* st) {
2474 Klass::oop_verify_on(obj, st); 3091 Klass::oop_verify_on(obj, st);
2475 VerifyFieldClosure blk; 3092 VerifyFieldClosure blk;
2476 oop_oop_iterate(obj, &blk); 3093 obj->oop_iterate_no_header(&blk);
2477 } 3094 }
3095
2478 3096
2479 // JNIid class for jfieldIDs only 3097 // JNIid class for jfieldIDs only
2480 // Note to reviewers: 3098 // Note to reviewers:
2481 // These JNI functions are just moved over to column 1 and not changed 3099 // These JNI functions are just moved over to column 1 and not changed
2482 // in the compressed oops workspace. 3100 // in the compressed oops workspace.
2483 JNIid::JNIid(klassOop holder, int offset, JNIid* next) { 3101 JNIid::JNIid(Klass* holder, int offset, JNIid* next) {
2484 _holder = holder; 3102 _holder = holder;
2485 _offset = offset; 3103 _offset = offset;
2486 _next = next; 3104 _next = next;
2487 debug_only(_is_static_field_id = false;) 3105 debug_only(_is_static_field_id = false;)
2488 } 3106 }
2495 current = current->next(); 3113 current = current->next();
2496 } 3114 }
2497 return NULL; 3115 return NULL;
2498 } 3116 }
2499 3117
2500 void JNIid::oops_do(OopClosure* f) {
2501 for (JNIid* cur = this; cur != NULL; cur = cur->next()) {
2502 f->do_oop(cur->holder_addr());
2503 }
2504 }
2505
2506 void JNIid::deallocate(JNIid* current) { 3118 void JNIid::deallocate(JNIid* current) {
2507 while (current != NULL) { 3119 while (current != NULL) {
2508 JNIid* next = current->next(); 3120 JNIid* next = current->next();
2509 delete current; 3121 delete current;
2510 current = next; 3122 current = next;
2511 } 3123 }
2512 } 3124 }
2513 3125
2514 3126
2515 void JNIid::verify(klassOop holder) { 3127 void JNIid::verify(Klass* holder) {
2516 int first_field_offset = instanceMirrorKlass::offset_of_static_fields(); 3128 int first_field_offset = InstanceMirrorKlass::offset_of_static_fields();
2517 int end_field_offset; 3129 int end_field_offset;
2518 end_field_offset = first_field_offset + (instanceKlass::cast(holder)->static_field_size() * wordSize); 3130 end_field_offset = first_field_offset + (InstanceKlass::cast(holder)->static_field_size() * wordSize);
2519 3131
2520 JNIid* current = this; 3132 JNIid* current = this;
2521 while (current != NULL) { 3133 while (current != NULL) {
2522 guarantee(current->holder() == holder, "Invalid klass in JNIid"); 3134 guarantee(current->holder() == holder, "Invalid klass in JNIid");
2523 #ifdef ASSERT 3135 #ifdef ASSERT
2530 } 3142 }
2531 } 3143 }
2532 3144
2533 3145
2534 #ifdef ASSERT 3146 #ifdef ASSERT
2535 void instanceKlass::set_init_state(ClassState state) { 3147 void InstanceKlass::set_init_state(ClassState state) {
2536 bool good_state = as_klassOop()->is_shared() ? (_init_state <= state) 3148 bool good_state = is_shared() ? (_init_state <= state)
2537 : (_init_state < state); 3149 : (_init_state < state);
2538 assert(good_state || state == allocated, "illegal state transition"); 3150 assert(good_state || state == allocated, "illegal state transition");
2539 _init_state = (u1)state; 3151 _init_state = (u1)state;
2540 } 3152 }
2541 #endif 3153 #endif
2542 3154
2543 3155
2544 // RedefineClasses() support for previous versions: 3156 // RedefineClasses() support for previous versions:
2545 3157
2546 // Add an information node that contains weak references to the 3158 // Purge previous versions
3159 static void purge_previous_versions_internal(InstanceKlass* ik, int emcp_method_count) {
3160 if (ik->previous_versions() != NULL) {
3161 // This klass has previous versions so see what we can cleanup
3162 // while it is safe to do so.
3163
3164 int deleted_count = 0; // leave debugging breadcrumbs
3165 int live_count = 0;
3166 ClassLoaderData* loader_data = ik->class_loader_data() == NULL ?
3167 ClassLoaderData::the_null_class_loader_data() :
3168 ik->class_loader_data();
3169
3170 // RC_TRACE macro has an embedded ResourceMark
3171 RC_TRACE(0x00000200, ("purge: %s: previous version length=%d",
3172 ik->external_name(), ik->previous_versions()->length()));
3173
3174 for (int i = ik->previous_versions()->length() - 1; i >= 0; i--) {
3175 // check the previous versions array
3176 PreviousVersionNode * pv_node = ik->previous_versions()->at(i);
3177 ConstantPool* cp_ref = pv_node->prev_constant_pool();
3178 assert(cp_ref != NULL, "cp ref was unexpectedly cleared");
3179
3180 ConstantPool* pvcp = cp_ref;
3181 if (!pvcp->on_stack()) {
3182 // If the constant pool isn't on stack, none of the methods
3183 // are executing. Delete all the methods, the constant pool and
3184 // and this previous version node.
3185 GrowableArray<Method*>* method_refs = pv_node->prev_EMCP_methods();
3186 if (method_refs != NULL) {
3187 for (int j = method_refs->length() - 1; j >= 0; j--) {
3188 Method* method = method_refs->at(j);
3189 assert(method != NULL, "method ref was unexpectedly cleared");
3190 method_refs->remove_at(j);
3191 // method will be freed with associated class.
3192 }
3193 }
3194 // Remove the constant pool
3195 delete pv_node;
3196 // Since we are traversing the array backwards, we don't have to
3197 // do anything special with the index.
3198 ik->previous_versions()->remove_at(i);
3199 deleted_count++;
3200 continue;
3201 } else {
3202 RC_TRACE(0x00000200, ("purge: previous version @%d is alive", i));
3203 assert(pvcp->pool_holder() != NULL, "Constant pool with no holder");
3204 guarantee (!loader_data->is_unloading(), "unloaded classes can't be on the stack");
3205 live_count++;
3206 }
3207
3208 // At least one method is live in this previous version, clean out
3209 // the others or mark them as obsolete.
3210 GrowableArray<Method*>* method_refs = pv_node->prev_EMCP_methods();
3211 if (method_refs != NULL) {
3212 RC_TRACE(0x00000200, ("purge: previous methods length=%d",
3213 method_refs->length()));
3214 for (int j = method_refs->length() - 1; j >= 0; j--) {
3215 Method* method = method_refs->at(j);
3216 assert(method != NULL, "method ref was unexpectedly cleared");
3217
3218 // Remove the emcp method if it's not executing
3219 // If it's been made obsolete by a redefinition of a non-emcp
3220 // method, mark it as obsolete but leave it to clean up later.
3221 if (!method->on_stack()) {
3222 method_refs->remove_at(j);
3223 } else if (emcp_method_count == 0) {
3224 method->set_is_obsolete();
3225 } else {
3226 // RC_TRACE macro has an embedded ResourceMark
3227 RC_TRACE(0x00000200,
3228 ("purge: %s(%s): prev method @%d in version @%d is alive",
3229 method->name()->as_C_string(),
3230 method->signature()->as_C_string(), j, i));
3231 }
3232 }
3233 }
3234 }
3235 assert(ik->previous_versions()->length() == live_count, "sanity check");
3236 RC_TRACE(0x00000200,
3237 ("purge: previous version stats: live=%d, deleted=%d", live_count,
3238 deleted_count));
3239 }
3240 }
3241
3242 // External interface for use during class unloading.
3243 void InstanceKlass::purge_previous_versions(InstanceKlass* ik) {
3244 // Call with >0 emcp methods since they are not currently being redefined.
3245 purge_previous_versions_internal(ik, 1);
3246 }
3247
3248
3249 // Potentially add an information node that contains pointers to the
2547 // interesting parts of the previous version of the_class. 3250 // interesting parts of the previous version of the_class.
2548 // This is also where we clean out any unused weak references. 3251 // This is also where we clean out any unused references.
2549 // Note that while we delete nodes from the _previous_versions 3252 // Note that while we delete nodes from the _previous_versions
2550 // array, we never delete the array itself until the klass is 3253 // array, we never delete the array itself until the klass is
2551 // unloaded. The has_been_redefined() query depends on that fact. 3254 // unloaded. The has_been_redefined() query depends on that fact.
2552 // 3255 //
2553 void instanceKlass::add_previous_version(instanceKlassHandle ikh, 3256 void InstanceKlass::add_previous_version(instanceKlassHandle ikh,
2554 BitMap* emcp_methods, int emcp_method_count) { 3257 BitMap* emcp_methods, int emcp_method_count) {
2555 assert(Thread::current()->is_VM_thread(), 3258 assert(Thread::current()->is_VM_thread(),
2556 "only VMThread can add previous versions"); 3259 "only VMThread can add previous versions");
2557 3260
2558 if (_previous_versions == NULL) { 3261 if (_previous_versions == NULL) {
2561 // won't be redefined much. 3264 // won't be redefined much.
2562 _previous_versions = new (ResourceObj::C_HEAP, mtClass) 3265 _previous_versions = new (ResourceObj::C_HEAP, mtClass)
2563 GrowableArray<PreviousVersionNode *>(2, true); 3266 GrowableArray<PreviousVersionNode *>(2, true);
2564 } 3267 }
2565 3268
3269 ConstantPool* cp_ref = ikh->constants();
3270
2566 // RC_TRACE macro has an embedded ResourceMark 3271 // RC_TRACE macro has an embedded ResourceMark
2567 RC_TRACE(0x00000100, ("adding previous version ref for %s @%d, EMCP_cnt=%d", 3272 RC_TRACE(0x00000400, ("adding previous version ref for %s @%d, EMCP_cnt=%d "
2568 ikh->external_name(), _previous_versions->length(), emcp_method_count)); 3273 "on_stack=%d",
2569 constantPoolHandle cp_h(ikh->constants()); 3274 ikh->external_name(), _previous_versions->length(), emcp_method_count,
2570 jobject cp_ref; 3275 cp_ref->on_stack()));
2571 if (cp_h->is_shared()) { 3276
2572 // a shared ConstantPool requires a regular reference; a weak 3277 // If the constant pool for this previous version of the class
2573 // reference would be collectible 3278 // is not marked as being on the stack, then none of the methods
2574 cp_ref = JNIHandles::make_global(cp_h); 3279 // in this previous version of the class are on the stack so
2575 } else { 3280 // we don't need to create a new PreviousVersionNode. However,
2576 cp_ref = JNIHandles::make_weak_global(cp_h); 3281 // we still need to examine older previous versions below.
2577 } 3282 Array<Method*>* old_methods = ikh->methods();
3283
3284 if (cp_ref->on_stack()) {
2578 PreviousVersionNode * pv_node = NULL; 3285 PreviousVersionNode * pv_node = NULL;
2579 objArrayOop old_methods = ikh->methods();
2580
2581 if (emcp_method_count == 0) { 3286 if (emcp_method_count == 0) {
2582 // non-shared ConstantPool gets a weak reference 3287 // non-shared ConstantPool gets a reference
2583 pv_node = new PreviousVersionNode(cp_ref, !cp_h->is_shared(), NULL); 3288 pv_node = new PreviousVersionNode(cp_ref, !cp_ref->is_shared(), NULL);
2584 RC_TRACE(0x00000400, 3289 RC_TRACE(0x00000400,
2585 ("add: all methods are obsolete; flushing any EMCP weak refs")); 3290 ("add: all methods are obsolete; flushing any EMCP refs"));
2586 } else { 3291 } else {
2587 int local_count = 0; 3292 int local_count = 0;
2588 GrowableArray<jweak>* method_refs = new (ResourceObj::C_HEAP, mtClass) 3293 GrowableArray<Method*>* method_refs = new (ResourceObj::C_HEAP, mtClass)
2589 GrowableArray<jweak>(emcp_method_count, true); 3294 GrowableArray<Method*>(emcp_method_count, true);
2590 for (int i = 0; i < old_methods->length(); i++) { 3295 for (int i = 0; i < old_methods->length(); i++) {
2591 if (emcp_methods->at(i)) { 3296 if (emcp_methods->at(i)) {
2592 // this old method is EMCP so save a weak ref 3297 // this old method is EMCP. Save it only if it's on the stack
2593 methodOop old_method = (methodOop) old_methods->obj_at(i); 3298 Method* old_method = old_methods->at(i);
2594 methodHandle old_method_h(old_method); 3299 if (old_method->on_stack()) {
2595 jweak method_ref = JNIHandles::make_weak_global(old_method_h); 3300 method_refs->append(old_method);
2596 method_refs->append(method_ref); 3301 }
2597 if (++local_count >= emcp_method_count) { 3302 if (++local_count >= emcp_method_count) {
2598 // no more EMCP methods so bail out now 3303 // no more EMCP methods so bail out now
2599 break; 3304 break;
2600 } 3305 }
2601 } 3306 }
2602 } 3307 }
2603 // non-shared ConstantPool gets a weak reference 3308 // non-shared ConstantPool gets a reference
2604 pv_node = new PreviousVersionNode(cp_ref, !cp_h->is_shared(), method_refs); 3309 pv_node = new PreviousVersionNode(cp_ref, !cp_ref->is_shared(), method_refs);
2605 } 3310 }
2606 3311 // append new previous version.
2607 _previous_versions->append(pv_node); 3312 _previous_versions->append(pv_node);
2608 3313 }
2609 // Using weak references allows the interesting parts of previous 3314
2610 // classes to be GC'ed when they are no longer needed. Since the 3315 // Since the caller is the VMThread and we are at a safepoint, this
2611 // caller is the VMThread and we are at a safepoint, this is a good 3316 // is a good time to clear out unused references.
2612 // time to clear out unused weak references.
2613 3317
2614 RC_TRACE(0x00000400, ("add: previous version length=%d", 3318 RC_TRACE(0x00000400, ("add: previous version length=%d",
2615 _previous_versions->length())); 3319 _previous_versions->length()));
2616 3320
2617 // skip the last entry since we just added it 3321 // Purge previous versions not executing on the stack
2618 for (int i = _previous_versions->length() - 2; i >= 0; i--) { 3322 purge_previous_versions_internal(this, emcp_method_count);
2619 // check the previous versions array for a GC'ed weak refs
2620 pv_node = _previous_versions->at(i);
2621 cp_ref = pv_node->prev_constant_pool();
2622 assert(cp_ref != NULL, "cp ref was unexpectedly cleared");
2623 if (cp_ref == NULL) {
2624 delete pv_node;
2625 _previous_versions->remove_at(i);
2626 // Since we are traversing the array backwards, we don't have to
2627 // do anything special with the index.
2628 continue; // robustness
2629 }
2630
2631 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref);
2632 if (cp == NULL) {
2633 // this entry has been GC'ed so remove it
2634 delete pv_node;
2635 _previous_versions->remove_at(i);
2636 // Since we are traversing the array backwards, we don't have to
2637 // do anything special with the index.
2638 continue;
2639 } else {
2640 RC_TRACE(0x00000400, ("add: previous version @%d is alive", i));
2641 }
2642
2643 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods();
2644 if (method_refs != NULL) {
2645 RC_TRACE(0x00000400, ("add: previous methods length=%d",
2646 method_refs->length()));
2647 for (int j = method_refs->length() - 1; j >= 0; j--) {
2648 jweak method_ref = method_refs->at(j);
2649 assert(method_ref != NULL, "weak method ref was unexpectedly cleared");
2650 if (method_ref == NULL) {
2651 method_refs->remove_at(j);
2652 // Since we are traversing the array backwards, we don't have to
2653 // do anything special with the index.
2654 continue; // robustness
2655 }
2656
2657 methodOop method = (methodOop)JNIHandles::resolve(method_ref);
2658 if (method == NULL || emcp_method_count == 0) {
2659 // This method entry has been GC'ed or the current
2660 // RedefineClasses() call has made all methods obsolete
2661 // so remove it.
2662 JNIHandles::destroy_weak_global(method_ref);
2663 method_refs->remove_at(j);
2664 } else {
2665 // RC_TRACE macro has an embedded ResourceMark
2666 RC_TRACE(0x00000400,
2667 ("add: %s(%s): previous method @%d in version @%d is alive",
2668 method->name()->as_C_string(), method->signature()->as_C_string(),
2669 j, i));
2670 }
2671 }
2672 }
2673 }
2674 3323
2675 int obsolete_method_count = old_methods->length() - emcp_method_count; 3324 int obsolete_method_count = old_methods->length() - emcp_method_count;
2676 3325
2677 if (emcp_method_count != 0 && obsolete_method_count != 0 && 3326 if (emcp_method_count != 0 && obsolete_method_count != 0 &&
2678 _previous_versions->length() > 1) { 3327 _previous_versions->length() > 0) {
2679 // We have a mix of obsolete and EMCP methods. If there is more 3328 // We have a mix of obsolete and EMCP methods so we have to
2680 // than the previous version that we just added, then we have to
2681 // clear out any matching EMCP method entries the hard way. 3329 // clear out any matching EMCP method entries the hard way.
2682 int local_count = 0; 3330 int local_count = 0;
2683 for (int i = 0; i < old_methods->length(); i++) { 3331 for (int i = 0; i < old_methods->length(); i++) {
2684 if (!emcp_methods->at(i)) { 3332 if (!emcp_methods->at(i)) {
2685 // only obsolete methods are interesting 3333 // only obsolete methods are interesting
2686 methodOop old_method = (methodOop) old_methods->obj_at(i); 3334 Method* old_method = old_methods->at(i);
2687 Symbol* m_name = old_method->name(); 3335 Symbol* m_name = old_method->name();
2688 Symbol* m_signature = old_method->signature(); 3336 Symbol* m_signature = old_method->signature();
2689 3337
2690 // skip the last entry since we just added it 3338 // we might not have added the last entry
2691 for (int j = _previous_versions->length() - 2; j >= 0; j--) { 3339 for (int j = _previous_versions->length() - 1; j >= 0; j--) {
2692 // check the previous versions array for a GC'ed weak refs 3340 // check the previous versions array for non executing obsolete methods
2693 pv_node = _previous_versions->at(j); 3341 PreviousVersionNode * pv_node = _previous_versions->at(j);
2694 cp_ref = pv_node->prev_constant_pool(); 3342
2695 assert(cp_ref != NULL, "cp ref was unexpectedly cleared"); 3343 GrowableArray<Method*>* method_refs = pv_node->prev_EMCP_methods();
2696 if (cp_ref == NULL) {
2697 delete pv_node;
2698 _previous_versions->remove_at(j);
2699 // Since we are traversing the array backwards, we don't have to
2700 // do anything special with the index.
2701 continue; // robustness
2702 }
2703
2704 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref);
2705 if (cp == NULL) {
2706 // this entry has been GC'ed so remove it
2707 delete pv_node;
2708 _previous_versions->remove_at(j);
2709 // Since we are traversing the array backwards, we don't have to
2710 // do anything special with the index.
2711 continue;
2712 }
2713
2714 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods();
2715 if (method_refs == NULL) { 3344 if (method_refs == NULL) {
2716 // We have run into a PreviousVersion generation where 3345 // We have run into a PreviousVersion generation where
2717 // all methods were made obsolete during that generation's 3346 // all methods were made obsolete during that generation's
2718 // RedefineClasses() operation. At the time of that 3347 // RedefineClasses() operation. At the time of that
2719 // operation, all EMCP methods were flushed so we don't 3348 // operation, all EMCP methods were flushed so we don't
2724 // from an empty method_refs for the current generation. 3353 // from an empty method_refs for the current generation.
2725 break; 3354 break;
2726 } 3355 }
2727 3356
2728 for (int k = method_refs->length() - 1; k >= 0; k--) { 3357 for (int k = method_refs->length() - 1; k >= 0; k--) {
2729 jweak method_ref = method_refs->at(k); 3358 Method* method = method_refs->at(k);
2730 assert(method_ref != NULL, 3359
2731 "weak method ref was unexpectedly cleared"); 3360 if (!method->is_obsolete() &&
2732 if (method_ref == NULL) { 3361 method->name() == m_name &&
2733 method_refs->remove_at(k);
2734 // Since we are traversing the array backwards, we don't
2735 // have to do anything special with the index.
2736 continue; // robustness
2737 }
2738
2739 methodOop method = (methodOop)JNIHandles::resolve(method_ref);
2740 if (method == NULL) {
2741 // this method entry has been GC'ed so skip it
2742 JNIHandles::destroy_weak_global(method_ref);
2743 method_refs->remove_at(k);
2744 continue;
2745 }
2746
2747 if (method->name() == m_name &&
2748 method->signature() == m_signature) { 3362 method->signature() == m_signature) {
2749 // The current RedefineClasses() call has made all EMCP 3363 // The current RedefineClasses() call has made all EMCP
2750 // versions of this method obsolete so mark it as obsolete 3364 // versions of this method obsolete so mark it as obsolete
2751 // and remove the weak ref. 3365 // and remove the reference.
2752 RC_TRACE(0x00000400, 3366 RC_TRACE(0x00000400,
2753 ("add: %s(%s): flush obsolete method @%d in version @%d", 3367 ("add: %s(%s): flush obsolete method @%d in version @%d",
2754 m_name->as_C_string(), m_signature->as_C_string(), k, j)); 3368 m_name->as_C_string(), m_signature->as_C_string(), k, j));
2755 3369
2756 method->set_is_obsolete(); 3370 method->set_is_obsolete();
2757 JNIHandles::destroy_weak_global(method_ref); 3371 // Leave obsolete methods on the previous version list to
2758 method_refs->remove_at(k); 3372 // clean up later.
2759 break; 3373 break;
2760 } 3374 }
2761 } 3375 }
2762 3376
2763 // The previous loop may not find a matching EMCP method, but 3377 // The previous loop may not find a matching EMCP method, but
2764 // that doesn't mean that we can optimize and not go any 3378 // that doesn't mean that we can optimize and not go any
2765 // further back in the PreviousVersion generations. The EMCP 3379 // further back in the PreviousVersion generations. The EMCP
2766 // method for this generation could have already been GC'ed, 3380 // method for this generation could have already been deleted,
2767 // but there still may be an older EMCP method that has not 3381 // but there still may be an older EMCP method that has not
2768 // been GC'ed. 3382 // been deleted.
2769 } 3383 }
2770 3384
2771 if (++local_count >= obsolete_method_count) { 3385 if (++local_count >= obsolete_method_count) {
2772 // no more obsolete methods so bail out now 3386 // no more obsolete methods so bail out now
2773 break; 3387 break;
2776 } 3390 }
2777 } 3391 }
2778 } // end add_previous_version() 3392 } // end add_previous_version()
2779 3393
2780 3394
2781 // Determine if instanceKlass has a previous version. 3395 // Determine if InstanceKlass has a previous version.
2782 bool instanceKlass::has_previous_version() const { 3396 bool InstanceKlass::has_previous_version() const {
2783 if (_previous_versions == NULL) { 3397 return (_previous_versions != NULL && _previous_versions->length() > 0);
2784 // no previous versions array so answer is easy
2785 return false;
2786 }
2787
2788 for (int i = _previous_versions->length() - 1; i >= 0; i--) {
2789 // Check the previous versions array for an info node that hasn't
2790 // been GC'ed
2791 PreviousVersionNode * pv_node = _previous_versions->at(i);
2792
2793 jobject cp_ref = pv_node->prev_constant_pool();
2794 assert(cp_ref != NULL, "cp reference was unexpectedly cleared");
2795 if (cp_ref == NULL) {
2796 continue; // robustness
2797 }
2798
2799 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref);
2800 if (cp != NULL) {
2801 // we have at least one previous version
2802 return true;
2803 }
2804
2805 // We don't have to check the method refs. If the constant pool has
2806 // been GC'ed then so have the methods.
2807 }
2808
2809 // all of the underlying nodes' info has been GC'ed
2810 return false;
2811 } // end has_previous_version() 3398 } // end has_previous_version()
2812 3399
2813 methodOop instanceKlass::method_with_idnum(int idnum) { 3400
2814 methodOop m = NULL; 3401 Method* InstanceKlass::method_with_idnum(int idnum) {
3402 Method* m = NULL;
2815 if (idnum < methods()->length()) { 3403 if (idnum < methods()->length()) {
2816 m = (methodOop) methods()->obj_at(idnum); 3404 m = methods()->at(idnum);
2817 } 3405 }
2818 if (m == NULL || m->method_idnum() != idnum) { 3406 if (m == NULL || m->method_idnum() != idnum) {
2819 for (int index = 0; index < methods()->length(); ++index) { 3407 for (int index = 0; index < methods()->length(); ++index) {
2820 m = (methodOop) methods()->obj_at(index); 3408 m = methods()->at(index);
2821 if (m->method_idnum() == idnum) { 3409 if (m->method_idnum() == idnum) {
2822 return m; 3410 return m;
2823 } 3411 }
2824 } 3412 }
2825 } 3413 }
2826 return m; 3414 return m;
2827 } 3415 }
2828 3416
2829 3417
2830 // Set the annotation at 'idnum' to 'anno'.
2831 // We don't want to create or extend the array if 'anno' is NULL, since that is the
2832 // default value. However, if the array exists and is long enough, we must set NULL values.
2833 void instanceKlass::set_methods_annotations_of(int idnum, typeArrayOop anno, objArrayOop* md_p) {
2834 objArrayOop md = *md_p;
2835 if (md != NULL && md->length() > idnum) {
2836 md->obj_at_put(idnum, anno);
2837 } else if (anno != NULL) {
2838 // create the array
2839 int length = MAX2(idnum+1, (int)_idnum_allocated_count);
2840 md = oopFactory::new_system_objArray(length, Thread::current());
2841 if (*md_p != NULL) {
2842 // copy the existing entries
2843 for (int index = 0; index < (*md_p)->length(); index++) {
2844 md->obj_at_put(index, (*md_p)->obj_at(index));
2845 }
2846 }
2847 set_annotations(md, md_p);
2848 md->obj_at_put(idnum, anno);
2849 } // if no array and idnum isn't included there is nothing to do
2850 }
2851
2852 // Construct a PreviousVersionNode entry for the array hung off 3418 // Construct a PreviousVersionNode entry for the array hung off
2853 // the instanceKlass. 3419 // the InstanceKlass.
2854 PreviousVersionNode::PreviousVersionNode(jobject prev_constant_pool, 3420 PreviousVersionNode::PreviousVersionNode(ConstantPool* prev_constant_pool,
2855 bool prev_cp_is_weak, GrowableArray<jweak>* prev_EMCP_methods) { 3421 bool prev_cp_is_weak, GrowableArray<Method*>* prev_EMCP_methods) {
2856 3422
2857 _prev_constant_pool = prev_constant_pool; 3423 _prev_constant_pool = prev_constant_pool;
2858 _prev_cp_is_weak = prev_cp_is_weak; 3424 _prev_cp_is_weak = prev_cp_is_weak;
2859 _prev_EMCP_methods = prev_EMCP_methods; 3425 _prev_EMCP_methods = prev_EMCP_methods;
2860 } 3426 }
2861 3427
2862 3428
2863 // Destroy a PreviousVersionNode 3429 // Destroy a PreviousVersionNode
2864 PreviousVersionNode::~PreviousVersionNode() { 3430 PreviousVersionNode::~PreviousVersionNode() {
2865 if (_prev_constant_pool != NULL) { 3431 if (_prev_constant_pool != NULL) {
2866 if (_prev_cp_is_weak) { 3432 _prev_constant_pool = NULL;
2867 JNIHandles::destroy_weak_global(_prev_constant_pool);
2868 } else {
2869 JNIHandles::destroy_global(_prev_constant_pool);
2870 }
2871 } 3433 }
2872 3434
2873 if (_prev_EMCP_methods != NULL) { 3435 if (_prev_EMCP_methods != NULL) {
2874 for (int i = _prev_EMCP_methods->length() - 1; i >= 0; i--) {
2875 jweak method_ref = _prev_EMCP_methods->at(i);
2876 if (method_ref != NULL) {
2877 JNIHandles::destroy_weak_global(method_ref);
2878 }
2879 }
2880 delete _prev_EMCP_methods; 3436 delete _prev_EMCP_methods;
2881 } 3437 }
2882 } 3438 }
2883 3439
2884 3440
2885 // Construct a PreviousVersionInfo entry 3441 // Construct a PreviousVersionInfo entry
2886 PreviousVersionInfo::PreviousVersionInfo(PreviousVersionNode *pv_node) { 3442 PreviousVersionInfo::PreviousVersionInfo(PreviousVersionNode *pv_node) {
2887 _prev_constant_pool_handle = constantPoolHandle(); // NULL handle 3443 _prev_constant_pool_handle = constantPoolHandle(); // NULL handle
2888 _prev_EMCP_method_handles = NULL; 3444 _prev_EMCP_method_handles = NULL;
2889 3445
2890 jobject cp_ref = pv_node->prev_constant_pool(); 3446 ConstantPool* cp = pv_node->prev_constant_pool();
2891 assert(cp_ref != NULL, "constant pool ref was unexpectedly cleared"); 3447 assert(cp != NULL, "constant pool ref was unexpectedly cleared");
2892 if (cp_ref == NULL) { 3448 if (cp == NULL) {
2893 return; // robustness 3449 return; // robustness
2894 } 3450 }
2895 3451
2896 constantPoolOop cp = (constantPoolOop)JNIHandles::resolve(cp_ref); 3452 // make the ConstantPool* safe to return
2897 if (cp == NULL) {
2898 // Weak reference has been GC'ed. Since the constant pool has been
2899 // GC'ed, the methods have also been GC'ed.
2900 return;
2901 }
2902
2903 // make the constantPoolOop safe to return
2904 _prev_constant_pool_handle = constantPoolHandle(cp); 3453 _prev_constant_pool_handle = constantPoolHandle(cp);
2905 3454
2906 GrowableArray<jweak>* method_refs = pv_node->prev_EMCP_methods(); 3455 GrowableArray<Method*>* method_refs = pv_node->prev_EMCP_methods();
2907 if (method_refs == NULL) { 3456 if (method_refs == NULL) {
2908 // the instanceKlass did not have any EMCP methods 3457 // the InstanceKlass did not have any EMCP methods
2909 return; 3458 return;
2910 } 3459 }
2911 3460
2912 _prev_EMCP_method_handles = new GrowableArray<methodHandle>(10); 3461 _prev_EMCP_method_handles = new GrowableArray<methodHandle>(10);
2913 3462
2914 int n_methods = method_refs->length(); 3463 int n_methods = method_refs->length();
2915 for (int i = 0; i < n_methods; i++) { 3464 for (int i = 0; i < n_methods; i++) {
2916 jweak method_ref = method_refs->at(i); 3465 Method* method = method_refs->at(i);
2917 assert(method_ref != NULL, "weak method ref was unexpectedly cleared"); 3466 assert (method != NULL, "method has been cleared");
2918 if (method_ref == NULL) { 3467 if (method == NULL) {
2919 continue; // robustness 3468 continue; // robustness
2920 } 3469 }
2921 3470 // make the Method* safe to return
2922 methodOop method = (methodOop)JNIHandles::resolve(method_ref);
2923 if (method == NULL) {
2924 // this entry has been GC'ed so skip it
2925 continue;
2926 }
2927
2928 // make the methodOop safe to return
2929 _prev_EMCP_method_handles->append(methodHandle(method)); 3471 _prev_EMCP_method_handles->append(methodHandle(method));
2930 } 3472 }
2931 } 3473 }
2932 3474
2933 3475
2937 // don't have to delete it. 3479 // don't have to delete it.
2938 } 3480 }
2939 3481
2940 3482
2941 // Construct a helper for walking the previous versions array 3483 // Construct a helper for walking the previous versions array
2942 PreviousVersionWalker::PreviousVersionWalker(instanceKlass *ik) { 3484 PreviousVersionWalker::PreviousVersionWalker(InstanceKlass *ik) {
2943 _previous_versions = ik->previous_versions(); 3485 _previous_versions = ik->previous_versions();
2944 _current_index = 0; 3486 _current_index = 0;
2945 // _hm needs no initialization 3487 // _hm needs no initialization
2946 _current_p = NULL; 3488 _current_p = NULL;
2947 } 3489 }
2979 PreviousVersionNode * pv_node = _previous_versions->at(_current_index++); 3521 PreviousVersionNode * pv_node = _previous_versions->at(_current_index++);
2980 PreviousVersionInfo * pv_info = new (ResourceObj::C_HEAP, mtClass) 3522 PreviousVersionInfo * pv_info = new (ResourceObj::C_HEAP, mtClass)
2981 PreviousVersionInfo(pv_node); 3523 PreviousVersionInfo(pv_node);
2982 3524
2983 constantPoolHandle cp_h = pv_info->prev_constant_pool_handle(); 3525 constantPoolHandle cp_h = pv_info->prev_constant_pool_handle();
2984 if (cp_h.is_null()) { 3526 assert (!cp_h.is_null(), "null cp found in previous version");
2985 delete pv_info; 3527
2986 3528 // The caller will need to delete pv_info when they are done with it.
2987 // The underlying node's info has been GC'ed so try the next one.
2988 // We don't have to check the methods. If the constant pool has
2989 // GC'ed then so have the methods.
2990 continue;
2991 }
2992
2993 // Found a node with non GC'ed info so return it. The caller will
2994 // need to delete pv_info when they are done with it.
2995 _current_p = pv_info; 3529 _current_p = pv_info;
2996 return pv_info; 3530 return pv_info;
2997 } 3531 }
2998 3532
2999 // all of the underlying nodes' info has been GC'ed 3533 // all of the underlying nodes' info has been deleted
3000 return NULL; 3534 return NULL;
3001 } // end next_previous_version() 3535 } // end next_previous_version()