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