1 /*
   2  * Copyright (c) 2018, 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 #include "precompiled.hpp"
  26 #include "classfile/classLoaderDataGraph.inline.hpp"
  27 #include "classfile/dictionary.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/metadataOnStackMark.hpp"
  30 #include "classfile/moduleEntry.hpp"
  31 #include "classfile/packageEntry.hpp"
  32 #include "logging/log.hpp"
  33 #include "logging/logStream.hpp"
  34 #include "memory/allocation.inline.hpp"
  35 #include "memory/metaspace.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "runtime/atomic.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "runtime/mutex.hpp"
  40 #include "runtime/safepoint.hpp"
  41 #include "runtime/safepointVerifiers.hpp"
  42 #include "utilities/growableArray.hpp"
  43 #include "utilities/macros.hpp"
  44 #include "utilities/ostream.hpp"
  45 
  46 volatile size_t ClassLoaderDataGraph::_num_array_classes = 0;
  47 volatile size_t ClassLoaderDataGraph::_num_instance_classes = 0;
  48 
  49 void ClassLoaderDataGraph::clear_claimed_marks() {
  50   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
  51     cld->clear_claim();
  52   }
  53 }
  54 
  55 // Class iterator used by the compiler.  It gets some number of classes at
  56 // a safepoint to decay invocation counters on the methods.
  57 class ClassLoaderDataGraphKlassIteratorStatic {
  58   ClassLoaderData* _current_loader_data;
  59   Klass*           _current_class_entry;
  60  public:
  61 
  62   ClassLoaderDataGraphKlassIteratorStatic() : _current_loader_data(NULL), _current_class_entry(NULL) {}
  63 
  64   InstanceKlass* try_get_next_class() {
  65     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
  66     size_t max_classes = ClassLoaderDataGraph::num_instance_classes();
  67     assert(max_classes > 0, "should not be called with no instance classes");
  68     for (size_t i = 0; i < max_classes; ) {
  69 
  70       if (_current_class_entry != NULL) {
  71         Klass* k = _current_class_entry;
  72         _current_class_entry = _current_class_entry->next_link();
  73 
  74         if (k->is_instance_klass()) {
  75           InstanceKlass* ik = InstanceKlass::cast(k);
  76           i++;  // count all instance classes found
  77           // Not yet loaded classes are counted in max_classes
  78           // but only return loaded classes.
  79           if (ik->is_loaded()) {
  80             return ik;
  81           }
  82         }
  83       } else {
  84         // Go to next CLD
  85         if (_current_loader_data != NULL) {
  86           _current_loader_data = _current_loader_data->next();
  87         }
  88         // Start at the beginning
  89         if (_current_loader_data == NULL) {
  90           _current_loader_data = ClassLoaderDataGraph::_head;
  91         }
  92 
  93         _current_class_entry = _current_loader_data->klasses();
  94       }
  95     }
  96     // Should never be reached unless all instance classes have failed or are not fully loaded.
  97     // Caller handles NULL.
  98     return NULL;
  99   }
 100 
 101   // If the current class for the static iterator is a class being unloaded or
 102   // deallocated, adjust the current class.
 103   void adjust_saved_class(ClassLoaderData* cld) {
 104     if (_current_loader_data == cld) {
 105       _current_loader_data = cld->next();
 106       if (_current_loader_data != NULL) {
 107         _current_class_entry = _current_loader_data->klasses();
 108       }  // else try_get_next_class will start at the head
 109     }
 110   }
 111 
 112   void adjust_saved_class(Klass* klass) {
 113     if (_current_class_entry == klass) {
 114       _current_class_entry = klass->next_link();
 115     }
 116   }
 117 };
 118 
 119 static ClassLoaderDataGraphKlassIteratorStatic static_klass_iterator;
 120 
 121 InstanceKlass* ClassLoaderDataGraph::try_get_next_class() {
 122   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 123   return static_klass_iterator.try_get_next_class();
 124 }
 125 
 126 void ClassLoaderDataGraph::adjust_saved_class(ClassLoaderData* cld) {
 127   return static_klass_iterator.adjust_saved_class(cld);
 128 }
 129 
 130 void ClassLoaderDataGraph::adjust_saved_class(Klass* klass) {
 131   return static_klass_iterator.adjust_saved_class(klass);
 132 }
 133 
 134 void ClassLoaderDataGraph::clean_deallocate_lists(bool walk_previous_versions) {
 135   assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint");
 136   uint loaders_processed = 0;
 137   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 138     // is_alive check will be necessary for concurrent class unloading.
 139     if (cld->is_alive()) {
 140       // clean metaspace
 141       if (walk_previous_versions) {
 142         cld->classes_do(InstanceKlass::purge_previous_versions);
 143       }
 144       cld->free_deallocate_list();
 145       loaders_processed++;
 146     }
 147   }
 148   log_debug(class, loader, data)("clean_deallocate_lists: loaders processed %u %s",
 149                                  loaders_processed, walk_previous_versions ? "walk_previous_versions" : "");
 150 }
 151 
 152 void ClassLoaderDataGraph::walk_metadata_and_clean_metaspaces() {
 153   assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint");
 154 
 155   _should_clean_deallocate_lists = false; // assume everything gets cleaned
 156 
 157   // Mark metadata seen on the stack so we can delete unreferenced entries.
 158   // Walk all metadata, including the expensive code cache walk, only for class redefinition.
 159   // The MetadataOnStackMark walk during redefinition saves previous versions if it finds old methods
 160   // on the stack or in the code cache, so we only have to repeat the full walk if
 161   // they were found at that time.
 162   // TODO: have redefinition clean old methods out of the code cache.  They still exist in some places.
 163   bool walk_all_metadata = InstanceKlass::has_previous_versions_and_reset();
 164 
 165   MetadataOnStackMark md_on_stack(walk_all_metadata);
 166   clean_deallocate_lists(walk_all_metadata);
 167 }
 168 
 169 // GC root of class loader data created.
 170 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
 171 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
 172 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
 173 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
 174 
 175 bool ClassLoaderDataGraph::_should_purge = false;
 176 bool ClassLoaderDataGraph::_should_clean_deallocate_lists = false;
 177 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false;
 178 bool ClassLoaderDataGraph::_metaspace_oom = false;
 179 
 180 // Add a new class loader data node to the list.  Assign the newly created
 181 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
 182 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_unsafe_anonymous) {
 183 
 184   assert_lock_strong(ClassLoaderDataGraph_lock);
 185 
 186   ClassLoaderData* cld;
 187 
 188   // First check if another thread beat us to creating the CLD and installing
 189   // it into the loader while we were waiting for the lock.
 190   if (!is_unsafe_anonymous && loader.not_null()) {
 191     cld = java_lang_ClassLoader::loader_data_acquire(loader());
 192     if (cld != NULL) {
 193       return cld;
 194     }
 195   }
 196 
 197   // We mustn't GC until we've installed the ClassLoaderData in the Graph since the CLD
 198   // contains oops in _handles that must be walked.  GC doesn't walk CLD from the
 199   // loader oop in all collections, particularly young collections.
 200   NoSafepointVerifier no_safepoints;
 201 
 202   cld = new ClassLoaderData(loader, is_unsafe_anonymous);
 203 
 204   // First install the new CLD to the Graph.
 205   cld->set_next(_head);
 206   _head = cld;
 207 
 208   // Next associate with the class_loader.
 209   if (!is_unsafe_anonymous) {
 210     // Use OrderAccess, since readers need to get the loader_data only after
 211     // it's added to the Graph
 212     java_lang_ClassLoader::release_set_loader_data(loader(), cld);
 213   }
 214 
 215   // Lastly log, if requested
 216   LogTarget(Trace, class, loader, data) lt;
 217   if (lt.is_enabled()) {
 218     ResourceMark rm;
 219     LogStream ls(lt);
 220     ls.print("create ");
 221     cld->print_value_on(&ls);
 222     ls.cr();
 223   }
 224   return cld;
 225 }
 226 
 227 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_unsafe_anonymous) {
 228   MutexLocker ml(ClassLoaderDataGraph_lock);
 229   ClassLoaderData* loader_data = add_to_graph(loader, is_unsafe_anonymous);
 230   return loader_data;
 231 }
 232 
 233 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
 234   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 235   for (ClassLoaderData* cld = _head;  cld != NULL; cld = cld->_next) {
 236     cl->do_cld(cld);
 237   }
 238 }
 239 
 240 void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) {
 241   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 242   // Only walk the head until any clds not purged from prior unloading
 243   // (CMS doesn't purge right away).
 244   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 245     assert(cld->is_unloading(), "invariant");
 246     cl->do_cld(cld);
 247   }
 248 }
 249 
 250 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) {
 251   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 252   for (ClassLoaderData* cld = _head;  cld != NULL; cld = cld->_next) {
 253     CLDClosure* closure = cld->keep_alive() ? strong : weak;
 254     if (closure != NULL) {
 255       closure->do_cld(cld);
 256     }
 257   }
 258 }
 259 
 260 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) {
 261   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 262   if (ClassUnloading) {
 263     roots_cld_do(cl, NULL);
 264   } else {
 265     cld_do(cl);
 266   }
 267 }
 268 
 269 // Closure for locking and iterating through classes.
 270 LockedClassesDo::LockedClassesDo(classes_do_func_t f) : _function(f) {
 271   ClassLoaderDataGraph_lock->lock();
 272 }
 273 
 274 LockedClassesDo::LockedClassesDo() : _function(NULL) {
 275   // callers provide their own do_klass
 276   ClassLoaderDataGraph_lock->lock();
 277 }
 278 
 279 LockedClassesDo::~LockedClassesDo() { ClassLoaderDataGraph_lock->unlock(); }
 280 
 281 
 282 // Iterating over the CLDG needs to be locked because
 283 // unloading can remove entries concurrently soon.
 284 class ClassLoaderDataGraphIterator : public StackObj {
 285   ClassLoaderData* _next;
 286   HandleMark       _hm;  // clean up handles when this is done.
 287   Handle           _holder;
 288   Thread*          _thread;
 289 
 290   void hold_next() {
 291     if (_next != NULL) {
 292       _holder = Handle(_thread, _next->holder_phantom());
 293     }
 294   }
 295 public:
 296   ClassLoaderDataGraphIterator() : _next(ClassLoaderDataGraph::_head) {
 297     _thread = Thread::current();
 298     assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 299     hold_next();
 300   }
 301 
 302   bool repeat() const {
 303     return _next != NULL;
 304   }
 305 
 306   ClassLoaderData* get_next() {
 307     ClassLoaderData* next = _next;
 308     if (_next != NULL) {
 309       _next = _next->next();
 310       hold_next();
 311     }
 312     return next;
 313   }
 314 };
 315 
 316 // These functions assume that the caller has locked the ClassLoaderDataGraph_lock
 317 // if they are not calling the function from a safepoint.
 318 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
 319   ClassLoaderDataGraphIterator iter;
 320   while (iter.repeat()) {
 321     ClassLoaderData* cld = iter.get_next();
 322     cld->classes_do(klass_closure);
 323   }
 324 }
 325 
 326 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {
 327   ClassLoaderDataGraphIterator iter;
 328   while (iter.repeat()) {
 329     ClassLoaderData* cld = iter.get_next();
 330     cld->classes_do(f);
 331   }
 332 }
 333 
 334 void ClassLoaderDataGraph::methods_do(void f(Method*)) {
 335   ClassLoaderDataGraphIterator iter;
 336   while (iter.repeat()) {
 337     ClassLoaderData* cld = iter.get_next();
 338     cld->methods_do(f);
 339   }
 340 }
 341 
 342 void ClassLoaderDataGraph::modules_do(void f(ModuleEntry*)) {
 343   assert_locked_or_safepoint(Module_lock);
 344   ClassLoaderDataGraphIterator iter;
 345   while (iter.repeat()) {
 346     ClassLoaderData* cld = iter.get_next();
 347     cld->modules_do(f);
 348   }
 349 }
 350 
 351 void ClassLoaderDataGraph::modules_unloading_do(void f(ModuleEntry*)) {
 352   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 353   // Only walk the head until any clds not purged from prior unloading
 354   // (CMS doesn't purge right away).
 355   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 356     assert(cld->is_unloading(), "invariant");
 357     cld->modules_do(f);
 358   }
 359 }
 360 
 361 void ClassLoaderDataGraph::packages_do(void f(PackageEntry*)) {
 362   assert_locked_or_safepoint(Module_lock);
 363   ClassLoaderDataGraphIterator iter;
 364   while (iter.repeat()) {
 365     ClassLoaderData* cld = iter.get_next();
 366     cld->packages_do(f);
 367   }
 368 }
 369 
 370 void ClassLoaderDataGraph::packages_unloading_do(void f(PackageEntry*)) {
 371   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 372   // Only walk the head until any clds not purged from prior unloading
 373   // (CMS doesn't purge right away).
 374   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 375     assert(cld->is_unloading(), "invariant");
 376     cld->packages_do(f);
 377   }
 378 }
 379 
 380 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
 381   ClassLoaderDataGraphIterator iter;
 382   while (iter.repeat()) {
 383     ClassLoaderData* cld = iter.get_next();
 384     cld->loaded_classes_do(klass_closure);
 385   }
 386 }
 387 
 388 // This case can block but cannot do unloading (called from CDS)
 389 void ClassLoaderDataGraph::unlocked_loaded_classes_do(KlassClosure* klass_closure) {
 390   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 391     cld->loaded_classes_do(klass_closure);
 392   }
 393 }
 394 
 395 
 396 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
 397   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 398   // Only walk the head until any clds not purged from prior unloading
 399   // (CMS doesn't purge right away).
 400   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 401     assert(cld->is_unloading(), "invariant");
 402     cld->classes_do(f);
 403   }
 404 }
 405 
 406 #define FOR_ALL_DICTIONARY(X)   ClassLoaderDataGraphIterator iter; \
 407                                 ClassLoaderData* X; \
 408                                 while ((X = iter.get_next()) != NULL) \
 409                                   if (X->dictionary() != NULL)
 410 
 411 // Walk classes in the loaded class dictionaries in various forms.
 412 // Only walks the classes defined in this class loader.
 413 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*)) {
 414   FOR_ALL_DICTIONARY(cld) {
 415     cld->dictionary()->classes_do(f);
 416   }
 417 }
 418 
 419 // Only walks the classes defined in this class loader.
 420 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS) {
 421   FOR_ALL_DICTIONARY(cld) {
 422     cld->dictionary()->classes_do(f, CHECK);
 423   }
 424 }
 425 
 426 void ClassLoaderDataGraph::verify_dictionary() {
 427   FOR_ALL_DICTIONARY(cld) {
 428     cld->dictionary()->verify();
 429   }
 430 }
 431 
 432 void ClassLoaderDataGraph::print_dictionary(outputStream* st) {
 433   FOR_ALL_DICTIONARY(cld) {
 434     st->print("Dictionary for ");
 435     cld->print_value_on(st);
 436     st->cr();
 437     cld->dictionary()->print_on(st);
 438     st->cr();
 439   }
 440 }
 441 
 442 void ClassLoaderDataGraph::print_dictionary_statistics(outputStream* st) {
 443   FOR_ALL_DICTIONARY(cld) {
 444     ResourceMark rm;
 445     stringStream tempst;
 446     tempst.print("System Dictionary for %s class loader", cld->loader_name_and_id());
 447     cld->dictionary()->print_table_statistics(st, tempst.as_string());
 448   }
 449 }
 450 
 451 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
 452   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 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       LogTarget(Debug, class, loader, data) lt;
 463       if (lt.is_enabled()) {
 464         LogStream ls(lt);
 465         ls.print("found new CLD: ");
 466         curr->print_value_on(&ls);
 467         ls.cr();
 468       }
 469     }
 470 
 471     curr = curr->_next;
 472   }
 473 
 474   return array;
 475 }
 476 
 477 #ifndef PRODUCT
 478 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
 479   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 480   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 481     if (loader_data == data) {
 482       return true;
 483     }
 484   }
 485 
 486   return false;
 487 }
 488 #endif // PRODUCT
 489 
 490 bool ClassLoaderDataGraph::is_valid(ClassLoaderData* loader_data) {
 491   DEBUG_ONLY( if (!VMError::is_error_reported()) { assert_locked_or_safepoint(ClassLoaderDataGraph_lock); } )
 492   if (loader_data != NULL) {
 493     if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
 494       return true;
 495     }
 496     for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 497       if (loader_data == data) {
 498         return true;
 499       }
 500     }
 501   }
 502   return false;
 503 }
 504 
 505 // Move class loader data from main list to the unloaded list for unloading
 506 // and deallocation later.
 507 bool ClassLoaderDataGraph::do_unloading(bool do_cleaning) {
 508   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 509 
 510   // Indicate whether safepoint cleanup is needed.
 511   _safepoint_cleanup_needed |= do_cleaning;
 512 
 513   ClassLoaderData* data = _head;
 514   ClassLoaderData* prev = NULL;
 515   bool seen_dead_loader = false;
 516   uint loaders_processed = 0;
 517   uint loaders_removed = 0;
 518 
 519   // Save previous _unloading pointer for CMS which may add to unloading list before
 520   // purging and we don't want to rewalk the previously unloaded class loader data.
 521   _saved_unloading = _unloading;
 522 
 523   data = _head;
 524   while (data != NULL) {
 525     if (data->is_alive()) {
 526       prev = data;
 527       data = data->next();
 528       loaders_processed++;
 529       continue;
 530     }
 531     seen_dead_loader = true;
 532     loaders_removed++;
 533     ClassLoaderData* dead = data;
 534     dead->unload();
 535     data = data->next();
 536     // Remove from loader list.
 537     // This class loader data will no longer be found
 538     // in the ClassLoaderDataGraph.
 539     if (prev != NULL) {
 540       prev->set_next(data);
 541     } else {
 542       assert(dead == _head, "sanity check");
 543       _head = data;
 544     }
 545     dead->set_next(_unloading);
 546     _unloading = dead;
 547   }
 548 
 549   log_debug(class, loader, data)("do_unloading: loaders processed %u, loaders removed %u", loaders_processed, loaders_removed);
 550 
 551   return seen_dead_loader;
 552 }
 553 
 554 // There's at least one dead class loader.  Purge refererences of healthy module
 555 // reads lists and package export lists to modules belonging to dead loaders.
 556 void ClassLoaderDataGraph::clean_module_and_package_info() {
 557   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 558 
 559   ClassLoaderData* data = _head;
 560   while (data != NULL) {
 561     // Remove entries in the dictionary of live class loader that have
 562     // initiated loading classes in a dead class loader.
 563     if (data->dictionary() != NULL) {
 564       data->dictionary()->do_unloading();
 565     }
 566     // Walk a ModuleEntry's reads, and a PackageEntry's exports
 567     // lists to determine if there are modules on those lists that are now
 568     // dead and should be removed.  A module's life cycle is equivalent
 569     // to its defining class loader's life cycle.  Since a module is
 570     // considered dead if its class loader is dead, these walks must
 571     // occur after each class loader's aliveness is determined.
 572     if (data->packages() != NULL) {
 573       data->packages()->purge_all_package_exports();
 574     }
 575     if (data->modules_defined()) {
 576       data->modules()->purge_all_module_reads();
 577     }
 578     data = data->next();
 579   }
 580 }
 581 
 582 void ClassLoaderDataGraph::purge() {
 583   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 584   ClassLoaderData* list = _unloading;
 585   _unloading = NULL;
 586   ClassLoaderData* next = list;
 587   bool classes_unloaded = false;
 588   while (next != NULL) {
 589     ClassLoaderData* purge_me = next;
 590     next = purge_me->next();
 591     delete purge_me;
 592     classes_unloaded = true;
 593   }
 594   if (classes_unloaded) {
 595     Metaspace::purge();
 596     set_metaspace_oom(false);
 597   }
 598 }
 599 
 600 int ClassLoaderDataGraph::resize_if_needed() {
 601   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 602   int resized = 0;
 603   if (Dictionary::does_any_dictionary_needs_resizing()) {
 604     FOR_ALL_DICTIONARY(cld) {
 605       if (cld->dictionary()->resize_if_needed()) {
 606         resized++;
 607       }
 608     }
 609   }
 610   return resized;
 611 }
 612 
 613 ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic()
 614     : _next_klass(NULL) {
 615   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 616   ClassLoaderData* cld = ClassLoaderDataGraph::_head;
 617   Klass* klass = NULL;
 618 
 619   // Find the first klass in the CLDG.
 620   while (cld != NULL) {
 621     assert_locked_or_safepoint(cld->metaspace_lock());
 622     klass = cld->_klasses;
 623     if (klass != NULL) {
 624       _next_klass = klass;
 625       return;
 626     }
 627     cld = cld->next();
 628   }
 629 }
 630 
 631 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) {
 632   Klass* next = klass->next_link();
 633   if (next != NULL) {
 634     return next;
 635   }
 636 
 637   // No more klasses in the current CLD. Time to find a new CLD.
 638   ClassLoaderData* cld = klass->class_loader_data();
 639   assert_locked_or_safepoint(cld->metaspace_lock());
 640   while (next == NULL) {
 641     cld = cld->next();
 642     if (cld == NULL) {
 643       break;
 644     }
 645     next = cld->_klasses;
 646   }
 647 
 648   return next;
 649 }
 650 
 651 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() {
 652   Klass* head = _next_klass;
 653 
 654   while (head != NULL) {
 655     Klass* next = next_klass_in_cldg(head);
 656 
 657     Klass* old_head = Atomic::cmpxchg(next, &_next_klass, head);
 658 
 659     if (old_head == head) {
 660       return head; // Won the CAS.
 661     }
 662 
 663     head = old_head;
 664   }
 665 
 666   // Nothing more for the iterator to hand out.
 667   assert(head == NULL, "head is " PTR_FORMAT ", expected not null:", p2i(head));
 668   return NULL;
 669 }
 670 
 671 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
 672   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 673   _data = ClassLoaderDataGraph::_head;
 674 }
 675 
 676 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
 677 
 678 ClassLoaderMetaspace* ClassLoaderDataGraphMetaspaceIterator::get_next() {
 679   assert(_data != NULL, "Should not be NULL in call to the iterator");
 680   ClassLoaderMetaspace* result = _data->metaspace_or_null();
 681   _data = _data->next();
 682   // This result might be NULL for class loaders without metaspace
 683   // yet.  It would be nice to return only non-null results but
 684   // there is no guarantee that there will be a non-null result
 685   // down the list so the caller is going to have to check.
 686   return result;
 687 }
 688 
 689 #ifndef PRODUCT
 690 // callable from debugger
 691 extern "C" int print_loader_data_graph() {
 692   ResourceMark rm;
 693   ClassLoaderDataGraph::print_on(tty);
 694   return 0;
 695 }
 696 
 697 void ClassLoaderDataGraph::verify() {
 698   ClassLoaderDataGraphIterator iter;
 699   while (iter.repeat()) {
 700     ClassLoaderData* cld = iter.get_next();
 701     cld->verify();
 702   }
 703 }
 704 
 705 void ClassLoaderDataGraph::print_on(outputStream * const out) {
 706   ClassLoaderDataGraphIterator iter;
 707   while (iter.repeat()) {
 708     ClassLoaderData* cld = iter.get_next();
 709     cld->print_on(out);
 710   }
 711 }
 712 #endif // PRODUCT
--- EOF ---