src/share/vm/classfile/classLoaderData.cpp

Wed, 12 Dec 2012 22:41:13 +0100

author
stefank
date
Wed, 12 Dec 2012 22:41:13 +0100
changeset 4353
1b1e16471e46
parent 4304
90273fc0a981
child 4367
c52660592f37
permissions
-rw-r--r--

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

mercurial