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