src/share/vm/classfile/classLoaderData.cpp

Print this page
rev 9245 : [mq]: class_loading_log


  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()),


 265   } else {
 266     last->obj_at_put(1, new_dependency());
 267   }
 268 }
 269 
 270 void ClassLoaderDataGraph::clear_claimed_marks() {
 271   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 272     cld->clear_claimed();
 273   }
 274 }
 275 
 276 void ClassLoaderData::add_class(Klass* k) {
 277   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 278   Klass* old_value = _klasses;
 279   k->set_next_link(old_value);
 280   // Make sure linked class is stable, since the class list is walked without a lock
 281   OrderAccess::storestore();
 282   // link the new item into the list
 283   _klasses = k;
 284 
 285   if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
 286     ResourceMark rm;
 287     tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "

 288                   PTR_FORMAT " loader: " PTR_FORMAT " %s",
 289                   p2i(k),
 290                   k->external_name(),
 291                   p2i(k->class_loader_data()),
 292                   p2i((void *)k->class_loader()),
 293                   loader_name());
 294   }
 295 }
 296 
 297 // This is called by InstanceKlass::deallocate_contents() to remove the
 298 // scratch_class for redefine classes.  We need a lock because there it may not
 299 // be called at a safepoint if there's an error.
 300 void ClassLoaderData::remove_class(Klass* scratch_class) {
 301   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 302   Klass* prev = NULL;
 303   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
 304     if (k == scratch_class) {
 305       if (prev == NULL) {
 306         _klasses = k->next_link();
 307       } else {
 308         Klass* next = k->next_link();
 309         prev->set_next_link(next);
 310       }
 311       return;
 312     }
 313     prev = k;
 314     assert(k != k->next_link(), "no loops!");
 315   }
 316   ShouldNotReachHere();   // should have found this class!!
 317 }
 318 
 319 void ClassLoaderData::unload() {
 320   _unloading = true;
 321 
 322   // Tell serviceability tools these classes are unloading
 323   classes_do(InstanceKlass::notify_unload_class);
 324 
 325   if (TraceClassLoaderData) {
 326     ResourceMark rm;
 327     tty->print("[ClassLoaderData: unload loader data " INTPTR_FORMAT, p2i(this));
 328     tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),

 329                loader_name());
 330     if (is_anonymous()) {
 331       tty->print(" for anonymous class  " INTPTR_FORMAT " ", p2i(_klasses));
 332     }
 333     tty->print_cr("]");
 334   }
 335 }
 336 
 337 oop ClassLoaderData::keep_alive_object() const {
 338   assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive");
 339   return is_anonymous() ? _klasses->java_mirror() : class_loader();
 340 }
 341 
 342 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
 343   bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
 344       || is_alive_closure->do_object_b(keep_alive_object());
 345 
 346   return alive;
 347 }
 348 
 349 
 350 ClassLoaderData::~ClassLoaderData() {
 351   // Release C heap structures for all the classes.
 352   classes_do(InstanceKlass::release_C_heap_structures);
 353 


 387 bool ClassLoaderData::is_ext_class_loader_data() const {
 388   return SystemDictionary::is_ext_class_loader(class_loader());
 389 }
 390 
 391 Metaspace* ClassLoaderData::metaspace_non_null() {
 392   assert(!DumpSharedSpaces, "wrong metaspace!");
 393   // If the metaspace has not been allocated, create a new one.  Might want
 394   // to create smaller arena for Reflection class loaders also.
 395   // The reason for the delayed allocation is because some class loaders are
 396   // simply for delegating with no metadata of their own.
 397   if (_metaspace == NULL) {
 398     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 399     // Check again if metaspace has been allocated while we were getting this lock.
 400     if (_metaspace != NULL) {
 401       return _metaspace;
 402     }
 403     if (this == the_null_class_loader_data()) {
 404       assert (class_loader() == NULL, "Must be");
 405       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
 406     } else if (is_anonymous()) {
 407       if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
 408         tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());

 409       }
 410       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
 411     } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
 412       if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
 413         tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());

 414       }
 415       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
 416     } else {
 417       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
 418     }
 419   }
 420   return _metaspace;
 421 }
 422 
 423 JNIHandleBlock* ClassLoaderData::handles() const           { return _handles; }
 424 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
 425 
 426 jobject ClassLoaderData::add_handle(Handle h) {
 427   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 428   if (handles() == NULL) {
 429     set_handles(JNIHandleBlock::allocate_block());
 430   }
 431   return handles()->allocate_handle(h());
 432 }
 433 


 552   }
 553   return false;
 554 }
 555 
 556 
 557 // GC root of class loader data created.
 558 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
 559 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
 560 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
 561 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
 562 
 563 bool ClassLoaderDataGraph::_should_purge = false;
 564 bool ClassLoaderDataGraph::_metaspace_oom = false;
 565 
 566 // Add a new class loader data node to the list.  Assign the newly created
 567 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
 568 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {
 569   // We need to allocate all the oops for the ClassLoaderData before allocating the
 570   // actual ClassLoaderData object.
 571   ClassLoaderData::Dependencies dependencies(CHECK_NULL);

 572 



 573   No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the
 574                                        // ClassLoaderData in the graph since the CLD
 575                                        // contains unhandled oops
 576 
 577   ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies);
 578 
 579 
 580   if (!is_anonymous) {
 581     ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
 582     // First, Atomically set it
 583     ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
 584     if (old != NULL) {
 585       delete cld;
 586       // Returns the data.
 587       return old;
 588     }
 589   }
 590 
 591   // We won the race, and therefore the task of adding the data to the list of
 592   // class loader data
 593   ClassLoaderData** list_head = &_head;
 594   ClassLoaderData* next = _head;
 595 
 596   do {
 597     cld->set_next(next);
 598     ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
 599     if (exchanged == next) {
 600       if (TraceClassLoaderData) {
 601         ResourceMark rm;
 602         tty->print("[ClassLoaderData: ");
 603         tty->print("create class loader data " INTPTR_FORMAT, p2i(cld));
 604         tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),
 605                    cld->loader_name());
 606         tty->print_cr("]");
 607       }
 608       return cld;
 609     }
 610     next = exchanged;
 611   } while (true);


 612 































 613 }
 614 
 615 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
 616   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 617     cld->oops_do(f, klass_closure, must_claim);
 618   }
 619 }
 620 
 621 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
 622   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 623     if (cld->keep_alive()) {
 624       cld->oops_do(f, klass_closure, must_claim);
 625     }
 626   }
 627 }
 628 
 629 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
 630   if (ClassUnloading) {
 631     keep_alive_oops_do(f, klass_closure, must_claim);
 632   } else {


 688 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
 689   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 690   // Only walk the head until any clds not purged from prior unloading
 691   // (CMS doesn't purge right away).
 692   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 693     cld->classes_do(f);
 694   }
 695 }
 696 
 697 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
 698   assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
 699 
 700   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
 701 
 702   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
 703   ClassLoaderData* curr = _head;
 704   while (curr != _saved_head) {
 705     if (!curr->claimed()) {
 706       array->push(curr);
 707 
 708       if (TraceClassLoaderData) {
 709         tty->print("[ClassLoaderData] found new CLD: ");
 710         curr->print_value_on(tty);
 711         tty->cr();

 712       }
 713     }
 714 
 715     curr = curr->_next;
 716   }
 717 
 718   return array;
 719 }
 720 
 721 bool ClassLoaderDataGraph::unload_list_contains(const void* x) {
 722   assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint");
 723   for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
 724     if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
 725       return true;
 726     }
 727   }
 728   return false;
 729 }
 730 
 731 #ifndef PRODUCT




  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()),


 267   } else {
 268     last->obj_at_put(1, new_dependency());
 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) {
 279   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 280   Klass* old_value = _klasses;
 281   k->set_next_link(old_value);
 282   // Make sure linked class is stable, since the class list is walked without a lock
 283   OrderAccess::storestore();
 284   // link the new item into the list
 285   _klasses = k;
 286 
 287   if (log_is_enabled(Trace, classloaderdata) && k->class_loader_data() != NULL) {
 288     ResourceMark rm;
 289     outputStream* log = LogHandle(classloaderdata)::trace_stream();
 290     log->print_cr("Adding k: " PTR_FORMAT " %s to CLD: "
 291                   PTR_FORMAT " loader: " PTR_FORMAT " %s",
 292                   p2i(k),
 293                   k->external_name(),
 294                   p2i(k->class_loader_data()),
 295                   p2i((void *)k->class_loader()),
 296                   loader_name());
 297   }
 298 }
 299 
 300 // This is called by InstanceKlass::deallocate_contents() to remove the
 301 // scratch_class for redefine classes.  We need a lock because there it may not
 302 // be called at a safepoint if there's an error.
 303 void ClassLoaderData::remove_class(Klass* scratch_class) {
 304   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 305   Klass* prev = NULL;
 306   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
 307     if (k == scratch_class) {
 308       if (prev == NULL) {
 309         _klasses = k->next_link();
 310       } else {
 311         Klass* next = k->next_link();
 312         prev->set_next_link(next);
 313       }
 314       return;
 315     }
 316     prev = k;
 317     assert(k != k->next_link(), "no loops!");
 318   }
 319   ShouldNotReachHere();   // should have found this class!!
 320 }
 321 
 322 void ClassLoaderData::unload() {
 323   _unloading = true;
 324 
 325   // Tell serviceability tools these classes are unloading
 326   classes_do(InstanceKlass::notify_unload_class);
 327 
 328   if (log_is_enabled(Debug, classloaderdata)) {
 329     ResourceMark rm;
 330     outputStream* log = LogHandle(classloaderdata)::debug_stream();
 331     log->print(": unload loader data " INTPTR_FORMAT, p2i(this));
 332     log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
 333                loader_name());
 334     if (is_anonymous()) {
 335       log->print(" for anonymous class  " INTPTR_FORMAT " ", p2i(_klasses));
 336     }
 337     log->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 (log_is_enabled(Trace, classloaderdata) && class_loader() != NULL) {
 412         outputStream* log = LogHandle(classloaderdata)::trace_stream();
 413         log->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
 414       }
 415       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
 416     } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
 417       if (log_is_enabled(Trace, classloaderdata) && class_loader() != NULL) {
 418         outputStream* log = LogHandle(classloaderdata)::trace_stream();
 419         log->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
 420       }
 421       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
 422     } else {
 423       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
 424     }
 425   }
 426   return _metaspace;
 427 }
 428 
 429 JNIHandleBlock* ClassLoaderData::handles() const           { return _handles; }
 430 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
 431 
 432 jobject ClassLoaderData::add_handle(Handle h) {
 433   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 434   if (handles() == NULL) {
 435     set_handles(JNIHandleBlock::allocate_block());
 436   }
 437   return handles()->allocate_handle(h());
 438 }
 439 


 558   }
 559   return false;
 560 }
 561 
 562 
 563 // GC root of class loader data created.
 564 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
 565 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
 566 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
 567 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
 568 
 569 bool ClassLoaderDataGraph::_should_purge = false;
 570 bool ClassLoaderDataGraph::_metaspace_oom = false;
 571 
 572 // Add a new class loader data node to the list.  Assign the newly created
 573 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
 574 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {
 575   // We need to allocate all the oops for the ClassLoaderData before allocating the
 576   // actual ClassLoaderData object.
 577   ClassLoaderData::Dependencies dependencies(CHECK_NULL);
 578   ClassLoaderData* cld;
 579 
 580 
 581 //BEGIN no_safepoints
 582 {
 583   No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the
 584                                        // ClassLoaderData in the graph since the CLD
 585                                        // contains unhandled oops
 586 
 587   cld = new ClassLoaderData(loader, is_anonymous, dependencies);

 588 
 589   if (!is_anonymous) {
 590     ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
 591     // First, Atomically set it
 592     ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
 593     if (old != NULL) {
 594       delete cld;
 595       // Returns the data.
 596       return old;
 597     }
 598   }
 599 
 600   // We won the race, and therefore the task of adding the data to the list of
 601   // class loader data
 602   ClassLoaderData** list_head = &_head;
 603   ClassLoaderData* next = _head;
 604 
 605   do {
 606     cld->set_next(next);
 607     ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
 608     if (exchanged == next) {
 609       break;








 610     }
 611     next = exchanged;
 612   } while (true);
 613  }
 614 //END no_safepoints
 615 
 616   // It's OK to do safe points now (and we need to do that in JavaCalls::call_virtual)
 617   if (log_is_enabled(Debug, classloaderdata)) {
 618     Handle string;
 619     if (loader.not_null()) {
 620       // Include the result of loader.toString() in the output. This allows
 621       // the user of the log to identify the class loader instance.
 622       JavaValue result(T_OBJECT);
 623       KlassHandle spec_klass(THREAD, SystemDictionary::ClassLoader_klass());
 624       JavaCalls::call_virtual(&result,
 625                               loader,
 626                               spec_klass,
 627                               vmSymbols::toString_name(),
 628                               vmSymbols::void_string_signature(),
 629                               CHECK_NULL);
 630       assert(result.get_type() == T_OBJECT, "just checking");
 631       string = (oop)result.get_jobject();
 632     }
 633 
 634     ResourceMark rm;
 635     outputStream* log = LogHandle(classloaderdata)::debug_stream();
 636     log->print("create class loader data " INTPTR_FORMAT, p2i(cld));
 637     log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),
 638                cld->loader_name());
 639 
 640     if (string.not_null()) {
 641       log->print(": ");
 642       java_lang_String::print(string(), log);
 643     }
 644     log->cr();
 645   }
 646   return cld;
 647 }
 648 
 649 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
 650   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 651     cld->oops_do(f, klass_closure, must_claim);
 652   }
 653 }
 654 
 655 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
 656   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 657     if (cld->keep_alive()) {
 658       cld->oops_do(f, klass_closure, must_claim);
 659     }
 660   }
 661 }
 662 
 663 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
 664   if (ClassUnloading) {
 665     keep_alive_oops_do(f, klass_closure, must_claim);
 666   } else {


 722 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
 723   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 724   // Only walk the head until any clds not purged from prior unloading
 725   // (CMS doesn't purge right away).
 726   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 727     cld->classes_do(f);
 728   }
 729 }
 730 
 731 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
 732   assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
 733 
 734   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
 735 
 736   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
 737   ClassLoaderData* curr = _head;
 738   while (curr != _saved_head) {
 739     if (!curr->claimed()) {
 740       array->push(curr);
 741 
 742       if (log_is_enabled(Debug, classloaderdata)) {
 743         outputStream* log = LogHandle(classloaderdata)::debug_stream();
 744         log->print("[ClassLoaderData] found new CLD: ");
 745         curr->print_value_on(log);
 746         log->cr();
 747       }
 748     }
 749 
 750     curr = curr->_next;
 751   }
 752 
 753   return array;
 754 }
 755 
 756 bool ClassLoaderDataGraph::unload_list_contains(const void* x) {
 757   assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint");
 758   for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
 759     if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
 760       return true;
 761     }
 762   }
 763   return false;
 764 }
 765 
 766 #ifndef PRODUCT