hotspot/src/share/vm/classfile/classLoaderData.cpp
changeset 13728 882756847a04
child 14588 8ec26d2d9339
equal deleted inserted replaced
13727:caf5eb7dd4a7 13728:882756847a04
       
     1 /*
       
     2  * Copyright (c) 2012, 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  */
       
    24 
       
    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().
       
    48 
       
    49 #include "precompiled.hpp"
       
    50 #include "classfile/classLoaderData.hpp"
       
    51 #include "classfile/classLoaderData.inline.hpp"
       
    52 #include "classfile/javaClasses.hpp"
       
    53 #include "classfile/systemDictionary.hpp"
       
    54 #include "code/codeCache.hpp"
       
    55 #include "memory/metadataFactory.hpp"
       
    56 #include "memory/metaspaceShared.hpp"
       
    57 #include "prims/jvmtiRedefineClasses.hpp"
       
    58 #include "runtime/jniHandles.hpp"
       
    59 #include "runtime/mutex.hpp"
       
    60 #include "runtime/safepoint.hpp"
       
    61 #include "runtime/synchronizer.hpp"
       
    62 #include "utilities/growableArray.hpp"
       
    63 #include "utilities/ostream.hpp"
       
    64 
       
    65 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
       
    66 
       
    67 ClassLoaderData::ClassLoaderData(Handle h_class_loader) : _class_loader(h_class_loader()),
       
    68   _metaspace(NULL), _unloading(false), _klasses(NULL),
       
    69   _claimed(0), _jmethod_ids(NULL), _handles(NULL),
       
    70   _deallocate_list(NULL), _next(NULL),
       
    71   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
       
    72     // empty
       
    73 }
       
    74 
       
    75 bool ClassLoaderData::claim() {
       
    76   if (_claimed == 1) {
       
    77     return false;
       
    78   }
       
    79 
       
    80   return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
       
    81 }
       
    82 
       
    83 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
       
    84   if (must_claim && !claim()) {
       
    85     return;
       
    86   }
       
    87 
       
    88   f->do_oop(&_class_loader);
       
    89   _handles->oops_do(f);
       
    90   if (klass_closure != NULL) {
       
    91     classes_do(klass_closure);
       
    92   }
       
    93 }
       
    94 
       
    95 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
       
    96   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
       
    97     klass_closure->do_klass(k);
       
    98   }
       
    99 }
       
   100 
       
   101 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
       
   102   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
       
   103     if (k->oop_is_instance()) {
       
   104       f(InstanceKlass::cast(k));
       
   105     }
       
   106   }
       
   107 }
       
   108 
       
   109 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
       
   110   ClassLoaderData * const from_cld = this;
       
   111   ClassLoaderData * const to_cld = k->class_loader_data();
       
   112 
       
   113   // Records dependency between non-null class loaders only.
       
   114   if (to_cld->is_the_null_class_loader_data() || from_cld->is_the_null_class_loader_data()) {
       
   115     return;
       
   116   }
       
   117 
       
   118   // Check that this dependency isn't from the same or parent class_loader
       
   119   oop to = to_cld->class_loader();
       
   120   oop from = from_cld->class_loader();
       
   121 
       
   122   oop curr = from;
       
   123   while (curr != NULL) {
       
   124     if (curr == to) {
       
   125       return; // this class loader is in the parent list, no need to add it.
       
   126     }
       
   127     curr = java_lang_ClassLoader::parent(curr);
       
   128   }
       
   129 
       
   130   // It's a dependency we won't find through GC, add it. This is relatively rare
       
   131   from_cld->add_dependency(to_cld, CHECK);
       
   132 }
       
   133 
       
   134 bool ClassLoaderData::has_dependency(ClassLoaderData* dependency) {
       
   135   oop loader = dependency->class_loader();
       
   136 
       
   137   // Get objArrayOop out of the class_loader oop and see if this dependency
       
   138   // is there.  Don't safepoint!  These are all oops.
       
   139   // Dependency list is (oop class_loader, objArrayOop next)
       
   140   objArrayOop ok = (objArrayOop)java_lang_ClassLoader::dependencies(class_loader());
       
   141   while (ok != NULL) {
       
   142     if (ok->obj_at(0) == loader) {
       
   143       return true;
       
   144     }
       
   145     ok = (objArrayOop)ok->obj_at(1);
       
   146   }
       
   147   return false;
       
   148 }
       
   149 
       
   150 void ClassLoaderData::add_dependency(ClassLoaderData* dependency, TRAPS) {
       
   151   // Minimize the number of duplicates in the list.
       
   152   if (has_dependency(dependency)) {
       
   153     return;
       
   154   }
       
   155 
       
   156   // Create a new dependency node with fields for (class_loader, next)
       
   157   objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
       
   158   deps->obj_at_put(0, dependency->class_loader());
       
   159 
       
   160   // Add this lock free, using compare and exchange, need barriers for GC
       
   161   // Do the barrier first.
       
   162   HeapWord* addr = java_lang_ClassLoader::dependencies_addr(class_loader());
       
   163   while (true) {
       
   164     oop old_dependency = java_lang_ClassLoader::dependencies(class_loader());
       
   165     deps->obj_at_put(1, old_dependency);
       
   166 
       
   167     oop newold = oopDesc::atomic_compare_exchange_oop((oop)deps, addr, old_dependency, true);
       
   168     if (newold == old_dependency) {
       
   169       update_barrier_set((void*)addr, (oop)deps);
       
   170       // we won the race to add this dependency
       
   171       break;
       
   172     }
       
   173   }
       
   174 }
       
   175 
       
   176 
       
   177 void ClassLoaderDataGraph::clear_claimed_marks() {
       
   178   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
       
   179     cld->clear_claimed();
       
   180   }
       
   181 }
       
   182 
       
   183 void ClassLoaderData::add_class(Klass* k) {
       
   184   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
       
   185   Klass* old_value = _klasses;
       
   186   k->set_next_link(old_value);
       
   187   // link the new item into the list
       
   188   _klasses = k;
       
   189 
       
   190   if (TraceClassLoaderData && k->class_loader_data() != NULL) {
       
   191     ResourceMark rm;
       
   192     tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
       
   193                   PTR_FORMAT " loader: " PTR_FORMAT " %s",
       
   194                   k,
       
   195                   k->external_name(),
       
   196                   k->class_loader_data(),
       
   197                   k->class_loader(),
       
   198                   k->class_loader() != NULL ? k->class_loader()->klass()->external_name() : "NULL"
       
   199       );
       
   200   }
       
   201 }
       
   202 
       
   203 // This is called by InstanceKlass::deallocate_contents() to remove the
       
   204 // scratch_class for redefine classes.  We need a lock because there it may not
       
   205 // be called at a safepoint if there's an error.
       
   206 void ClassLoaderData::remove_class(Klass* scratch_class) {
       
   207   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
       
   208   Klass* prev = NULL;
       
   209   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
       
   210     if (k == scratch_class) {
       
   211       if (prev == NULL) {
       
   212         _klasses = k->next_link();
       
   213       } else {
       
   214         Klass* next = k->next_link();
       
   215         prev->set_next_link(next);
       
   216       }
       
   217       return;
       
   218     }
       
   219     prev = k;
       
   220   }
       
   221   ShouldNotReachHere();   // should have found this class!!
       
   222 }
       
   223 
       
   224 ClassLoaderData::~ClassLoaderData() {
       
   225   Metaspace *m = _metaspace;
       
   226   if (m != NULL) {
       
   227     _metaspace = NULL;
       
   228     // release the metaspace
       
   229     delete m;
       
   230     // release the handles
       
   231     if (_handles != NULL) {
       
   232       JNIHandleBlock::release_block(_handles);
       
   233       _handles = NULL;
       
   234     }
       
   235   }
       
   236 
       
   237   // Clear all the JNI handles for methods
       
   238   // These aren't deallocated and are going to look like a leak, but that's
       
   239   // needed because we can't really get rid of jmethodIDs because we don't
       
   240   // know when native code is going to stop using them.  The spec says that
       
   241   // they're "invalid" but existing programs likely rely on their being
       
   242   // NULL after class unloading.
       
   243   if (_jmethod_ids != NULL) {
       
   244     Method::clear_jmethod_ids(this);
       
   245   }
       
   246   // Delete lock
       
   247   delete _metaspace_lock;
       
   248 
       
   249   // Delete free list
       
   250   if (_deallocate_list != NULL) {
       
   251     delete _deallocate_list;
       
   252   }
       
   253 }
       
   254 
       
   255 Metaspace* ClassLoaderData::metaspace_non_null() {
       
   256   // If the metaspace has not been allocated, create a new one.  Might want
       
   257   // to create smaller arena for Reflection class loaders also.
       
   258   // The reason for the delayed allocation is because some class loaders are
       
   259   // simply for delegating with no metadata of their own.
       
   260   if (_metaspace == NULL) {
       
   261     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
       
   262     // Check again if metaspace has been allocated while we were getting this lock.
       
   263     if (_metaspace != NULL) {
       
   264       return _metaspace;
       
   265     }
       
   266     if (class_loader() == NULL) {
       
   267       assert(this == the_null_class_loader_data(), "Must be");
       
   268       size_t word_size = Metaspace::first_chunk_word_size();
       
   269       set_metaspace(new Metaspace(_metaspace_lock, word_size));
       
   270     } else {
       
   271       set_metaspace(new Metaspace(_metaspace_lock));  // default size for now.
       
   272     }
       
   273   }
       
   274   return _metaspace;
       
   275 }
       
   276 
       
   277 JNIHandleBlock* ClassLoaderData::handles() const           { return _handles; }
       
   278 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
       
   279 
       
   280 jobject ClassLoaderData::add_handle(Handle h) {
       
   281   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
       
   282   if (handles() == NULL) {
       
   283     set_handles(JNIHandleBlock::allocate_block());
       
   284   }
       
   285   return handles()->allocate_handle(h());
       
   286 }
       
   287 
       
   288 // Add this metadata pointer to be freed when it's safe.  This is only during
       
   289 // class unloading because Handles might point to this metadata field.
       
   290 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
       
   291   // Metadata in shared region isn't deleted.
       
   292   if (!m->is_shared()) {
       
   293     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
       
   294     if (_deallocate_list == NULL) {
       
   295       _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
       
   296     }
       
   297     _deallocate_list->append_if_missing(m);
       
   298   }
       
   299 }
       
   300 
       
   301 // Deallocate free metadata on the free list.  How useful the PermGen was!
       
   302 void ClassLoaderData::free_deallocate_list() {
       
   303   // Don't need lock, at safepoint
       
   304   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
       
   305   if (_deallocate_list == NULL) {
       
   306     return;
       
   307   }
       
   308   // Go backwards because this removes entries that are freed.
       
   309   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
       
   310     Metadata* m = _deallocate_list->at(i);
       
   311     if (!m->on_stack()) {
       
   312       _deallocate_list->remove_at(i);
       
   313       // There are only three types of metadata that we deallocate directly.
       
   314       // Cast them so they can be used by the template function.
       
   315       if (m->is_method()) {
       
   316         MetadataFactory::free_metadata(this, (Method*)m);
       
   317       } else if (m->is_constantPool()) {
       
   318         MetadataFactory::free_metadata(this, (ConstantPool*)m);
       
   319       } else if (m->is_klass()) {
       
   320         MetadataFactory::free_metadata(this, (InstanceKlass*)m);
       
   321       } else {
       
   322         ShouldNotReachHere();
       
   323       }
       
   324     }
       
   325   }
       
   326 }
       
   327 
       
   328 #ifndef PRODUCT
       
   329 void ClassLoaderData::print_loader(ClassLoaderData *loader_data, outputStream* out) {
       
   330   oop class_loader = loader_data->class_loader();
       
   331   out->print("%s", SystemDictionary::loader_name(class_loader));
       
   332 }
       
   333 
       
   334 // Define to dump klasses
       
   335 #undef CLD_DUMP_KLASSES
       
   336 
       
   337 void ClassLoaderData::dump(outputStream * const out) {
       
   338   ResourceMark rm;
       
   339   out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
       
   340       this, class_loader(),
       
   341       class_loader() != NULL ? class_loader()->klass() : NULL,
       
   342       class_loader() != NULL ? class_loader()->klass()->external_name() : "NULL");
       
   343   if (claimed()) out->print(" claimed ");
       
   344   if (is_unloading()) out->print(" unloading ");
       
   345   out->print(" handles " INTPTR_FORMAT, handles());
       
   346   out->cr();
       
   347   if (metaspace_or_null() != NULL) {
       
   348     out->print_cr("metaspace: " PTR_FORMAT, metaspace_or_null());
       
   349     metaspace_or_null()->dump(out);
       
   350   } else {
       
   351     out->print_cr("metaspace: NULL");
       
   352   }
       
   353 
       
   354 #ifdef CLD_DUMP_KLASSES
       
   355   if (Verbose) {
       
   356     ResourceMark rm;
       
   357     Klass* k = _klasses;
       
   358     while (k != NULL) {
       
   359       out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
       
   360           k->has_modified_oops(), k->has_accumulated_modified_oops());
       
   361       k = k->next_link();
       
   362     }
       
   363   }
       
   364 #endif  // CLD_DUMP_KLASSES
       
   365 #undef CLD_DUMP_KLASSES
       
   366   if (_jmethod_ids != NULL) {
       
   367     Method::print_jmethod_ids(this, out);
       
   368   }
       
   369   out->print_cr("}");
       
   370 }
       
   371 #endif // PRODUCT
       
   372 
       
   373 void ClassLoaderData::verify() {
       
   374   oop cl = class_loader();
       
   375 
       
   376   guarantee(this == class_loader_data(cl), "Must be the same");
       
   377   guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data(), "must be");
       
   378 
       
   379   // Verify the integrity of the allocated space.
       
   380   if (metaspace_or_null() != NULL) {
       
   381     metaspace_or_null()->verify();
       
   382   }
       
   383 
       
   384   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
       
   385     guarantee(k->class_loader_data() == this, "Must be the same");
       
   386     k->verify();
       
   387   }
       
   388 }
       
   389 
       
   390 // GC root of class loader data created.
       
   391 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
       
   392 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
       
   393 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
       
   394 
       
   395 
       
   396 // Add a new class loader data node to the list.  Assign the newly created
       
   397 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
       
   398 ClassLoaderData* ClassLoaderDataGraph::add(ClassLoaderData** cld_addr, Handle loader_data) {
       
   399   // Not assigned a class loader data yet.
       
   400   // Create one.
       
   401   ClassLoaderData* *list_head = &_head;
       
   402   ClassLoaderData* next = _head;
       
   403   ClassLoaderData* cld = new ClassLoaderData(loader_data);
       
   404 
       
   405   // First, Atomically set it.
       
   406   ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
       
   407   if (old != NULL) {
       
   408     delete cld;
       
   409     // Returns the data.
       
   410     return old;
       
   411   }
       
   412 
       
   413   // We won the race, and therefore the task of adding the data to the list of
       
   414   // class loader data
       
   415   do {
       
   416     cld->set_next(next);
       
   417     ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
       
   418     if (exchanged == next) {
       
   419       if (TraceClassLoaderData) {
       
   420         tty->print("[ClassLoaderData: ");
       
   421         tty->print("create class loader data "PTR_FORMAT, cld);
       
   422         tty->print(" for instance "PTR_FORMAT" of ", cld->class_loader());
       
   423         loader_data->klass()->name()->print_symbol_on(tty);
       
   424         tty->print_cr("]");
       
   425       }
       
   426       return cld;
       
   427     }
       
   428     next = exchanged;
       
   429   } while (true);
       
   430 }
       
   431 
       
   432 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
       
   433   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
       
   434     cld->oops_do(f, klass_closure, must_claim);
       
   435   }
       
   436 }
       
   437 
       
   438 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
       
   439   if (ClassUnloading) {
       
   440     ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
       
   441   } else {
       
   442     ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
       
   443   }
       
   444 }
       
   445 
       
   446 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
       
   447   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
       
   448     cld->classes_do(klass_closure);
       
   449   }
       
   450 }
       
   451 
       
   452 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
       
   453   assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
       
   454 
       
   455   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
       
   456 
       
   457   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
       
   458   ClassLoaderData* curr = _head;
       
   459   while (curr != _saved_head) {
       
   460     if (!curr->claimed()) {
       
   461       array->push(curr);
       
   462 
       
   463       if (TraceClassLoaderData) {
       
   464         tty->print("[ClassLoaderData] found new CLD: ");
       
   465         curr->print_value_on(tty);
       
   466         tty->cr();
       
   467       }
       
   468     }
       
   469 
       
   470     curr = curr->_next;
       
   471   }
       
   472 
       
   473   return array;
       
   474 }
       
   475 
       
   476 #ifndef PRODUCT
       
   477 // for debugging and hsfind(x)
       
   478 bool ClassLoaderDataGraph::contains(address x) {
       
   479   // I think we need the _metaspace_lock taken here because the class loader
       
   480   // data graph could be changing while we are walking it (new entries added,
       
   481   // new entries being unloaded, etc).
       
   482   if (DumpSharedSpaces) {
       
   483     // There are only two metaspaces to worry about.
       
   484     ClassLoaderData* ncld = ClassLoaderData::the_null_class_loader_data();
       
   485     return (ncld->ro_metaspace()->contains(x) || ncld->rw_metaspace()->contains(x));
       
   486   }
       
   487 
       
   488   if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(x)) {
       
   489     return true;
       
   490   }
       
   491 
       
   492   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
       
   493     if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
       
   494       return true;
       
   495     }
       
   496   }
       
   497 
       
   498   // Could also be on an unloading list which is okay, ie. still allocated
       
   499   // for a little while.
       
   500   for (ClassLoaderData* ucld = _unloading; ucld != NULL; ucld = ucld->next()) {
       
   501     if (ucld->metaspace_or_null() != NULL && ucld->metaspace_or_null()->contains(x)) {
       
   502       return true;
       
   503     }
       
   504   }
       
   505   return false;
       
   506 }
       
   507 
       
   508 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
       
   509   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
       
   510     if (loader_data == data) {
       
   511       return true;
       
   512     }
       
   513   }
       
   514 
       
   515   return false;
       
   516 }
       
   517 #endif // PRODUCT
       
   518 
       
   519 // Move class loader data from main list to the unloaded list for unloading
       
   520 // and deallocation later.
       
   521 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive) {
       
   522   ClassLoaderData* data = _head;
       
   523   ClassLoaderData* prev = NULL;
       
   524   bool seen_dead_loader = false;
       
   525   // mark metadata seen on the stack and code cache so we can delete
       
   526   // unneeded entries.
       
   527   bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
       
   528   MetadataOnStackMark md_on_stack;
       
   529   while (data != NULL) {
       
   530     if (data->class_loader() == NULL || is_alive->do_object_b(data->class_loader())) {
       
   531       assert(data->claimed(), "class loader data must have been claimed");
       
   532       if (has_redefined_a_class) {
       
   533         data->classes_do(InstanceKlass::purge_previous_versions);
       
   534       }
       
   535       data->free_deallocate_list();
       
   536       prev = data;
       
   537       data = data->next();
       
   538       continue;
       
   539     }
       
   540     seen_dead_loader = true;
       
   541     ClassLoaderData* dead = data;
       
   542     dead->mark_for_unload();
       
   543     if (TraceClassLoaderData) {
       
   544       tty->print("[ClassLoaderData: unload loader data "PTR_FORMAT, dead);
       
   545       tty->print(" for instance "PTR_FORMAT" of ", dead->class_loader());
       
   546       dead->class_loader()->klass()->name()->print_symbol_on(tty);
       
   547       tty->print_cr("]");
       
   548     }
       
   549     data = data->next();
       
   550     // Remove from loader list.
       
   551     if (prev != NULL) {
       
   552       prev->set_next(data);
       
   553     } else {
       
   554       assert(dead == _head, "sanity check");
       
   555       _head = data;
       
   556     }
       
   557     dead->set_next(_unloading);
       
   558     _unloading = dead;
       
   559   }
       
   560   return seen_dead_loader;
       
   561 }
       
   562 
       
   563 void ClassLoaderDataGraph::purge() {
       
   564   ClassLoaderData* list = _unloading;
       
   565   _unloading = NULL;
       
   566   ClassLoaderData* next = list;
       
   567   while (next != NULL) {
       
   568     ClassLoaderData* purge_me = next;
       
   569     next = purge_me->next();
       
   570     delete purge_me;
       
   571   }
       
   572 }
       
   573 
       
   574 // CDS support
       
   575 
       
   576 // Global metaspaces for writing information to the shared archive.  When
       
   577 // application CDS is supported, we may need one per metaspace, so this
       
   578 // sort of looks like it.
       
   579 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
       
   580 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
       
   581 static bool _shared_metaspaces_initialized = false;
       
   582 
       
   583 // Initialize shared metaspaces (change to call from somewhere not lazily)
       
   584 void ClassLoaderData::initialize_shared_metaspaces() {
       
   585   assert(DumpSharedSpaces, "only use this for dumping shared spaces");
       
   586   assert(this == ClassLoaderData::the_null_class_loader_data(),
       
   587          "only supported for null loader data for now");
       
   588   assert (!_shared_metaspaces_initialized, "only initialize once");
       
   589   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
       
   590   _ro_metaspace = new Metaspace(_metaspace_lock, SharedReadOnlySize/wordSize);
       
   591   _rw_metaspace = new Metaspace(_metaspace_lock, SharedReadWriteSize/wordSize);
       
   592   _shared_metaspaces_initialized = true;
       
   593 }
       
   594 
       
   595 Metaspace* ClassLoaderData::ro_metaspace() {
       
   596   assert(_ro_metaspace != NULL, "should already be initialized");
       
   597   return _ro_metaspace;
       
   598 }
       
   599 
       
   600 Metaspace* ClassLoaderData::rw_metaspace() {
       
   601   assert(_rw_metaspace != NULL, "should already be initialized");
       
   602   return _rw_metaspace;
       
   603 }
       
   604 
       
   605 
       
   606 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
       
   607   _data = ClassLoaderDataGraph::_head;
       
   608 }
       
   609 
       
   610 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
       
   611 
       
   612 #ifndef PRODUCT
       
   613 // callable from debugger
       
   614 extern "C" int print_loader_data_graph() {
       
   615   ClassLoaderDataGraph::dump_on(tty);
       
   616   return 0;
       
   617 }
       
   618 
       
   619 void ClassLoaderDataGraph::verify() {
       
   620   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
       
   621     data->verify();
       
   622   }
       
   623 }
       
   624 
       
   625 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
       
   626   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
       
   627     data->dump(out);
       
   628   }
       
   629   MetaspaceAux::dump(out);
       
   630 }
       
   631 
       
   632 void ClassLoaderData::print_value_on(outputStream* out) const {
       
   633   if (class_loader() == NULL) {
       
   634     out->print_cr("NULL class_loader");
       
   635   } else {
       
   636     out->print("class loader "PTR_FORMAT, this);
       
   637     class_loader()->print_value_on(out);
       
   638   }
       
   639 }
       
   640 #endif // PRODUCT