1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderData.inline.hpp"
  27 #include "classfile/sharedClassUtil.hpp"
  28 #include "classfile/dictionary.hpp"
  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 "prims/jvmtiRedefineClassesTrace.hpp"
  35 #include "runtime/orderAccess.inline.hpp"
  36 #include "utilities/hashtable.inline.hpp"
  37 
  38 DictionaryEntry*  Dictionary::_current_class_entry = NULL;
  39 int               Dictionary::_current_class_index =    0;
  40 
  41 size_t Dictionary::entry_size() {
  42   if (DumpSharedSpaces) {
  43     return SystemDictionaryShared::dictionary_entry_size();
  44   } else {
  45     return sizeof(DictionaryEntry);
  46   }
  47 }
  48 
  49 Dictionary::Dictionary(int table_size)
  50   : TwoOopHashtable<Klass*, mtClass>(table_size, (int)entry_size()) {
  51   _current_class_index = 0;
  52   _current_class_entry = NULL;
  53   _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize);
  54 };
  55 
  56 
  57 Dictionary::Dictionary(int table_size, HashtableBucket<mtClass>* t,
  58                        int number_of_entries)
  59   : TwoOopHashtable<Klass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries) {
  60   _current_class_index = 0;
  61   _current_class_entry = NULL;
  62   _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize);
  63 };
  64 
  65 ProtectionDomainCacheEntry* Dictionary::cache_get(oop protection_domain) {
  66   return _pd_cache_table->get(protection_domain);
  67 }
  68 
  69 DictionaryEntry* Dictionary::new_entry(unsigned int hash, Klass* klass,
  70                                        ClassLoaderData* loader_data) {
  71   DictionaryEntry* entry = (DictionaryEntry*)Hashtable<Klass*, mtClass>::new_entry(hash, klass);
  72   entry->set_loader_data(loader_data);
  73   entry->set_pd_set(NULL);
  74   assert(klass->is_instance_klass(), "Must be");
  75   if (DumpSharedSpaces) {
  76     SystemDictionaryShared::init_shared_dictionary_entry(klass, entry);
  77   }
  78   return entry;
  79 }
  80 
  81 
  82 void Dictionary::free_entry(DictionaryEntry* entry) {
  83   // avoid recursion when deleting linked list
  84   while (entry->pd_set() != NULL) {
  85     ProtectionDomainEntry* to_delete = entry->pd_set();
  86     entry->set_pd_set(to_delete->next());
  87     delete to_delete;
  88   }
  89   Hashtable<Klass*, mtClass>::free_entry(entry);
  90 }
  91 
  92 
  93 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
  94 #ifdef ASSERT
  95   if (protection_domain == klass()->protection_domain()) {
  96     // Ensure this doesn't show up in the pd_set (invariant)
  97     bool in_pd_set = false;
  98     for (ProtectionDomainEntry* current = _pd_set;
  99                                 current != NULL;
 100                                 current = current->next()) {
 101       if (current->protection_domain() == protection_domain) {
 102         in_pd_set = true;
 103         break;
 104       }
 105     }
 106     if (in_pd_set) {
 107       assert(false, "A klass's protection domain should not show up "
 108                     "in its sys. dict. PD set");
 109     }
 110   }
 111 #endif /* ASSERT */
 112 
 113   if (protection_domain == klass()->protection_domain()) {
 114     // Succeeds trivially
 115     return true;
 116   }
 117 
 118   for (ProtectionDomainEntry* current = _pd_set;
 119                               current != NULL;
 120                               current = current->next()) {
 121     if (current->protection_domain() == protection_domain) return true;
 122   }
 123   return false;
 124 }
 125 
 126 
 127 void DictionaryEntry::add_protection_domain(Dictionary* dict, oop protection_domain) {
 128   assert_locked_or_safepoint(SystemDictionary_lock);
 129   if (!contains_protection_domain(protection_domain)) {
 130     ProtectionDomainCacheEntry* entry = dict->cache_get(protection_domain);
 131     ProtectionDomainEntry* new_head =
 132                 new ProtectionDomainEntry(entry, _pd_set);
 133     // Warning: Preserve store ordering.  The SystemDictionary is read
 134     //          without locks.  The new ProtectionDomainEntry must be
 135     //          complete before other threads can be allowed to see it
 136     //          via a store to _pd_set.
 137     OrderAccess::release_store_ptr(&_pd_set, new_head);
 138   }
 139   if (log_is_enabled(Trace, protectiondomain)) {
 140     ResourceMark rm;
 141     outputStream* log = LogHandle(protectiondomain)::trace_stream();
 142     print_count(log);
 143   }
 144 }
 145 
 146 
 147 void Dictionary::do_unloading() {
 148   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 149 
 150   // Remove unloadable entries and classes from system dictionary
 151   // The placeholder array has been handled in always_strong_oops_do.
 152   DictionaryEntry* probe = NULL;
 153   for (int index = 0; index < table_size(); index++) {
 154     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
 155       probe = *p;
 156       Klass* e = probe->klass();
 157       ClassLoaderData* loader_data = probe->loader_data();
 158 
 159       InstanceKlass* ik = InstanceKlass::cast(e);
 160 
 161       // Non-unloadable classes were handled in always_strong_oops_do
 162       if (!is_strongly_reachable(loader_data, e)) {
 163         // Entry was not visited in phase1 (negated test from phase1)
 164         assert(!loader_data->is_the_null_class_loader_data(), "unloading entry with null class loader");
 165         ClassLoaderData* k_def_class_loader_data = ik->class_loader_data();
 166 
 167         // Do we need to delete this system dictionary entry?
 168         bool purge_entry = false;
 169 
 170         // Do we need to delete this system dictionary entry?
 171         if (loader_data->is_unloading()) {
 172           // If the loader is not live this entry should always be
 173           // removed (will never be looked up again).
 174           purge_entry = true;
 175         } else {
 176           // The loader in this entry is alive. If the klass is dead,
 177           // (determined by checking the defining class loader)
 178           // the loader must be an initiating loader (rather than the
 179           // defining loader). Remove this entry.
 180           if (k_def_class_loader_data->is_unloading()) {
 181             // If we get here, the class_loader_data must not be the defining
 182             // loader, it must be an initiating one.
 183             assert(k_def_class_loader_data != loader_data,
 184                    "cannot have live defining loader and unreachable klass");
 185             // Loader is live, but class and its defining loader are dead.
 186             // Remove the entry. The class is going away.
 187             purge_entry = true;
 188           }
 189         }
 190 
 191         if (purge_entry) {
 192           *p = probe->next();
 193           if (probe == _current_class_entry) {
 194             _current_class_entry = NULL;
 195           }
 196           free_entry(probe);
 197           continue;
 198         }
 199       }
 200       p = probe->next_addr();
 201     }
 202   }
 203 }
 204 
 205 void Dictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) {
 206   // Skip the strong roots probe marking if the closures are the same.
 207   if (strong == weak) {
 208     oops_do(strong);
 209     return;
 210   }
 211 
 212   for (int index = 0; index < table_size(); index++) {
 213     for (DictionaryEntry *probe = bucket(index);
 214                           probe != NULL;
 215                           probe = probe->next()) {
 216       Klass* e = probe->klass();
 217       ClassLoaderData* loader_data = probe->loader_data();
 218       if (is_strongly_reachable(loader_data, e)) {
 219         probe->set_strongly_reachable();
 220       }
 221     }
 222   }
 223   _pd_cache_table->roots_oops_do(strong, weak);
 224 }
 225 
 226 void Dictionary::remove_classes_in_error_state() {
 227   assert(DumpSharedSpaces, "supported only when dumping");
 228   DictionaryEntry* probe = NULL;
 229   for (int index = 0; index < table_size(); index++) {
 230     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
 231       probe = *p;
 232       InstanceKlass* ik = InstanceKlass::cast(probe->klass());
 233       if (ik->is_in_error_state()) { // purge this entry
 234         *p = probe->next();
 235         if (probe == _current_class_entry) {
 236           _current_class_entry = NULL;
 237         }
 238         free_entry(probe);
 239         ResourceMark rm;
 240         tty->print_cr("Preload Warning: Removed error class: %s", ik->external_name());
 241         continue;
 242       }
 243 
 244       p = probe->next_addr();
 245     }
 246   }
 247 }
 248 
 249 void Dictionary::always_strong_oops_do(OopClosure* blk) {
 250   // Follow all system classes and temporary placeholders in dictionary; only
 251   // protection domain oops contain references into the heap. In a first
 252   // pass over the system dictionary determine which need to be treated as
 253   // strongly reachable and mark them as such.
 254   for (int index = 0; index < table_size(); index++) {
 255     for (DictionaryEntry *probe = bucket(index);
 256                           probe != NULL;
 257                           probe = probe->next()) {
 258       Klass* e = probe->klass();
 259       ClassLoaderData* loader_data = probe->loader_data();
 260       if (is_strongly_reachable(loader_data, e)) {
 261         probe->set_strongly_reachable();
 262       }
 263     }
 264   }
 265   // Then iterate over the protection domain cache to apply the closure on the
 266   // previously marked ones.
 267   _pd_cache_table->always_strong_oops_do(blk);
 268 }
 269 
 270 
 271 void Dictionary::always_strong_classes_do(KlassClosure* closure) {
 272   // Follow all system classes and temporary placeholders in dictionary
 273   for (int index = 0; index < table_size(); index++) {
 274     for (DictionaryEntry* probe = bucket(index);
 275                           probe != NULL;
 276                           probe = probe->next()) {
 277       Klass* e = probe->klass();
 278       ClassLoaderData* loader_data = probe->loader_data();
 279       if (is_strongly_reachable(loader_data, e)) {
 280         closure->do_klass(e);
 281       }
 282     }
 283   }
 284 }
 285 
 286 
 287 //   Just the classes from defining class loaders
 288 void Dictionary::classes_do(void f(Klass*)) {
 289   for (int index = 0; index < table_size(); index++) {
 290     for (DictionaryEntry* probe = bucket(index);
 291                           probe != NULL;
 292                           probe = probe->next()) {
 293       Klass* k = probe->klass();
 294       if (probe->loader_data() == k->class_loader_data()) {
 295         f(k);
 296       }
 297     }
 298   }
 299 }
 300 
 301 // Added for initialize_itable_for_klass to handle exceptions
 302 //   Just the classes from defining class loaders
 303 void Dictionary::classes_do(void f(Klass*, TRAPS), TRAPS) {
 304   for (int index = 0; index < table_size(); index++) {
 305     for (DictionaryEntry* probe = bucket(index);
 306                           probe != NULL;
 307                           probe = probe->next()) {
 308       Klass* k = probe->klass();
 309       if (probe->loader_data() == k->class_loader_data()) {
 310         f(k, CHECK);
 311       }
 312     }
 313   }
 314 }
 315 
 316 //   All classes, and their class loaders
 317 // Don't iterate over placeholders
 318 void Dictionary::classes_do(void f(Klass*, ClassLoaderData*)) {
 319   for (int index = 0; index < table_size(); index++) {
 320     for (DictionaryEntry* probe = bucket(index);
 321                           probe != NULL;
 322                           probe = probe->next()) {
 323       Klass* k = probe->klass();
 324       f(k, probe->loader_data());
 325     }
 326   }
 327 }
 328 
 329 void Dictionary::oops_do(OopClosure* f) {
 330   // Only the protection domain oops contain references into the heap. Iterate
 331   // over all of them.
 332   _pd_cache_table->oops_do(f);
 333 }
 334 
 335 void Dictionary::methods_do(void f(Method*)) {
 336   for (int index = 0; index < table_size(); index++) {
 337     for (DictionaryEntry* probe = bucket(index);
 338                           probe != NULL;
 339                           probe = probe->next()) {
 340       Klass* k = probe->klass();
 341       if (probe->loader_data() == k->class_loader_data()) {
 342         // only take klass is we have the entry with the defining class loader
 343         InstanceKlass::cast(k)->methods_do(f);
 344       }
 345     }
 346   }
 347 }
 348 
 349 void Dictionary::unlink(BoolObjectClosure* is_alive) {
 350   // Only the protection domain cache table may contain references to the heap
 351   // that need to be unlinked.
 352   _pd_cache_table->unlink(is_alive);
 353 }
 354 
 355 Klass* Dictionary::try_get_next_class() {
 356   while (true) {
 357     if (_current_class_entry != NULL) {
 358       Klass* k = _current_class_entry->klass();
 359       _current_class_entry = _current_class_entry->next();
 360       return k;
 361     }
 362     _current_class_index = (_current_class_index + 1) % table_size();
 363     _current_class_entry = bucket(_current_class_index);
 364   }
 365   // never reached
 366 }
 367 
 368 // Add a loaded class to the system dictionary.
 369 // Readers of the SystemDictionary aren't always locked, so _buckets
 370 // is volatile. The store of the next field in the constructor is
 371 // also cast to volatile;  we do this to ensure store order is maintained
 372 // by the compilers.
 373 
 374 void Dictionary::add_klass(Symbol* class_name, ClassLoaderData* loader_data,
 375                            KlassHandle obj) {
 376   assert_locked_or_safepoint(SystemDictionary_lock);
 377   assert(obj() != NULL, "adding NULL obj");
 378   assert(obj()->name() == class_name, "sanity check on name");
 379   assert(loader_data != NULL, "Must be non-NULL");
 380 
 381   unsigned int hash = compute_hash(class_name, loader_data);
 382   int index = hash_to_index(hash);
 383   DictionaryEntry* entry = new_entry(hash, obj(), loader_data);
 384   add_entry(index, entry);
 385 }
 386 
 387 
 388 // This routine does not lock the system dictionary.
 389 //
 390 // Since readers don't hold a lock, we must make sure that system
 391 // dictionary entries are only removed at a safepoint (when only one
 392 // thread is running), and are added to in a safe way (all links must
 393 // be updated in an MT-safe manner).
 394 //
 395 // Callers should be aware that an entry could be added just after
 396 // _buckets[index] is read here, so the caller will not see the new entry.
 397 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
 398                                        Symbol* class_name,
 399                                        ClassLoaderData* loader_data) {
 400   debug_only(_lookup_count++);
 401   for (DictionaryEntry* entry = bucket(index);
 402                         entry != NULL;
 403                         entry = entry->next()) {
 404     if (entry->hash() == hash && entry->equals(class_name, loader_data)) {
 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) {
 468   DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
 469   return entry->is_valid_protection_domain(protection_domain);
 470 }
 471 
 472 
 473 void Dictionary::reorder_dictionary() {
 474 
 475   // Copy all the dictionary entries into a single master list.
 476 
 477   DictionaryEntry* master_list = NULL;
 478   for (int i = 0; i < table_size(); ++i) {
 479     DictionaryEntry* p = bucket(i);
 480     while (p != NULL) {
 481       DictionaryEntry* tmp;
 482       tmp = p->next();
 483       p->set_next(master_list);
 484       master_list = p;
 485       p = tmp;
 486     }
 487     set_entry(i, NULL);
 488   }
 489 
 490   // Add the dictionary entries back to the list in the correct buckets.
 491   while (master_list != NULL) {
 492     DictionaryEntry* p = master_list;
 493     master_list = master_list->next();
 494     p->set_next(NULL);
 495     Symbol* class_name = p->klass()->name();
 496     // Since the null class loader data isn't copied to the CDS archive,
 497     // compute the hash with NULL for loader data.
 498     unsigned int hash = compute_hash(class_name, NULL);
 499     int index = hash_to_index(hash);
 500     p->set_hash(hash);
 501     p->set_loader_data(NULL);   // loader_data isn't copied to CDS
 502     p->set_next(bucket(index));
 503     set_entry(index, p);
 504   }
 505 }
 506 
 507 
 508 unsigned int ProtectionDomainCacheTable::compute_hash(oop protection_domain) {
 509   return (unsigned int)(protection_domain->identity_hash());
 510 }
 511 
 512 int ProtectionDomainCacheTable::index_for(oop protection_domain) {
 513   return hash_to_index(compute_hash(protection_domain));
 514 }
 515 
 516 ProtectionDomainCacheTable::ProtectionDomainCacheTable(int table_size)
 517   : Hashtable<oop, mtClass>(table_size, sizeof(ProtectionDomainCacheEntry))
 518 {
 519 }
 520 
 521 void ProtectionDomainCacheTable::unlink(BoolObjectClosure* is_alive) {
 522   assert(SafepointSynchronize::is_at_safepoint(), "must be");
 523   for (int i = 0; i < table_size(); ++i) {
 524     ProtectionDomainCacheEntry** p = bucket_addr(i);
 525     ProtectionDomainCacheEntry* entry = bucket(i);
 526     while (entry != NULL) {
 527       if (is_alive->do_object_b(entry->literal())) {
 528         p = entry->next_addr();
 529       } else {
 530         *p = entry->next();
 531         free_entry(entry);
 532       }
 533       entry = *p;
 534     }
 535   }
 536 }
 537 
 538 void ProtectionDomainCacheTable::oops_do(OopClosure* f) {
 539   for (int index = 0; index < table_size(); index++) {
 540     for (ProtectionDomainCacheEntry* probe = bucket(index);
 541                                      probe != NULL;
 542                                      probe = probe->next()) {
 543       probe->oops_do(f);
 544     }
 545   }
 546 }
 547 
 548 void ProtectionDomainCacheTable::roots_oops_do(OopClosure* strong, OopClosure* weak) {
 549   for (int index = 0; index < table_size(); index++) {
 550     for (ProtectionDomainCacheEntry* probe = bucket(index);
 551                                      probe != NULL;
 552                                      probe = probe->next()) {
 553       if (probe->is_strongly_reachable()) {
 554         probe->reset_strongly_reachable();
 555         probe->oops_do(strong);
 556       } else {
 557         if (weak != NULL) {
 558           probe->oops_do(weak);
 559         }
 560       }
 561     }
 562   }
 563 }
 564 
 565 uint ProtectionDomainCacheTable::bucket_size() {
 566   return sizeof(ProtectionDomainCacheEntry);
 567 }
 568 
 569 #ifndef PRODUCT
 570 void ProtectionDomainCacheTable::print() {
 571   tty->print_cr("Protection domain cache table (table_size=%d, classes=%d)",
 572                 table_size(), number_of_entries());
 573   for (int index = 0; index < table_size(); index++) {
 574     for (ProtectionDomainCacheEntry* probe = bucket(index);
 575                                      probe != NULL;
 576                                      probe = probe->next()) {
 577       probe->print();
 578     }
 579   }
 580 }
 581 
 582 void ProtectionDomainCacheEntry::print() {
 583   tty->print_cr("entry " PTR_FORMAT " value " PTR_FORMAT " strongly_reachable %d next " PTR_FORMAT,
 584                 p2i(this), p2i(literal()), _strongly_reachable, p2i(next()));
 585 }
 586 #endif
 587 
 588 void ProtectionDomainCacheTable::verify() {
 589   int element_count = 0;
 590   for (int index = 0; index < table_size(); index++) {
 591     for (ProtectionDomainCacheEntry* probe = bucket(index);
 592                                      probe != NULL;
 593                                      probe = probe->next()) {
 594       probe->verify();
 595       element_count++;
 596     }
 597   }
 598   guarantee(number_of_entries() == element_count,
 599             "Verify of protection domain cache table failed");
 600   debug_only(verify_lookup_length((double)number_of_entries() / table_size()));
 601 }
 602 
 603 void ProtectionDomainCacheEntry::verify() {
 604   guarantee(literal()->is_oop(), "must be an oop");
 605 }
 606 
 607 void ProtectionDomainCacheTable::always_strong_oops_do(OopClosure* f) {
 608   // the caller marked the protection domain cache entries that we need to apply
 609   // the closure on. Only process them.
 610   for (int index = 0; index < table_size(); index++) {
 611     for (ProtectionDomainCacheEntry* probe = bucket(index);
 612                                      probe != NULL;
 613                                      probe = probe->next()) {
 614       if (probe->is_strongly_reachable()) {
 615         probe->reset_strongly_reachable();
 616         probe->oops_do(f);
 617       }
 618     }
 619   }
 620 }
 621 
 622 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::get(oop protection_domain) {
 623   unsigned int hash = compute_hash(protection_domain);
 624   int index = hash_to_index(hash);
 625 
 626   ProtectionDomainCacheEntry* entry = find_entry(index, protection_domain);
 627   if (entry == NULL) {
 628     entry = add_entry(index, hash, protection_domain);
 629   }
 630   return entry;
 631 }
 632 
 633 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::find_entry(int index, oop protection_domain) {
 634   for (ProtectionDomainCacheEntry* e = bucket(index); e != NULL; e = e->next()) {
 635     if (e->protection_domain() == protection_domain) {
 636       return e;
 637     }
 638   }
 639 
 640   return NULL;
 641 }
 642 
 643 ProtectionDomainCacheEntry* ProtectionDomainCacheTable::add_entry(int index, unsigned int hash, oop protection_domain) {
 644   assert_locked_or_safepoint(SystemDictionary_lock);
 645   assert(index == index_for(protection_domain), "incorrect index?");
 646   assert(find_entry(index, protection_domain) == NULL, "no double entry");
 647 
 648   ProtectionDomainCacheEntry* p = new_entry(hash, protection_domain);
 649   Hashtable<oop, mtClass>::add_entry(index, p);
 650   return p;
 651 }
 652 
 653 void ProtectionDomainCacheTable::free(ProtectionDomainCacheEntry* to_delete) {
 654   unsigned int hash = compute_hash(to_delete->protection_domain());
 655   int index = hash_to_index(hash);
 656 
 657   ProtectionDomainCacheEntry** p = bucket_addr(index);
 658   ProtectionDomainCacheEntry* entry = bucket(index);
 659   while (true) {
 660     assert(entry != NULL, "sanity");
 661 
 662     if (entry == to_delete) {
 663       *p = entry->next();
 664       Hashtable<oop, mtClass>::free_entry(entry);
 665       break;
 666     } else {
 667       p = entry->next_addr();
 668       entry = *p;
 669     }
 670   }
 671 }
 672 
 673 SymbolPropertyTable::SymbolPropertyTable(int table_size)
 674   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry))
 675 {
 676 }
 677 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t,
 678                                          int number_of_entries)
 679   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries)
 680 {
 681 }
 682 
 683 
 684 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash,
 685                                                      Symbol* sym,
 686                                                      intptr_t sym_mode) {
 687   assert(index == index_for(sym, sym_mode), "incorrect index?");
 688   for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
 689     if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) {
 690       return p;
 691     }
 692   }
 693   return NULL;
 694 }
 695 
 696 
 697 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash,
 698                                                     Symbol* sym, intptr_t sym_mode) {
 699   assert_locked_or_safepoint(SystemDictionary_lock);
 700   assert(index == index_for(sym, sym_mode), "incorrect index?");
 701   assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry");
 702 
 703   SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode);
 704   Hashtable<Symbol*, mtSymbol>::add_entry(index, p);
 705   return p;
 706 }
 707 
 708 void SymbolPropertyTable::oops_do(OopClosure* f) {
 709   for (int index = 0; index < table_size(); index++) {
 710     for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
 711       if (p->method_type() != NULL) {
 712         f->do_oop(p->method_type_addr());
 713       }
 714     }
 715   }
 716 }
 717 
 718 void SymbolPropertyTable::methods_do(void f(Method*)) {
 719   for (int index = 0; index < table_size(); index++) {
 720     for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
 721       Method* prop = p->method();
 722       if (prop != NULL) {
 723         f((Method*)prop);
 724       }
 725     }
 726   }
 727 }
 728 
 729 
 730 // ----------------------------------------------------------------------------
 731 
 732 void Dictionary::print(bool details) {
 733   ResourceMark rm;
 734   HandleMark   hm;
 735 
 736   if (details) {
 737     tty->print_cr("Java system dictionary (table_size=%d, classes=%d)",
 738                    table_size(), number_of_entries());
 739     tty->print_cr("^ indicates that initiating loader is different from "
 740                   "defining loader");
 741   }
 742 
 743   for (int index = 0; index < table_size(); index++) {
 744     for (DictionaryEntry* probe = bucket(index);
 745                           probe != NULL;
 746                           probe = probe->next()) {
 747       if (Verbose) tty->print("%4d: ", index);
 748       Klass* e = probe->klass();
 749       ClassLoaderData* loader_data =  probe->loader_data();
 750       bool is_defining_class =
 751          (loader_data == e->class_loader_data());
 752       tty->print("%s%s", ((!details) || is_defining_class) ? " " : "^",
 753                    e->external_name());
 754 
 755       if (details) {
 756         tty->print(", loader ");
 757         if (loader_data != NULL) {
 758           loader_data->print_value();
 759         } else {
 760           tty->print("NULL");
 761         }
 762       }
 763       tty->cr();
 764     }
 765   }
 766 
 767   if (details) {
 768     tty->cr();
 769     _pd_cache_table->print();
 770   }
 771   tty->cr();
 772 }
 773 
 774 void Dictionary::verify() {
 775   guarantee(number_of_entries() >= 0, "Verify of system dictionary failed");
 776 
 777   int element_count = 0;
 778   for (int index = 0; index < table_size(); index++) {
 779     for (DictionaryEntry* probe = bucket(index);
 780                           probe != NULL;
 781                           probe = probe->next()) {
 782       Klass* e = probe->klass();
 783       ClassLoaderData* loader_data = probe->loader_data();
 784       guarantee(e->is_instance_klass(),
 785                               "Verify of system dictionary failed");
 786       // class loader must be present;  a null class loader is the
 787       // boostrap loader
 788       guarantee(loader_data != NULL || DumpSharedSpaces ||
 789                 loader_data->class_loader() == NULL ||
 790                 loader_data->class_loader()->is_instance(),
 791                 "checking type of class_loader");
 792       e->verify();
 793       probe->verify_protection_domain_set();
 794       element_count++;
 795     }
 796   }
 797   guarantee(number_of_entries() == element_count,
 798             "Verify of system dictionary failed");
 799   debug_only(verify_lookup_length((double)number_of_entries() / table_size()));
 800 
 801   _pd_cache_table->verify();
 802 }
 803