< prev index next >

src/hotspot/share/classfile/classLoaderData.cpp

Print this page




 112       _name = SymbolTable::new_symbol(cl_instance_name, CATCH);
 113     }
 114   }
 115 
 116   // Obtain the class loader's name and identity hash.  If the class loader's
 117   // name was not explicitly set during construction, the class loader's name and id
 118   // will be set to the qualified class name of the class loader along with its
 119   // identity hash.
 120   // If for some reason the ClassLoader's constructor has not been run, instead of
 121   // leaving the _name_and_id field null, fall back to the external qualified class
 122   // name.  Thus CLD's _name_and_id field should never have a null value.
 123   oop cl_name_and_id = java_lang_ClassLoader::nameAndId(class_loader());
 124   const char* cl_instance_name_and_id =
 125                   (cl_name_and_id == NULL) ? _class_loader_klass->external_name() :
 126                                              java_lang_String::as_utf8_string(cl_name_and_id);
 127   assert(cl_instance_name_and_id != NULL && cl_instance_name_and_id[0] != '\0', "class loader has no name and id");
 128   // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
 129   _name_and_id = SymbolTable::new_symbol(cl_instance_name_and_id, CATCH);
 130 }
 131 
 132 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_unsafe_anonymous) :
 133   _metaspace(NULL),
 134   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
 135                             Monitor::_safepoint_check_never)),
 136   _unloading(false), _is_unsafe_anonymous(is_unsafe_anonymous),
 137   _modified_oops(true), _accumulated_modified_oops(false),
 138   // An unsafe anonymous class loader data doesn't have anything to keep
 139   // it from being unloaded during parsing of the unsafe anonymous class.
 140   // The null-class-loader should always be kept alive.
 141   _keep_alive((is_unsafe_anonymous || h_class_loader.is_null()) ? 1 : 0),
 142   _claim(0),
 143   _handles(),
 144   _klasses(NULL), _packages(NULL), _modules(NULL), _unnamed_module(NULL), _dictionary(NULL),
 145   _jmethod_ids(NULL),
 146   _deallocate_list(NULL),
 147   _next(NULL),
 148   _class_loader_klass(NULL), _name(NULL), _name_and_id(NULL) {
 149 
 150   if (!h_class_loader.is_null()) {
 151     _class_loader = _handles.add(h_class_loader());
 152     _class_loader_klass = h_class_loader->klass();
 153     initialize_name(h_class_loader);
 154   }
 155 
 156   if (!is_unsafe_anonymous) {
 157     // The holder is initialized later for unsafe anonymous classes, and before calling anything
 158     // that call class_loader().
 159     initialize_holder(h_class_loader);
 160 
 161     // A ClassLoaderData created solely for an unsafe anonymous class should never have a
 162     // ModuleEntryTable or PackageEntryTable created for it. The defining package
 163     // and module for an unsafe anonymous class will be found in its host class.
 164     _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
 165     if (h_class_loader.is_null()) {
 166       // Create unnamed module for boot loader
 167       _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
 168     } else {
 169       // Create unnamed module for all other loaders
 170       _unnamed_module = ModuleEntry::create_unnamed_module(this);
 171     }
 172     _dictionary = create_dictionary();
 173   }
 174 
 175   NOT_PRODUCT(_dependency_count = 0); // number of class loader dependencies
 176 
 177   JFR_ONLY(INIT_ID(this);)
 178 }
 179 
 180 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
 181   Chunk* c = _head;
 182   while (c != NULL) {


 264     }
 265     chunk = chunk->_next;
 266   }
 267   return false;
 268 }
 269 #endif // PRODUCT
 270 
 271 bool ClassLoaderData::try_claim(int claim) {
 272   for (;;) {
 273     int old_claim = Atomic::load(&_claim);
 274     if ((old_claim & claim) == claim) {
 275       return false;
 276     }
 277     int new_claim = old_claim | claim;
 278     if (Atomic::cmpxchg(new_claim, &_claim, old_claim) == old_claim) {
 279       return true;
 280     }
 281   }
 282 }
 283 
 284 // Unsafe anonymous classes have their own ClassLoaderData that is marked to keep alive
 285 // while the class is being parsed, and if the class appears on the module fixup list.
 286 // Due to the uniqueness that no other class shares the unsafe anonymous class' name or
 287 // ClassLoaderData, no other non-GC thread has knowledge of the unsafe anonymous class while
 288 // it is being defined, therefore _keep_alive is not volatile or atomic.
 289 void ClassLoaderData::inc_keep_alive() {
 290   if (is_unsafe_anonymous()) {
 291     assert(_keep_alive > 0, "Invalid keep alive increment count");
 292     _keep_alive++;
 293   }
 294 }
 295 
 296 void ClassLoaderData::dec_keep_alive() {
 297   if (is_unsafe_anonymous()) {
 298     assert(_keep_alive > 0, "Invalid keep alive decrement count");
 299     _keep_alive--;
 300   }
 301 }
 302 
 303 void ClassLoaderData::oops_do(OopClosure* f, int claim_value, bool clear_mod_oops) {
 304   if (claim_value != ClassLoaderData::_claim_none && !try_claim(claim_value)) {
 305     return;
 306   }
 307 
 308   // Only clear modified_oops after the ClassLoaderData is claimed.
 309   if (clear_mod_oops) {
 310     clear_modified_oops();
 311   }
 312 
 313   _handles.oops_do(f);
 314 }
 315 
 316 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
 317   // Lock-free access requires load_acquire


 384   if (_packages != NULL) {
 385     for (int i = 0; i < _packages->table_size(); i++) {
 386       for (PackageEntry* entry = _packages->bucket(i);
 387            entry != NULL;
 388            entry = entry->next()) {
 389         f(entry);
 390       }
 391     }
 392   }
 393 }
 394 
 395 void ClassLoaderData::record_dependency(const Klass* k) {
 396   assert(k != NULL, "invariant");
 397 
 398   ClassLoaderData * const from_cld = this;
 399   ClassLoaderData * const to_cld = k->class_loader_data();
 400 
 401   // Do not need to record dependency if the dependency is to a class whose
 402   // class loader data is never freed.  (i.e. the dependency's class loader
 403   // is one of the three builtin class loaders and the dependency is not
 404   // unsafe anonymous.)
 405   if (to_cld->is_permanent_class_loader_data()) {
 406     return;
 407   }
 408 
 409   oop to;
 410   if (to_cld->is_unsafe_anonymous()) {
 411     // Just return if an unsafe anonymous class is attempting to record a dependency
 412     // to itself.  (Note that every unsafe anonymous class has its own unique class
 413     // loader data.)
 414     if (to_cld == from_cld) {
 415       return;
 416     }
 417     // Unsafe anonymous class dependencies are through the mirror.
 418     to = k->java_mirror();
 419   } else {
 420     to = to_cld->class_loader();
 421     oop from = from_cld->class_loader();
 422 
 423     // Just return if this dependency is to a class with the same or a parent
 424     // class_loader.
 425     if (oopDesc::equals(from, to) || java_lang_ClassLoader::isAncestor(from, to)) {
 426       return; // this class loader is in the parent list, no need to add it.
 427     }
 428   }
 429 
 430   // It's a dependency we won't find through GC, add it.
 431   if (!_handles.contains(to)) {
 432     NOT_PRODUCT(Atomic::inc(&_dependency_count));
 433     LogTarget(Trace, class, loader, data) lt;
 434     if (lt.is_enabled()) {
 435       ResourceMark rm;
 436       LogStream ls(lt);
 437       ls.print("adding dependency from ");


 545   if (modules == NULL) {
 546     MutexLocker m1(Module_lock);
 547     // Check if _modules got allocated while we were waiting for this lock.
 548     if ((modules = _modules) == NULL) {
 549       modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
 550 
 551       {
 552         MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
 553         // Ensure _modules is stable, since it is examined without a lock
 554         OrderAccess::release_store(&_modules, modules);
 555       }
 556     }
 557   }
 558   return modules;
 559 }
 560 
 561 const int _boot_loader_dictionary_size    = 1009;
 562 const int _default_loader_dictionary_size = 107;
 563 
 564 Dictionary* ClassLoaderData::create_dictionary() {
 565   assert(!is_unsafe_anonymous(), "unsafe anonymous class loader data do not have a dictionary");
 566   int size;
 567   bool resizable = false;
 568   if (_the_null_class_loader_data == NULL) {
 569     size = _boot_loader_dictionary_size;
 570     resizable = true;
 571   } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
 572     size = 1;  // there's only one class in relection class loader and no initiated classes
 573   } else if (is_system_class_loader_data()) {
 574     size = _boot_loader_dictionary_size;
 575     resizable = true;
 576   } else {
 577     size = _default_loader_dictionary_size;
 578     resizable = true;
 579   }
 580   if (!DynamicallyResizeSystemDictionaries || DumpSharedSpaces) {
 581     resizable = false;
 582   }
 583   return new Dictionary(this, size, resizable);
 584 }
 585 


 591   // to get notified about this potential resurrection, otherwise the marking
 592   // might not find the object.
 593   if (!_holder.is_null()) {  // NULL class_loader
 594     return _holder.resolve();
 595   } else {
 596     return NULL;
 597   }
 598 }
 599 
 600 // Let the GC read the holder without keeping it alive.
 601 oop ClassLoaderData::holder_no_keepalive() const {
 602   if (!_holder.is_null()) {  // NULL class_loader
 603     return _holder.peek();
 604   } else {
 605     return NULL;
 606   }
 607 }
 608 
 609 // Unloading support
 610 bool ClassLoaderData::is_alive() const {
 611   bool alive = keep_alive()         // null class loader and incomplete unsafe anonymous klasses.
 612       || (_holder.peek() != NULL);  // and not cleaned by the GC weak handle processing.
 613 
 614   return alive;
 615 }
 616 
 617 class ReleaseKlassClosure: public KlassClosure {
 618 private:
 619   size_t  _instance_class_released;
 620   size_t  _array_class_released;
 621 public:
 622   ReleaseKlassClosure() : _instance_class_released(0), _array_class_released(0) { }
 623 
 624   size_t instance_class_released() const { return _instance_class_released; }
 625   size_t array_class_released()    const { return _array_class_released;    }
 626 
 627   void do_klass(Klass* k) {
 628     if (k->is_array_klass()) {
 629       _array_class_released ++;
 630     } else {
 631       assert(k->is_instance_klass(), "Must be");


 689   }
 690   // Delete lock
 691   delete _metaspace_lock;
 692 
 693   // Delete free list
 694   if (_deallocate_list != NULL) {
 695     delete _deallocate_list;
 696   }
 697 
 698   // Decrement refcounts of Symbols if created.
 699   if (_name != NULL) {
 700     _name->decrement_refcount();
 701   }
 702   if (_name_and_id != NULL) {
 703     _name_and_id->decrement_refcount();
 704   }
 705 }
 706 
 707 // Returns true if this class loader data is for the app class loader
 708 // or a user defined system class loader.  (Note that the class loader
 709 // data may be unsafe anonymous.)
 710 bool ClassLoaderData::is_system_class_loader_data() const {
 711   return SystemDictionary::is_system_class_loader(class_loader());
 712 }
 713 
 714 // Returns true if this class loader data is for the platform class loader.
 715 // (Note that the class loader data may be unsafe anonymous.)
 716 bool ClassLoaderData::is_platform_class_loader_data() const {
 717   return SystemDictionary::is_platform_class_loader(class_loader());
 718 }
 719 
 720 // Returns true if the class loader for this class loader data is one of
 721 // the 3 builtin (boot application/system or platform) class loaders,
 722 // including a user-defined system class loader.  Note that if the class
 723 // loader data is for an unsafe anonymous class then it may get freed by a GC
 724 // even if its class loader is one of these loaders.
 725 bool ClassLoaderData::is_builtin_class_loader_data() const {
 726   return (is_boot_class_loader_data() ||
 727           SystemDictionary::is_system_class_loader(class_loader()) ||
 728           SystemDictionary::is_platform_class_loader(class_loader()));
 729 }
 730 
 731 // Returns true if this class loader data is a class loader data
 732 // that is not ever freed by a GC.  It must be the CLD for one of the builtin
 733 // class loaders and not the CLD for an unsafe anonymous class.
 734 bool ClassLoaderData::is_permanent_class_loader_data() const {
 735   return is_builtin_class_loader_data() && !is_unsafe_anonymous();
 736 }
 737 
 738 ClassLoaderMetaspace* ClassLoaderData::metaspace_non_null() {
 739   // If the metaspace has not been allocated, create a new one.  Might want
 740   // to create smaller arena for Reflection class loaders also.
 741   // The reason for the delayed allocation is because some class loaders are
 742   // simply for delegating with no metadata of their own.
 743   // Lock-free access requires load_acquire.
 744   ClassLoaderMetaspace* metaspace = OrderAccess::load_acquire(&_metaspace);
 745   if (metaspace == NULL) {
 746     MutexLockerEx ml(_metaspace_lock,  Mutex::_no_safepoint_check_flag);
 747     // Check if _metaspace got allocated while we were waiting for this lock.
 748     if ((metaspace = _metaspace) == NULL) {
 749       if (this == the_null_class_loader_data()) {
 750         assert (class_loader() == NULL, "Must be");
 751         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::BootMetaspaceType);
 752       } else if (is_unsafe_anonymous()) {
 753         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::UnsafeAnonymousMetaspaceType);
 754       } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
 755         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType);
 756       } else {
 757         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::StandardMetaspaceType);
 758       }
 759       // Ensure _metaspace is stable, since it is examined without a lock
 760       OrderAccess::release_store(&_metaspace, metaspace);
 761     }
 762   }
 763   return metaspace;
 764 }
 765 
 766 OopHandle ClassLoaderData::add_handle(Handle h) {
 767   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 768   record_modified_oops();
 769   return OopHandle(_handles.add(h()));
 770 }
 771 
 772 void ClassLoaderData::remove_handle(OopHandle h) {
 773   assert(!is_unloading(), "Do not remove a handle for a CLD that is unloading");


 850   if (_deallocate_list == NULL) {
 851     return;
 852   }
 853   // Go backwards because this removes entries that are freed.
 854   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
 855     Metadata* m = _deallocate_list->at(i);
 856     _deallocate_list->remove_at(i);
 857     if (m->is_constantPool()) {
 858       ((ConstantPool*)m)->release_C_heap_structures();
 859     } else if (m->is_klass()) {
 860       InstanceKlass* ik = (InstanceKlass*)m;
 861       // also releases ik->constants() C heap memory
 862       InstanceKlass::release_C_heap_structures(ik);
 863       // Remove the class so unloading events aren't triggered for
 864       // this class (scratch or error class) in do_unloading().
 865       remove_class(ik);
 866     }
 867   }
 868 }
 869 
 870 // These CLDs are to contain unsafe anonymous classes used for JSR292
 871 ClassLoaderData* ClassLoaderData::unsafe_anonymous_class_loader_data(Handle loader) {
 872   // Add a new class loader data to the graph.
 873   return ClassLoaderDataGraph::add(loader, true);
 874 }
 875 
 876 // Caller needs ResourceMark
 877 // If the class loader's _name has not been explicitly set, the class loader's
 878 // qualified class name is returned.
 879 const char* ClassLoaderData::loader_name() const {
 880    if (_class_loader_klass == NULL) {
 881      return BOOTSTRAP_LOADER_NAME;
 882    } else if (_name != NULL) {
 883      return _name->as_C_string();
 884    } else {
 885      return _class_loader_klass->external_name();
 886    }
 887 }
 888 
 889 // Caller needs ResourceMark
 890 // Format of the _name_and_id is as follows:
 891 //   If the defining loader has a name explicitly set then '<loader-name>' @<id>


 893 //   If built-in loader, then omit '@<id>' as there is only one instance.
 894 const char* ClassLoaderData::loader_name_and_id() const {
 895   if (_class_loader_klass == NULL) {
 896     return "'" BOOTSTRAP_LOADER_NAME "'";
 897   } else if (_name_and_id != NULL) {
 898     return _name_and_id->as_C_string();
 899   } else {
 900     // May be called in a race before _name_and_id is initialized.
 901     return _class_loader_klass->external_name();
 902   }
 903 }
 904 
 905 void ClassLoaderData::print_value_on(outputStream* out) const {
 906   if (!is_unloading() && class_loader() != NULL) {
 907     out->print("loader data: " INTPTR_FORMAT " for instance ", p2i(this));
 908     class_loader()->print_value_on(out);  // includes loader_name_and_id() and address of class loader instance
 909   } else {
 910     // loader data: 0xsomeaddr of 'bootstrap'
 911     out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name_and_id());
 912   }
 913   if (is_unsafe_anonymous()) {
 914     out->print(" unsafe anonymous");
 915   }
 916 }
 917 
 918 #ifndef PRODUCT
 919 void ClassLoaderData::print_on(outputStream* out) const {
 920   out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: %s {",
 921               p2i(this), p2i(_class_loader.ptr_raw()), loader_name_and_id());
 922   if (is_unsafe_anonymous()) out->print(" unsafe anonymous");
 923   if (claimed()) out->print(" claimed");
 924   if (is_unloading()) out->print(" unloading");
 925   out->print(" metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
 926 
 927   if (_jmethod_ids != NULL) {
 928     Method::print_jmethod_ids(this, out);
 929   }
 930   out->print(" handles count %d", _handles.count());
 931   out->print(" dependencies %d", _dependency_count);
 932   out->print_cr("}");
 933 }
 934 #endif // PRODUCT
 935 
 936 void ClassLoaderData::verify() {
 937   assert_locked_or_safepoint(_metaspace_lock);
 938   oop cl = class_loader();
 939 
 940   guarantee(this == class_loader_data(cl) || is_unsafe_anonymous(), "Must be the same");
 941   guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_unsafe_anonymous(), "must be");
 942 
 943   // Verify the integrity of the allocated space.
 944   if (metaspace_or_null() != NULL) {
 945     metaspace_or_null()->verify();
 946   }
 947 
 948   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
 949     guarantee(k->class_loader_data() == this, "Must be the same");
 950     k->verify();
 951     assert(k != k->next_link(), "no loops!");
 952   }
 953 }
 954 
 955 bool ClassLoaderData::contains_klass(Klass* klass) {
 956   // Lock-free access requires load_acquire
 957   for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
 958     if (k == klass) return true;
 959   }
 960   return false;
 961 }


 112       _name = SymbolTable::new_symbol(cl_instance_name, CATCH);
 113     }
 114   }
 115 
 116   // Obtain the class loader's name and identity hash.  If the class loader's
 117   // name was not explicitly set during construction, the class loader's name and id
 118   // will be set to the qualified class name of the class loader along with its
 119   // identity hash.
 120   // If for some reason the ClassLoader's constructor has not been run, instead of
 121   // leaving the _name_and_id field null, fall back to the external qualified class
 122   // name.  Thus CLD's _name_and_id field should never have a null value.
 123   oop cl_name_and_id = java_lang_ClassLoader::nameAndId(class_loader());
 124   const char* cl_instance_name_and_id =
 125                   (cl_name_and_id == NULL) ? _class_loader_klass->external_name() :
 126                                              java_lang_String::as_utf8_string(cl_name_and_id);
 127   assert(cl_instance_name_and_id != NULL && cl_instance_name_and_id[0] != '\0', "class loader has no name and id");
 128   // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
 129   _name_and_id = SymbolTable::new_symbol(cl_instance_name_and_id, CATCH);
 130 }
 131 
 132 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_shortlived) :
 133   _metaspace(NULL),
 134   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
 135                             Monitor::_safepoint_check_never)),
 136   _unloading(false), _is_shortlived(is_shortlived),
 137   _modified_oops(true), _accumulated_modified_oops(false),
 138   // An unsafe anonymous class loader data doesn't have anything to keep
 139   // it from being unloaded during parsing of the unsafe anonymous class.
 140   // The null-class-loader should always be kept alive.
 141   _keep_alive((is_shortlived || h_class_loader.is_null()) ? 1 : 0),
 142   _claim(0),
 143   _handles(),
 144   _klasses(NULL), _packages(NULL), _modules(NULL), _unnamed_module(NULL), _dictionary(NULL),
 145   _jmethod_ids(NULL),
 146   _deallocate_list(NULL),
 147   _next(NULL),
 148   _class_loader_klass(NULL), _name(NULL), _name_and_id(NULL) {
 149 
 150   if (!h_class_loader.is_null()) {
 151     _class_loader = _handles.add(h_class_loader());
 152     _class_loader_klass = h_class_loader->klass();
 153     initialize_name(h_class_loader);
 154   }
 155 
 156   if (!is_shortlived) {
 157     // The holder is initialized later for weak nonfindable and unsafe anonymous classes,
 158     // and before calling anything that call class_loader().
 159     initialize_holder(h_class_loader);
 160 
 161     // A ClassLoaderData created solely for an weak nonfindable or unsafe anonymous class should
 162     // never have a ModuleEntryTable or PackageEntryTable created for it. The defining package
 163     // and module for an unsafe anonymous class will be found in its host class.
 164     _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
 165     if (h_class_loader.is_null()) {
 166       // Create unnamed module for boot loader
 167       _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
 168     } else {
 169       // Create unnamed module for all other loaders
 170       _unnamed_module = ModuleEntry::create_unnamed_module(this);
 171     }
 172     _dictionary = create_dictionary();
 173   }
 174 
 175   NOT_PRODUCT(_dependency_count = 0); // number of class loader dependencies
 176 
 177   JFR_ONLY(INIT_ID(this);)
 178 }
 179 
 180 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
 181   Chunk* c = _head;
 182   while (c != NULL) {


 264     }
 265     chunk = chunk->_next;
 266   }
 267   return false;
 268 }
 269 #endif // PRODUCT
 270 
 271 bool ClassLoaderData::try_claim(int claim) {
 272   for (;;) {
 273     int old_claim = Atomic::load(&_claim);
 274     if ((old_claim & claim) == claim) {
 275       return false;
 276     }
 277     int new_claim = old_claim | claim;
 278     if (Atomic::cmpxchg(new_claim, &_claim, old_claim) == old_claim) {
 279       return true;
 280     }
 281   }
 282 }
 283 
 284 // Weak nonfindable and unsafe anonymous classes have their own ClassLoaderData that is marked to keep alive
 285 // while the class is being parsed, and if the class appears on the module fixup list.
 286 // Due to the uniqueness that no other class shares the nonfindable or unsafe anonymous class' name or
 287 // ClassLoaderData, no other non-GC thread has knowledge of the nonfindable or unsafe anonymous class while
 288 // it is being defined, therefore _keep_alive is not volatile or atomic.
 289 void ClassLoaderData::inc_keep_alive() {
 290   if (is_shortlived()) {
 291     assert(_keep_alive > 0, "Invalid keep alive increment count");
 292     _keep_alive++;
 293   }
 294 }
 295 
 296 void ClassLoaderData::dec_keep_alive() {
 297   if (is_shortlived()) {
 298     assert(_keep_alive > 0, "Invalid keep alive decrement count");
 299     _keep_alive--;
 300   }
 301 }
 302 
 303 void ClassLoaderData::oops_do(OopClosure* f, int claim_value, bool clear_mod_oops) {
 304   if (claim_value != ClassLoaderData::_claim_none && !try_claim(claim_value)) {
 305     return;
 306   }
 307 
 308   // Only clear modified_oops after the ClassLoaderData is claimed.
 309   if (clear_mod_oops) {
 310     clear_modified_oops();
 311   }
 312 
 313   _handles.oops_do(f);
 314 }
 315 
 316 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
 317   // Lock-free access requires load_acquire


 384   if (_packages != NULL) {
 385     for (int i = 0; i < _packages->table_size(); i++) {
 386       for (PackageEntry* entry = _packages->bucket(i);
 387            entry != NULL;
 388            entry = entry->next()) {
 389         f(entry);
 390       }
 391     }
 392   }
 393 }
 394 
 395 void ClassLoaderData::record_dependency(const Klass* k) {
 396   assert(k != NULL, "invariant");
 397 
 398   ClassLoaderData * const from_cld = this;
 399   ClassLoaderData * const to_cld = k->class_loader_data();
 400 
 401   // Do not need to record dependency if the dependency is to a class whose
 402   // class loader data is never freed.  (i.e. the dependency's class loader
 403   // is one of the three builtin class loaders and the dependency is not
 404   // short-lived.)
 405   if (to_cld->is_permanent_class_loader_data()) {
 406     return;
 407   }
 408 
 409   oop to;
 410   if (to_cld->is_shortlived()) {
 411     // Just return if a weak nonfindable or unsafe anonymous class is attempting to record a dependency
 412     // to itself.  (Note that every weak nonfindable or unsafe anonymous class has its own unique class
 413     // loader data.)
 414     if (to_cld == from_cld) {
 415       return;
 416     }
 417     // Nonfindable and unsafe anonymous class dependencies are through the mirror.
 418     to = k->java_mirror();
 419   } else {
 420     to = to_cld->class_loader();
 421     oop from = from_cld->class_loader();
 422 
 423     // Just return if this dependency is to a class with the same or a parent
 424     // class_loader.
 425     if (oopDesc::equals(from, to) || java_lang_ClassLoader::isAncestor(from, to)) {
 426       return; // this class loader is in the parent list, no need to add it.
 427     }
 428   }
 429 
 430   // It's a dependency we won't find through GC, add it.
 431   if (!_handles.contains(to)) {
 432     NOT_PRODUCT(Atomic::inc(&_dependency_count));
 433     LogTarget(Trace, class, loader, data) lt;
 434     if (lt.is_enabled()) {
 435       ResourceMark rm;
 436       LogStream ls(lt);
 437       ls.print("adding dependency from ");


 545   if (modules == NULL) {
 546     MutexLocker m1(Module_lock);
 547     // Check if _modules got allocated while we were waiting for this lock.
 548     if ((modules = _modules) == NULL) {
 549       modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
 550 
 551       {
 552         MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
 553         // Ensure _modules is stable, since it is examined without a lock
 554         OrderAccess::release_store(&_modules, modules);
 555       }
 556     }
 557   }
 558   return modules;
 559 }
 560 
 561 const int _boot_loader_dictionary_size    = 1009;
 562 const int _default_loader_dictionary_size = 107;
 563 
 564 Dictionary* ClassLoaderData::create_dictionary() {
 565   assert(!is_shortlived(), "short lived class loader data do not have a dictionary");
 566   int size;
 567   bool resizable = false;
 568   if (_the_null_class_loader_data == NULL) {
 569     size = _boot_loader_dictionary_size;
 570     resizable = true;
 571   } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
 572     size = 1;  // there's only one class in relection class loader and no initiated classes
 573   } else if (is_system_class_loader_data()) {
 574     size = _boot_loader_dictionary_size;
 575     resizable = true;
 576   } else {
 577     size = _default_loader_dictionary_size;
 578     resizable = true;
 579   }
 580   if (!DynamicallyResizeSystemDictionaries || DumpSharedSpaces) {
 581     resizable = false;
 582   }
 583   return new Dictionary(this, size, resizable);
 584 }
 585 


 591   // to get notified about this potential resurrection, otherwise the marking
 592   // might not find the object.
 593   if (!_holder.is_null()) {  // NULL class_loader
 594     return _holder.resolve();
 595   } else {
 596     return NULL;
 597   }
 598 }
 599 
 600 // Let the GC read the holder without keeping it alive.
 601 oop ClassLoaderData::holder_no_keepalive() const {
 602   if (!_holder.is_null()) {  // NULL class_loader
 603     return _holder.peek();
 604   } else {
 605     return NULL;
 606   }
 607 }
 608 
 609 // Unloading support
 610 bool ClassLoaderData::is_alive() const {
 611   bool alive = keep_alive()         // null class loader and incomplete weak nonfindable or unsafe anonymous klasses.
 612       || (_holder.peek() != NULL);  // and not cleaned by the GC weak handle processing.
 613 
 614   return alive;
 615 }
 616 
 617 class ReleaseKlassClosure: public KlassClosure {
 618 private:
 619   size_t  _instance_class_released;
 620   size_t  _array_class_released;
 621 public:
 622   ReleaseKlassClosure() : _instance_class_released(0), _array_class_released(0) { }
 623 
 624   size_t instance_class_released() const { return _instance_class_released; }
 625   size_t array_class_released()    const { return _array_class_released;    }
 626 
 627   void do_klass(Klass* k) {
 628     if (k->is_array_klass()) {
 629       _array_class_released ++;
 630     } else {
 631       assert(k->is_instance_klass(), "Must be");


 689   }
 690   // Delete lock
 691   delete _metaspace_lock;
 692 
 693   // Delete free list
 694   if (_deallocate_list != NULL) {
 695     delete _deallocate_list;
 696   }
 697 
 698   // Decrement refcounts of Symbols if created.
 699   if (_name != NULL) {
 700     _name->decrement_refcount();
 701   }
 702   if (_name_and_id != NULL) {
 703     _name_and_id->decrement_refcount();
 704   }
 705 }
 706 
 707 // Returns true if this class loader data is for the app class loader
 708 // or a user defined system class loader.  (Note that the class loader
 709 // data may be short-lived.)
 710 bool ClassLoaderData::is_system_class_loader_data() const {
 711   return SystemDictionary::is_system_class_loader(class_loader());
 712 }
 713 
 714 // Returns true if this class loader data is for the platform class loader.
 715 // (Note that the class loader data may be short-lived.)
 716 bool ClassLoaderData::is_platform_class_loader_data() const {
 717   return SystemDictionary::is_platform_class_loader(class_loader());
 718 }
 719 
 720 // Returns true if the class loader for this class loader data is one of
 721 // the 3 builtin (boot application/system or platform) class loaders,
 722 // including a user-defined system class loader.  Note that if the class
 723 // loader data is for a weak nonfindable or unsafe anonymous class then it may
 724 // get freed by a GC even if its class loader is one of these loaders.
 725 bool ClassLoaderData::is_builtin_class_loader_data() const {
 726   return (is_boot_class_loader_data() ||
 727           SystemDictionary::is_system_class_loader(class_loader()) ||
 728           SystemDictionary::is_platform_class_loader(class_loader()));
 729 }
 730 
 731 // Returns true if this class loader data is a class loader data
 732 // that is not ever freed by a GC.  It must be the CLD for one of the builtin
 733 // class loaders and not the CLD for a weak nonfindable or unsafe anonymous class.
 734 bool ClassLoaderData::is_permanent_class_loader_data() const {
 735   return is_builtin_class_loader_data() && !is_shortlived();
 736 }
 737 
 738 ClassLoaderMetaspace* ClassLoaderData::metaspace_non_null() {
 739   // If the metaspace has not been allocated, create a new one.  Might want
 740   // to create smaller arena for Reflection class loaders also.
 741   // The reason for the delayed allocation is because some class loaders are
 742   // simply for delegating with no metadata of their own.
 743   // Lock-free access requires load_acquire.
 744   ClassLoaderMetaspace* metaspace = OrderAccess::load_acquire(&_metaspace);
 745   if (metaspace == NULL) {
 746     MutexLockerEx ml(_metaspace_lock,  Mutex::_no_safepoint_check_flag);
 747     // Check if _metaspace got allocated while we were waiting for this lock.
 748     if ((metaspace = _metaspace) == NULL) {
 749       if (this == the_null_class_loader_data()) {
 750         assert (class_loader() == NULL, "Must be");
 751         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::BootMetaspaceType);
 752       } else if (is_shortlived()) {
 753         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ShortLivedMetaspaceType);
 754       } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
 755         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType);
 756       } else {
 757         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::StandardMetaspaceType);
 758       }
 759       // Ensure _metaspace is stable, since it is examined without a lock
 760       OrderAccess::release_store(&_metaspace, metaspace);
 761     }
 762   }
 763   return metaspace;
 764 }
 765 
 766 OopHandle ClassLoaderData::add_handle(Handle h) {
 767   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 768   record_modified_oops();
 769   return OopHandle(_handles.add(h()));
 770 }
 771 
 772 void ClassLoaderData::remove_handle(OopHandle h) {
 773   assert(!is_unloading(), "Do not remove a handle for a CLD that is unloading");


 850   if (_deallocate_list == NULL) {
 851     return;
 852   }
 853   // Go backwards because this removes entries that are freed.
 854   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
 855     Metadata* m = _deallocate_list->at(i);
 856     _deallocate_list->remove_at(i);
 857     if (m->is_constantPool()) {
 858       ((ConstantPool*)m)->release_C_heap_structures();
 859     } else if (m->is_klass()) {
 860       InstanceKlass* ik = (InstanceKlass*)m;
 861       // also releases ik->constants() C heap memory
 862       InstanceKlass::release_C_heap_structures(ik);
 863       // Remove the class so unloading events aren't triggered for
 864       // this class (scratch or error class) in do_unloading().
 865       remove_class(ik);
 866     }
 867   }
 868 }
 869 
 870 // These CLDs are to contain weak nonfindable or unsafe anonymous classes used for JSR292
 871 ClassLoaderData* ClassLoaderData::shortlived_class_loader_data(Handle loader) {
 872   // Add a new class loader data to the graph.
 873   return ClassLoaderDataGraph::add(loader, true);
 874 }
 875 
 876 // Caller needs ResourceMark
 877 // If the class loader's _name has not been explicitly set, the class loader's
 878 // qualified class name is returned.
 879 const char* ClassLoaderData::loader_name() const {
 880    if (_class_loader_klass == NULL) {
 881      return BOOTSTRAP_LOADER_NAME;
 882    } else if (_name != NULL) {
 883      return _name->as_C_string();
 884    } else {
 885      return _class_loader_klass->external_name();
 886    }
 887 }
 888 
 889 // Caller needs ResourceMark
 890 // Format of the _name_and_id is as follows:
 891 //   If the defining loader has a name explicitly set then '<loader-name>' @<id>


 893 //   If built-in loader, then omit '@<id>' as there is only one instance.
 894 const char* ClassLoaderData::loader_name_and_id() const {
 895   if (_class_loader_klass == NULL) {
 896     return "'" BOOTSTRAP_LOADER_NAME "'";
 897   } else if (_name_and_id != NULL) {
 898     return _name_and_id->as_C_string();
 899   } else {
 900     // May be called in a race before _name_and_id is initialized.
 901     return _class_loader_klass->external_name();
 902   }
 903 }
 904 
 905 void ClassLoaderData::print_value_on(outputStream* out) const {
 906   if (!is_unloading() && class_loader() != NULL) {
 907     out->print("loader data: " INTPTR_FORMAT " for instance ", p2i(this));
 908     class_loader()->print_value_on(out);  // includes loader_name_and_id() and address of class loader instance
 909   } else {
 910     // loader data: 0xsomeaddr of 'bootstrap'
 911     out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name_and_id());
 912   }
 913   if (_is_shortlived) {
 914     out->print(" short-lived");
 915   }
 916 }
 917 
 918 #ifndef PRODUCT
 919 void ClassLoaderData::print_on(outputStream* out) const {
 920   out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: %s {",
 921               p2i(this), p2i(_class_loader.ptr_raw()), loader_name_and_id());
 922   if (is_shortlived()) out->print(" short-lived");
 923   if (claimed()) out->print(" claimed");
 924   if (is_unloading()) out->print(" unloading");
 925   out->print(" metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
 926 
 927   if (_jmethod_ids != NULL) {
 928     Method::print_jmethod_ids(this, out);
 929   }
 930   out->print(" handles count %d", _handles.count());
 931   out->print(" dependencies %d", _dependency_count);
 932   out->print_cr("}");
 933 }
 934 #endif // PRODUCT
 935 
 936 void ClassLoaderData::verify() {
 937   assert_locked_or_safepoint(_metaspace_lock);
 938   oop cl = class_loader();
 939 
 940   guarantee(this == class_loader_data(cl) || is_shortlived(), "Must be the same");
 941   guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_shortlived(), "must be");
 942 
 943   // Verify the integrity of the allocated space.
 944   if (metaspace_or_null() != NULL) {
 945     metaspace_or_null()->verify();
 946   }
 947 
 948   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
 949     guarantee(k->class_loader_data() == this, "Must be the same");
 950     k->verify();
 951     assert(k != k->next_link(), "no loops!");
 952   }
 953 }
 954 
 955 bool ClassLoaderData::contains_klass(Klass* klass) {
 956   // Lock-free access requires load_acquire
 957   for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
 958     if (k == klass) return true;
 959   }
 960   return false;
 961 }
< prev index next >