duke@435: /* coleenp@6316: * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/dictionary.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" tschatzl@5862: #include "memory/iterator.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "prims/jvmtiRedefineClassesTrace.hpp" goetz@6911: #include "runtime/orderAccess.inline.hpp" stefank@2314: #include "utilities/hashtable.inline.hpp" duke@435: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC duke@435: duke@435: DictionaryEntry* Dictionary::_current_class_entry = NULL; duke@435: int Dictionary::_current_class_index = 0; duke@435: duke@435: duke@435: Dictionary::Dictionary(int table_size) coleenp@4037: : TwoOopHashtable(table_size, sizeof(DictionaryEntry)) { duke@435: _current_class_index = 0; duke@435: _current_class_entry = NULL; tschatzl@5862: _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize); duke@435: }; duke@435: duke@435: zgu@3900: Dictionary::Dictionary(int table_size, HashtableBucket* t, duke@435: int number_of_entries) coleenp@4037: : TwoOopHashtable(table_size, sizeof(DictionaryEntry), t, number_of_entries) { duke@435: _current_class_index = 0; duke@435: _current_class_entry = NULL; tschatzl@5862: _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize); duke@435: }; duke@435: tschatzl@5862: ProtectionDomainCacheEntry* Dictionary::cache_get(oop protection_domain) { tschatzl@5862: return _pd_cache_table->get(protection_domain); tschatzl@5862: } duke@435: coleenp@4037: DictionaryEntry* Dictionary::new_entry(unsigned int hash, Klass* klass, coleenp@4037: ClassLoaderData* loader_data) { coleenp@4037: DictionaryEntry* entry = (DictionaryEntry*)Hashtable::new_entry(hash, klass); coleenp@4037: entry->set_loader_data(loader_data); duke@435: entry->set_pd_set(NULL); coleenp@4037: assert(klass->oop_is_instance(), "Must be"); duke@435: return entry; duke@435: } duke@435: duke@435: duke@435: void Dictionary::free_entry(DictionaryEntry* entry) { duke@435: // avoid recursion when deleting linked list duke@435: while (entry->pd_set() != NULL) { duke@435: ProtectionDomainEntry* to_delete = entry->pd_set(); duke@435: entry->set_pd_set(to_delete->next()); duke@435: delete to_delete; duke@435: } coleenp@4037: Hashtable::free_entry(entry); duke@435: } duke@435: duke@435: duke@435: bool DictionaryEntry::contains_protection_domain(oop protection_domain) const { duke@435: #ifdef ASSERT coleenp@4037: if (protection_domain == InstanceKlass::cast(klass())->protection_domain()) { duke@435: // Ensure this doesn't show up in the pd_set (invariant) duke@435: bool in_pd_set = false; duke@435: for (ProtectionDomainEntry* current = _pd_set; duke@435: current != NULL; duke@435: current = current->next()) { duke@435: if (current->protection_domain() == protection_domain) { duke@435: in_pd_set = true; duke@435: break; duke@435: } duke@435: } duke@435: if (in_pd_set) { duke@435: assert(false, "A klass's protection domain should not show up " duke@435: "in its sys. dict. PD set"); duke@435: } duke@435: } duke@435: #endif /* ASSERT */ duke@435: coleenp@4037: if (protection_domain == InstanceKlass::cast(klass())->protection_domain()) { duke@435: // Succeeds trivially duke@435: return true; duke@435: } duke@435: duke@435: for (ProtectionDomainEntry* current = _pd_set; duke@435: current != NULL; duke@435: current = current->next()) { duke@435: if (current->protection_domain() == protection_domain) return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: tschatzl@5862: void DictionaryEntry::add_protection_domain(Dictionary* dict, oop protection_domain) { duke@435: assert_locked_or_safepoint(SystemDictionary_lock); duke@435: if (!contains_protection_domain(protection_domain)) { tschatzl@5862: ProtectionDomainCacheEntry* entry = dict->cache_get(protection_domain); duke@435: ProtectionDomainEntry* new_head = tschatzl@5862: new ProtectionDomainEntry(entry, _pd_set); duke@435: // Warning: Preserve store ordering. The SystemDictionary is read duke@435: // without locks. The new ProtectionDomainEntry must be duke@435: // complete before other threads can be allowed to see it duke@435: // via a store to _pd_set. duke@435: OrderAccess::release_store_ptr(&_pd_set, new_head); duke@435: } duke@435: if (TraceProtectionDomainVerification && WizardMode) { duke@435: print(); duke@435: } duke@435: } duke@435: duke@435: thartmann@7064: void Dictionary::do_unloading() { jcoomes@1844: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); duke@435: duke@435: // Remove unloadable entries and classes from system dictionary duke@435: // The placeholder array has been handled in always_strong_oops_do. duke@435: DictionaryEntry* probe = NULL; thartmann@7064: for (int index = 0; index < table_size(); index++) { duke@435: for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) { duke@435: probe = *p; coleenp@4037: Klass* e = probe->klass(); coleenp@4037: ClassLoaderData* loader_data = probe->loader_data(); duke@435: coleenp@4037: InstanceKlass* ik = InstanceKlass::cast(e); duke@435: duke@435: // Non-unloadable classes were handled in always_strong_oops_do coleenp@4037: if (!is_strongly_reachable(loader_data, e)) { duke@435: // Entry was not visited in phase1 (negated test from phase1) coleenp@4037: assert(!loader_data->is_the_null_class_loader_data(), "unloading entry with null class loader"); coleenp@4037: ClassLoaderData* k_def_class_loader_data = ik->class_loader_data(); duke@435: duke@435: // Do we need to delete this system dictionary entry? duke@435: bool purge_entry = false; duke@435: duke@435: // Do we need to delete this system dictionary entry? coleenp@4037: if (loader_data->is_unloading()) { duke@435: // If the loader is not live this entry should always be thartmann@7064: // removed (will never be looked up again). duke@435: purge_entry = true; duke@435: } else { duke@435: // The loader in this entry is alive. If the klass is dead, coleenp@4037: // (determined by checking the defining class loader) duke@435: // the loader must be an initiating loader (rather than the duke@435: // defining loader). Remove this entry. coleenp@4037: if (k_def_class_loader_data->is_unloading()) { coleenp@4037: // If we get here, the class_loader_data must not be the defining duke@435: // loader, it must be an initiating one. coleenp@4037: assert(k_def_class_loader_data != loader_data, duke@435: "cannot have live defining loader and unreachable klass"); duke@435: // Loader is live, but class and its defining loader are dead. duke@435: // Remove the entry. The class is going away. duke@435: purge_entry = true; duke@435: } duke@435: } duke@435: duke@435: if (purge_entry) { duke@435: *p = probe->next(); duke@435: if (probe == _current_class_entry) { duke@435: _current_class_entry = NULL; duke@435: } duke@435: free_entry(probe); duke@435: continue; duke@435: } duke@435: } duke@435: p = probe->next_addr(); duke@435: } duke@435: } duke@435: } duke@435: stefank@6992: void Dictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) { stefank@6992: // Skip the strong roots probe marking if the closures are the same. stefank@6992: if (strong == weak) { stefank@6992: oops_do(strong); stefank@6992: return; stefank@6992: } stefank@6992: stefank@6992: for (int index = 0; index < table_size(); index++) { stefank@6992: for (DictionaryEntry *probe = bucket(index); stefank@6992: probe != NULL; stefank@6992: probe = probe->next()) { stefank@6992: Klass* e = probe->klass(); stefank@6992: ClassLoaderData* loader_data = probe->loader_data(); stefank@6992: if (is_strongly_reachable(loader_data, e)) { stefank@6992: probe->set_strongly_reachable(); stefank@6992: } stefank@6992: } stefank@6992: } stefank@6992: _pd_cache_table->roots_oops_do(strong, weak); stefank@6992: } duke@435: coleenp@4037: void Dictionary::always_strong_oops_do(OopClosure* blk) { tschatzl@5862: // Follow all system classes and temporary placeholders in dictionary; only tschatzl@5862: // protection domain oops contain references into the heap. In a first tschatzl@5862: // pass over the system dictionary determine which need to be treated as tschatzl@5862: // strongly reachable and mark them as such. duke@435: for (int index = 0; index < table_size(); index++) { duke@435: for (DictionaryEntry *probe = bucket(index); duke@435: probe != NULL; duke@435: probe = probe->next()) { coleenp@4037: Klass* e = probe->klass(); coleenp@4037: ClassLoaderData* loader_data = probe->loader_data(); coleenp@4037: if (is_strongly_reachable(loader_data, e)) { tschatzl@5862: probe->set_strongly_reachable(); duke@435: } duke@435: } duke@435: } tschatzl@5862: // Then iterate over the protection domain cache to apply the closure on the tschatzl@5862: // previously marked ones. tschatzl@5862: _pd_cache_table->always_strong_oops_do(blk); duke@435: } duke@435: duke@435: coleenp@4037: void Dictionary::always_strong_classes_do(KlassClosure* closure) { coleenp@4037: // Follow all system classes and temporary placeholders in dictionary duke@435: for (int index = 0; index < table_size(); index++) { duke@435: for (DictionaryEntry* probe = bucket(index); duke@435: probe != NULL; duke@435: probe = probe->next()) { coleenp@4037: Klass* e = probe->klass(); coleenp@4037: ClassLoaderData* loader_data = probe->loader_data(); coleenp@4037: if (is_strongly_reachable(loader_data, e)) { coleenp@4037: closure->do_klass(e); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: // Just the classes from defining class loaders coleenp@4037: void Dictionary::classes_do(void f(Klass*)) { coleenp@4037: for (int index = 0; index < table_size(); index++) { coleenp@4037: for (DictionaryEntry* probe = bucket(index); coleenp@4037: probe != NULL; coleenp@4037: probe = probe->next()) { coleenp@4037: Klass* k = probe->klass(); coleenp@4037: if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) { duke@435: f(k); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Added for initialize_itable_for_klass to handle exceptions duke@435: // Just the classes from defining class loaders coleenp@4037: void Dictionary::classes_do(void f(Klass*, TRAPS), TRAPS) { duke@435: for (int index = 0; index < table_size(); index++) { duke@435: for (DictionaryEntry* probe = bucket(index); duke@435: probe != NULL; duke@435: probe = probe->next()) { coleenp@4037: Klass* k = probe->klass(); coleenp@4037: if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) { duke@435: f(k, CHECK); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: // All classes, and their class loaders duke@435: // Don't iterate over placeholders coleenp@4037: void Dictionary::classes_do(void f(Klass*, ClassLoaderData*)) { duke@435: for (int index = 0; index < table_size(); index++) { duke@435: for (DictionaryEntry* probe = bucket(index); duke@435: probe != NULL; duke@435: probe = probe->next()) { coleenp@4037: Klass* k = probe->klass(); coleenp@4037: f(k, probe->loader_data()); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void Dictionary::oops_do(OopClosure* f) { tschatzl@5862: // Only the protection domain oops contain references into the heap. Iterate tschatzl@5862: // over all of them. tschatzl@5862: _pd_cache_table->oops_do(f); duke@435: } duke@435: coleenp@4037: void Dictionary::methods_do(void f(Method*)) { duke@435: for (int index = 0; index < table_size(); index++) { duke@435: for (DictionaryEntry* probe = bucket(index); duke@435: probe != NULL; duke@435: probe = probe->next()) { coleenp@4037: Klass* k = probe->klass(); coleenp@4037: if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) { duke@435: // only take klass is we have the entry with the defining class loader coleenp@4037: InstanceKlass::cast(k)->methods_do(f); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: tschatzl@5862: void Dictionary::unlink(BoolObjectClosure* is_alive) { tschatzl@5862: // Only the protection domain cache table may contain references to the heap tschatzl@5862: // that need to be unlinked. tschatzl@5862: _pd_cache_table->unlink(is_alive); tschatzl@5862: } duke@435: coleenp@4037: Klass* Dictionary::try_get_next_class() { duke@435: while (true) { duke@435: if (_current_class_entry != NULL) { coleenp@4037: Klass* k = _current_class_entry->klass(); duke@435: _current_class_entry = _current_class_entry->next(); duke@435: return k; duke@435: } duke@435: _current_class_index = (_current_class_index + 1) % table_size(); duke@435: _current_class_entry = bucket(_current_class_index); duke@435: } duke@435: // never reached duke@435: } duke@435: duke@435: // Add a loaded class to the system dictionary. duke@435: // Readers of the SystemDictionary aren't always locked, so _buckets duke@435: // is volatile. The store of the next field in the constructor is duke@435: // also cast to volatile; we do this to ensure store order is maintained duke@435: // by the compilers. duke@435: coleenp@4037: void Dictionary::add_klass(Symbol* class_name, ClassLoaderData* loader_data, duke@435: KlassHandle obj) { duke@435: assert_locked_or_safepoint(SystemDictionary_lock); duke@435: assert(obj() != NULL, "adding NULL obj"); hseigel@4278: assert(obj()->name() == class_name, "sanity check on name"); stefank@4667: assert(loader_data != NULL, "Must be non-NULL"); duke@435: coleenp@4037: unsigned int hash = compute_hash(class_name, loader_data); duke@435: int index = hash_to_index(hash); coleenp@4037: DictionaryEntry* entry = new_entry(hash, obj(), loader_data); duke@435: add_entry(index, entry); duke@435: } duke@435: duke@435: duke@435: // This routine does not lock the system dictionary. duke@435: // duke@435: // Since readers don't hold a lock, we must make sure that system duke@435: // dictionary entries are only removed at a safepoint (when only one duke@435: // thread is running), and are added to in a safe way (all links must duke@435: // be updated in an MT-safe manner). duke@435: // duke@435: // Callers should be aware that an entry could be added just after duke@435: // _buckets[index] is read here, so the caller will not see the new entry. duke@435: DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash, coleenp@2497: Symbol* class_name, coleenp@4037: ClassLoaderData* loader_data) { duke@435: debug_only(_lookup_count++); duke@435: for (DictionaryEntry* entry = bucket(index); duke@435: entry != NULL; duke@435: entry = entry->next()) { coleenp@4037: if (entry->hash() == hash && entry->equals(class_name, loader_data)) { duke@435: return entry; duke@435: } duke@435: debug_only(_lookup_length++); duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: coleenp@4037: Klass* Dictionary::find(int index, unsigned int hash, Symbol* name, coleenp@4037: ClassLoaderData* loader_data, Handle protection_domain, TRAPS) { coleenp@4037: DictionaryEntry* entry = get_entry(index, hash, name, loader_data); duke@435: if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) { duke@435: return entry->klass(); duke@435: } else { duke@435: return NULL; duke@435: } duke@435: } duke@435: duke@435: coleenp@4037: Klass* Dictionary::find_class(int index, unsigned int hash, coleenp@4037: Symbol* name, ClassLoaderData* loader_data) { duke@435: assert_locked_or_safepoint(SystemDictionary_lock); coleenp@4037: assert (index == index_for(name, loader_data), "incorrect index?"); duke@435: coleenp@4037: DictionaryEntry* entry = get_entry(index, hash, name, loader_data); coleenp@4037: return (entry != NULL) ? entry->klass() : (Klass*)NULL; duke@435: } duke@435: duke@435: duke@435: // Variant of find_class for shared classes. No locking required, as duke@435: // that table is static. duke@435: coleenp@4037: Klass* Dictionary::find_shared_class(int index, unsigned int hash, coleenp@2497: Symbol* name) { coleenp@4037: assert (index == index_for(name, NULL), "incorrect index?"); duke@435: coleenp@4037: DictionaryEntry* entry = get_entry(index, hash, name, NULL); coleenp@4037: return (entry != NULL) ? entry->klass() : (Klass*)NULL; duke@435: } duke@435: duke@435: duke@435: void Dictionary::add_protection_domain(int index, unsigned int hash, duke@435: instanceKlassHandle klass, coleenp@4037: ClassLoaderData* loader_data, Handle protection_domain, duke@435: TRAPS) { coleenp@2497: Symbol* klass_name = klass->name(); coleenp@4037: DictionaryEntry* entry = get_entry(index, hash, klass_name, loader_data); duke@435: duke@435: assert(entry != NULL,"entry must be present, we just created it"); duke@435: assert(protection_domain() != NULL, duke@435: "real protection domain should be present"); duke@435: tschatzl@5862: entry->add_protection_domain(this, protection_domain()); duke@435: duke@435: assert(entry->contains_protection_domain(protection_domain()), duke@435: "now protection domain should be present"); duke@435: } duke@435: duke@435: duke@435: bool Dictionary::is_valid_protection_domain(int index, unsigned int hash, coleenp@2497: Symbol* name, coleenp@4037: ClassLoaderData* loader_data, duke@435: Handle protection_domain) { coleenp@4037: DictionaryEntry* entry = get_entry(index, hash, name, loader_data); duke@435: return entry->is_valid_protection_domain(protection_domain); duke@435: } duke@435: duke@435: duke@435: void Dictionary::reorder_dictionary() { duke@435: duke@435: // Copy all the dictionary entries into a single master list. duke@435: duke@435: DictionaryEntry* master_list = NULL; duke@435: for (int i = 0; i < table_size(); ++i) { duke@435: DictionaryEntry* p = bucket(i); duke@435: while (p != NULL) { duke@435: DictionaryEntry* tmp; duke@435: tmp = p->next(); duke@435: p->set_next(master_list); duke@435: master_list = p; duke@435: p = tmp; duke@435: } duke@435: set_entry(i, NULL); duke@435: } duke@435: duke@435: // Add the dictionary entries back to the list in the correct buckets. duke@435: while (master_list != NULL) { duke@435: DictionaryEntry* p = master_list; duke@435: master_list = master_list->next(); duke@435: p->set_next(NULL); coleenp@4037: Symbol* class_name = InstanceKlass::cast((Klass*)(p->klass()))->name(); coleenp@4037: // Since the null class loader data isn't copied to the CDS archive, coleenp@4037: // compute the hash with NULL for loader data. coleenp@4037: unsigned int hash = compute_hash(class_name, NULL); duke@435: int index = hash_to_index(hash); duke@435: p->set_hash(hash); coleenp@4037: p->set_loader_data(NULL); // loader_data isn't copied to CDS duke@435: p->set_next(bucket(index)); duke@435: set_entry(index, p); duke@435: } duke@435: } duke@435: tschatzl@5862: ProtectionDomainCacheTable::ProtectionDomainCacheTable(int table_size) tschatzl@5862: : Hashtable(table_size, sizeof(ProtectionDomainCacheEntry)) tschatzl@5862: { tschatzl@5862: } tschatzl@5862: tschatzl@5862: void ProtectionDomainCacheTable::unlink(BoolObjectClosure* is_alive) { tschatzl@5862: assert(SafepointSynchronize::is_at_safepoint(), "must be"); tschatzl@5862: for (int i = 0; i < table_size(); ++i) { tschatzl@5862: ProtectionDomainCacheEntry** p = bucket_addr(i); tschatzl@5862: ProtectionDomainCacheEntry* entry = bucket(i); tschatzl@5862: while (entry != NULL) { tschatzl@5862: if (is_alive->do_object_b(entry->literal())) { tschatzl@5862: p = entry->next_addr(); tschatzl@5862: } else { tschatzl@5862: *p = entry->next(); tschatzl@5862: free_entry(entry); tschatzl@5862: } tschatzl@5862: entry = *p; tschatzl@5862: } tschatzl@5862: } tschatzl@5862: } tschatzl@5862: tschatzl@5862: void ProtectionDomainCacheTable::oops_do(OopClosure* f) { tschatzl@5862: for (int index = 0; index < table_size(); index++) { tschatzl@5862: for (ProtectionDomainCacheEntry* probe = bucket(index); tschatzl@5862: probe != NULL; tschatzl@5862: probe = probe->next()) { tschatzl@5862: probe->oops_do(f); tschatzl@5862: } tschatzl@5862: } tschatzl@5862: } tschatzl@5862: stefank@6992: void ProtectionDomainCacheTable::roots_oops_do(OopClosure* strong, OopClosure* weak) { stefank@6992: for (int index = 0; index < table_size(); index++) { stefank@6992: for (ProtectionDomainCacheEntry* probe = bucket(index); stefank@6992: probe != NULL; stefank@6992: probe = probe->next()) { stefank@6992: if (probe->is_strongly_reachable()) { stefank@6992: probe->reset_strongly_reachable(); stefank@6992: probe->oops_do(strong); stefank@6992: } else { stefank@6992: if (weak != NULL) { stefank@6992: probe->oops_do(weak); stefank@6992: } stefank@6992: } stefank@6992: } stefank@6992: } stefank@6992: } stefank@6992: tschatzl@5862: uint ProtectionDomainCacheTable::bucket_size() { tschatzl@5862: return sizeof(ProtectionDomainCacheEntry); tschatzl@5862: } tschatzl@5862: tschatzl@5862: #ifndef PRODUCT tschatzl@5862: void ProtectionDomainCacheTable::print() { tschatzl@5862: tty->print_cr("Protection domain cache table (table_size=%d, classes=%d)", tschatzl@5862: table_size(), number_of_entries()); tschatzl@5862: for (int index = 0; index < table_size(); index++) { tschatzl@5862: for (ProtectionDomainCacheEntry* probe = bucket(index); tschatzl@5862: probe != NULL; tschatzl@5862: probe = probe->next()) { tschatzl@5862: probe->print(); tschatzl@5862: } tschatzl@5862: } tschatzl@5862: } tschatzl@5862: tschatzl@5862: void ProtectionDomainCacheEntry::print() { tschatzl@5862: tty->print_cr("entry "PTR_FORMAT" value "PTR_FORMAT" strongly_reachable %d next "PTR_FORMAT, jcoomes@5865: this, (void*)literal(), _strongly_reachable, next()); tschatzl@5862: } tschatzl@5862: #endif tschatzl@5862: tschatzl@5862: void ProtectionDomainCacheTable::verify() { tschatzl@5862: int element_count = 0; tschatzl@5862: for (int index = 0; index < table_size(); index++) { tschatzl@5862: for (ProtectionDomainCacheEntry* probe = bucket(index); tschatzl@5862: probe != NULL; tschatzl@5862: probe = probe->next()) { tschatzl@5862: probe->verify(); tschatzl@5862: element_count++; tschatzl@5862: } tschatzl@5862: } tschatzl@5862: guarantee(number_of_entries() == element_count, tschatzl@5862: "Verify of protection domain cache table failed"); tschatzl@5862: debug_only(verify_lookup_length((double)number_of_entries() / table_size())); tschatzl@5862: } tschatzl@5862: tschatzl@5862: void ProtectionDomainCacheEntry::verify() { tschatzl@5862: guarantee(literal()->is_oop(), "must be an oop"); tschatzl@5862: } tschatzl@5862: tschatzl@5862: void ProtectionDomainCacheTable::always_strong_oops_do(OopClosure* f) { tschatzl@5862: // the caller marked the protection domain cache entries that we need to apply tschatzl@5862: // the closure on. Only process them. tschatzl@5862: for (int index = 0; index < table_size(); index++) { tschatzl@5862: for (ProtectionDomainCacheEntry* probe = bucket(index); tschatzl@5862: probe != NULL; tschatzl@5862: probe = probe->next()) { tschatzl@5862: if (probe->is_strongly_reachable()) { tschatzl@5862: probe->reset_strongly_reachable(); tschatzl@5862: probe->oops_do(f); tschatzl@5862: } tschatzl@5862: } tschatzl@5862: } tschatzl@5862: } tschatzl@5862: tschatzl@5862: ProtectionDomainCacheEntry* ProtectionDomainCacheTable::get(oop protection_domain) { tschatzl@5862: unsigned int hash = compute_hash(protection_domain); tschatzl@5862: int index = hash_to_index(hash); tschatzl@5862: tschatzl@5862: ProtectionDomainCacheEntry* entry = find_entry(index, protection_domain); tschatzl@5862: if (entry == NULL) { tschatzl@5862: entry = add_entry(index, hash, protection_domain); tschatzl@5862: } tschatzl@5862: return entry; tschatzl@5862: } tschatzl@5862: tschatzl@5862: ProtectionDomainCacheEntry* ProtectionDomainCacheTable::find_entry(int index, oop protection_domain) { tschatzl@5862: for (ProtectionDomainCacheEntry* e = bucket(index); e != NULL; e = e->next()) { tschatzl@5862: if (e->protection_domain() == protection_domain) { tschatzl@5862: return e; tschatzl@5862: } tschatzl@5862: } tschatzl@5862: tschatzl@5862: return NULL; tschatzl@5862: } tschatzl@5862: tschatzl@5862: ProtectionDomainCacheEntry* ProtectionDomainCacheTable::add_entry(int index, unsigned int hash, oop protection_domain) { tschatzl@5862: assert_locked_or_safepoint(SystemDictionary_lock); tschatzl@5862: assert(index == index_for(protection_domain), "incorrect index?"); tschatzl@5862: assert(find_entry(index, protection_domain) == NULL, "no double entry"); tschatzl@5862: tschatzl@5862: ProtectionDomainCacheEntry* p = new_entry(hash, protection_domain); tschatzl@5862: Hashtable::add_entry(index, p); tschatzl@5862: return p; tschatzl@5862: } tschatzl@5862: tschatzl@5862: void ProtectionDomainCacheTable::free(ProtectionDomainCacheEntry* to_delete) { tschatzl@5862: unsigned int hash = compute_hash(to_delete->protection_domain()); tschatzl@5862: int index = hash_to_index(hash); tschatzl@5862: tschatzl@5862: ProtectionDomainCacheEntry** p = bucket_addr(index); tschatzl@5862: ProtectionDomainCacheEntry* entry = bucket(index); tschatzl@5862: while (true) { tschatzl@5862: assert(entry != NULL, "sanity"); tschatzl@5862: tschatzl@5862: if (entry == to_delete) { tschatzl@5862: *p = entry->next(); tschatzl@5862: Hashtable::free_entry(entry); tschatzl@5862: break; tschatzl@5862: } else { tschatzl@5862: p = entry->next_addr(); tschatzl@5862: entry = *p; tschatzl@5862: } tschatzl@5862: } tschatzl@5862: } tschatzl@5862: jrose@1145: SymbolPropertyTable::SymbolPropertyTable(int table_size) zgu@3900: : Hashtable(table_size, sizeof(SymbolPropertyEntry)) jrose@1145: { jrose@1145: } zgu@3900: SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket* t, jrose@1145: int number_of_entries) zgu@3900: : Hashtable(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries) jrose@1145: { jrose@1145: } jrose@1145: jrose@1145: jrose@1145: SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash, coleenp@2497: Symbol* sym, jrose@1862: intptr_t sym_mode) { jrose@1862: assert(index == index_for(sym, sym_mode), "incorrect index?"); jrose@1145: for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { coleenp@2497: if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) { jrose@1145: return p; jrose@1145: } jrose@1145: } jrose@1145: return NULL; jrose@1145: } jrose@1145: jrose@1145: jrose@1145: SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash, coleenp@2497: Symbol* sym, intptr_t sym_mode) { jrose@1145: assert_locked_or_safepoint(SystemDictionary_lock); jrose@1862: assert(index == index_for(sym, sym_mode), "incorrect index?"); jrose@1862: assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry"); jrose@1145: coleenp@2497: SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode); zgu@3900: Hashtable::add_entry(index, p); jrose@1145: return p; jrose@1145: } jrose@1145: jrose@1145: void SymbolPropertyTable::oops_do(OopClosure* f) { jrose@1145: for (int index = 0; index < table_size(); index++) { jrose@1145: for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { coleenp@4037: if (p->method_type() != NULL) { coleenp@4037: f->do_oop(p->method_type_addr()); jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: coleenp@4037: void SymbolPropertyTable::methods_do(void f(Method*)) { jrose@1145: for (int index = 0; index < table_size(); index++) { jrose@1145: for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) { coleenp@4037: Method* prop = p->method(); coleenp@4037: if (prop != NULL) { coleenp@4037: f((Method*)prop); jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: } jrose@1145: duke@435: duke@435: // ---------------------------------------------------------------------------- duke@435: #ifndef PRODUCT duke@435: duke@435: void Dictionary::print() { duke@435: ResourceMark rm; duke@435: HandleMark hm; duke@435: acorn@3491: tty->print_cr("Java system dictionary (table_size=%d, classes=%d)", acorn@3491: table_size(), number_of_entries()); duke@435: tty->print_cr("^ indicates that initiating loader is different from " duke@435: "defining loader"); duke@435: duke@435: for (int index = 0; index < table_size(); index++) { duke@435: for (DictionaryEntry* probe = bucket(index); duke@435: probe != NULL; duke@435: probe = probe->next()) { duke@435: if (Verbose) tty->print("%4d: ", index); coleenp@4037: Klass* e = probe->klass(); coleenp@4037: ClassLoaderData* loader_data = probe->loader_data(); duke@435: bool is_defining_class = coleenp@4037: (loader_data == InstanceKlass::cast(e)->class_loader_data()); duke@435: tty->print("%s%s", is_defining_class ? " " : "^", hseigel@4278: e->external_name()); coleenp@4037: duke@435: tty->print(", loader "); coleenp@4037: loader_data->print_value(); duke@435: tty->cr(); duke@435: } duke@435: } tschatzl@5862: tty->cr(); tschatzl@5862: _pd_cache_table->print(); tschatzl@5862: tty->cr(); duke@435: } duke@435: duke@435: #endif duke@435: duke@435: void Dictionary::verify() { duke@435: guarantee(number_of_entries() >= 0, "Verify of system dictionary failed"); coleenp@4037: duke@435: int element_count = 0; duke@435: for (int index = 0; index < table_size(); index++) { duke@435: for (DictionaryEntry* probe = bucket(index); duke@435: probe != NULL; duke@435: probe = probe->next()) { coleenp@4037: Klass* e = probe->klass(); coleenp@4037: ClassLoaderData* loader_data = probe->loader_data(); hseigel@4278: guarantee(e->oop_is_instance(), duke@435: "Verify of system dictionary failed"); duke@435: // class loader must be present; a null class loader is the duke@435: // boostrap loader coleenp@4037: guarantee(loader_data != NULL || DumpSharedSpaces || coleenp@4304: loader_data->class_loader() == NULL || coleenp@4037: loader_data->class_loader()->is_instance(), duke@435: "checking type of class_loader"); coleenp@6316: e->verify(); duke@435: probe->verify_protection_domain_set(); duke@435: element_count++; duke@435: } duke@435: } duke@435: guarantee(number_of_entries() == element_count, duke@435: "Verify of system dictionary failed"); duke@435: debug_only(verify_lookup_length((double)number_of_entries() / table_size())); tschatzl@5862: tschatzl@5862: _pd_cache_table->verify(); duke@435: } coleenp@4037: