134 _modified_oops(true), _accumulated_modified_oops(false),
135 // An unsafe anonymous class loader data doesn't have anything to keep
136 // it from being unloaded during parsing of the unsafe anonymous class.
137 // The null-class-loader should always be kept alive.
138 _keep_alive((has_class_mirror_holder || h_class_loader.is_null()) ? 1 : 0),
139 _claim(0),
140 _handles(),
141 _klasses(NULL), _packages(NULL), _modules(NULL), _unnamed_module(NULL), _dictionary(NULL),
142 _jmethod_ids(NULL),
143 _deallocate_list(NULL),
144 _next(NULL),
145 _class_loader_klass(NULL), _name(NULL), _name_and_id(NULL) {
146
147 if (!h_class_loader.is_null()) {
148 _class_loader = _handles.add(h_class_loader());
149 _class_loader_klass = h_class_loader->klass();
150 initialize_name(h_class_loader);
151 }
152
153 if (!has_class_mirror_holder) {
154 // The holder is initialized later for weak hidden and unsafe anonymous classes,
155 // and before calling anything that call class_loader().
156 initialize_holder(h_class_loader);
157
158 // A ClassLoaderData created solely for an weak hidden or unsafe anonymous class should
159 // never have a ModuleEntryTable or PackageEntryTable created for it. The defining package
160 // and module for an unsafe anonymous class will be found in its host class.
161 _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
162 if (h_class_loader.is_null()) {
163 // Create unnamed module for boot loader
164 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
165 } else {
166 // Create unnamed module for all other loaders
167 _unnamed_module = ModuleEntry::create_unnamed_module(this);
168 }
169 _dictionary = create_dictionary();
170 }
171
172 NOT_PRODUCT(_dependency_count = 0); // number of class loader dependencies
173
174 JFR_ONLY(INIT_ID(this);)
175 }
176
177 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
178 Chunk* c = _head;
401 }
402 }
403 }
404
405 void ClassLoaderData::record_dependency(const Klass* k) {
406 assert(k != NULL, "invariant");
407
408 ClassLoaderData * const from_cld = this;
409 ClassLoaderData * const to_cld = k->class_loader_data();
410
411 // Do not need to record dependency if the dependency is to a class whose
412 // class loader data is never freed. (i.e. the dependency's class loader
413 // is one of the three builtin class loaders and the dependency's class
414 // loader data has a ClassLoader holder, not a Class holder.)
415 if (to_cld->is_permanent_class_loader_data()) {
416 return;
417 }
418
419 oop to;
420 if (to_cld->has_class_mirror_holder()) {
421 // Just return if a weak hidden or unsafe anonymous class is attempting to record a dependency
422 // to itself. (Note that every weak hidden or unsafe anonymous class has its own unique class
423 // loader data.)
424 if (to_cld == from_cld) {
425 return;
426 }
427 // Hidden and unsafe anonymous class dependencies are through the mirror.
428 to = k->java_mirror();
429 } else {
430 to = to_cld->class_loader();
431 oop from = from_cld->class_loader();
432
433 // Just return if this dependency is to a class with the same or a parent
434 // class_loader.
435 if (from == to || java_lang_ClassLoader::isAncestor(from, to)) {
436 return; // this class loader is in the parent list, no need to add it.
437 }
438 }
439
440 // It's a dependency we won't find through GC, add it.
441 if (!_handles.contains(to)) {
442 NOT_PRODUCT(Atomic::inc(&_dependency_count));
601 // to get notified about this potential resurrection, otherwise the marking
602 // might not find the object.
603 if (!_holder.is_null()) { // NULL class_loader
604 return _holder.resolve();
605 } else {
606 return NULL;
607 }
608 }
609
610 // Let the GC read the holder without keeping it alive.
611 oop ClassLoaderData::holder_no_keepalive() const {
612 if (!_holder.is_null()) { // NULL class_loader
613 return _holder.peek();
614 } else {
615 return NULL;
616 }
617 }
618
619 // Unloading support
620 bool ClassLoaderData::is_alive() const {
621 bool alive = keep_alive() // null class loader and incomplete weak hidden or unsafe anonymous klasses.
622 || (_holder.peek() != NULL); // and not cleaned by the GC weak handle processing.
623
624 return alive;
625 }
626
627 class ReleaseKlassClosure: public KlassClosure {
628 private:
629 size_t _instance_class_released;
630 size_t _array_class_released;
631 public:
632 ReleaseKlassClosure() : _instance_class_released(0), _array_class_released(0) { }
633
634 size_t instance_class_released() const { return _instance_class_released; }
635 size_t array_class_released() const { return _array_class_released; }
636
637 void do_klass(Klass* k) {
638 if (k->is_array_klass()) {
639 _array_class_released ++;
640 } else {
641 assert(k->is_instance_klass(), "Must be");
713 _name_and_id->decrement_refcount();
714 }
715 }
716
717 // Returns true if this class loader data is for the app class loader
718 // or a user defined system class loader. (Note that the class loader
719 // data may have a Class holder.)
720 bool ClassLoaderData::is_system_class_loader_data() const {
721 return SystemDictionary::is_system_class_loader(class_loader());
722 }
723
724 // Returns true if this class loader data is for the platform class loader.
725 // (Note that the class loader data may have a Class holder.)
726 bool ClassLoaderData::is_platform_class_loader_data() const {
727 return SystemDictionary::is_platform_class_loader(class_loader());
728 }
729
730 // Returns true if the class loader for this class loader data is one of
731 // the 3 builtin (boot application/system or platform) class loaders,
732 // including a user-defined system class loader. Note that if the class
733 // loader data is for a weak hidden or unsafe anonymous class then it may
734 // get freed by a GC even if its class loader is one of these loaders.
735 bool ClassLoaderData::is_builtin_class_loader_data() const {
736 return (is_boot_class_loader_data() ||
737 SystemDictionary::is_system_class_loader(class_loader()) ||
738 SystemDictionary::is_platform_class_loader(class_loader()));
739 }
740
741 // Returns true if this class loader data is a class loader data
742 // that is not ever freed by a GC. It must be the CLD for one of the builtin
743 // class loaders and not the CLD for a weak hidden or unsafe anonymous class.
744 bool ClassLoaderData::is_permanent_class_loader_data() const {
745 return is_builtin_class_loader_data() && !has_class_mirror_holder();
746 }
747
748 ClassLoaderMetaspace* ClassLoaderData::metaspace_non_null() {
749 // If the metaspace has not been allocated, create a new one. Might want
750 // to create smaller arena for Reflection class loaders also.
751 // The reason for the delayed allocation is because some class loaders are
752 // simply for delegating with no metadata of their own.
753 // Lock-free access requires load_acquire.
754 ClassLoaderMetaspace* metaspace = Atomic::load_acquire(&_metaspace);
755 if (metaspace == NULL) {
756 MutexLocker ml(_metaspace_lock, Mutex::_no_safepoint_check_flag);
757 // Check if _metaspace got allocated while we were waiting for this lock.
758 if ((metaspace = _metaspace) == NULL) {
759 if (this == the_null_class_loader_data()) {
760 assert (class_loader() == NULL, "Must be");
761 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::BootMetaspaceType);
762 } else if (has_class_mirror_holder()) {
763 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ClassMirrorHolderMetaspaceType);
860 if (_deallocate_list == NULL) {
861 return;
862 }
863 // Go backwards because this removes entries that are freed.
864 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
865 Metadata* m = _deallocate_list->at(i);
866 _deallocate_list->remove_at(i);
867 if (m->is_constantPool()) {
868 ((ConstantPool*)m)->release_C_heap_structures();
869 } else if (m->is_klass()) {
870 InstanceKlass* ik = (InstanceKlass*)m;
871 // also releases ik->constants() C heap memory
872 InstanceKlass::release_C_heap_structures(ik);
873 // Remove the class so unloading events aren't triggered for
874 // this class (scratch or error class) in do_unloading().
875 remove_class(ik);
876 }
877 }
878 }
879
880 // These CLDs are to contain weak hidden or unsafe anonymous classes used for JSR292
881 ClassLoaderData* ClassLoaderData::has_class_mirror_holder_cld(Handle loader) {
882 // Add a new class loader data to the graph.
883 return ClassLoaderDataGraph::add(loader, true);
884 }
885
886 // Caller needs ResourceMark
887 // If the class loader's _name has not been explicitly set, the class loader's
888 // qualified class name is returned.
889 const char* ClassLoaderData::loader_name() const {
890 if (_class_loader_klass == NULL) {
891 return BOOTSTRAP_LOADER_NAME;
892 } else if (_name != NULL) {
893 return _name->as_C_string();
894 } else {
895 return _class_loader_klass->external_name();
896 }
897 }
898
899 // Caller needs ResourceMark
900 // Format of the _name_and_id is as follows:
|
134 _modified_oops(true), _accumulated_modified_oops(false),
135 // An unsafe anonymous class loader data doesn't have anything to keep
136 // it from being unloaded during parsing of the unsafe anonymous class.
137 // The null-class-loader should always be kept alive.
138 _keep_alive((has_class_mirror_holder || h_class_loader.is_null()) ? 1 : 0),
139 _claim(0),
140 _handles(),
141 _klasses(NULL), _packages(NULL), _modules(NULL), _unnamed_module(NULL), _dictionary(NULL),
142 _jmethod_ids(NULL),
143 _deallocate_list(NULL),
144 _next(NULL),
145 _class_loader_klass(NULL), _name(NULL), _name_and_id(NULL) {
146
147 if (!h_class_loader.is_null()) {
148 _class_loader = _handles.add(h_class_loader());
149 _class_loader_klass = h_class_loader->klass();
150 initialize_name(h_class_loader);
151 }
152
153 if (!has_class_mirror_holder) {
154 // The holder is initialized later for non-strong hidden classes and unsafe anonymous classes,
155 // and before calling anything that call class_loader().
156 initialize_holder(h_class_loader);
157
158 // A ClassLoaderData created solely for a non-strong hidden class or unsafe anonymous class should
159 // never have a ModuleEntryTable or PackageEntryTable created for it. The defining package
160 // and module for an unsafe anonymous class will be found in its host class.
161 _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
162 if (h_class_loader.is_null()) {
163 // Create unnamed module for boot loader
164 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
165 } else {
166 // Create unnamed module for all other loaders
167 _unnamed_module = ModuleEntry::create_unnamed_module(this);
168 }
169 _dictionary = create_dictionary();
170 }
171
172 NOT_PRODUCT(_dependency_count = 0); // number of class loader dependencies
173
174 JFR_ONLY(INIT_ID(this);)
175 }
176
177 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
178 Chunk* c = _head;
401 }
402 }
403 }
404
405 void ClassLoaderData::record_dependency(const Klass* k) {
406 assert(k != NULL, "invariant");
407
408 ClassLoaderData * const from_cld = this;
409 ClassLoaderData * const to_cld = k->class_loader_data();
410
411 // Do not need to record dependency if the dependency is to a class whose
412 // class loader data is never freed. (i.e. the dependency's class loader
413 // is one of the three builtin class loaders and the dependency's class
414 // loader data has a ClassLoader holder, not a Class holder.)
415 if (to_cld->is_permanent_class_loader_data()) {
416 return;
417 }
418
419 oop to;
420 if (to_cld->has_class_mirror_holder()) {
421 // Just return if a non-strong hidden class or unsafe anonymous class is attempting to record a dependency
422 // to itself. (Note that every non-strong hidden class or unsafe anonymous class has its own unique class
423 // loader data.)
424 if (to_cld == from_cld) {
425 return;
426 }
427 // Hidden and unsafe anonymous class dependencies are through the mirror.
428 to = k->java_mirror();
429 } else {
430 to = to_cld->class_loader();
431 oop from = from_cld->class_loader();
432
433 // Just return if this dependency is to a class with the same or a parent
434 // class_loader.
435 if (from == to || java_lang_ClassLoader::isAncestor(from, to)) {
436 return; // this class loader is in the parent list, no need to add it.
437 }
438 }
439
440 // It's a dependency we won't find through GC, add it.
441 if (!_handles.contains(to)) {
442 NOT_PRODUCT(Atomic::inc(&_dependency_count));
601 // to get notified about this potential resurrection, otherwise the marking
602 // might not find the object.
603 if (!_holder.is_null()) { // NULL class_loader
604 return _holder.resolve();
605 } else {
606 return NULL;
607 }
608 }
609
610 // Let the GC read the holder without keeping it alive.
611 oop ClassLoaderData::holder_no_keepalive() const {
612 if (!_holder.is_null()) { // NULL class_loader
613 return _holder.peek();
614 } else {
615 return NULL;
616 }
617 }
618
619 // Unloading support
620 bool ClassLoaderData::is_alive() const {
621 bool alive = keep_alive() // null class loader and incomplete non-strong hidden class or unsafe anonymous class.
622 || (_holder.peek() != NULL); // and not cleaned by the GC weak handle processing.
623
624 return alive;
625 }
626
627 class ReleaseKlassClosure: public KlassClosure {
628 private:
629 size_t _instance_class_released;
630 size_t _array_class_released;
631 public:
632 ReleaseKlassClosure() : _instance_class_released(0), _array_class_released(0) { }
633
634 size_t instance_class_released() const { return _instance_class_released; }
635 size_t array_class_released() const { return _array_class_released; }
636
637 void do_klass(Klass* k) {
638 if (k->is_array_klass()) {
639 _array_class_released ++;
640 } else {
641 assert(k->is_instance_klass(), "Must be");
713 _name_and_id->decrement_refcount();
714 }
715 }
716
717 // Returns true if this class loader data is for the app class loader
718 // or a user defined system class loader. (Note that the class loader
719 // data may have a Class holder.)
720 bool ClassLoaderData::is_system_class_loader_data() const {
721 return SystemDictionary::is_system_class_loader(class_loader());
722 }
723
724 // Returns true if this class loader data is for the platform class loader.
725 // (Note that the class loader data may have a Class holder.)
726 bool ClassLoaderData::is_platform_class_loader_data() const {
727 return SystemDictionary::is_platform_class_loader(class_loader());
728 }
729
730 // Returns true if the class loader for this class loader data is one of
731 // the 3 builtin (boot application/system or platform) class loaders,
732 // including a user-defined system class loader. Note that if the class
733 // loader data is for a non-strong hidden class or unsafe anonymous class then it may
734 // get freed by a GC even if its class loader is one of these loaders.
735 bool ClassLoaderData::is_builtin_class_loader_data() const {
736 return (is_boot_class_loader_data() ||
737 SystemDictionary::is_system_class_loader(class_loader()) ||
738 SystemDictionary::is_platform_class_loader(class_loader()));
739 }
740
741 // Returns true if this class loader data is a class loader data
742 // that is not ever freed by a GC. It must be the CLD for one of the builtin
743 // class loaders and not the CLD for a non-strong hidden class or unsafe anonymous class.
744 bool ClassLoaderData::is_permanent_class_loader_data() const {
745 return is_builtin_class_loader_data() && !has_class_mirror_holder();
746 }
747
748 ClassLoaderMetaspace* ClassLoaderData::metaspace_non_null() {
749 // If the metaspace has not been allocated, create a new one. Might want
750 // to create smaller arena for Reflection class loaders also.
751 // The reason for the delayed allocation is because some class loaders are
752 // simply for delegating with no metadata of their own.
753 // Lock-free access requires load_acquire.
754 ClassLoaderMetaspace* metaspace = Atomic::load_acquire(&_metaspace);
755 if (metaspace == NULL) {
756 MutexLocker ml(_metaspace_lock, Mutex::_no_safepoint_check_flag);
757 // Check if _metaspace got allocated while we were waiting for this lock.
758 if ((metaspace = _metaspace) == NULL) {
759 if (this == the_null_class_loader_data()) {
760 assert (class_loader() == NULL, "Must be");
761 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::BootMetaspaceType);
762 } else if (has_class_mirror_holder()) {
763 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ClassMirrorHolderMetaspaceType);
860 if (_deallocate_list == NULL) {
861 return;
862 }
863 // Go backwards because this removes entries that are freed.
864 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
865 Metadata* m = _deallocate_list->at(i);
866 _deallocate_list->remove_at(i);
867 if (m->is_constantPool()) {
868 ((ConstantPool*)m)->release_C_heap_structures();
869 } else if (m->is_klass()) {
870 InstanceKlass* ik = (InstanceKlass*)m;
871 // also releases ik->constants() C heap memory
872 InstanceKlass::release_C_heap_structures(ik);
873 // Remove the class so unloading events aren't triggered for
874 // this class (scratch or error class) in do_unloading().
875 remove_class(ik);
876 }
877 }
878 }
879
880 // These CLDs are to contain non-strong hidden classes or unsafe anonymous classes used for JSR292
881 ClassLoaderData* ClassLoaderData::has_class_mirror_holder_cld(Handle loader) {
882 // Add a new class loader data to the graph.
883 return ClassLoaderDataGraph::add(loader, true);
884 }
885
886 // Caller needs ResourceMark
887 // If the class loader's _name has not been explicitly set, the class loader's
888 // qualified class name is returned.
889 const char* ClassLoaderData::loader_name() const {
890 if (_class_loader_klass == NULL) {
891 return BOOTSTRAP_LOADER_NAME;
892 } else if (_name != NULL) {
893 return _name->as_C_string();
894 } else {
895 return _class_loader_klass->external_name();
896 }
897 }
898
899 // Caller needs ResourceMark
900 // Format of the _name_and_id is as follows:
|