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.hpp"
51 #include "classfile/classLoaderData.inline.hpp"
52 #include "classfile/javaClasses.hpp"
53 #include "classfile/metadataOnStackMark.hpp"
54 #include "classfile/moduleEntry.hpp"
55 #include "classfile/packageEntry.hpp"
56 #include "classfile/systemDictionary.hpp"
57 #include "code/codeCache.hpp"
58 #include "gc/shared/gcLocker.hpp"
59 #include "logging/log.hpp"
60 #include "memory/metadataFactory.hpp"
61 #include "memory/metaspaceShared.hpp"
62 #include "memory/oopFactory.hpp"
63 #include "memory/resourceArea.hpp"
64 #include "oops/objArrayOop.inline.hpp"
65 #include "oops/oop.inline.hpp"
66 #include "runtime/atomic.hpp"
67 #include "runtime/javaCalls.hpp"
68 #include "runtime/jniHandles.hpp"
69 #include "runtime/mutex.hpp"
70 #include "runtime/orderAccess.hpp"
71 #include "runtime/safepoint.hpp"
72 #include "runtime/synchronizer.hpp"
73 #include "utilities/growableArray.hpp"
74 #include "utilities/macros.hpp"
75 #include "utilities/ostream.hpp"
76 #if INCLUDE_TRACE
77 #include "trace/tracing.hpp"
78 #endif
79
80 // helper function to avoid in-line casts
81 template <typename T> static T* load_ptr_acquire(T* volatile *p) {
82 return static_cast<T*>(OrderAccess::load_ptr_acquire(p));
83 }
84
85 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
273 // Lock to avoid classes being modified/added/removed during iteration
274 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
275 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
276 // Do not filter ArrayKlass oops here...
277 if (k->is_array_klass() || (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded())) {
278 klass_closure->do_klass(k);
279 }
280 }
281 }
282
283 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
284 // Lock-free access requires load_ptr_acquire
285 for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) {
286 if (k->is_instance_klass()) {
287 f(InstanceKlass::cast(k));
288 }
289 assert(k != k->next_link(), "no loops!");
290 }
291 }
292
293 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
294 assert_locked_or_safepoint(Module_lock);
295 if (_unnamed_module != NULL) {
296 f(_unnamed_module);
297 }
298 if (_modules != NULL) {
299 for (int i = 0; i < _modules->table_size(); i++) {
300 for (ModuleEntry* entry = _modules->bucket(i);
301 entry != NULL;
302 entry = entry->next()) {
303 f(entry);
304 }
305 }
306 }
307 }
308
309 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
310 assert_locked_or_safepoint(Module_lock);
311 if (_packages != NULL) {
312 for (int i = 0; i < _packages->table_size(); i++) {
457 Klass* prev = NULL;
458 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
459 if (k == scratch_class) {
460 if (prev == NULL) {
461 _klasses = k->next_link();
462 } else {
463 Klass* next = k->next_link();
464 prev->set_next_link(next);
465 }
466 return;
467 }
468 prev = k;
469 assert(k != k->next_link(), "no loops!");
470 }
471 ShouldNotReachHere(); // should have found this class!!
472 }
473
474 void ClassLoaderData::unload() {
475 _unloading = true;
476
477 // Tell serviceability tools these classes are unloading
478 classes_do(InstanceKlass::notify_unload_class);
479
480 if (log_is_enabled(Debug, class, loader, data)) {
481 ResourceMark rm;
482 outputStream* log = Log(class, loader, data)::debug_stream();
483 log->print(": unload loader data " INTPTR_FORMAT, p2i(this));
484 log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
485 loader_name());
486 if (is_anonymous()) {
487 log->print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses));
488 }
489 log->cr();
490 }
491
492 // In some rare cases items added to this list will not be freed elsewhere.
493 // To keep it simple, just free everything in it here.
494 free_deallocate_list();
495 }
496
653
654 // Deallocate free metadata on the free list. How useful the PermGen was!
655 void ClassLoaderData::free_deallocate_list() {
656 // Don't need lock, at safepoint
657 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
658 if (_deallocate_list == NULL) {
659 return;
660 }
661 // Go backwards because this removes entries that are freed.
662 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
663 Metadata* m = _deallocate_list->at(i);
664 if (!m->on_stack()) {
665 _deallocate_list->remove_at(i);
666 // There are only three types of metadata that we deallocate directly.
667 // Cast them so they can be used by the template function.
668 if (m->is_method()) {
669 MetadataFactory::free_metadata(this, (Method*)m);
670 } else if (m->is_constantPool()) {
671 MetadataFactory::free_metadata(this, (ConstantPool*)m);
672 } else if (m->is_klass()) {
673 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
674 } else {
675 ShouldNotReachHere();
676 }
677 } else {
678 // Metadata is alive.
679 // If scratch_class is on stack then it shouldn't be on this list!
680 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
681 "scratch classes on this list should be dead");
682 // Also should assert that other metadata on the list was found in handles.
683 }
684 }
685 }
686
687 // These anonymous class loaders are to contain classes used for JSR292
688 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
689 // Add a new class loader data to the graph.
690 Handle lh(THREAD, loader);
691 return ClassLoaderDataGraph::add(lh, true, THREAD);
692 }
693
|
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.hpp"
51 #include "classfile/classLoaderData.inline.hpp"
52 #include "classfile/javaClasses.hpp"
53 #include "classfile/metadataOnStackMark.hpp"
54 #include "classfile/moduleEntry.hpp"
55 #include "classfile/packageEntry.hpp"
56 #include "classfile/systemDictionary.hpp"
57 #include "code/codeCache.hpp"
58 #include "gc/shared/gcLocker.hpp"
59 #include "logging/log.hpp"
60 #include "memory/metadataFactory.hpp"
61 #include "memory/metaspaceShared.hpp"
62 #include "memory/oopFactory.hpp"
63 #include "memory/resourceArea.hpp"
64 #include "oops/objArrayOop.inline.hpp"
65 #include "oops/oop.inline.hpp"
66 #include "oops/valueKlass.hpp"
67 #include "runtime/atomic.hpp"
68 #include "runtime/javaCalls.hpp"
69 #include "runtime/jniHandles.hpp"
70 #include "runtime/mutex.hpp"
71 #include "runtime/orderAccess.hpp"
72 #include "runtime/safepoint.hpp"
73 #include "runtime/synchronizer.hpp"
74 #include "utilities/growableArray.hpp"
75 #include "utilities/macros.hpp"
76 #include "utilities/ostream.hpp"
77 #if INCLUDE_TRACE
78 #include "trace/tracing.hpp"
79 #endif
80
81 // helper function to avoid in-line casts
82 template <typename T> static T* load_ptr_acquire(T* volatile *p) {
83 return static_cast<T*>(OrderAccess::load_ptr_acquire(p));
84 }
85
86 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
274 // Lock to avoid classes being modified/added/removed during iteration
275 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
276 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
277 // Do not filter ArrayKlass oops here...
278 if (k->is_array_klass() || (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded())) {
279 klass_closure->do_klass(k);
280 }
281 }
282 }
283
284 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
285 // Lock-free access requires load_ptr_acquire
286 for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) {
287 if (k->is_instance_klass()) {
288 f(InstanceKlass::cast(k));
289 }
290 assert(k != k->next_link(), "no loops!");
291 }
292 }
293
294 void ClassLoaderData::value_classes_do(void f(ValueKlass*)) {
295 // Lock-free access requires load_ptr_acquire
296 for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) {
297 if (k->is_value()) {
298 f(ValueKlass::cast(k));
299 }
300 assert(k != k->next_link(), "no loops!");
301 }
302 }
303
304 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
305 assert_locked_or_safepoint(Module_lock);
306 if (_unnamed_module != NULL) {
307 f(_unnamed_module);
308 }
309 if (_modules != NULL) {
310 for (int i = 0; i < _modules->table_size(); i++) {
311 for (ModuleEntry* entry = _modules->bucket(i);
312 entry != NULL;
313 entry = entry->next()) {
314 f(entry);
315 }
316 }
317 }
318 }
319
320 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
321 assert_locked_or_safepoint(Module_lock);
322 if (_packages != NULL) {
323 for (int i = 0; i < _packages->table_size(); i++) {
468 Klass* prev = NULL;
469 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
470 if (k == scratch_class) {
471 if (prev == NULL) {
472 _klasses = k->next_link();
473 } else {
474 Klass* next = k->next_link();
475 prev->set_next_link(next);
476 }
477 return;
478 }
479 prev = k;
480 assert(k != k->next_link(), "no loops!");
481 }
482 ShouldNotReachHere(); // should have found this class!!
483 }
484
485 void ClassLoaderData::unload() {
486 _unloading = true;
487
488 value_classes_do(ValueKlass::cleanup);
489 // Tell serviceability tools these classes are unloading
490 classes_do(InstanceKlass::notify_unload_class);
491
492 if (log_is_enabled(Debug, class, loader, data)) {
493 ResourceMark rm;
494 outputStream* log = Log(class, loader, data)::debug_stream();
495 log->print(": unload loader data " INTPTR_FORMAT, p2i(this));
496 log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
497 loader_name());
498 if (is_anonymous()) {
499 log->print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses));
500 }
501 log->cr();
502 }
503
504 // In some rare cases items added to this list will not be freed elsewhere.
505 // To keep it simple, just free everything in it here.
506 free_deallocate_list();
507 }
508
665
666 // Deallocate free metadata on the free list. How useful the PermGen was!
667 void ClassLoaderData::free_deallocate_list() {
668 // Don't need lock, at safepoint
669 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
670 if (_deallocate_list == NULL) {
671 return;
672 }
673 // Go backwards because this removes entries that are freed.
674 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
675 Metadata* m = _deallocate_list->at(i);
676 if (!m->on_stack()) {
677 _deallocate_list->remove_at(i);
678 // There are only three types of metadata that we deallocate directly.
679 // Cast them so they can be used by the template function.
680 if (m->is_method()) {
681 MetadataFactory::free_metadata(this, (Method*)m);
682 } else if (m->is_constantPool()) {
683 MetadataFactory::free_metadata(this, (ConstantPool*)m);
684 } else if (m->is_klass()) {
685 if (!((Klass*)m)->is_value()) {
686 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
687 } else {
688 MetadataFactory::free_metadata(this, (ValueKlass*)m);
689 }
690 } else {
691 ShouldNotReachHere();
692 }
693 } else {
694 // Metadata is alive.
695 // If scratch_class is on stack then it shouldn't be on this list!
696 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
697 "scratch classes on this list should be dead");
698 // Also should assert that other metadata on the list was found in handles.
699 }
700 }
701 }
702
703 // These anonymous class loaders are to contain classes used for JSR292
704 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
705 // Add a new class loader data to the graph.
706 Handle lh(THREAD, loader);
707 return ClassLoaderDataGraph::add(lh, true, THREAD);
708 }
709
|