hotspot/src/share/vm/classfile/classLoaderData.cpp
changeset 46554 aa1cfd918c4f
parent 46464 6432a858a220
child 46701 f559541c0daa
equal deleted inserted replaced
45608:9927a9f16738 46554:aa1cfd918c4f
    95   _modules(NULL), _packages(NULL),
    95   _modules(NULL), _packages(NULL),
    96   _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
    96   _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
    97   _next(NULL), _dependencies(dependencies),
    97   _next(NULL), _dependencies(dependencies),
    98   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
    98   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
    99                             Monitor::_safepoint_check_never)) {
    99                             Monitor::_safepoint_check_never)) {
       
   100 
       
   101   // A ClassLoaderData created solely for an anonymous class should never have a
       
   102   // ModuleEntryTable or PackageEntryTable created for it. The defining package
       
   103   // and module for an anonymous class will be found in its host class.
       
   104   if (!is_anonymous) {
       
   105     _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
       
   106     if (h_class_loader.is_null()) {
       
   107       // Create unnamed module for boot loader
       
   108       _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
       
   109     } else {
       
   110       // Create unnamed module for all other loaders
       
   111       _unnamed_module = ModuleEntry::create_unnamed_module(this);
       
   112     }
       
   113   } else {
       
   114     _unnamed_module = NULL;
       
   115   }
   100   TRACE_INIT_ID(this);
   116   TRACE_INIT_ID(this);
   101 }
   117 }
   102 
   118 
   103 void ClassLoaderData::init_dependencies(TRAPS) {
   119 void ClassLoaderData::init_dependencies(TRAPS) {
   104   assert(!Universe::is_fully_initialized(), "should only be called when initializing");
   120   assert(!Universe::is_fully_initialized(), "should only be called when initializing");
   245 }
   261 }
   246 
   262 
   247 void ClassLoaderData::methods_do(void f(Method*)) {
   263 void ClassLoaderData::methods_do(void f(Method*)) {
   248   // Lock-free access requires load_ptr_acquire
   264   // Lock-free access requires load_ptr_acquire
   249   for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) {
   265   for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) {
   250     if (k->is_instance_klass()) {
   266     if (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded()) {
   251       InstanceKlass::cast(k)->methods_do(f);
   267       InstanceKlass::cast(k)->methods_do(f);
   252     }
   268     }
   253   }
   269   }
   254 }
   270 }
   255 
   271 
   256 void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) {
   272 void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) {
   257   // Lock to avoid classes being modified/added/removed during iteration
   273   // Lock-free access requires load_ptr_acquire
   258   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   274   for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) {
   259   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
       
   260     // Do not filter ArrayKlass oops here...
   275     // Do not filter ArrayKlass oops here...
   261     if (k->is_array_klass() || (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded())) {
   276     if (k->is_array_klass() || (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded())) {
   262       klass_closure->do_klass(k);
   277       klass_closure->do_klass(k);
   263     }
   278     }
   264   }
   279   }
   274   }
   289   }
   275 }
   290 }
   276 
   291 
   277 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
   292 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
   278   assert_locked_or_safepoint(Module_lock);
   293   assert_locked_or_safepoint(Module_lock);
       
   294   if (_unnamed_module != NULL) {
       
   295     f(_unnamed_module);
       
   296   }
   279   if (_modules != NULL) {
   297   if (_modules != NULL) {
   280     for (int i = 0; i < _modules->table_size(); i++) {
   298     for (int i = 0; i < _modules->table_size(); i++) {
   281       for (ModuleEntry* entry = _modules->bucket(i);
   299       for (ModuleEntry* entry = _modules->bucket(i);
   282                               entry != NULL;
   300            entry != NULL;
   283                               entry = entry->next()) {
   301            entry = entry->next()) {
   284         f(entry);
   302         f(entry);
   285       }
   303       }
   286     }
   304     }
   287   }
   305   }
   288 }
   306 }
   289 
   307 
   290 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
   308 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
   291   // Lock-free access requires load_ptr_acquire
   309   assert_locked_or_safepoint(Module_lock);
   292   PackageEntryTable* packages = load_ptr_acquire(&_packages);
   310   if (_packages != NULL) {
   293   if (packages != NULL) {
   311     for (int i = 0; i < _packages->table_size(); i++) {
   294     for (int i = 0; i < packages->table_size(); i++) {
   312       for (PackageEntry* entry = _packages->bucket(i);
   295       for (PackageEntry* entry = packages->bucket(i);
   313            entry != NULL;
   296                               entry != NULL;
   314            entry = entry->next()) {
   297                               entry = entry->next()) {
       
   298         f(entry);
   315         f(entry);
   299       }
   316       }
   300     }
   317     }
   301   }
   318   }
   302 }
   319 }
   474   // In some rare cases items added to this list will not be freed elsewhere.
   491   // In some rare cases items added to this list will not be freed elsewhere.
   475   // To keep it simple, just free everything in it here.
   492   // To keep it simple, just free everything in it here.
   476   free_deallocate_list();
   493   free_deallocate_list();
   477 }
   494 }
   478 
   495 
   479 PackageEntryTable* ClassLoaderData::packages() {
       
   480   // Lazily create the package entry table at first request.
       
   481   // Lock-free access requires load_ptr_acquire.
       
   482   PackageEntryTable* packages = load_ptr_acquire(&_packages);
       
   483   if (packages == NULL) {
       
   484     MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
       
   485     // Check if _packages got allocated while we were waiting for this lock.
       
   486     if ((packages = _packages) == NULL) {
       
   487       packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
       
   488       // Ensure _packages is stable, since it is examined without a lock
       
   489       OrderAccess::release_store_ptr(&_packages, packages);
       
   490     }
       
   491   }
       
   492   return packages;
       
   493 }
       
   494 
       
   495 ModuleEntryTable* ClassLoaderData::modules() {
   496 ModuleEntryTable* ClassLoaderData::modules() {
   496   // Lazily create the module entry table at first request.
   497   // Lazily create the module entry table at first request.
   497   // Lock-free access requires load_ptr_acquire.
   498   // Lock-free access requires load_ptr_acquire.
   498   ModuleEntryTable* modules = load_ptr_acquire(&_modules);
   499   ModuleEntryTable* modules = load_ptr_acquire(&_modules);
   499   if (modules == NULL) {
   500   if (modules == NULL) {
   500     MutexLocker m1(Module_lock);
   501     MutexLocker m1(Module_lock);
   501     // Check if _modules got allocated while we were waiting for this lock.
   502     // Check if _modules got allocated while we were waiting for this lock.
   502     if ((modules = _modules) == NULL) {
   503     if ((modules = _modules) == NULL) {
   503       modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
   504       modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
   504       // Each loader has one unnamed module entry. Create it before
       
   505       // any classes, loaded by this loader, are defined in case
       
   506       // they end up being defined in loader's unnamed module.
       
   507       modules->create_unnamed_module(this);
       
   508 
   505 
   509       {
   506       {
   510         MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
   507         MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
   511         // Ensure _modules is stable, since it is examined without a lock
   508         // Ensure _modules is stable, since it is examined without a lock
   512         OrderAccess::release_store_ptr(&_modules, modules);
   509         OrderAccess::release_store_ptr(&_modules, modules);
   527       || is_alive_closure->do_object_b(keep_alive_object());
   524       || is_alive_closure->do_object_b(keep_alive_object());
   528 
   525 
   529   return alive;
   526   return alive;
   530 }
   527 }
   531 
   528 
   532 
       
   533 ClassLoaderData::~ClassLoaderData() {
   529 ClassLoaderData::~ClassLoaderData() {
   534   // Release C heap structures for all the classes.
   530   // Release C heap structures for all the classes.
   535   classes_do(InstanceKlass::release_C_heap_structures);
   531   classes_do(InstanceKlass::release_C_heap_structures);
   536 
   532 
   537   // Release C heap allocated hashtable for all the packages.
   533   // Release C heap allocated hashtable for all the packages.
   544   // Release C heap allocated hashtable for all the modules.
   540   // Release C heap allocated hashtable for all the modules.
   545   if (_modules != NULL) {
   541   if (_modules != NULL) {
   546     // Destroy the table itself
   542     // Destroy the table itself
   547     delete _modules;
   543     delete _modules;
   548     _modules = NULL;
   544     _modules = NULL;
       
   545   }
       
   546 
       
   547   if (_unnamed_module != NULL) {
       
   548     _unnamed_module->delete_unnamed_module();
       
   549     _unnamed_module = NULL;
   549   }
   550   }
   550 
   551 
   551   // release the metaspace
   552   // release the metaspace
   552   Metaspace *m = _metaspace;
   553   Metaspace *m = _metaspace;
   553   if (m != NULL) {
   554   if (m != NULL) {
   584 
   585 
   585 // Returns true if this class loader data is one of the 3 builtin
   586 // Returns true if this class loader data is one of the 3 builtin
   586 // (boot, application/system or platform) class loaders. Note, the
   587 // (boot, application/system or platform) class loaders. Note, the
   587 // builtin loaders are not freed by a GC.
   588 // builtin loaders are not freed by a GC.
   588 bool ClassLoaderData::is_builtin_class_loader_data() const {
   589 bool ClassLoaderData::is_builtin_class_loader_data() const {
   589   Handle classLoaderHandle = class_loader();
       
   590   return (is_the_null_class_loader_data() ||
   590   return (is_the_null_class_loader_data() ||
   591           SystemDictionary::is_system_class_loader(classLoaderHandle) ||
   591           SystemDictionary::is_system_class_loader(class_loader()) ||
   592           SystemDictionary::is_platform_class_loader(classLoaderHandle));
   592           SystemDictionary::is_platform_class_loader(class_loader()));
   593 }
   593 }
   594 
   594 
   595 Metaspace* ClassLoaderData::metaspace_non_null() {
   595 Metaspace* ClassLoaderData::metaspace_non_null() {
   596   assert(!DumpSharedSpaces, "wrong metaspace!");
   596   assert(!DumpSharedSpaces, "wrong metaspace!");
   597   // If the metaspace has not been allocated, create a new one.  Might want
   597   // If the metaspace has not been allocated, create a new one.  Might want
   684 }
   684 }
   685 
   685 
   686 // These anonymous class loaders are to contain classes used for JSR292
   686 // These anonymous class loaders are to contain classes used for JSR292
   687 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
   687 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
   688   // Add a new class loader data to the graph.
   688   // Add a new class loader data to the graph.
   689   return ClassLoaderDataGraph::add(loader, true, THREAD);
   689   Handle lh(THREAD, loader);
       
   690   return ClassLoaderDataGraph::add(lh, true, THREAD);
   690 }
   691 }
   691 
   692 
   692 const char* ClassLoaderData::loader_name() {
   693 const char* ClassLoaderData::loader_name() {
   693   // Handles null class loader
   694   // Handles null class loader
   694   return SystemDictionary::loader_name(class_loader());
   695   return SystemDictionary::loader_name(class_loader());
   816   Handle string;
   817   Handle string;
   817   if (loader.not_null()) {
   818   if (loader.not_null()) {
   818     // Include the result of loader.toString() in the output. This allows
   819     // Include the result of loader.toString() in the output. This allows
   819     // the user of the log to identify the class loader instance.
   820     // the user of the log to identify the class loader instance.
   820     JavaValue result(T_OBJECT);
   821     JavaValue result(T_OBJECT);
   821     KlassHandle spec_klass(THREAD, SystemDictionary::ClassLoader_klass());
   822     Klass* spec_klass = SystemDictionary::ClassLoader_klass();
   822     JavaCalls::call_virtual(&result,
   823     JavaCalls::call_virtual(&result,
   823                             loader,
   824                             loader,
   824                             spec_klass,
   825                             spec_klass,
   825                             vmSymbols::toString_name(),
   826                             vmSymbols::toString_name(),
   826                             vmSymbols::void_string_signature(),
   827                             vmSymbols::void_string_signature(),
   827                             CHECK);
   828                             CHECK);
   828     assert(result.get_type() == T_OBJECT, "just checking");
   829     assert(result.get_type() == T_OBJECT, "just checking");
   829     string = (oop)result.get_jobject();
   830     string = Handle(THREAD, (oop)result.get_jobject());
   830   }
   831   }
   831 
   832 
   832   ResourceMark rm;
   833   ResourceMark rm;
   833   outputStream* log = Log(class, loader, data)::debug_stream();
   834   outputStream* log = Log(class, loader, data)::debug_stream();
   834   log->print("create class loader data " INTPTR_FORMAT, p2i(cld));
   835   log->print("create class loader data " INTPTR_FORMAT, p2i(cld));
  1076     // to its defining class loader's life cycle.  Since a module is
  1077     // to its defining class loader's life cycle.  Since a module is
  1077     // considered dead if its class loader is dead, these walks must
  1078     // considered dead if its class loader is dead, these walks must
  1078     // occur after each class loader's aliveness is determined.
  1079     // occur after each class loader's aliveness is determined.
  1079     data = _head;
  1080     data = _head;
  1080     while (data != NULL) {
  1081     while (data != NULL) {
  1081       if (data->packages_defined()) {
  1082       if (data->packages() != NULL) {
  1082         data->packages()->purge_all_package_exports();
  1083         data->packages()->purge_all_package_exports();
  1083       }
  1084       }
  1084       if (data->modules_defined()) {
  1085       if (data->modules_defined()) {
  1085         data->modules()->purge_all_module_reads();
  1086         data->modules()->purge_all_module_reads();
  1086       }
  1087       }