29 #include "classfile/systemDictionary.hpp" 30 #include "classfile/systemDictionaryShared.hpp" 31 #include "memory/iterator.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "runtime/orderAccess.inline.hpp" 35 #include "utilities/hashtable.inline.hpp" 36 37 DictionaryEntry* Dictionary::_current_class_entry = NULL; 38 int Dictionary::_current_class_index = 0; 39 40 size_t Dictionary::entry_size() { 41 if (DumpSharedSpaces) { 42 return SystemDictionaryShared::dictionary_entry_size(); 43 } else { 44 return sizeof(DictionaryEntry); 45 } 46 } 47 48 Dictionary::Dictionary(int table_size) 49 : TwoOopHashtable<Klass*, mtClass>(table_size, (int)entry_size()) { 50 _current_class_index = 0; 51 _current_class_entry = NULL; 52 _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize); 53 }; 54 55 56 Dictionary::Dictionary(int table_size, HashtableBucket<mtClass>* t, 57 int number_of_entries) 58 : TwoOopHashtable<Klass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries) { 59 _current_class_index = 0; 60 _current_class_entry = NULL; 61 _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize); 62 }; 63 64 ProtectionDomainCacheEntry* Dictionary::cache_get(Handle protection_domain) { 65 return _pd_cache_table->get(protection_domain); 66 } 67 68 DictionaryEntry* Dictionary::new_entry(unsigned int hash, Klass* klass, 69 ClassLoaderData* loader_data) { 70 DictionaryEntry* entry = (DictionaryEntry*)Hashtable<Klass*, mtClass>::new_entry(hash, klass); 71 entry->set_loader_data(loader_data); 72 entry->set_pd_set(NULL); 73 assert(klass->is_instance_klass(), "Must be"); 74 if (DumpSharedSpaces) { 75 SystemDictionaryShared::init_shared_dictionary_entry(klass, entry); 76 } 77 return entry; 78 } 79 80 81 void Dictionary::free_entry(DictionaryEntry* entry) { 82 // avoid recursion when deleting linked list 83 while (entry->pd_set() != NULL) { 84 ProtectionDomainEntry* to_delete = entry->pd_set(); 85 entry->set_pd_set(to_delete->next()); 86 delete to_delete; 87 } 88 Hashtable<Klass*, mtClass>::free_entry(entry); 89 } 90 91 92 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const { 93 #ifdef ASSERT 94 if (protection_domain == klass()->protection_domain()) { 95 // Ensure this doesn't show up in the pd_set (invariant) 96 bool in_pd_set = false; 97 for (ProtectionDomainEntry* current = _pd_set; 98 current != NULL; 99 current = current->next()) { 100 if (current->protection_domain() == protection_domain) { 101 in_pd_set = true; 102 break; 103 } 104 } 105 if (in_pd_set) { 106 assert(false, "A klass's protection domain should not show up " 107 "in its sys. dict. PD set"); 108 } 334 void Dictionary::methods_do(void f(Method*)) { 335 for (int index = 0; index < table_size(); index++) { 336 for (DictionaryEntry* probe = bucket(index); 337 probe != NULL; 338 probe = probe->next()) { 339 Klass* k = probe->klass(); 340 if (probe->loader_data() == k->class_loader_data()) { 341 // only take klass is we have the entry with the defining class loader 342 InstanceKlass::cast(k)->methods_do(f); 343 } 344 } 345 } 346 } 347 348 void Dictionary::unlink(BoolObjectClosure* is_alive) { 349 // Only the protection domain cache table may contain references to the heap 350 // that need to be unlinked. 351 _pd_cache_table->unlink(is_alive); 352 } 353 354 Klass* Dictionary::try_get_next_class() { 355 while (true) { 356 if (_current_class_entry != NULL) { 357 Klass* k = _current_class_entry->klass(); 358 _current_class_entry = _current_class_entry->next(); 359 return k; 360 } 361 _current_class_index = (_current_class_index + 1) % table_size(); 362 _current_class_entry = bucket(_current_class_index); 363 } 364 // never reached 365 } 366 367 // Add a loaded class to the system dictionary. 368 // Readers of the SystemDictionary aren't always locked, so _buckets 369 // is volatile. The store of the next field in the constructor is 370 // also cast to volatile; we do this to ensure store order is maintained 371 // by the compilers. 372 373 void Dictionary::add_klass(Symbol* class_name, ClassLoaderData* loader_data, 374 KlassHandle obj) { 375 assert_locked_or_safepoint(SystemDictionary_lock); 376 assert(obj() != NULL, "adding NULL obj"); 377 assert(obj()->name() == class_name, "sanity check on name"); 378 assert(loader_data != NULL, "Must be non-NULL"); 379 380 unsigned int hash = compute_hash(class_name, loader_data); 381 int index = hash_to_index(hash); 382 DictionaryEntry* entry = new_entry(hash, obj(), loader_data); 383 add_entry(index, entry); 384 } 385 386 387 // This routine does not lock the system dictionary. 388 // 389 // Since readers don't hold a lock, we must make sure that system 390 // dictionary entries are only removed at a safepoint (when only one 391 // thread is running), and are added to in a safe way (all links must 392 // be updated in an MT-safe manner). 393 // 394 // Callers should be aware that an entry could be added just after 395 // _buckets[index] is read here, so the caller will not see the new entry. 396 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash, 397 Symbol* class_name, 398 ClassLoaderData* loader_data) { 399 DEBUG_ONLY(_lookup_count++); 400 for (DictionaryEntry* entry = bucket(index); 401 entry != NULL; 402 entry = entry->next()) { 403 if (entry->hash() == hash && entry->equals(class_name, loader_data)) { 404 DEBUG_ONLY(bucket_count_hit(index)); 405 return entry; 406 } 407 DEBUG_ONLY(_lookup_length++); 408 } 409 return NULL; 410 } 411 412 413 Klass* Dictionary::find(int index, unsigned int hash, Symbol* name, 414 ClassLoaderData* loader_data, Handle protection_domain, TRAPS) { 415 DictionaryEntry* entry = get_entry(index, hash, name, loader_data); 416 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) { 417 return entry->klass(); 418 } else { 419 return NULL; 420 } 421 } 422 423 424 Klass* Dictionary::find_class(int index, unsigned int hash, 425 Symbol* name, ClassLoaderData* loader_data) { 426 assert_locked_or_safepoint(SystemDictionary_lock); 427 assert (index == index_for(name, loader_data), "incorrect index?"); 428 429 DictionaryEntry* entry = get_entry(index, hash, name, loader_data); 430 return (entry != NULL) ? entry->klass() : (Klass*)NULL; 431 } 432 433 434 // Variant of find_class for shared classes. No locking required, as 435 // that table is static. 436 437 Klass* Dictionary::find_shared_class(int index, unsigned int hash, 438 Symbol* name) { 439 assert (index == index_for(name, NULL), "incorrect index?"); 440 441 DictionaryEntry* entry = get_entry(index, hash, name, NULL); 442 return (entry != NULL) ? entry->klass() : (Klass*)NULL; 443 } 444 445 446 void Dictionary::add_protection_domain(int index, unsigned int hash, 447 instanceKlassHandle klass, 448 ClassLoaderData* loader_data, Handle protection_domain, 449 TRAPS) { 450 Symbol* klass_name = klass->name(); 451 DictionaryEntry* entry = get_entry(index, hash, klass_name, loader_data); 452 453 assert(entry != NULL,"entry must be present, we just created it"); 454 assert(protection_domain() != NULL, 455 "real protection domain should be present"); 456 457 entry->add_protection_domain(this, protection_domain); 458 459 assert(entry->contains_protection_domain(protection_domain()), 460 "now protection domain should be present"); 461 } 462 463 464 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash, 465 Symbol* name, 466 ClassLoaderData* loader_data, 467 Handle protection_domain) { | 29 #include "classfile/systemDictionary.hpp" 30 #include "classfile/systemDictionaryShared.hpp" 31 #include "memory/iterator.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "runtime/orderAccess.inline.hpp" 35 #include "utilities/hashtable.inline.hpp" 36 37 DictionaryEntry* Dictionary::_current_class_entry = NULL; 38 int Dictionary::_current_class_index = 0; 39 40 size_t Dictionary::entry_size() { 41 if (DumpSharedSpaces) { 42 return SystemDictionaryShared::dictionary_entry_size(); 43 } else { 44 return sizeof(DictionaryEntry); 45 } 46 } 47 48 Dictionary::Dictionary(int table_size) 49 : TwoOopHashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size()) { 50 _current_class_index = 0; 51 _current_class_entry = NULL; 52 _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize); 53 }; 54 55 56 Dictionary::Dictionary(int table_size, HashtableBucket<mtClass>* t, 57 int number_of_entries) 58 : TwoOopHashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries) { 59 _current_class_index = 0; 60 _current_class_entry = NULL; 61 _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize); 62 }; 63 64 ProtectionDomainCacheEntry* Dictionary::cache_get(Handle protection_domain) { 65 return _pd_cache_table->get(protection_domain); 66 } 67 68 DictionaryEntry* Dictionary::new_entry(unsigned int hash, InstanceKlass* klass, 69 ClassLoaderData* loader_data) { 70 DictionaryEntry* entry = (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::new_entry(hash, klass); 71 entry->set_loader_data(loader_data); 72 entry->set_pd_set(NULL); 73 assert(klass->is_instance_klass(), "Must be"); 74 if (DumpSharedSpaces) { 75 SystemDictionaryShared::init_shared_dictionary_entry(klass, entry); 76 } 77 return entry; 78 } 79 80 81 void Dictionary::free_entry(DictionaryEntry* entry) { 82 // avoid recursion when deleting linked list 83 while (entry->pd_set() != NULL) { 84 ProtectionDomainEntry* to_delete = entry->pd_set(); 85 entry->set_pd_set(to_delete->next()); 86 delete to_delete; 87 } 88 Hashtable<InstanceKlass*, mtClass>::free_entry(entry); 89 } 90 91 92 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const { 93 #ifdef ASSERT 94 if (protection_domain == klass()->protection_domain()) { 95 // Ensure this doesn't show up in the pd_set (invariant) 96 bool in_pd_set = false; 97 for (ProtectionDomainEntry* current = _pd_set; 98 current != NULL; 99 current = current->next()) { 100 if (current->protection_domain() == protection_domain) { 101 in_pd_set = true; 102 break; 103 } 104 } 105 if (in_pd_set) { 106 assert(false, "A klass's protection domain should not show up " 107 "in its sys. dict. PD set"); 108 } 334 void Dictionary::methods_do(void f(Method*)) { 335 for (int index = 0; index < table_size(); index++) { 336 for (DictionaryEntry* probe = bucket(index); 337 probe != NULL; 338 probe = probe->next()) { 339 Klass* k = probe->klass(); 340 if (probe->loader_data() == k->class_loader_data()) { 341 // only take klass is we have the entry with the defining class loader 342 InstanceKlass::cast(k)->methods_do(f); 343 } 344 } 345 } 346 } 347 348 void Dictionary::unlink(BoolObjectClosure* is_alive) { 349 // Only the protection domain cache table may contain references to the heap 350 // that need to be unlinked. 351 _pd_cache_table->unlink(is_alive); 352 } 353 354 InstanceKlass* Dictionary::try_get_next_class() { 355 while (true) { 356 if (_current_class_entry != NULL) { 357 InstanceKlass* k = _current_class_entry->klass(); 358 _current_class_entry = _current_class_entry->next(); 359 return k; 360 } 361 _current_class_index = (_current_class_index + 1) % table_size(); 362 _current_class_entry = bucket(_current_class_index); 363 } 364 // never reached 365 } 366 367 // Add a loaded class to the system dictionary. 368 // Readers of the SystemDictionary aren't always locked, so _buckets 369 // is volatile. The store of the next field in the constructor is 370 // also cast to volatile; we do this to ensure store order is maintained 371 // by the compilers. 372 373 void Dictionary::add_klass(Symbol* class_name, ClassLoaderData* loader_data, 374 InstanceKlass* obj) { 375 assert_locked_or_safepoint(SystemDictionary_lock); 376 assert(obj != NULL, "adding NULL obj"); 377 assert(obj->name() == class_name, "sanity check on name"); 378 assert(loader_data != NULL, "Must be non-NULL"); 379 380 unsigned int hash = compute_hash(class_name, loader_data); 381 int index = hash_to_index(hash); 382 DictionaryEntry* entry = new_entry(hash, obj, loader_data); 383 add_entry(index, entry); 384 } 385 386 387 // This routine does not lock the system dictionary. 388 // 389 // Since readers don't hold a lock, we must make sure that system 390 // dictionary entries are only removed at a safepoint (when only one 391 // thread is running), and are added to in a safe way (all links must 392 // be updated in an MT-safe manner). 393 // 394 // Callers should be aware that an entry could be added just after 395 // _buckets[index] is read here, so the caller will not see the new entry. 396 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash, 397 Symbol* class_name, 398 ClassLoaderData* loader_data) { 399 DEBUG_ONLY(_lookup_count++); 400 for (DictionaryEntry* entry = bucket(index); 401 entry != NULL; 402 entry = entry->next()) { 403 if (entry->hash() == hash && entry->equals(class_name, loader_data)) { 404 DEBUG_ONLY(bucket_count_hit(index)); 405 return entry; 406 } 407 DEBUG_ONLY(_lookup_length++); 408 } 409 return NULL; 410 } 411 412 413 InstanceKlass* Dictionary::find(int index, unsigned int hash, Symbol* name, 414 ClassLoaderData* loader_data, Handle protection_domain, TRAPS) { 415 DictionaryEntry* entry = get_entry(index, hash, name, loader_data); 416 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) { 417 return entry->klass(); 418 } else { 419 return NULL; 420 } 421 } 422 423 424 InstanceKlass* Dictionary::find_class(int index, unsigned int hash, 425 Symbol* name, ClassLoaderData* loader_data) { 426 assert_locked_or_safepoint(SystemDictionary_lock); 427 assert (index == index_for(name, loader_data), "incorrect index?"); 428 429 DictionaryEntry* entry = get_entry(index, hash, name, loader_data); 430 return (entry != NULL) ? entry->klass() : NULL; 431 } 432 433 434 // Variant of find_class for shared classes. No locking required, as 435 // that table is static. 436 437 InstanceKlass* Dictionary::find_shared_class(int index, unsigned int hash, 438 Symbol* name) { 439 assert (index == index_for(name, NULL), "incorrect index?"); 440 441 DictionaryEntry* entry = get_entry(index, hash, name, NULL); 442 return (entry != NULL) ? entry->klass() : NULL; 443 } 444 445 446 void Dictionary::add_protection_domain(int index, unsigned int hash, 447 InstanceKlass* klass, 448 ClassLoaderData* loader_data, Handle protection_domain, 449 TRAPS) { 450 Symbol* klass_name = klass->name(); 451 DictionaryEntry* entry = get_entry(index, hash, klass_name, loader_data); 452 453 assert(entry != NULL,"entry must be present, we just created it"); 454 assert(protection_domain() != NULL, 455 "real protection domain should be present"); 456 457 entry->add_protection_domain(this, protection_domain); 458 459 assert(entry->contains_protection_domain(protection_domain()), 460 "now protection domain should be present"); 461 } 462 463 464 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash, 465 Symbol* name, 466 ClassLoaderData* loader_data, 467 Handle protection_domain) { |