src/share/vm/classfile/classLoaderData.cpp

Fri, 01 Mar 2013 10:19:29 -0800

author
jmasa
date
Fri, 01 Mar 2013 10:19:29 -0800
changeset 5007
c23dbf0e8ab7
parent 4903
ba42fd5e00e6
child 5013
1cb4795305b9
permissions
-rw-r--r--

8011268: NPG: Free unused VirtualSpaceNodes
Reviewed-by: mgerdin, coleenp, johnc

     1 /*
     2  * Copyright (c) 2012, 2013, 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  *
    23  */
    25 // A ClassLoaderData identifies the full set of class types that a class
    26 // loader's name resolution strategy produces for a given configuration of the
    27 // class loader.
    28 // Class types in the ClassLoaderData may be defined by from class file binaries
    29 // provided by the class loader, or from other class loader it interacts with
    30 // according to its name resolution strategy.
    31 //
    32 // Class loaders that implement a deterministic name resolution strategy
    33 // (including with respect to their delegation behavior), such as the boot, the
    34 // extension, and the system loaders of the JDK's built-in class loader
    35 // hierarchy, always produce the same linkset for a given configuration.
    36 //
    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().
    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 "memory/metadataFactory.hpp"
    57 #include "memory/metaspaceShared.hpp"
    58 #include "memory/oopFactory.hpp"
    59 #include "runtime/jniHandles.hpp"
    60 #include "runtime/mutex.hpp"
    61 #include "runtime/safepoint.hpp"
    62 #include "runtime/synchronizer.hpp"
    63 #include "utilities/growableArray.hpp"
    64 #include "utilities/ostream.hpp"
    66 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
    68 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) :
    69   _class_loader(h_class_loader()),
    70   _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially
    71   _metaspace(NULL), _unloading(false), _klasses(NULL),
    72   _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
    73   _next(NULL), _dependencies(),
    74   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
    75     // empty
    76 }
    78 void ClassLoaderData::init_dependencies(TRAPS) {
    79   _dependencies.init(CHECK);
    80 }
    82 void ClassLoaderData::Dependencies::init(TRAPS) {
    83   // Create empty dependencies array to add to. CMS requires this to be
    84   // an oop so that it can track additions via card marks.  We think.
    85   _list_head = oopFactory::new_objectArray(2, CHECK);
    86 }
    88 bool ClassLoaderData::claim() {
    89   if (_claimed == 1) {
    90     return false;
    91   }
    93   return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
    94 }
    96 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
    97   if (must_claim && !claim()) {
    98     return;
    99   }
   101   f->do_oop(&_class_loader);
   102   _dependencies.oops_do(f);
   103   _handles->oops_do(f);
   104   if (klass_closure != NULL) {
   105     classes_do(klass_closure);
   106   }
   107 }
   109 void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
   110   f->do_oop((oop*)&_list_head);
   111 }
   113 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
   114   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   115     klass_closure->do_klass(k);
   116     assert(k != k->next_link(), "no loops!");
   117   }
   118 }
   120 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
   121   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   122     if (k->oop_is_instance()) {
   123       f(InstanceKlass::cast(k));
   124     }
   125     assert(k != k->next_link(), "no loops!");
   126   }
   127 }
   129 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
   130   ClassLoaderData * const from_cld = this;
   131   ClassLoaderData * const to_cld = k->class_loader_data();
   133   // Dependency to the null class loader data doesn't need to be recorded
   134   // because the null class loader data never goes away.
   135   if (to_cld->is_the_null_class_loader_data()) {
   136     return;
   137   }
   139   oop to;
   140   if (to_cld->is_anonymous()) {
   141     // Anonymous class dependencies are through the mirror.
   142     to = k->java_mirror();
   143   } else {
   144     to = to_cld->class_loader();
   146     // If from_cld is anonymous, even if it's class_loader is a parent of 'to'
   147     // we still have to add it.  The class_loader won't keep from_cld alive.
   148     if (!from_cld->is_anonymous()) {
   149       // Check that this dependency isn't from the same or parent class_loader
   150       oop from = from_cld->class_loader();
   152       oop curr = from;
   153       while (curr != NULL) {
   154         if (curr == to) {
   155           return; // this class loader is in the parent list, no need to add it.
   156         }
   157         curr = java_lang_ClassLoader::parent(curr);
   158       }
   159     }
   160   }
   162   // It's a dependency we won't find through GC, add it. This is relatively rare
   163   // Must handle over GC point.
   164   Handle dependency(THREAD, to);
   165   from_cld->_dependencies.add(dependency, CHECK);
   166 }
   169 void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) {
   170   // Check first if this dependency is already in the list.
   171   // Save a pointer to the last to add to under the lock.
   172   objArrayOop ok = _list_head;
   173   objArrayOop last = NULL;
   174   while (ok != NULL) {
   175     last = ok;
   176     if (ok->obj_at(0) == dependency()) {
   177       // Don't need to add it
   178       return;
   179     }
   180     ok = (objArrayOop)ok->obj_at(1);
   181   }
   183   // Must handle over GC points
   184   assert (last != NULL, "dependencies should be initialized");
   185   objArrayHandle last_handle(THREAD, last);
   187   // Create a new dependency node with fields for (class_loader or mirror, next)
   188   objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
   189   deps->obj_at_put(0, dependency());
   191   // Must handle over GC points
   192   objArrayHandle new_dependency(THREAD, deps);
   194   // Add the dependency under lock
   195   locked_add(last_handle, new_dependency, THREAD);
   196 }
   198 void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle,
   199                                                objArrayHandle new_dependency,
   200                                                Thread* THREAD) {
   202   // Have to lock and put the new dependency on the end of the dependency
   203   // array so the card mark for CMS sees that this dependency is new.
   204   // Can probably do this lock free with some effort.
   205   ObjectLocker ol(Handle(THREAD, _list_head), THREAD);
   207   oop loader_or_mirror = new_dependency->obj_at(0);
   209   // Since the dependencies are only added, add to the end.
   210   objArrayOop end = last_handle();
   211   objArrayOop last = NULL;
   212   while (end != NULL) {
   213     last = end;
   214     // check again if another thread added it to the end.
   215     if (end->obj_at(0) == loader_or_mirror) {
   216       // Don't need to add it
   217       return;
   218     }
   219     end = (objArrayOop)end->obj_at(1);
   220   }
   221   assert (last != NULL, "dependencies should be initialized");
   222   // fill in the first element with the oop in new_dependency.
   223   if (last->obj_at(0) == NULL) {
   224     last->obj_at_put(0, new_dependency->obj_at(0));
   225   } else {
   226     last->obj_at_put(1, new_dependency());
   227   }
   228 }
   230 void ClassLoaderDataGraph::clear_claimed_marks() {
   231   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   232     cld->clear_claimed();
   233   }
   234 }
   236 void ClassLoaderData::add_class(Klass* k) {
   237   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   238   Klass* old_value = _klasses;
   239   k->set_next_link(old_value);
   240   // link the new item into the list
   241   _klasses = k;
   243   if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
   244     ResourceMark rm;
   245     tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
   246                   PTR_FORMAT " loader: " PTR_FORMAT " %s",
   247                   k,
   248                   k->external_name(),
   249                   k->class_loader_data(),
   250                   k->class_loader(),
   251                   loader_name());
   252   }
   253 }
   255 // This is called by InstanceKlass::deallocate_contents() to remove the
   256 // scratch_class for redefine classes.  We need a lock because there it may not
   257 // be called at a safepoint if there's an error.
   258 void ClassLoaderData::remove_class(Klass* scratch_class) {
   259   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   260   Klass* prev = NULL;
   261   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   262     if (k == scratch_class) {
   263       if (prev == NULL) {
   264         _klasses = k->next_link();
   265       } else {
   266         Klass* next = k->next_link();
   267         prev->set_next_link(next);
   268       }
   269       return;
   270     }
   271     prev = k;
   272     assert(k != k->next_link(), "no loops!");
   273   }
   274   ShouldNotReachHere();   // should have found this class!!
   275 }
   277 void ClassLoaderData::unload() {
   278   _unloading = true;
   280   if (TraceClassLoaderData) {
   281     ResourceMark rm;
   282     tty->print("[ClassLoaderData: unload loader data "PTR_FORMAT, this);
   283     tty->print(" for instance "PTR_FORMAT" of %s", class_loader(),
   284                loader_name());
   285     if (is_anonymous()) {
   286       tty->print(" for anonymous class  "PTR_FORMAT " ", _klasses);
   287     }
   288     tty->print_cr("]");
   289   }
   290 }
   292 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
   293   bool alive =
   294     is_anonymous() ?
   295        is_alive_closure->do_object_b(_klasses->java_mirror()) :
   296        class_loader() == NULL || is_alive_closure->do_object_b(class_loader());
   297   assert(!alive || claimed(), "must be claimed");
   298   return alive;
   299 }
   302 ClassLoaderData::~ClassLoaderData() {
   303   Metaspace *m = _metaspace;
   304   if (m != NULL) {
   305     _metaspace = NULL;
   306     // release the metaspace
   307     delete m;
   308     // release the handles
   309     if (_handles != NULL) {
   310       JNIHandleBlock::release_block(_handles);
   311       _handles = NULL;
   312     }
   313   }
   315   // Clear all the JNI handles for methods
   316   // These aren't deallocated and are going to look like a leak, but that's
   317   // needed because we can't really get rid of jmethodIDs because we don't
   318   // know when native code is going to stop using them.  The spec says that
   319   // they're "invalid" but existing programs likely rely on their being
   320   // NULL after class unloading.
   321   if (_jmethod_ids != NULL) {
   322     Method::clear_jmethod_ids(this);
   323   }
   324   // Delete lock
   325   delete _metaspace_lock;
   327   // Delete free list
   328   if (_deallocate_list != NULL) {
   329     delete _deallocate_list;
   330   }
   331 }
   333 /**
   334  * Returns true if this class loader data is for the extension class loader.
   335  */
   336 bool ClassLoaderData::is_ext_class_loader_data() const {
   337   return SystemDictionary::is_ext_class_loader(class_loader());
   338 }
   340 Metaspace* ClassLoaderData::metaspace_non_null() {
   341   assert(!DumpSharedSpaces, "wrong metaspace!");
   342   // If the metaspace has not been allocated, create a new one.  Might want
   343   // to create smaller arena for Reflection class loaders also.
   344   // The reason for the delayed allocation is because some class loaders are
   345   // simply for delegating with no metadata of their own.
   346   if (_metaspace == NULL) {
   347     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   348     // Check again if metaspace has been allocated while we were getting this lock.
   349     if (_metaspace != NULL) {
   350       return _metaspace;
   351     }
   352     if (this == the_null_class_loader_data()) {
   353       assert (class_loader() == NULL, "Must be");
   354       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
   355     } else if (is_anonymous()) {
   356       if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
   357         tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
   358       }
   359       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
   360     } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
   361       if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
   362         tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
   363       }
   364       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
   365     } else {
   366       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
   367     }
   368   }
   369   return _metaspace;
   370 }
   372 JNIHandleBlock* ClassLoaderData::handles() const           { return _handles; }
   373 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
   375 jobject ClassLoaderData::add_handle(Handle h) {
   376   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   377   if (handles() == NULL) {
   378     set_handles(JNIHandleBlock::allocate_block());
   379   }
   380   return handles()->allocate_handle(h());
   381 }
   383 // Add this metadata pointer to be freed when it's safe.  This is only during
   384 // class unloading because Handles might point to this metadata field.
   385 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
   386   // Metadata in shared region isn't deleted.
   387   if (!m->is_shared()) {
   388     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   389     if (_deallocate_list == NULL) {
   390       _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
   391     }
   392     _deallocate_list->append_if_missing(m);
   393   }
   394 }
   396 // Deallocate free metadata on the free list.  How useful the PermGen was!
   397 void ClassLoaderData::free_deallocate_list() {
   398   // Don't need lock, at safepoint
   399   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
   400   if (_deallocate_list == NULL) {
   401     return;
   402   }
   403   // Go backwards because this removes entries that are freed.
   404   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
   405     Metadata* m = _deallocate_list->at(i);
   406     if (!m->on_stack()) {
   407       _deallocate_list->remove_at(i);
   408       // There are only three types of metadata that we deallocate directly.
   409       // Cast them so they can be used by the template function.
   410       if (m->is_method()) {
   411         MetadataFactory::free_metadata(this, (Method*)m);
   412       } else if (m->is_constantPool()) {
   413         MetadataFactory::free_metadata(this, (ConstantPool*)m);
   414       } else if (m->is_klass()) {
   415         MetadataFactory::free_metadata(this, (InstanceKlass*)m);
   416       } else {
   417         ShouldNotReachHere();
   418       }
   419     }
   420   }
   421 }
   423 // These anonymous class loaders are to contain classes used for JSR292
   424 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
   425   // Add a new class loader data to the graph.
   426   return ClassLoaderDataGraph::add(NULL, loader, CHECK_NULL);
   427 }
   429 const char* ClassLoaderData::loader_name() {
   430   // Handles null class loader
   431   return SystemDictionary::loader_name(class_loader());
   432 }
   434 #ifndef PRODUCT
   435 // Define to dump klasses
   436 #undef CLD_DUMP_KLASSES
   438 void ClassLoaderData::dump(outputStream * const out) {
   439   ResourceMark rm;
   440   out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
   441       this, class_loader(),
   442       class_loader() != NULL ? class_loader()->klass() : NULL, loader_name());
   443   if (claimed()) out->print(" claimed ");
   444   if (is_unloading()) out->print(" unloading ");
   445   out->print(" handles " INTPTR_FORMAT, handles());
   446   out->cr();
   447   if (metaspace_or_null() != NULL) {
   448     out->print_cr("metaspace: " PTR_FORMAT, metaspace_or_null());
   449     metaspace_or_null()->dump(out);
   450   } else {
   451     out->print_cr("metaspace: NULL");
   452   }
   454 #ifdef CLD_DUMP_KLASSES
   455   if (Verbose) {
   456     ResourceMark rm;
   457     Klass* k = _klasses;
   458     while (k != NULL) {
   459       out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
   460           k->has_modified_oops(), k->has_accumulated_modified_oops());
   461       assert(k != k->next_link(), "no loops!");
   462       k = k->next_link();
   463     }
   464   }
   465 #endif  // CLD_DUMP_KLASSES
   466 #undef CLD_DUMP_KLASSES
   467   if (_jmethod_ids != NULL) {
   468     Method::print_jmethod_ids(this, out);
   469   }
   470   out->print_cr("}");
   471 }
   472 #endif // PRODUCT
   474 void ClassLoaderData::verify() {
   475   oop cl = class_loader();
   477   guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
   478   guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
   480   // Verify the integrity of the allocated space.
   481   if (metaspace_or_null() != NULL) {
   482     metaspace_or_null()->verify();
   483   }
   485   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   486     guarantee(k->class_loader_data() == this, "Must be the same");
   487     k->verify();
   488     assert(k != k->next_link(), "no loops!");
   489   }
   490 }
   493 // GC root of class loader data created.
   494 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
   495 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
   496 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
   499 // Add a new class loader data node to the list.  Assign the newly created
   500 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
   501 ClassLoaderData* ClassLoaderDataGraph::add(ClassLoaderData** cld_addr, Handle loader, TRAPS) {
   502   // Not assigned a class loader data yet.
   503   // Create one.
   504   ClassLoaderData* *list_head = &_head;
   505   ClassLoaderData* next = _head;
   507   bool is_anonymous = (cld_addr == NULL);
   508   ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous);
   510   if (cld_addr != NULL) {
   511     // First, Atomically set it
   512     ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
   513     if (old != NULL) {
   514       delete cld;
   515       // Returns the data.
   516       return old;
   517     }
   518   }
   520   // We won the race, and therefore the task of adding the data to the list of
   521   // class loader data
   522   do {
   523     cld->set_next(next);
   524     ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
   525     if (exchanged == next) {
   526       if (TraceClassLoaderData) {
   527         ResourceMark rm;
   528         tty->print("[ClassLoaderData: ");
   529         tty->print("create class loader data "PTR_FORMAT, cld);
   530         tty->print(" for instance "PTR_FORMAT" of %s", cld->class_loader(),
   531                    cld->loader_name());
   532         tty->print_cr("]");
   533       }
   534       // Create dependencies after the CLD is added to the list.  Otherwise,
   535       // the GC GC will not find the CLD and the _class_loader field will
   536       // not be updated.
   537       cld->init_dependencies(CHECK_NULL);
   538       return cld;
   539     }
   540     next = exchanged;
   541   } while (true);
   543 }
   545 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   546   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   547     cld->oops_do(f, klass_closure, must_claim);
   548   }
   549 }
   551 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   552   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   553     if (cld->keep_alive()) {
   554       cld->oops_do(f, klass_closure, must_claim);
   555     }
   556   }
   557 }
   559 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   560   if (ClassUnloading) {
   561     ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
   562     // keep any special CLDs alive.
   563     ClassLoaderDataGraph::keep_alive_oops_do(f, klass_closure, must_claim);
   564   } else {
   565     ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
   566   }
   567 }
   569 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
   570   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   571     cld->classes_do(klass_closure);
   572   }
   573 }
   575 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
   576   assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
   578   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
   580   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
   581   ClassLoaderData* curr = _head;
   582   while (curr != _saved_head) {
   583     if (!curr->claimed()) {
   584       array->push(curr);
   586       if (TraceClassLoaderData) {
   587         tty->print("[ClassLoaderData] found new CLD: ");
   588         curr->print_value_on(tty);
   589         tty->cr();
   590       }
   591     }
   593     curr = curr->_next;
   594   }
   596   return array;
   597 }
   599 #ifndef PRODUCT
   600 // for debugging and hsfind(x)
   601 bool ClassLoaderDataGraph::contains(address x) {
   602   // I think we need the _metaspace_lock taken here because the class loader
   603   // data graph could be changing while we are walking it (new entries added,
   604   // new entries being unloaded, etc).
   605   if (DumpSharedSpaces) {
   606     // There are only two metaspaces to worry about.
   607     ClassLoaderData* ncld = ClassLoaderData::the_null_class_loader_data();
   608     return (ncld->ro_metaspace()->contains(x) || ncld->rw_metaspace()->contains(x));
   609   }
   611   if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(x)) {
   612     return true;
   613   }
   615   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   616     if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
   617       return true;
   618     }
   619   }
   621   // Could also be on an unloading list which is okay, ie. still allocated
   622   // for a little while.
   623   for (ClassLoaderData* ucld = _unloading; ucld != NULL; ucld = ucld->next()) {
   624     if (ucld->metaspace_or_null() != NULL && ucld->metaspace_or_null()->contains(x)) {
   625       return true;
   626     }
   627   }
   628   return false;
   629 }
   631 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
   632   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   633     if (loader_data == data) {
   634       return true;
   635     }
   636   }
   638   return false;
   639 }
   640 #endif // PRODUCT
   643 // Move class loader data from main list to the unloaded list for unloading
   644 // and deallocation later.
   645 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure) {
   646   ClassLoaderData* data = _head;
   647   ClassLoaderData* prev = NULL;
   648   bool seen_dead_loader = false;
   649   // mark metadata seen on the stack and code cache so we can delete
   650   // unneeded entries.
   651   bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
   652   MetadataOnStackMark md_on_stack;
   653   while (data != NULL) {
   654     if (data->keep_alive() || data->is_alive(is_alive_closure)) {
   655       if (has_redefined_a_class) {
   656         data->classes_do(InstanceKlass::purge_previous_versions);
   657       }
   658       data->free_deallocate_list();
   659       prev = data;
   660       data = data->next();
   661       continue;
   662     }
   663     seen_dead_loader = true;
   664     ClassLoaderData* dead = data;
   665     dead->unload();
   666     data = data->next();
   667     // Remove from loader list.
   668     if (prev != NULL) {
   669       prev->set_next(data);
   670     } else {
   671       assert(dead == _head, "sanity check");
   672       _head = data;
   673     }
   674     dead->set_next(_unloading);
   675     _unloading = dead;
   676   }
   677   return seen_dead_loader;
   678 }
   680 void ClassLoaderDataGraph::purge() {
   681   ClassLoaderData* list = _unloading;
   682   _unloading = NULL;
   683   ClassLoaderData* next = list;
   684   while (next != NULL) {
   685     ClassLoaderData* purge_me = next;
   686     next = purge_me->next();
   687     delete purge_me;
   688   }
   689   Metaspace::purge();
   690 }
   692 // CDS support
   694 // Global metaspaces for writing information to the shared archive.  When
   695 // application CDS is supported, we may need one per metaspace, so this
   696 // sort of looks like it.
   697 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
   698 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
   699 static bool _shared_metaspaces_initialized = false;
   701 // Initialize shared metaspaces (change to call from somewhere not lazily)
   702 void ClassLoaderData::initialize_shared_metaspaces() {
   703   assert(DumpSharedSpaces, "only use this for dumping shared spaces");
   704   assert(this == ClassLoaderData::the_null_class_loader_data(),
   705          "only supported for null loader data for now");
   706   assert (!_shared_metaspaces_initialized, "only initialize once");
   707   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   708   _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
   709   _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
   710   _shared_metaspaces_initialized = true;
   711 }
   713 Metaspace* ClassLoaderData::ro_metaspace() {
   714   assert(_ro_metaspace != NULL, "should already be initialized");
   715   return _ro_metaspace;
   716 }
   718 Metaspace* ClassLoaderData::rw_metaspace() {
   719   assert(_rw_metaspace != NULL, "should already be initialized");
   720   return _rw_metaspace;
   721 }
   724 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
   725   _data = ClassLoaderDataGraph::_head;
   726 }
   728 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
   730 #ifndef PRODUCT
   731 // callable from debugger
   732 extern "C" int print_loader_data_graph() {
   733   ClassLoaderDataGraph::dump_on(tty);
   734   return 0;
   735 }
   737 void ClassLoaderDataGraph::verify() {
   738   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   739     data->verify();
   740   }
   741 }
   743 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
   744   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   745     data->dump(out);
   746   }
   747   MetaspaceAux::dump(out);
   748 }
   749 #endif // PRODUCT
   751 void ClassLoaderData::print_value_on(outputStream* out) const {
   752   if (class_loader() == NULL) {
   753     out->print("NULL class_loader");
   754   } else {
   755     out->print("class loader "PTR_FORMAT, this);
   756     class_loader()->print_value_on(out);
   757   }
   758 }

mercurial