42 //
43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
44 // that represent the loader's "linking domain" in the JVM.
45 //
46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
47 // the singleton class the_null_class_loader_data().
48
49 #include "precompiled.hpp"
50 #include "classfile/classLoaderData.inline.hpp"
51 #include "classfile/classLoaderDataGraph.inline.hpp"
52 #include "classfile/dictionary.hpp"
53 #include "classfile/javaClasses.hpp"
54 #include "classfile/moduleEntry.hpp"
55 #include "classfile/packageEntry.hpp"
56 #include "classfile/symbolTable.hpp"
57 #include "classfile/systemDictionary.hpp"
58 #include "logging/log.hpp"
59 #include "logging/logStream.hpp"
60 #include "memory/allocation.inline.hpp"
61 #include "memory/metadataFactory.hpp"
62 #include "memory/resourceArea.hpp"
63 #include "oops/access.inline.hpp"
64 #include "oops/oop.inline.hpp"
65 #include "oops/oopHandle.inline.hpp"
66 #include "oops/weakHandle.inline.hpp"
67 #include "runtime/atomic.hpp"
68 #include "runtime/handles.inline.hpp"
69 #include "runtime/mutex.hpp"
70 #include "runtime/orderAccess.hpp"
71 #include "runtime/safepoint.hpp"
72 #include "utilities/growableArray.hpp"
73 #include "utilities/macros.hpp"
74 #include "utilities/ostream.hpp"
75
76 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
77
78 void ClassLoaderData::init_null_class_loader_data() {
79 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
80 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
81
82 _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
83 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
84 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
85
86 LogTarget(Trace, class, loader, data) lt;
87 if (lt.is_enabled()) {
88 ResourceMark rm;
89 LogStream ls(lt);
90 ls.print("create ");
91 _the_null_class_loader_data->print_value_on(&ls);
92 ls.cr();
93 }
94 }
95
742 // Returns true if this class loader data is a class loader data
743 // that is not ever freed by a GC. It must be the CLD for one of the builtin
744 // class loaders and not the CLD for an unsafe anonymous class.
745 bool ClassLoaderData::is_permanent_class_loader_data() const {
746 return is_builtin_class_loader_data() && !is_unsafe_anonymous();
747 }
748
749 ClassLoaderMetaspace* ClassLoaderData::metaspace_non_null() {
750 // If the metaspace has not been allocated, create a new one. Might want
751 // to create smaller arena for Reflection class loaders also.
752 // The reason for the delayed allocation is because some class loaders are
753 // simply for delegating with no metadata of their own.
754 // Lock-free access requires load_acquire.
755 ClassLoaderMetaspace* metaspace = OrderAccess::load_acquire(&_metaspace);
756 if (metaspace == NULL) {
757 MutexLocker ml(_metaspace_lock, Mutex::_no_safepoint_check_flag);
758 // Check if _metaspace got allocated while we were waiting for this lock.
759 if ((metaspace = _metaspace) == NULL) {
760 if (this == the_null_class_loader_data()) {
761 assert (class_loader() == NULL, "Must be");
762 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::BootMetaspaceType);
763 } else if (is_unsafe_anonymous()) {
764 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::UnsafeAnonymousMetaspaceType);
765 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
766 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType);
767 } else {
768 metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::StandardMetaspaceType);
769 }
770 // Ensure _metaspace is stable, since it is examined without a lock
771 OrderAccess::release_store(&_metaspace, metaspace);
772 }
773 }
774 return metaspace;
775 }
776
777 OopHandle ClassLoaderData::add_handle(Handle h) {
778 MutexLocker ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
779 record_modified_oops();
780 return OopHandle(_handles.add(h()));
781 }
782
783 void ClassLoaderData::remove_handle(OopHandle h) {
784 assert(!is_unloading(), "Do not remove a handle for a CLD that is unloading");
785 oop* ptr = h.ptr_raw();
786 if (ptr != NULL) {
787 assert(_handles.owner_of(ptr), "Got unexpected handle " PTR_FORMAT, p2i(ptr));
788 NativeAccess<>::oop_store(ptr, oop(NULL));
939
940 if (_jmethod_ids != NULL) {
941 Method::print_jmethod_ids(this, out);
942 }
943 out->print(" handles count %d", _handles.count());
944 out->print(" dependencies %d", _dependency_count);
945 out->print_cr("}");
946 }
947 #endif // PRODUCT
948
949 void ClassLoaderData::print() const { print_on(tty); }
950
951 void ClassLoaderData::verify() {
952 assert_locked_or_safepoint(_metaspace_lock);
953 oop cl = class_loader();
954
955 guarantee(this == class_loader_data(cl) || is_unsafe_anonymous(), "Must be the same");
956 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_unsafe_anonymous(), "must be");
957
958 // Verify the integrity of the allocated space.
959 if (metaspace_or_null() != NULL) {
960 metaspace_or_null()->verify();
961 }
962
963 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
964 guarantee(k->class_loader_data() == this, "Must be the same");
965 k->verify();
966 assert(k != k->next_link(), "no loops!");
967 }
968 }
969
970 bool ClassLoaderData::contains_klass(Klass* klass) {
971 // Lock-free access requires load_acquire
972 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
973 if (k == klass) return true;
974 }
975 return false;
976 }
|
42 //
43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
44 // that represent the loader's "linking domain" in the JVM.
45 //
46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
47 // the singleton class the_null_class_loader_data().
48
49 #include "precompiled.hpp"
50 #include "classfile/classLoaderData.inline.hpp"
51 #include "classfile/classLoaderDataGraph.inline.hpp"
52 #include "classfile/dictionary.hpp"
53 #include "classfile/javaClasses.hpp"
54 #include "classfile/moduleEntry.hpp"
55 #include "classfile/packageEntry.hpp"
56 #include "classfile/symbolTable.hpp"
57 #include "classfile/systemDictionary.hpp"
58 #include "logging/log.hpp"
59 #include "logging/logStream.hpp"
60 #include "memory/allocation.inline.hpp"
61 #include "memory/metadataFactory.hpp"
62 #include "memory/metaspace/classLoaderMetaspace.hpp"
63 #include "memory/metaspace/metaspaceEnums.hpp"
64 #include "memory/resourceArea.hpp"
65 #include "oops/access.inline.hpp"
66 #include "oops/oop.inline.hpp"
67 #include "oops/oopHandle.inline.hpp"
68 #include "oops/weakHandle.inline.hpp"
69 #include "runtime/atomic.hpp"
70 #include "runtime/handles.inline.hpp"
71 #include "runtime/mutex.hpp"
72 #include "runtime/orderAccess.hpp"
73 #include "runtime/safepoint.hpp"
74 #include "utilities/growableArray.hpp"
75 #include "utilities/macros.hpp"
76 #include "utilities/ostream.hpp"
77
78 using metaspace::ClassLoaderMetaspace;
79
80 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
81
82 void ClassLoaderData::init_null_class_loader_data() {
83 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
84 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
85
86 _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
87 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
88 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
89
90 LogTarget(Trace, class, loader, data) lt;
91 if (lt.is_enabled()) {
92 ResourceMark rm;
93 LogStream ls(lt);
94 ls.print("create ");
95 _the_null_class_loader_data->print_value_on(&ls);
96 ls.cr();
97 }
98 }
99
746 // Returns true if this class loader data is a class loader data
747 // that is not ever freed by a GC. It must be the CLD for one of the builtin
748 // class loaders and not the CLD for an unsafe anonymous class.
749 bool ClassLoaderData::is_permanent_class_loader_data() const {
750 return is_builtin_class_loader_data() && !is_unsafe_anonymous();
751 }
752
753 ClassLoaderMetaspace* ClassLoaderData::metaspace_non_null() {
754 // If the metaspace has not been allocated, create a new one. Might want
755 // to create smaller arena for Reflection class loaders also.
756 // The reason for the delayed allocation is because some class loaders are
757 // simply for delegating with no metadata of their own.
758 // Lock-free access requires load_acquire.
759 ClassLoaderMetaspace* metaspace = OrderAccess::load_acquire(&_metaspace);
760 if (metaspace == NULL) {
761 MutexLocker ml(_metaspace_lock, Mutex::_no_safepoint_check_flag);
762 // Check if _metaspace got allocated while we were waiting for this lock.
763 if ((metaspace = _metaspace) == NULL) {
764 if (this == the_null_class_loader_data()) {
765 assert (class_loader() == NULL, "Must be");
766 metaspace = new ClassLoaderMetaspace(_metaspace_lock, metaspace::BootMetaspaceType);
767 } else if (is_unsafe_anonymous()) {
768 metaspace = new ClassLoaderMetaspace(_metaspace_lock, metaspace::UnsafeAnonymousMetaspaceType);
769 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
770 metaspace = new ClassLoaderMetaspace(_metaspace_lock, metaspace::ReflectionMetaspaceType);
771 } else {
772 metaspace = new ClassLoaderMetaspace(_metaspace_lock, metaspace::StandardMetaspaceType);
773 }
774 // Ensure _metaspace is stable, since it is examined without a lock
775 OrderAccess::release_store(&_metaspace, metaspace);
776 }
777 }
778 return metaspace;
779 }
780
781 OopHandle ClassLoaderData::add_handle(Handle h) {
782 MutexLocker ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
783 record_modified_oops();
784 return OopHandle(_handles.add(h()));
785 }
786
787 void ClassLoaderData::remove_handle(OopHandle h) {
788 assert(!is_unloading(), "Do not remove a handle for a CLD that is unloading");
789 oop* ptr = h.ptr_raw();
790 if (ptr != NULL) {
791 assert(_handles.owner_of(ptr), "Got unexpected handle " PTR_FORMAT, p2i(ptr));
792 NativeAccess<>::oop_store(ptr, oop(NULL));
943
944 if (_jmethod_ids != NULL) {
945 Method::print_jmethod_ids(this, out);
946 }
947 out->print(" handles count %d", _handles.count());
948 out->print(" dependencies %d", _dependency_count);
949 out->print_cr("}");
950 }
951 #endif // PRODUCT
952
953 void ClassLoaderData::print() const { print_on(tty); }
954
955 void ClassLoaderData::verify() {
956 assert_locked_or_safepoint(_metaspace_lock);
957 oop cl = class_loader();
958
959 guarantee(this == class_loader_data(cl) || is_unsafe_anonymous(), "Must be the same");
960 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_unsafe_anonymous(), "must be");
961
962 // Verify the integrity of the allocated space.
963 #ifdef ASSERT
964 if (metaspace_or_null() != NULL) {
965 metaspace_or_null()->verify(false);
966 }
967 #endif
968
969 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
970 guarantee(k->class_loader_data() == this, "Must be the same");
971 k->verify();
972 assert(k != k->next_link(), "no loops!");
973 }
974 }
975
976 bool ClassLoaderData::contains_klass(Klass* klass) {
977 // Lock-free access requires load_acquire
978 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
979 if (k == klass) return true;
980 }
981 return false;
982 }
|