< prev index next >

src/hotspot/share/oops/klass.cpp

Print this page

 186 #ifdef ASSERT
 187   tty->print_cr("Error: uncached_lookup_method called on a klass oop."
 188                 " Likely error: reflection method does not correctly"
 189                 " wrap return value in a mirror object.");
 190 #endif
 191   ShouldNotReachHere();
 192   return NULL;
 193 }
 194 
 195 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() {
 196   return Metaspace::allocate(loader_data, word_size, MetaspaceObj::ClassType, THREAD);
 197 }
 198 
 199 // "Normal" instantiation is preceeded by a MetaspaceObj allocation
 200 // which zeros out memory - calloc equivalent.
 201 // The constructor is also used from CppVtableCloner,
 202 // which doesn't zero out the memory before calling the constructor.
 203 Klass::Klass(KlassID id) : _id(id),
 204                            _prototype_header(markWord::prototype()),
 205                            _shared_class_path_index(-1) {
 206   CDS_ONLY(_shared_class_flags = 0;)
 207   CDS_JAVA_HEAP_ONLY(_archived_mirror = narrowOop::null;)
 208   _primary_supers[0] = this;
 209   set_super_check_offset(in_bytes(primary_supers_offset()));
 210 }
 211 
 212 jint Klass::array_layout_helper(BasicType etype) {
 213   assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
 214   // Note that T_ARRAY is not allowed here.
 215   int  hsize = arrayOopDesc::base_offset_in_bytes(etype);
 216   int  esize = type2aelembytes(etype);
 217   bool isobj = (etype == T_OBJECT);
 218   int  tag   =  isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value;
 219   int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize));
 220 
 221   assert(lh < (int)_lh_neutral_value, "must look like an array layout");
 222   assert(layout_helper_is_array(lh), "correct kind");
 223   assert(layout_helper_is_objArray(lh) == isobj, "correct kind");
 224   assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
 225   assert(layout_helper_header_size(lh) == hsize, "correct decode");
 226   assert(layout_helper_element_type(lh) == etype, "correct decode");
 227   assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");

 556     ResourceMark rm;
 557     log_trace(cds, unshareable)("remove java_mirror: %s", external_name());
 558   }
 559   // Just null out the mirror.  The class_loader_data() no longer exists.
 560   clear_java_mirror_handle();
 561 }
 562 
 563 void Klass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
 564   assert(is_klass(), "ensure C++ vtable is restored");
 565   assert(is_shared(), "must be set");
 566   JFR_ONLY(RESTORE_ID(this);)
 567   if (log_is_enabled(Trace, cds, unshareable)) {
 568     ResourceMark rm(THREAD);
 569     log_trace(cds, unshareable)("restore: %s", external_name());
 570   }
 571 
 572   // If an exception happened during CDS restore, some of these fields may already be
 573   // set.  We leave the class on the CLD list, even if incomplete so that we don't
 574   // modify the CLD list outside a safepoint.
 575   if (class_loader_data() == NULL) {
 576     // Restore class_loader_data to the null class loader data
 577     set_class_loader_data(loader_data);
 578 
 579     // Add to null class loader list first before creating the mirror
 580     // (same order as class file parsing)
 581     loader_data->add_class(this);
 582   }
 583 
 584   Handle loader(THREAD, loader_data->class_loader());
 585   ModuleEntry* module_entry = NULL;
 586   Klass* k = this;
 587   if (k->is_objArray_klass()) {
 588     k = ObjArrayKlass::cast(k)->bottom_klass();
 589   }
 590   // Obtain klass' module.
 591   if (k->is_instance_klass()) {
 592     InstanceKlass* ik = (InstanceKlass*) k;
 593     module_entry = ik->module();
 594   } else {
 595     module_entry = ModuleEntryTable::javabase_moduleEntry();
 596   }
 597   // Obtain java.lang.Module, if available
 598   Handle module_handle(THREAD, ((module_entry != NULL) ? module_entry->module() : (oop)NULL));
 599 
 600   if (this->has_raw_archived_mirror()) {
 601     ResourceMark rm(THREAD);
 602     log_debug(cds, mirror)("%s has raw archived mirror", external_name());
 603     if (HeapShared::open_archive_heap_region_mapped()) {
 604       bool present = java_lang_Class::restore_archived_mirror(this, loader, module_handle,
 605                                                               protection_domain,
 606                                                               CHECK);
 607       if (present) {
 608         return;
 609       }
 610     }
 611 
 612     // No archived mirror data
 613     log_debug(cds, mirror)("No archived mirror data for %s", external_name());
 614     clear_java_mirror_handle();
 615     this->clear_has_raw_archived_mirror();
 616   }
 617 
 618   // Only recreate it if not present.  A previous attempt to restore may have
 619   // gotten an OOM later but keep the mirror if it was created.
 620   if (java_mirror() == NULL) {
 621     log_trace(cds, mirror)("Recreate mirror for %s", external_name());
 622     java_lang_Class::create_mirror(this, loader, module_handle, protection_domain, Handle(), CHECK);
 623   }
 624 }
 625 
 626 #if INCLUDE_CDS_JAVA_HEAP
 627 // Used at CDS dump time to access the archived mirror. No GC barrier.
 628 oop Klass::archived_java_mirror_raw() {
 629   assert(has_raw_archived_mirror(), "must have raw archived mirror");
 630   return CompressedOops::decode(_archived_mirror);
 631 }
 632 
 633 narrowOop Klass::archived_java_mirror_raw_narrow() {
 634   assert(has_raw_archived_mirror(), "must have raw archived mirror");
 635   return _archived_mirror;


 636 }
 637 
 638 // No GC barrier
 639 void Klass::set_archived_java_mirror_raw(oop m) {
 640   assert(DumpSharedSpaces, "called only during runtime");
 641   _archived_mirror = CompressedOops::encode(m);
 642 }
 643 #endif // INCLUDE_CDS_JAVA_HEAP
 644 
 645 Klass* Klass::array_klass_or_null(int rank) {
 646   EXCEPTION_MARK;
 647   // No exception can be thrown by array_klass_impl when called with or_null == true.
 648   // (In anycase, the execption mark will fail if it do so)
 649   return array_klass_impl(true, rank, THREAD);
 650 }
 651 
 652 
 653 Klass* Klass::array_klass_or_null() {
 654   EXCEPTION_MARK;
 655   // No exception can be thrown by array_klass_impl when called with or_null == true.
 656   // (In anycase, the execption mark will fail if it do so)
 657   return array_klass_impl(true, THREAD);
 658 }
 659 
 660 
 661 Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) {

 186 #ifdef ASSERT
 187   tty->print_cr("Error: uncached_lookup_method called on a klass oop."
 188                 " Likely error: reflection method does not correctly"
 189                 " wrap return value in a mirror object.");
 190 #endif
 191   ShouldNotReachHere();
 192   return NULL;
 193 }
 194 
 195 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() {
 196   return Metaspace::allocate(loader_data, word_size, MetaspaceObj::ClassType, THREAD);
 197 }
 198 
 199 // "Normal" instantiation is preceeded by a MetaspaceObj allocation
 200 // which zeros out memory - calloc equivalent.
 201 // The constructor is also used from CppVtableCloner,
 202 // which doesn't zero out the memory before calling the constructor.
 203 Klass::Klass(KlassID id) : _id(id),
 204                            _prototype_header(markWord::prototype()),
 205                            _shared_class_path_index(-1) {
 206   CDS_ONLY(_shared_class_flags = 0);
 207   CDS_JAVA_HEAP_ONLY(_archived_mirror_index = -1);
 208   _primary_supers[0] = this;
 209   set_super_check_offset(in_bytes(primary_supers_offset()));
 210 }
 211 
 212 jint Klass::array_layout_helper(BasicType etype) {
 213   assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
 214   // Note that T_ARRAY is not allowed here.
 215   int  hsize = arrayOopDesc::base_offset_in_bytes(etype);
 216   int  esize = type2aelembytes(etype);
 217   bool isobj = (etype == T_OBJECT);
 218   int  tag   =  isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value;
 219   int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize));
 220 
 221   assert(lh < (int)_lh_neutral_value, "must look like an array layout");
 222   assert(layout_helper_is_array(lh), "correct kind");
 223   assert(layout_helper_is_objArray(lh) == isobj, "correct kind");
 224   assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
 225   assert(layout_helper_header_size(lh) == hsize, "correct decode");
 226   assert(layout_helper_element_type(lh) == etype, "correct decode");
 227   assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");

 556     ResourceMark rm;
 557     log_trace(cds, unshareable)("remove java_mirror: %s", external_name());
 558   }
 559   // Just null out the mirror.  The class_loader_data() no longer exists.
 560   clear_java_mirror_handle();
 561 }
 562 
 563 void Klass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
 564   assert(is_klass(), "ensure C++ vtable is restored");
 565   assert(is_shared(), "must be set");
 566   JFR_ONLY(RESTORE_ID(this);)
 567   if (log_is_enabled(Trace, cds, unshareable)) {
 568     ResourceMark rm(THREAD);
 569     log_trace(cds, unshareable)("restore: %s", external_name());
 570   }
 571 
 572   // If an exception happened during CDS restore, some of these fields may already be
 573   // set.  We leave the class on the CLD list, even if incomplete so that we don't
 574   // modify the CLD list outside a safepoint.
 575   if (class_loader_data() == NULL) {

 576     set_class_loader_data(loader_data);
 577 
 578     // Add to class loader list first before creating the mirror
 579     // (same order as class file parsing)
 580     loader_data->add_class(this);
 581   }
 582 
 583   Handle loader(THREAD, loader_data->class_loader());
 584   ModuleEntry* module_entry = NULL;
 585   Klass* k = this;
 586   if (k->is_objArray_klass()) {
 587     k = ObjArrayKlass::cast(k)->bottom_klass();
 588   }
 589   // Obtain klass' module.
 590   if (k->is_instance_klass()) {
 591     InstanceKlass* ik = (InstanceKlass*) k;
 592     module_entry = ik->module();
 593   } else {
 594     module_entry = ModuleEntryTable::javabase_moduleEntry();
 595   }
 596   // Obtain java.lang.Module, if available
 597   Handle module_handle(THREAD, ((module_entry != NULL) ? module_entry->module() : (oop)NULL));
 598 
 599   if (this->has_archived_mirror_index()) {
 600     ResourceMark rm(THREAD);
 601     log_debug(cds, mirror)("%s has raw archived mirror", external_name());
 602     if (HeapShared::open_archive_heap_region_mapped()) {
 603       bool present = java_lang_Class::restore_archived_mirror(this, loader, module_handle,
 604                                                               protection_domain,
 605                                                               CHECK);
 606       if (present) {
 607         return;
 608       }
 609     }
 610 
 611     // No archived mirror data
 612     log_debug(cds, mirror)("No archived mirror data for %s", external_name());
 613     clear_java_mirror_handle();
 614     this->clear_archived_mirror_index();
 615   }
 616 
 617   // Only recreate it if not present.  A previous attempt to restore may have
 618   // gotten an OOM later but keep the mirror if it was created.
 619   if (java_mirror() == NULL) {
 620     log_trace(cds, mirror)("Recreate mirror for %s", external_name());
 621     java_lang_Class::create_mirror(this, loader, module_handle, protection_domain, Handle(), CHECK);
 622   }
 623 }
 624 
 625 #if INCLUDE_CDS_JAVA_HEAP
 626 oop Klass::archived_java_mirror() {
 627   assert(has_archived_mirror_index(), "must have archived mirror");
 628   return HeapShared::get_root(_archived_mirror_index);

 629 }
 630 
 631 void Klass::clear_archived_mirror_index() {
 632   if (_archived_mirror_index >= 0) {
 633     HeapShared::clear_root(_archived_mirror_index);
 634   }
 635   _archived_mirror_index = -1;
 636 }
 637 
 638 // No GC barrier
 639 void Klass::set_archived_java_mirror(oop m) {
 640   assert(DumpSharedSpaces, "called only during runtime");
 641   _archived_mirror_index = HeapShared::append_root(m);
 642 }
 643 #endif // INCLUDE_CDS_JAVA_HEAP
 644 
 645 Klass* Klass::array_klass_or_null(int rank) {
 646   EXCEPTION_MARK;
 647   // No exception can be thrown by array_klass_impl when called with or_null == true.
 648   // (In anycase, the execption mark will fail if it do so)
 649   return array_klass_impl(true, rank, THREAD);
 650 }
 651 
 652 
 653 Klass* Klass::array_klass_or_null() {
 654   EXCEPTION_MARK;
 655   // No exception can be thrown by array_klass_impl when called with or_null == true.
 656   // (In anycase, the execption mark will fail if it do so)
 657   return array_klass_impl(true, THREAD);
 658 }
 659 
 660 
 661 Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) {
< prev index next >