src/share/vm/classfile/javaClasses.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 6642881_8u40_hotspot Sdiff src/share/vm/classfile

src/share/vm/classfile/javaClasses.cpp

Print this page
rev 6604 : 6642881: Improve performance of Class.getClassLoader()
Summary: Add classLoader to java/lang/Class instance for fast access
Reviewed-by: alanb, lfoltan, rriggs, vlivanov, twisti, jfranck


 531 }
 532 
 533 
 534 void java_lang_Class::fixup_mirror(KlassHandle k, TRAPS) {
 535   assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
 536 
 537   // If the offset was read from the shared archive, it was fixed up already
 538   if (!k->is_shared()) {
 539     if (k->oop_is_instance()) {
 540       // During bootstrap, java.lang.Class wasn't loaded so static field
 541       // offsets were computed without the size added it.  Go back and
 542       // update all the static field offsets to included the size.
 543         for (JavaFieldStream fs(InstanceKlass::cast(k())); !fs.done(); fs.next()) {
 544         if (fs.access_flags().is_static()) {
 545           int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
 546           fs.set_offset(real_offset);
 547         }
 548       }
 549     }
 550   }
 551   create_mirror(k, Handle(NULL), CHECK);
 552 }
 553 
 554 void java_lang_Class::initialize_mirror_fields(KlassHandle k,
 555                                                Handle mirror,
 556                                                Handle protection_domain,
 557                                                TRAPS) {
 558   // Allocate a simple java object for a lock.
 559   // This needs to be a java object because during class initialization
 560   // it can be held across a java call.
 561   typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
 562   set_init_lock(mirror(), r);
 563 
 564   // Set protection domain also
 565   set_protection_domain(mirror(), protection_domain());
 566 
 567   // Initialize static fields
 568   InstanceKlass::cast(k())->do_local_static_fields(&initialize_static_field, mirror, CHECK);
 569 }
 570 
 571 void java_lang_Class::create_mirror(KlassHandle k, Handle protection_domain, TRAPS) {

 572   assert(k->java_mirror() == NULL, "should only assign mirror once");
 573   // Use this moment of initialization to cache modifier_flags also,
 574   // to support Class.getModifiers().  Instance classes recalculate
 575   // the cached flags after the class file is parsed, but before the
 576   // class is put into the system dictionary.
 577   int computed_modifiers = k->compute_modifier_flags(CHECK);
 578   k->set_modifier_flags(computed_modifiers);
 579   // Class_klass has to be loaded because it is used to allocate
 580   // the mirror.
 581   if (SystemDictionary::Class_klass_loaded()) {
 582     // Allocate mirror (java.lang.Class instance)
 583     Handle mirror = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);
 584 
 585     // Setup indirection from mirror->klass
 586     if (!k.is_null()) {
 587       java_lang_Class::set_klass(mirror(), k());
 588     }
 589 
 590     InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
 591     assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");


 606       }
 607       assert(comp_mirror.not_null(), "must have a mirror");
 608 
 609       // Two-way link between the array klass and its component mirror:
 610       ArrayKlass::cast(k())->set_component_mirror(comp_mirror());
 611       set_array_klass(comp_mirror(), k());
 612     } else {
 613       assert(k->oop_is_instance(), "Must be");
 614 
 615       initialize_mirror_fields(k, mirror, protection_domain, THREAD);
 616       if (HAS_PENDING_EXCEPTION) {
 617         // If any of the fields throws an exception like OOM remove the klass field
 618         // from the mirror so GC doesn't follow it after the klass has been deallocated.
 619         // This mirror looks like a primitive type, which logically it is because it
 620         // it represents no class.
 621         java_lang_Class::set_klass(mirror(), NULL);
 622         return;
 623       }
 624     }
 625 



 626     // Setup indirection from klass->mirror last
 627     // after any exceptions can happen during allocations.
 628     if (!k.is_null()) {
 629       k->set_java_mirror(mirror());
 630     }
 631   } else {
 632     if (fixup_mirror_list() == NULL) {
 633       GrowableArray<Klass*>* list =
 634        new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true);
 635       set_fixup_mirror_list(list);
 636     }
 637     fixup_mirror_list()->push(k());
 638   }
 639 }
 640 
 641 
 642 int  java_lang_Class::oop_size(oop java_class) {
 643   assert(_oop_size_offset != 0, "must be set");
 644   return java_class->int_field(_oop_size_offset);
 645 }


 667 
 668 oop java_lang_Class::init_lock(oop java_class) {
 669   assert(_init_lock_offset != 0, "must be set");
 670   return java_class->obj_field(_init_lock_offset);
 671 }
 672 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
 673   assert(_init_lock_offset != 0, "must be set");
 674   java_class->obj_field_put(_init_lock_offset, init_lock);
 675 }
 676 
 677 objArrayOop java_lang_Class::signers(oop java_class) {
 678   assert(_signers_offset != 0, "must be set");
 679   return (objArrayOop)java_class->obj_field(_signers_offset);
 680 }
 681 void java_lang_Class::set_signers(oop java_class, objArrayOop signers) {
 682   assert(_signers_offset != 0, "must be set");
 683   java_class->obj_field_put(_signers_offset, (oop)signers);
 684 }
 685 
 686 












 687 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) {
 688   // This should be improved by adding a field at the Java level or by
 689   // introducing a new VM klass (see comment in ClassFileParser)
 690   oop java_class = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(NULL, CHECK_0);
 691   if (type != T_VOID) {
 692     Klass* aklass = Universe::typeArrayKlassObj(type);
 693     assert(aklass != NULL, "correct bootstrap");
 694     set_array_klass(java_class, aklass);
 695   }
 696 #ifdef ASSERT
 697   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(SystemDictionary::Class_klass());
 698   assert(java_lang_Class::static_oop_field_count(java_class) == 0, "should have been zeroed by allocation");
 699 #endif
 700   return java_class;
 701 }
 702 
 703 
 704 Klass* java_lang_Class::as_Klass(oop java_class) {
 705   //%note memory_2
 706   assert(java_lang_Class::is_instance(java_class), "must be a Class object");


 826 oop java_lang_Class::primitive_mirror(BasicType t) {
 827   oop mirror = Universe::java_mirror(t);
 828   assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class");
 829   assert(java_lang_Class::is_primitive(mirror), "must be primitive");
 830   return mirror;
 831 }
 832 
 833 bool java_lang_Class::offsets_computed = false;
 834 int  java_lang_Class::classRedefinedCount_offset = -1;
 835 
 836 void java_lang_Class::compute_offsets() {
 837   assert(!offsets_computed, "offsets should be initialized only once");
 838   offsets_computed = true;
 839 
 840   Klass* klass_oop = SystemDictionary::Class_klass();
 841   // The classRedefinedCount field is only present starting in 1.5,
 842   // so don't go fatal.
 843   compute_optional_offset(classRedefinedCount_offset,
 844                           klass_oop, vmSymbols::classRedefinedCount_name(), vmSymbols::int_signature());
 845 






 846   CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
 847 }
 848 
 849 int java_lang_Class::classRedefinedCount(oop the_class_mirror) {
 850   if (!JDK_Version::is_gte_jdk15x_version()
 851       || classRedefinedCount_offset == -1) {
 852     // The classRedefinedCount field is only present starting in 1.5.
 853     // If we don't have an offset for it then just return -1 as a marker.
 854     return -1;
 855   }
 856 
 857   return the_class_mirror->int_field(classRedefinedCount_offset);
 858 }
 859 
 860 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) {
 861   if (!JDK_Version::is_gte_jdk15x_version()
 862       || classRedefinedCount_offset == -1) {
 863     // The classRedefinedCount field is only present starting in 1.5.
 864     // If we don't have an offset for it then nothing to set.
 865     return;


3065 
3066 int java_lang_System::err_offset_in_bytes() {
3067   return (InstanceMirrorKlass::offset_of_static_fields() + static_err_offset);
3068 }
3069 
3070 
3071 bool java_lang_System::has_security_manager() {
3072   InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::System_klass());
3073   address addr = ik->static_field_addr(static_security_offset);
3074   if (UseCompressedOops) {
3075     return oopDesc::load_decode_heap_oop((narrowOop *)addr) != NULL;
3076   } else {
3077     return oopDesc::load_decode_heap_oop((oop*)addr) != NULL;
3078   }
3079 }
3080 
3081 int java_lang_Class::_klass_offset;
3082 int java_lang_Class::_array_klass_offset;
3083 int java_lang_Class::_oop_size_offset;
3084 int java_lang_Class::_static_oop_field_count_offset;

3085 int java_lang_Class::_protection_domain_offset;
3086 int java_lang_Class::_init_lock_offset;
3087 int java_lang_Class::_signers_offset;
3088 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
3089 int java_lang_Throwable::backtrace_offset;
3090 int java_lang_Throwable::detailMessage_offset;
3091 int java_lang_Throwable::cause_offset;
3092 int java_lang_Throwable::stackTrace_offset;
3093 int java_lang_Throwable::static_unassigned_stacktrace_offset;
3094 int java_lang_reflect_AccessibleObject::override_offset;
3095 int java_lang_reflect_Method::clazz_offset;
3096 int java_lang_reflect_Method::name_offset;
3097 int java_lang_reflect_Method::returnType_offset;
3098 int java_lang_reflect_Method::parameterTypes_offset;
3099 int java_lang_reflect_Method::exceptionTypes_offset;
3100 int java_lang_reflect_Method::slot_offset;
3101 int java_lang_reflect_Method::modifiers_offset;
3102 int java_lang_reflect_Method::signature_offset;
3103 int java_lang_reflect_Method::annotations_offset;
3104 int java_lang_reflect_Method::parameter_annotations_offset;




 531 }
 532 
 533 
 534 void java_lang_Class::fixup_mirror(KlassHandle k, TRAPS) {
 535   assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
 536 
 537   // If the offset was read from the shared archive, it was fixed up already
 538   if (!k->is_shared()) {
 539     if (k->oop_is_instance()) {
 540       // During bootstrap, java.lang.Class wasn't loaded so static field
 541       // offsets were computed without the size added it.  Go back and
 542       // update all the static field offsets to included the size.
 543         for (JavaFieldStream fs(InstanceKlass::cast(k())); !fs.done(); fs.next()) {
 544         if (fs.access_flags().is_static()) {
 545           int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
 546           fs.set_offset(real_offset);
 547         }
 548       }
 549     }
 550   }
 551   create_mirror(k, Handle(NULL), Handle(NULL), CHECK);
 552 }
 553 
 554 void java_lang_Class::initialize_mirror_fields(KlassHandle k,
 555                                                Handle mirror,
 556                                                Handle protection_domain,
 557                                                TRAPS) {
 558   // Allocate a simple java object for a lock.
 559   // This needs to be a java object because during class initialization
 560   // it can be held across a java call.
 561   typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
 562   set_init_lock(mirror(), r);
 563 
 564   // Set protection domain also
 565   set_protection_domain(mirror(), protection_domain());
 566 
 567   // Initialize static fields
 568   InstanceKlass::cast(k())->do_local_static_fields(&initialize_static_field, mirror, CHECK);
 569 }
 570 
 571 void java_lang_Class::create_mirror(KlassHandle k, Handle class_loader,
 572                                     Handle protection_domain, TRAPS) {
 573   assert(k->java_mirror() == NULL, "should only assign mirror once");
 574   // Use this moment of initialization to cache modifier_flags also,
 575   // to support Class.getModifiers().  Instance classes recalculate
 576   // the cached flags after the class file is parsed, but before the
 577   // class is put into the system dictionary.
 578   int computed_modifiers = k->compute_modifier_flags(CHECK);
 579   k->set_modifier_flags(computed_modifiers);
 580   // Class_klass has to be loaded because it is used to allocate
 581   // the mirror.
 582   if (SystemDictionary::Class_klass_loaded()) {
 583     // Allocate mirror (java.lang.Class instance)
 584     Handle mirror = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);
 585 
 586     // Setup indirection from mirror->klass
 587     if (!k.is_null()) {
 588       java_lang_Class::set_klass(mirror(), k());
 589     }
 590 
 591     InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
 592     assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");


 607       }
 608       assert(comp_mirror.not_null(), "must have a mirror");
 609 
 610       // Two-way link between the array klass and its component mirror:
 611       ArrayKlass::cast(k())->set_component_mirror(comp_mirror());
 612       set_array_klass(comp_mirror(), k());
 613     } else {
 614       assert(k->oop_is_instance(), "Must be");
 615 
 616       initialize_mirror_fields(k, mirror, protection_domain, THREAD);
 617       if (HAS_PENDING_EXCEPTION) {
 618         // If any of the fields throws an exception like OOM remove the klass field
 619         // from the mirror so GC doesn't follow it after the klass has been deallocated.
 620         // This mirror looks like a primitive type, which logically it is because it
 621         // it represents no class.
 622         java_lang_Class::set_klass(mirror(), NULL);
 623         return;
 624       }
 625     }
 626 
 627     // set the classLoader field in the java_lang_Class instance
 628     set_class_loader(mirror(), class_loader());
 629 
 630     // Setup indirection from klass->mirror last
 631     // after any exceptions can happen during allocations.
 632     if (!k.is_null()) {
 633       k->set_java_mirror(mirror());
 634     }
 635   } else {
 636     if (fixup_mirror_list() == NULL) {
 637       GrowableArray<Klass*>* list =
 638        new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true);
 639       set_fixup_mirror_list(list);
 640     }
 641     fixup_mirror_list()->push(k());
 642   }
 643 }
 644 
 645 
 646 int  java_lang_Class::oop_size(oop java_class) {
 647   assert(_oop_size_offset != 0, "must be set");
 648   return java_class->int_field(_oop_size_offset);
 649 }


 671 
 672 oop java_lang_Class::init_lock(oop java_class) {
 673   assert(_init_lock_offset != 0, "must be set");
 674   return java_class->obj_field(_init_lock_offset);
 675 }
 676 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
 677   assert(_init_lock_offset != 0, "must be set");
 678   java_class->obj_field_put(_init_lock_offset, init_lock);
 679 }
 680 
 681 objArrayOop java_lang_Class::signers(oop java_class) {
 682   assert(_signers_offset != 0, "must be set");
 683   return (objArrayOop)java_class->obj_field(_signers_offset);
 684 }
 685 void java_lang_Class::set_signers(oop java_class, objArrayOop signers) {
 686   assert(_signers_offset != 0, "must be set");
 687   java_class->obj_field_put(_signers_offset, (oop)signers);
 688 }
 689 
 690 
 691 void java_lang_Class::set_class_loader(oop java_class, oop loader) {
 692   // jdk7 runs Queens in bootstrapping and jdk8-9 has no coordinated pushes yet.
 693   if (_class_loader_offset != 0) {
 694     java_class->obj_field_put(_class_loader_offset, loader);
 695   }
 696 }
 697 
 698 oop java_lang_Class::class_loader(oop java_class) {
 699   assert(_class_loader_offset != 0, "must be set");
 700   return java_class->obj_field(_class_loader_offset);
 701 }
 702 
 703 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) {
 704   // This should be improved by adding a field at the Java level or by
 705   // introducing a new VM klass (see comment in ClassFileParser)
 706   oop java_class = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(NULL, CHECK_0);
 707   if (type != T_VOID) {
 708     Klass* aklass = Universe::typeArrayKlassObj(type);
 709     assert(aklass != NULL, "correct bootstrap");
 710     set_array_klass(java_class, aklass);
 711   }
 712 #ifdef ASSERT
 713   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(SystemDictionary::Class_klass());
 714   assert(java_lang_Class::static_oop_field_count(java_class) == 0, "should have been zeroed by allocation");
 715 #endif
 716   return java_class;
 717 }
 718 
 719 
 720 Klass* java_lang_Class::as_Klass(oop java_class) {
 721   //%note memory_2
 722   assert(java_lang_Class::is_instance(java_class), "must be a Class object");


 842 oop java_lang_Class::primitive_mirror(BasicType t) {
 843   oop mirror = Universe::java_mirror(t);
 844   assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class");
 845   assert(java_lang_Class::is_primitive(mirror), "must be primitive");
 846   return mirror;
 847 }
 848 
 849 bool java_lang_Class::offsets_computed = false;
 850 int  java_lang_Class::classRedefinedCount_offset = -1;
 851 
 852 void java_lang_Class::compute_offsets() {
 853   assert(!offsets_computed, "offsets should be initialized only once");
 854   offsets_computed = true;
 855 
 856   Klass* klass_oop = SystemDictionary::Class_klass();
 857   // The classRedefinedCount field is only present starting in 1.5,
 858   // so don't go fatal.
 859   compute_optional_offset(classRedefinedCount_offset,
 860                           klass_oop, vmSymbols::classRedefinedCount_name(), vmSymbols::int_signature());
 861 
 862   // Needs to be optional because the old build runs Queens during bootstrapping
 863   // and jdk8-9 doesn't have coordinated pushes yet.
 864   compute_optional_offset(_class_loader_offset,
 865                  klass_oop, vmSymbols::classLoader_name(),
 866                  vmSymbols::classloader_signature());
 867 
 868   CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
 869 }
 870 
 871 int java_lang_Class::classRedefinedCount(oop the_class_mirror) {
 872   if (!JDK_Version::is_gte_jdk15x_version()
 873       || classRedefinedCount_offset == -1) {
 874     // The classRedefinedCount field is only present starting in 1.5.
 875     // If we don't have an offset for it then just return -1 as a marker.
 876     return -1;
 877   }
 878 
 879   return the_class_mirror->int_field(classRedefinedCount_offset);
 880 }
 881 
 882 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) {
 883   if (!JDK_Version::is_gte_jdk15x_version()
 884       || classRedefinedCount_offset == -1) {
 885     // The classRedefinedCount field is only present starting in 1.5.
 886     // If we don't have an offset for it then nothing to set.
 887     return;


3087 
3088 int java_lang_System::err_offset_in_bytes() {
3089   return (InstanceMirrorKlass::offset_of_static_fields() + static_err_offset);
3090 }
3091 
3092 
3093 bool java_lang_System::has_security_manager() {
3094   InstanceKlass* ik = InstanceKlass::cast(SystemDictionary::System_klass());
3095   address addr = ik->static_field_addr(static_security_offset);
3096   if (UseCompressedOops) {
3097     return oopDesc::load_decode_heap_oop((narrowOop *)addr) != NULL;
3098   } else {
3099     return oopDesc::load_decode_heap_oop((oop*)addr) != NULL;
3100   }
3101 }
3102 
3103 int java_lang_Class::_klass_offset;
3104 int java_lang_Class::_array_klass_offset;
3105 int java_lang_Class::_oop_size_offset;
3106 int java_lang_Class::_static_oop_field_count_offset;
3107 int java_lang_Class::_class_loader_offset;
3108 int java_lang_Class::_protection_domain_offset;
3109 int java_lang_Class::_init_lock_offset;
3110 int java_lang_Class::_signers_offset;
3111 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
3112 int java_lang_Throwable::backtrace_offset;
3113 int java_lang_Throwable::detailMessage_offset;
3114 int java_lang_Throwable::cause_offset;
3115 int java_lang_Throwable::stackTrace_offset;
3116 int java_lang_Throwable::static_unassigned_stacktrace_offset;
3117 int java_lang_reflect_AccessibleObject::override_offset;
3118 int java_lang_reflect_Method::clazz_offset;
3119 int java_lang_reflect_Method::name_offset;
3120 int java_lang_reflect_Method::returnType_offset;
3121 int java_lang_reflect_Method::parameterTypes_offset;
3122 int java_lang_reflect_Method::exceptionTypes_offset;
3123 int java_lang_reflect_Method::slot_offset;
3124 int java_lang_reflect_Method::modifiers_offset;
3125 int java_lang_reflect_Method::signature_offset;
3126 int java_lang_reflect_Method::annotations_offset;
3127 int java_lang_reflect_Method::parameter_annotations_offset;


src/share/vm/classfile/javaClasses.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File