1 /*
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
37 // ClassLoaderData carries information related to a linkset (e.g.,
38 // metaspace holding its klass definitions).
39 // The System Dictionary and related data structures (e.g., placeholder table,
40 // loader constraints table) as well as the runtime representation of classes
41 // only reference ClassLoaderData.
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.hpp"
51 #include "classfile/classLoaderData.inline.hpp"
52 #include "classfile/javaClasses.hpp"
53 #include "classfile/metadataOnStackMark.hpp"
54 #include "classfile/systemDictionary.hpp"
55 #include "code/codeCache.hpp"
56 #include "gc/shared/gcLocker.hpp"
57 #include "memory/metadataFactory.hpp"
58 #include "memory/metaspaceShared.hpp"
59 #include "memory/oopFactory.hpp"
60 #include "oops/objArrayOop.inline.hpp"
61 #include "oops/oop.inline.hpp"
62 #include "runtime/atomic.inline.hpp"
63 #include "runtime/jniHandles.hpp"
64 #include "runtime/mutex.hpp"
65 #include "runtime/safepoint.hpp"
66 #include "runtime/synchronizer.hpp"
67 #include "utilities/growableArray.hpp"
68 #include "utilities/macros.hpp"
69 #include "utilities/ostream.hpp"
70 #if INCLUDE_TRACE
71 #include "trace/tracing.hpp"
72 #endif
73
74 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
75
76 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
77 _class_loader(h_class_loader()),
78 _is_anonymous(is_anonymous),
79 // An anonymous class loader data doesn't have anything to keep
80 // it from being unloaded during parsing of the anonymous class.
81 // The null-class-loader should always be kept alive.
82 _keep_alive(is_anonymous || h_class_loader.is_null()),
269 }
270 }
271
272 void ClassLoaderDataGraph::clear_claimed_marks() {
273 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
274 cld->clear_claimed();
275 }
276 }
277
278 void ClassLoaderData::add_class(Klass* k, bool publicize /* true */) {
279 {
280 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
281 Klass* old_value = _klasses;
282 k->set_next_link(old_value);
283 // Make sure linked class is stable, since the class list is walked without a lock
284 OrderAccess::storestore();
285 // link the new item into the list
286 _klasses = k;
287 }
288
289 if (publicize && TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
290 ResourceMark rm;
291 tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
292 PTR_FORMAT " loader: " PTR_FORMAT " %s",
293 p2i(k),
294 k->external_name(),
295 p2i(k->class_loader_data()),
296 p2i((void *)k->class_loader()),
297 loader_name());
298 }
299 }
300
301 // This is called by InstanceKlass::deallocate_contents() to remove the
302 // scratch_class for redefine classes. We need a lock because there it may not
303 // be called at a safepoint if there's an error.
304 void ClassLoaderData::remove_class(Klass* scratch_class) {
305 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
306 Klass* prev = NULL;
307 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
308 if (k == scratch_class) {
309 if (prev == NULL) {
310 _klasses = k->next_link();
311 } else {
312 Klass* next = k->next_link();
313 prev->set_next_link(next);
314 }
315 return;
316 }
317 prev = k;
318 assert(k != k->next_link(), "no loops!");
319 }
320 ShouldNotReachHere(); // should have found this class!!
321 }
322
323 void ClassLoaderData::unload() {
324 _unloading = true;
325
326 // Tell serviceability tools these classes are unloading
327 classes_do(InstanceKlass::notify_unload_class);
328
329 if (TraceClassLoaderData) {
330 ResourceMark rm;
331 tty->print("[ClassLoaderData: unload loader data " INTPTR_FORMAT, p2i(this));
332 tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
333 loader_name());
334 if (is_anonymous()) {
335 tty->print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses));
336 }
337 tty->print_cr("]");
338 }
339 }
340
341 oop ClassLoaderData::keep_alive_object() const {
342 assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive");
343 return is_anonymous() ? _klasses->java_mirror() : class_loader();
344 }
345
346 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
347 bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
348 || is_alive_closure->do_object_b(keep_alive_object());
349
350 return alive;
351 }
352
353
354 ClassLoaderData::~ClassLoaderData() {
355 // Release C heap structures for all the classes.
356 classes_do(InstanceKlass::release_C_heap_structures);
357
391 bool ClassLoaderData::is_ext_class_loader_data() const {
392 return SystemDictionary::is_ext_class_loader(class_loader());
393 }
394
395 Metaspace* ClassLoaderData::metaspace_non_null() {
396 assert(!DumpSharedSpaces, "wrong metaspace!");
397 // If the metaspace has not been allocated, create a new one. Might want
398 // to create smaller arena for Reflection class loaders also.
399 // The reason for the delayed allocation is because some class loaders are
400 // simply for delegating with no metadata of their own.
401 if (_metaspace == NULL) {
402 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
403 // Check again if metaspace has been allocated while we were getting this lock.
404 if (_metaspace != NULL) {
405 return _metaspace;
406 }
407 if (this == the_null_class_loader_data()) {
408 assert (class_loader() == NULL, "Must be");
409 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
410 } else if (is_anonymous()) {
411 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
412 tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
413 }
414 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
415 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
416 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
417 tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
418 }
419 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
420 } else {
421 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
422 }
423 }
424 return _metaspace;
425 }
426
427 JNIHandleBlock* ClassLoaderData::handles() const { return _handles; }
428 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
429
430 jobject ClassLoaderData::add_handle(Handle h) {
431 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
432 if (handles() == NULL) {
433 set_handles(JNIHandleBlock::allocate_block());
434 }
435 return handles()->allocate_handle(h());
436 }
437
556 }
557 return false;
558 }
559
560
561 // GC root of class loader data created.
562 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
563 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
564 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
565 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
566
567 bool ClassLoaderDataGraph::_should_purge = false;
568 bool ClassLoaderDataGraph::_metaspace_oom = false;
569
570 // Add a new class loader data node to the list. Assign the newly created
571 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
572 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {
573 // We need to allocate all the oops for the ClassLoaderData before allocating the
574 // actual ClassLoaderData object.
575 ClassLoaderData::Dependencies dependencies(CHECK_NULL);
576
577 No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the
578 // ClassLoaderData in the graph since the CLD
579 // contains unhandled oops
580
581 ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies);
582
583
584 if (!is_anonymous) {
585 ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
586 // First, Atomically set it
587 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
588 if (old != NULL) {
589 delete cld;
590 // Returns the data.
591 return old;
592 }
593 }
594
595 // We won the race, and therefore the task of adding the data to the list of
596 // class loader data
597 ClassLoaderData** list_head = &_head;
598 ClassLoaderData* next = _head;
599
600 do {
601 cld->set_next(next);
602 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
603 if (exchanged == next) {
604 if (TraceClassLoaderData) {
605 ResourceMark rm;
606 tty->print("[ClassLoaderData: ");
607 tty->print("create class loader data " INTPTR_FORMAT, p2i(cld));
608 tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),
609 cld->loader_name());
610 tty->print_cr("]");
611 }
612 return cld;
613 }
614 next = exchanged;
615 } while (true);
616
617 }
618
619 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
620 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
621 cld->oops_do(f, klass_closure, must_claim);
622 }
623 }
624
625 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
626 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
627 if (cld->keep_alive()) {
628 cld->oops_do(f, klass_closure, must_claim);
629 }
630 }
631 }
632
633 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
634 if (ClassUnloading) {
635 keep_alive_oops_do(f, klass_closure, must_claim);
636 } else {
692 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
693 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
694 // Only walk the head until any clds not purged from prior unloading
695 // (CMS doesn't purge right away).
696 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
697 cld->classes_do(f);
698 }
699 }
700
701 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
702 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
703
704 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
705
706 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
707 ClassLoaderData* curr = _head;
708 while (curr != _saved_head) {
709 if (!curr->claimed()) {
710 array->push(curr);
711
712 if (TraceClassLoaderData) {
713 tty->print("[ClassLoaderData] found new CLD: ");
714 curr->print_value_on(tty);
715 tty->cr();
716 }
717 }
718
719 curr = curr->_next;
720 }
721
722 return array;
723 }
724
725 bool ClassLoaderDataGraph::unload_list_contains(const void* x) {
726 assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint");
727 for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
728 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
729 return true;
730 }
731 }
732 return false;
733 }
734
735 #ifndef PRODUCT
|
1 /*
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
37 // ClassLoaderData carries information related to a linkset (e.g.,
38 // metaspace holding its klass definitions).
39 // The System Dictionary and related data structures (e.g., placeholder table,
40 // loader constraints table) as well as the runtime representation of classes
41 // only reference ClassLoaderData.
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.hpp"
51 #include "classfile/classLoaderData.inline.hpp"
52 #include "classfile/javaClasses.hpp"
53 #include "classfile/metadataOnStackMark.hpp"
54 #include "classfile/systemDictionary.hpp"
55 #include "code/codeCache.hpp"
56 #include "gc/shared/gcLocker.hpp"
57 #include "logging/log.hpp"
58 #include "memory/metadataFactory.hpp"
59 #include "memory/metaspaceShared.hpp"
60 #include "memory/oopFactory.hpp"
61 #include "oops/objArrayOop.inline.hpp"
62 #include "oops/oop.inline.hpp"
63 #include "runtime/atomic.inline.hpp"
64 #include "runtime/javaCalls.hpp"
65 #include "runtime/jniHandles.hpp"
66 #include "runtime/mutex.hpp"
67 #include "runtime/safepoint.hpp"
68 #include "runtime/synchronizer.hpp"
69 #include "utilities/growableArray.hpp"
70 #include "utilities/macros.hpp"
71 #include "utilities/ostream.hpp"
72 #if INCLUDE_TRACE
73 #include "trace/tracing.hpp"
74 #endif
75
76 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
77
78 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
79 _class_loader(h_class_loader()),
80 _is_anonymous(is_anonymous),
81 // An anonymous class loader data doesn't have anything to keep
82 // it from being unloaded during parsing of the anonymous class.
83 // The null-class-loader should always be kept alive.
84 _keep_alive(is_anonymous || h_class_loader.is_null()),
271 }
272 }
273
274 void ClassLoaderDataGraph::clear_claimed_marks() {
275 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
276 cld->clear_claimed();
277 }
278 }
279
280 void ClassLoaderData::add_class(Klass* k, bool publicize /* true */) {
281 {
282 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
283 Klass* old_value = _klasses;
284 k->set_next_link(old_value);
285 // Make sure linked class is stable, since the class list is walked without a lock
286 OrderAccess::storestore();
287 // link the new item into the list
288 _klasses = k;
289 }
290
291 if (publicize && log_is_enabled(Trace, classloaderdata) && k->class_loader_data() != NULL) {
292 ResourceMark rm;
293 outputStream* log = LogHandle(classloaderdata)::trace_stream();
294 log->print_cr("Adding k: " PTR_FORMAT " %s to CLD: "
295 PTR_FORMAT " loader: " PTR_FORMAT " %s",
296 p2i(k),
297 k->external_name(),
298 p2i(k->class_loader_data()),
299 p2i((void *)k->class_loader()),
300 loader_name());
301 }
302 }
303
304 // This is called by InstanceKlass::deallocate_contents() to remove the
305 // scratch_class for redefine classes. We need a lock because there it may not
306 // be called at a safepoint if there's an error.
307 void ClassLoaderData::remove_class(Klass* scratch_class) {
308 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
309 Klass* prev = NULL;
310 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
311 if (k == scratch_class) {
312 if (prev == NULL) {
313 _klasses = k->next_link();
314 } else {
315 Klass* next = k->next_link();
316 prev->set_next_link(next);
317 }
318 return;
319 }
320 prev = k;
321 assert(k != k->next_link(), "no loops!");
322 }
323 ShouldNotReachHere(); // should have found this class!!
324 }
325
326 void ClassLoaderData::unload() {
327 _unloading = true;
328
329 // Tell serviceability tools these classes are unloading
330 classes_do(InstanceKlass::notify_unload_class);
331
332 if (log_is_enabled(Debug, classloaderdata)) {
333 ResourceMark rm;
334 outputStream* log = LogHandle(classloaderdata)::debug_stream();
335 log->print(": unload loader data " INTPTR_FORMAT, p2i(this));
336 log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
337 loader_name());
338 if (is_anonymous()) {
339 log->print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses));
340 }
341 log->cr();
342 }
343 }
344
345 oop ClassLoaderData::keep_alive_object() const {
346 assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive");
347 return is_anonymous() ? _klasses->java_mirror() : class_loader();
348 }
349
350 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
351 bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
352 || is_alive_closure->do_object_b(keep_alive_object());
353
354 return alive;
355 }
356
357
358 ClassLoaderData::~ClassLoaderData() {
359 // Release C heap structures for all the classes.
360 classes_do(InstanceKlass::release_C_heap_structures);
361
395 bool ClassLoaderData::is_ext_class_loader_data() const {
396 return SystemDictionary::is_ext_class_loader(class_loader());
397 }
398
399 Metaspace* ClassLoaderData::metaspace_non_null() {
400 assert(!DumpSharedSpaces, "wrong metaspace!");
401 // If the metaspace has not been allocated, create a new one. Might want
402 // to create smaller arena for Reflection class loaders also.
403 // The reason for the delayed allocation is because some class loaders are
404 // simply for delegating with no metadata of their own.
405 if (_metaspace == NULL) {
406 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
407 // Check again if metaspace has been allocated while we were getting this lock.
408 if (_metaspace != NULL) {
409 return _metaspace;
410 }
411 if (this == the_null_class_loader_data()) {
412 assert (class_loader() == NULL, "Must be");
413 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
414 } else if (is_anonymous()) {
415 if (log_is_enabled(Trace, classloaderdata) && class_loader() != NULL) {
416 outputStream* log = LogHandle(classloaderdata)::trace_stream();
417 log->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
418 }
419 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
420 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
421 if (log_is_enabled(Trace, classloaderdata) && class_loader() != NULL) {
422 outputStream* log = LogHandle(classloaderdata)::trace_stream();
423 log->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
424 }
425 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
426 } else {
427 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
428 }
429 }
430 return _metaspace;
431 }
432
433 JNIHandleBlock* ClassLoaderData::handles() const { return _handles; }
434 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
435
436 jobject ClassLoaderData::add_handle(Handle h) {
437 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
438 if (handles() == NULL) {
439 set_handles(JNIHandleBlock::allocate_block());
440 }
441 return handles()->allocate_handle(h());
442 }
443
562 }
563 return false;
564 }
565
566
567 // GC root of class loader data created.
568 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
569 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
570 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
571 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
572
573 bool ClassLoaderDataGraph::_should_purge = false;
574 bool ClassLoaderDataGraph::_metaspace_oom = false;
575
576 // Add a new class loader data node to the list. Assign the newly created
577 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
578 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {
579 // We need to allocate all the oops for the ClassLoaderData before allocating the
580 // actual ClassLoaderData object.
581 ClassLoaderData::Dependencies dependencies(CHECK_NULL);
582 ClassLoaderData* cld;
583
584
585 //BEGIN no_safepoints
586 {
587 No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the
588 // ClassLoaderData in the graph since the CLD
589 // contains unhandled oops
590
591 cld = new ClassLoaderData(loader, is_anonymous, dependencies);
592
593 if (!is_anonymous) {
594 ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
595 // First, Atomically set it
596 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
597 if (old != NULL) {
598 delete cld;
599 // Returns the data.
600 return old;
601 }
602 }
603
604 // We won the race, and therefore the task of adding the data to the list of
605 // class loader data
606 ClassLoaderData** list_head = &_head;
607 ClassLoaderData* next = _head;
608
609 do {
610 cld->set_next(next);
611 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
612 if (exchanged == next) {
613 break;
614 }
615 next = exchanged;
616 } while (true);
617 }
618 //END no_safepoints
619
620 // It's OK to do safe points now (and we need to do that in JavaCalls::call_virtual)
621 if (log_is_enabled(Debug, classloaderdata)) {
622 Handle string;
623 if (loader.not_null()) {
624 // Include the result of loader.toString() in the output. This allows
625 // the user of the log to identify the class loader instance.
626 JavaValue result(T_OBJECT);
627 KlassHandle spec_klass(THREAD, SystemDictionary::ClassLoader_klass());
628 JavaCalls::call_virtual(&result,
629 loader,
630 spec_klass,
631 vmSymbols::toString_name(),
632 vmSymbols::void_string_signature(),
633 CHECK_NULL);
634 assert(result.get_type() == T_OBJECT, "just checking");
635 string = (oop)result.get_jobject();
636 }
637
638 ResourceMark rm;
639 outputStream* log = LogHandle(classloaderdata)::debug_stream();
640 log->print("create class loader data " INTPTR_FORMAT, p2i(cld));
641 log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),
642 cld->loader_name());
643
644 if (string.not_null()) {
645 log->print(": ");
646 java_lang_String::print(string(), log);
647 }
648 log->cr();
649 }
650 return cld;
651 }
652
653 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
654 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
655 cld->oops_do(f, klass_closure, must_claim);
656 }
657 }
658
659 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
660 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
661 if (cld->keep_alive()) {
662 cld->oops_do(f, klass_closure, must_claim);
663 }
664 }
665 }
666
667 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
668 if (ClassUnloading) {
669 keep_alive_oops_do(f, klass_closure, must_claim);
670 } else {
726 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
727 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
728 // Only walk the head until any clds not purged from prior unloading
729 // (CMS doesn't purge right away).
730 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
731 cld->classes_do(f);
732 }
733 }
734
735 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
736 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
737
738 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
739
740 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
741 ClassLoaderData* curr = _head;
742 while (curr != _saved_head) {
743 if (!curr->claimed()) {
744 array->push(curr);
745
746 if (log_is_enabled(Debug, classloaderdata)) {
747 outputStream* log = LogHandle(classloaderdata)::debug_stream();
748 log->print("[ClassLoaderData] found new CLD: ");
749 curr->print_value_on(log);
750 log->cr();
751 }
752 }
753
754 curr = curr->_next;
755 }
756
757 return array;
758 }
759
760 bool ClassLoaderDataGraph::unload_list_contains(const void* x) {
761 assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint");
762 for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
763 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
764 return true;
765 }
766 }
767 return false;
768 }
769
770 #ifndef PRODUCT
|