src/share/vm/classfile/classLoaderData.cpp

Mon, 03 Dec 2012 15:09:39 -0800

author
jmasa
date
Mon, 03 Dec 2012 15:09:39 -0800
changeset 4382
e51c9860cf66
parent 4367
c52660592f37
child 4457
59a58e20dc60
permissions
-rw-r--r--

8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
Reviewed-by: johnc, coleenp

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

mercurial