80 // helper function to avoid in-line casts 81 template <typename T> static T* load_ptr_acquire(T* volatile *p) { 82 return static_cast<T*>(OrderAccess::load_ptr_acquire(p)); 83 } 84 85 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL; 86 87 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) : 88 _class_loader(h_class_loader()), 89 _is_anonymous(is_anonymous), 90 // An anonymous class loader data doesn't have anything to keep 91 // it from being unloaded during parsing of the anonymous class. 92 // The null-class-loader should always be kept alive. 93 _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0), 94 _metaspace(NULL), _unloading(false), _klasses(NULL), 95 _modules(NULL), _packages(NULL), 96 _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL), 97 _next(NULL), _dependencies(dependencies), 98 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true, 99 Monitor::_safepoint_check_never)) { 100 TRACE_INIT_ID(this); 101 } 102 103 void ClassLoaderData::init_dependencies(TRAPS) { 104 assert(!Universe::is_fully_initialized(), "should only be called when initializing"); 105 assert(is_the_null_class_loader_data(), "should only call this for the null class loader"); 106 _dependencies.init(CHECK); 107 } 108 109 void ClassLoaderData::Dependencies::init(TRAPS) { 110 // Create empty dependencies array to add to. CMS requires this to be 111 // an oop so that it can track additions via card marks. We think. 112 _list_head = oopFactory::new_objectArray(2, CHECK); 113 } 114 115 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() { 116 Chunk* c = _head; 117 while (c != NULL) { 118 Chunk* next = c->_next; 119 delete c; 259 for (Klass* k = _klasses; k != NULL; k = k->next_link()) { 260 // Do not filter ArrayKlass oops here... 261 if (k->is_array_klass() || (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded())) { 262 klass_closure->do_klass(k); 263 } 264 } 265 } 266 267 void ClassLoaderData::classes_do(void f(InstanceKlass*)) { 268 // Lock-free access requires load_ptr_acquire 269 for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) { 270 if (k->is_instance_klass()) { 271 f(InstanceKlass::cast(k)); 272 } 273 assert(k != k->next_link(), "no loops!"); 274 } 275 } 276 277 void ClassLoaderData::modules_do(void f(ModuleEntry*)) { 278 assert_locked_or_safepoint(Module_lock); 279 if (_modules != NULL) { 280 for (int i = 0; i < _modules->table_size(); i++) { 281 for (ModuleEntry* entry = _modules->bucket(i); 282 entry != NULL; 283 entry = entry->next()) { 284 f(entry); 285 } 286 } 287 } 288 } 289 290 void ClassLoaderData::packages_do(void f(PackageEntry*)) { 291 // Lock-free access requires load_ptr_acquire 292 PackageEntryTable* packages = load_ptr_acquire(&_packages); 293 if (packages != NULL) { 294 for (int i = 0; i < packages->table_size(); i++) { 295 for (PackageEntry* entry = packages->bucket(i); 296 entry != NULL; 297 entry = entry->next()) { 298 f(entry); 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 // Lazily create the module entry table at first request. 497 // Lock-free access requires load_ptr_acquire. 498 ModuleEntryTable* modules = load_ptr_acquire(&_modules); 499 if (modules == NULL) { 500 MutexLocker m1(Module_lock); 501 // Check if _modules got allocated while we were waiting for this lock. 502 if ((modules = _modules) == NULL) { 503 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 509 { 510 MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag); 511 // Ensure _modules is stable, since it is examined without a lock 512 OrderAccess::release_store_ptr(&_modules, modules); 513 } 514 } 515 } 516 return modules; 517 } 518 519 oop ClassLoaderData::keep_alive_object() const { 520 assert_locked_or_safepoint(_metaspace_lock); 521 assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive"); 522 return is_anonymous() ? _klasses->java_mirror() : class_loader(); 523 } 524 525 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const { 526 bool alive = keep_alive() // null class loader and incomplete anonymous klasses. 527 || is_alive_closure->do_object_b(keep_alive_object()); 528 529 return alive; 530 } 531 532 533 ClassLoaderData::~ClassLoaderData() { 534 // Release C heap structures for all the classes. 535 classes_do(InstanceKlass::release_C_heap_structures); 536 537 // Release C heap allocated hashtable for all the packages. 538 if (_packages != NULL) { 539 // Destroy the table itself 540 delete _packages; 541 _packages = NULL; 542 } 543 544 // Release C heap allocated hashtable for all the modules. 545 if (_modules != NULL) { 546 // Destroy the table itself 547 delete _modules; 548 _modules = NULL; 549 } 550 551 // release the metaspace 552 Metaspace *m = _metaspace; 553 if (m != NULL) { 554 _metaspace = NULL; 555 delete m; 556 } 557 // Clear all the JNI handles for methods 558 // These aren't deallocated and are going to look like a leak, but that's 559 // needed because we can't really get rid of jmethodIDs because we don't 560 // know when native code is going to stop using them. The spec says that 561 // they're "invalid" but existing programs likely rely on their being 562 // NULL after class unloading. 563 if (_jmethod_ids != NULL) { 564 Method::clear_jmethod_ids(this); 565 } 566 // Delete lock 567 delete _metaspace_lock; 568 | 80 // helper function to avoid in-line casts 81 template <typename T> static T* load_ptr_acquire(T* volatile *p) { 82 return static_cast<T*>(OrderAccess::load_ptr_acquire(p)); 83 } 84 85 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL; 86 87 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) : 88 _class_loader(h_class_loader()), 89 _is_anonymous(is_anonymous), 90 // An anonymous class loader data doesn't have anything to keep 91 // it from being unloaded during parsing of the anonymous class. 92 // The null-class-loader should always be kept alive. 93 _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0), 94 _metaspace(NULL), _unloading(false), _klasses(NULL), 95 _modules(NULL), _packages(NULL), 96 _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL), 97 _next(NULL), _dependencies(dependencies), 98 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true, 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 if (h_class_loader.is_null()) { 106 // Create unnamed module for boot loader 107 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this); 108 } else { 109 // Create unnamed module for all other loaders 110 _unnamed_module = ModuleEntry::create_unnamed_module(this); 111 } 112 } else { 113 _unnamed_module = NULL; 114 } 115 TRACE_INIT_ID(this); 116 } 117 118 void ClassLoaderData::init_dependencies(TRAPS) { 119 assert(!Universe::is_fully_initialized(), "should only be called when initializing"); 120 assert(is_the_null_class_loader_data(), "should only call this for the null class loader"); 121 _dependencies.init(CHECK); 122 } 123 124 void ClassLoaderData::Dependencies::init(TRAPS) { 125 // Create empty dependencies array to add to. CMS requires this to be 126 // an oop so that it can track additions via card marks. We think. 127 _list_head = oopFactory::new_objectArray(2, CHECK); 128 } 129 130 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() { 131 Chunk* c = _head; 132 while (c != NULL) { 133 Chunk* next = c->_next; 134 delete c; 274 for (Klass* k = _klasses; k != NULL; k = k->next_link()) { 275 // Do not filter ArrayKlass oops here... 276 if (k->is_array_klass() || (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded())) { 277 klass_closure->do_klass(k); 278 } 279 } 280 } 281 282 void ClassLoaderData::classes_do(void f(InstanceKlass*)) { 283 // Lock-free access requires load_ptr_acquire 284 for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) { 285 if (k->is_instance_klass()) { 286 f(InstanceKlass::cast(k)); 287 } 288 assert(k != k->next_link(), "no loops!"); 289 } 290 } 291 292 void ClassLoaderData::modules_do(void f(ModuleEntry*)) { 293 assert_locked_or_safepoint(Module_lock); 294 if (_unnamed_module != NULL) { 295 f(_unnamed_module); 296 } 297 if (_modules != NULL) { 298 for (int i = 0; i < _modules->table_size(); i++) { 299 for (ModuleEntry* entry = _modules->bucket(i); 300 entry != NULL; 301 entry = entry->next()) { 302 f(entry); 303 } 304 } 305 } 306 } 307 308 void ClassLoaderData::packages_do(void f(PackageEntry*)) { 309 // Lock-free access requires load_ptr_acquire 310 PackageEntryTable* packages = load_ptr_acquire(&_packages); 311 if (packages != NULL) { 312 for (int i = 0; i < packages->table_size(); i++) { 313 for (PackageEntry* entry = packages->bucket(i); 314 entry != NULL; 315 entry = entry->next()) { 316 f(entry); 502 MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag); 503 // Check if _packages got allocated while we were waiting for this lock. 504 if ((packages = _packages) == NULL) { 505 packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size); 506 // Ensure _packages is stable, since it is examined without a lock 507 OrderAccess::release_store_ptr(&_packages, packages); 508 } 509 } 510 return packages; 511 } 512 513 ModuleEntryTable* ClassLoaderData::modules() { 514 // Lazily create the module entry table at first request. 515 // Lock-free access requires load_ptr_acquire. 516 ModuleEntryTable* modules = load_ptr_acquire(&_modules); 517 if (modules == NULL) { 518 MutexLocker m1(Module_lock); 519 // Check if _modules got allocated while we were waiting for this lock. 520 if ((modules = _modules) == NULL) { 521 modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size); 522 523 { 524 MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag); 525 // Ensure _modules is stable, since it is examined without a lock 526 OrderAccess::release_store_ptr(&_modules, modules); 527 } 528 } 529 } 530 return modules; 531 } 532 533 oop ClassLoaderData::keep_alive_object() const { 534 assert_locked_or_safepoint(_metaspace_lock); 535 assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive"); 536 return is_anonymous() ? _klasses->java_mirror() : class_loader(); 537 } 538 539 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const { 540 bool alive = keep_alive() // null class loader and incomplete anonymous klasses. 541 || is_alive_closure->do_object_b(keep_alive_object()); 542 543 return alive; 544 } 545 546 ClassLoaderData::~ClassLoaderData() { 547 // Release C heap structures for all the classes. 548 classes_do(InstanceKlass::release_C_heap_structures); 549 550 // Release C heap allocated hashtable for all the packages. 551 if (_packages != NULL) { 552 // Destroy the table itself 553 delete _packages; 554 _packages = NULL; 555 } 556 557 // Release C heap allocated hashtable for all the modules. 558 if (_modules != NULL) { 559 // Destroy the table itself 560 delete _modules; 561 _modules = NULL; 562 } 563 564 if (_unnamed_module != NULL) { 565 FREE_C_HEAP_ARRAY(char, _unnamed_module); 566 _unnamed_module = NULL; 567 } 568 569 // release the metaspace 570 Metaspace *m = _metaspace; 571 if (m != NULL) { 572 _metaspace = NULL; 573 delete m; 574 } 575 // Clear all the JNI handles for methods 576 // These aren't deallocated and are going to look like a leak, but that's 577 // needed because we can't really get rid of jmethodIDs because we don't 578 // know when native code is going to stop using them. The spec says that 579 // they're "invalid" but existing programs likely rely on their being 580 // NULL after class unloading. 581 if (_jmethod_ids != NULL) { 582 Method::clear_jmethod_ids(this); 583 } 584 // Delete lock 585 delete _metaspace_lock; 586 |