< prev index next >

src/share/vm/classfile/dictionary.cpp

Print this page




  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/protectionDomainCache.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/systemDictionaryShared.hpp"
  32 #include "memory/iterator.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "oops/oop.inline.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<InstanceKlass*, 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<InstanceKlass*, 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(Handle protection_domain) {
  66   return _pd_cache_table->get(protection_domain);










  67 }
  68 
  69 DictionaryEntry* Dictionary::new_entry(unsigned int hash, InstanceKlass* klass,
  70                                        ClassLoaderData* loader_data) {
  71   DictionaryEntry* entry = (DictionaryEntry*)Hashtable<InstanceKlass*, 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<InstanceKlass*, 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, Handle 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 = Log(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   DictionaryEntry* probe = NULL;
 152   for (int index = 0; index < table_size(); index++) {
 153     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
 154       probe = *p;
 155       Klass* e = probe->klass();
 156       ClassLoaderData* loader_data = probe->loader_data();
 157 
 158       InstanceKlass* ik = InstanceKlass::cast(e);
 159 
 160       // Only unload classes that are not strongly reachable
 161       if (!is_strongly_reachable(loader_data, e)) {
 162         // Entry was not visited in phase1 (negated test from phase1)
 163         assert(!loader_data->is_the_null_class_loader_data(), "unloading entry with null class loader");
 164         ClassLoaderData* k_def_class_loader_data = ik->class_loader_data();
 165 
 166         // Do we need to delete this system dictionary entry?
 167         bool purge_entry = false;
 168 
 169         // Do we need to delete this system dictionary entry?
 170         if (loader_data->is_unloading()) {
 171           // If the loader is not live this entry should always be
 172           // removed (will never be looked up again).
 173           purge_entry = true;
 174         } else {
 175           // The loader in this entry is alive. If the klass is dead,
 176           // (determined by checking the defining class loader)
 177           // the loader must be an initiating loader (rather than the
 178           // defining loader). Remove this entry.
 179           if (k_def_class_loader_data->is_unloading()) {
 180             // If we get here, the class_loader_data must not be the defining
 181             // loader, it must be an initiating one.
 182             assert(k_def_class_loader_data != loader_data,
 183                    "cannot have live defining loader and unreachable klass");
 184             // Loader is live, but class and its defining loader are dead.
 185             // Remove the entry. The class is going away.
 186             purge_entry = true;
 187           }
 188         }
 189 
 190         if (purge_entry) {
 191           *p = probe->next();
 192           if (probe == _current_class_entry) {
 193             _current_class_entry = NULL;
 194           }
 195           free_entry(probe);
 196           continue;
 197         }
 198       }
 199       p = probe->next_addr();
 200     }
 201   }
 202 }
 203 
 204 void Dictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) {
 205   // Do strong roots marking if the closures are the same.
 206   if (strong == weak || !ClassUnloading) {
 207     // Only the protection domain oops contain references into the heap. Iterate
 208     // over all of them.
 209     _pd_cache_table->oops_do(strong);
 210   } else {
 211    if (weak != NULL) {
 212      _pd_cache_table->oops_do(weak);
 213    }
 214   }
 215 }
 216 
 217 
 218 void Dictionary::remove_classes_in_error_state() {
 219   assert(DumpSharedSpaces, "supported only when dumping");
 220   DictionaryEntry* probe = NULL;
 221   for (int index = 0; index < table_size(); index++) {
 222     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
 223       probe = *p;
 224       InstanceKlass* ik = InstanceKlass::cast(probe->klass());
 225       if (ik->is_in_error_state()) { // purge this entry
 226         *p = probe->next();
 227         if (probe == _current_class_entry) {
 228           _current_class_entry = NULL;
 229         }
 230         free_entry(probe);
 231         ResourceMark rm;
 232         tty->print_cr("Preload Warning: Removed error class: %s", ik->external_name());
 233         continue;
 234       }
 235 
 236       p = probe->next_addr();
 237     }
 238   }
 239 }
 240 
 241 //   Just the classes from defining class loaders
 242 void Dictionary::classes_do(void f(Klass*)) {
 243   for (int index = 0; index < table_size(); index++) {
 244     for (DictionaryEntry* probe = bucket(index);
 245                           probe != NULL;
 246                           probe = probe->next()) {
 247       Klass* k = probe->klass();
 248       if (probe->loader_data() == k->class_loader_data()) {
 249         f(k);
 250       }
 251     }
 252   }
 253 }
 254 
 255 // Added for initialize_itable_for_klass to handle exceptions
 256 //   Just the classes from defining class loaders
 257 void Dictionary::classes_do(void f(Klass*, TRAPS), TRAPS) {
 258   for (int index = 0; index < table_size(); index++) {
 259     for (DictionaryEntry* probe = bucket(index);
 260                           probe != NULL;
 261                           probe = probe->next()) {
 262       Klass* k = probe->klass();
 263       if (probe->loader_data() == k->class_loader_data()) {
 264         f(k, CHECK);
 265       }
 266     }
 267   }
 268 }
 269 
 270 //   All classes, and their class loaders
 271 // Don't iterate over placeholders
 272 void Dictionary::classes_do(void f(Klass*, ClassLoaderData*)) {
 273   for (int index = 0; index < table_size(); index++) {
 274     for (DictionaryEntry* probe = bucket(index);
 275                           probe != NULL;
 276                           probe = probe->next()) {
 277       Klass* k = probe->klass();
 278       f(k, probe->loader_data());
 279     }
 280   }
 281 }
 282 
 283 void Dictionary::oops_do(OopClosure* f) {
 284   // Only the protection domain oops contain references into the heap. Iterate
 285   // over all of them.
 286   _pd_cache_table->oops_do(f);
 287 }
 288 
 289 void Dictionary::unlink(BoolObjectClosure* is_alive) {
 290   // Only the protection domain cache table may contain references to the heap
 291   // that need to be unlinked.
 292   _pd_cache_table->unlink(is_alive);
 293 }
 294 
 295 InstanceKlass* Dictionary::try_get_next_class() {
 296   while (true) {
 297     if (_current_class_entry != NULL) {
 298       InstanceKlass* k = _current_class_entry->klass();
 299       _current_class_entry = _current_class_entry->next();
 300       return k;
 301     }
 302     _current_class_index = (_current_class_index + 1) % table_size();
 303     _current_class_entry = bucket(_current_class_index);
 304   }
 305   // never reached
 306 }
 307 
 308 // Add a loaded class to the system dictionary.
 309 // Readers of the SystemDictionary aren't always locked, so _buckets
 310 // is volatile. The store of the next field in the constructor is
 311 // also cast to volatile;  we do this to ensure store order is maintained
 312 // by the compilers.
 313 
 314 void Dictionary::add_klass(Symbol* class_name, ClassLoaderData* loader_data,
 315                            InstanceKlass* obj) {
 316   assert_locked_or_safepoint(SystemDictionary_lock);
 317   assert(obj != NULL, "adding NULL obj");
 318   assert(obj->name() == class_name, "sanity check on name");
 319   assert(loader_data != NULL, "Must be non-NULL");
 320 
 321   unsigned int hash = compute_hash(class_name, loader_data);
 322   int index = hash_to_index(hash);
 323   DictionaryEntry* entry = new_entry(hash, obj, loader_data);
 324   add_entry(index, entry);
 325 }
 326 
 327 
 328 // This routine does not lock the system dictionary.
 329 //
 330 // Since readers don't hold a lock, we must make sure that system
 331 // dictionary entries are only removed at a safepoint (when only one
 332 // thread is running), and are added to in a safe way (all links must
 333 // be updated in an MT-safe manner).
 334 //
 335 // Callers should be aware that an entry could be added just after
 336 // _buckets[index] is read here, so the caller will not see the new entry.
 337 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
 338                                        Symbol* class_name,
 339                                        ClassLoaderData* loader_data) {
 340   for (DictionaryEntry* entry = bucket(index);
 341                         entry != NULL;
 342                         entry = entry->next()) {
 343     if (entry->hash() == hash && entry->equals(class_name, loader_data)) {

 344       return entry;
 345     }
 346   }

 347   return NULL;
 348 }
 349 
 350 
 351 InstanceKlass* Dictionary::find(int index, unsigned int hash, Symbol* name,
 352                                 ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
 353   DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
 354   if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
 355     return entry->klass();
 356   } else {
 357     return NULL;
 358   }
 359 }
 360 
 361 
 362 InstanceKlass* Dictionary::find_class(int index, unsigned int hash,
 363                                       Symbol* name, ClassLoaderData* loader_data) {
 364   assert_locked_or_safepoint(SystemDictionary_lock);
 365   assert (index == index_for(name, loader_data), "incorrect index?");
 366 
 367   DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
 368   return (entry != NULL) ? entry->klass() : NULL;
 369 }
 370 
 371 
 372 // Variant of find_class for shared classes.  No locking required, as
 373 // that table is static.
 374 
 375 InstanceKlass* Dictionary::find_shared_class(int index, unsigned int hash,
 376                                              Symbol* name) {
 377   assert (index == index_for(name, NULL), "incorrect index?");
 378 
 379   DictionaryEntry* entry = get_entry(index, hash, name, NULL);
 380   return (entry != NULL) ? entry->klass() : NULL;
 381 }
 382 
 383 
 384 void Dictionary::add_protection_domain(int index, unsigned int hash,
 385                                        InstanceKlass* klass,
 386                                        ClassLoaderData* loader_data, Handle protection_domain,
 387                                        TRAPS) {
 388   Symbol*  klass_name = klass->name();
 389   DictionaryEntry* entry = get_entry(index, hash, klass_name, loader_data);
 390 
 391   assert(entry != NULL,"entry must be present, we just created it");
 392   assert(protection_domain() != NULL,
 393          "real protection domain should be present");
 394 
 395   entry->add_protection_domain(this, protection_domain);
 396 
 397   assert(entry->contains_protection_domain(protection_domain()),
 398          "now protection domain should be present");
 399 }
 400 
 401 
 402 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash,
 403                                             Symbol* name,
 404                                             ClassLoaderData* loader_data,
 405                                             Handle protection_domain) {
 406   DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
 407   return entry->is_valid_protection_domain(protection_domain);
 408 }
 409 
 410 
 411 void Dictionary::reorder_dictionary() {
 412 
 413   // Copy all the dictionary entries into a single master list.
 414 
 415   DictionaryEntry* master_list = NULL;
 416   for (int i = 0; i < table_size(); ++i) {
 417     DictionaryEntry* p = bucket(i);
 418     while (p != NULL) {
 419       DictionaryEntry* tmp;
 420       tmp = p->next();
 421       p->set_next(master_list);
 422       master_list = p;
 423       p = tmp;
 424     }
 425     set_entry(i, NULL);
 426   }
 427 
 428   // Add the dictionary entries back to the list in the correct buckets.
 429   while (master_list != NULL) {
 430     DictionaryEntry* p = master_list;
 431     master_list = master_list->next();
 432     p->set_next(NULL);
 433     Symbol* class_name = p->klass()->name();
 434     // Since the null class loader data isn't copied to the CDS archive,
 435     // compute the hash with NULL for loader data.
 436     unsigned int hash = compute_hash(class_name, NULL);
 437     int index = hash_to_index(hash);
 438     p->set_hash(hash);
 439     p->set_loader_data(NULL);   // loader_data isn't copied to CDS
 440     p->set_next(bucket(index));
 441     set_entry(index, p);
 442   }
 443 }
 444 
 445 
 446 SymbolPropertyTable::SymbolPropertyTable(int table_size)
 447   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry))
 448 {
 449 }
 450 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t,
 451                                          int number_of_entries)
 452   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries)
 453 {
 454 }
 455 
 456 
 457 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash,
 458                                                      Symbol* sym,
 459                                                      intptr_t sym_mode) {


 500 }
 501 
 502 
 503 // ----------------------------------------------------------------------------
 504 
 505 void Dictionary::print(bool details) {
 506   ResourceMark rm;
 507 
 508   if (details) {
 509     tty->print_cr("Java system dictionary (table_size=%d, classes=%d)",
 510                    table_size(), number_of_entries());
 511     tty->print_cr("^ indicates that initiating loader is different from "
 512                   "defining loader");
 513   }
 514 
 515   for (int index = 0; index < table_size(); index++) {
 516     for (DictionaryEntry* probe = bucket(index);
 517                           probe != NULL;
 518                           probe = probe->next()) {
 519       Klass* e = probe->klass();
 520       ClassLoaderData* loader_data =  probe->loader_data();
 521       bool is_defining_class =
 522          (loader_data == e->class_loader_data());
 523       if (details) {
 524         tty->print("%4d: ", index);
 525       }
 526       tty->print("%s%s", ((!details) || is_defining_class) ? " " : "^",
 527                  e->external_name());
 528 
 529       if (details) {
 530         tty->print(", loader ");
 531         if (loader_data != NULL) {
 532           loader_data->print_value();
 533         } else {
 534           tty->print("NULL");
 535         }
 536       }
 537       tty->cr();
 538     }
 539   }
 540 
 541   if (details) {
 542     tty->cr();
 543     _pd_cache_table->print();
 544   }
 545   tty->cr();
 546 }
 547 
 548 void DictionaryEntry::verify() {
 549   Klass* e = klass();
 550   ClassLoaderData* cld = loader_data();
 551   guarantee(e->is_instance_klass(),
 552                           "Verify of system dictionary failed");








 553   // class loader must be present;  a null class loader is the
 554   // boostrap loader
 555   guarantee(cld != NULL || DumpSharedSpaces ||
 556             cld->class_loader() == NULL ||
 557             cld->class_loader()->is_instance(),
 558             "checking type of class_loader");
 559   e->verify();
 560   verify_protection_domain_set();
 561 }
 562 
 563 void Dictionary::verify() {
 564   guarantee(number_of_entries() >= 0, "Verify of system dictionary failed");
 565   verify_table<DictionaryEntry>("System Dictionary");
 566   _pd_cache_table->verify();

 567 }
 568 


  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/protectionDomainCache.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/systemDictionaryShared.hpp"
  32 #include "memory/iterator.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/atomic.hpp"
  36 #include "runtime/orderAccess.inline.hpp"
  37 #include "utilities/hashtable.inline.hpp"
  38 



  39 size_t Dictionary::entry_size() {
  40   if (DumpSharedSpaces) {
  41     return SystemDictionaryShared::dictionary_entry_size();
  42   } else {
  43     return sizeof(DictionaryEntry);
  44   }
  45 }
  46 
  47 Dictionary::Dictionary(ClassLoaderData* loader_data, int table_size)
  48   : _loader_data(loader_data), Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size()) {



  49 };
  50 
  51 
  52 Dictionary::Dictionary(ClassLoaderData* loader_data,
  53                        int table_size, HashtableBucket<mtClass>* t,
  54                        int number_of_entries)
  55   : _loader_data(loader_data), Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries) {



  56 };
  57 
  58 Dictionary::~Dictionary() {
  59   DictionaryEntry* probe = NULL;
  60   for (int index = 0; index < table_size(); index++) {
  61     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
  62       probe = *p;
  63       *p = probe->next();
  64       free_entry(probe);
  65     }
  66   }
  67   assert(number_of_entries() == 0, "should have removed all entries");
  68   assert(new_entry_free_list() == NULL, "entry present on Dictionary's free list");
  69   free_buckets();
  70 }
  71 
  72 DictionaryEntry* Dictionary::new_entry(unsigned int hash, InstanceKlass* klass) {
  73   DictionaryEntry* entry = (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::allocate_new_entry(hash, klass);


  74   entry->set_pd_set(NULL);
  75   assert(klass->is_instance_klass(), "Must be");
  76   if (DumpSharedSpaces) {
  77     SystemDictionaryShared::init_shared_dictionary_entry(klass, entry);
  78   }
  79   return entry;
  80 }
  81 
  82 
  83 void Dictionary::free_entry(DictionaryEntry* entry) {
  84   // avoid recursion when deleting linked list
  85   while (entry->pd_set() != NULL) {
  86     ProtectionDomainEntry* to_delete = entry->pd_set();
  87     entry->set_pd_set(to_delete->next());
  88     delete to_delete;
  89   }
  90   // Unlink from the Hashtable prior to freeing
  91   unlink_entry(entry);
  92   FREE_C_HEAP_ARRAY(char, entry);
  93 }
  94 
  95 
  96 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
  97 #ifdef ASSERT
  98   if (protection_domain == klass()->protection_domain()) {
  99     // Ensure this doesn't show up in the pd_set (invariant)
 100     bool in_pd_set = false;
 101     for (ProtectionDomainEntry* current = _pd_set;
 102                                 current != NULL;
 103                                 current = current->next()) {
 104       if (current->protection_domain() == protection_domain) {
 105         in_pd_set = true;
 106         break;
 107       }
 108     }
 109     if (in_pd_set) {
 110       assert(false, "A klass's protection domain should not show up "
 111                     "in its sys. dict. PD set");
 112     }
 113   }
 114 #endif /* ASSERT */
 115 
 116   if (protection_domain == klass()->protection_domain()) {
 117     // Succeeds trivially
 118     return true;
 119   }
 120 
 121   for (ProtectionDomainEntry* current = _pd_set;
 122                               current != NULL;
 123                               current = current->next()) {
 124     if (current->protection_domain() == protection_domain) return true;
 125   }
 126   return false;
 127 }
 128 
 129 
 130 void DictionaryEntry::add_protection_domain(Dictionary* dict, Handle protection_domain) {
 131   assert_locked_or_safepoint(SystemDictionary_lock);
 132   if (!contains_protection_domain(protection_domain())) {
 133     ProtectionDomainCacheEntry* entry = SystemDictionary::cache_get(protection_domain);
 134     ProtectionDomainEntry* new_head =
 135                 new ProtectionDomainEntry(entry, _pd_set);
 136     // Warning: Preserve store ordering.  The SystemDictionary is read
 137     //          without locks.  The new ProtectionDomainEntry must be
 138     //          complete before other threads can be allowed to see it
 139     //          via a store to _pd_set.
 140     OrderAccess::release_store_ptr(&_pd_set, new_head);
 141   }
 142   if (log_is_enabled(Trace, protectiondomain)) {
 143     ResourceMark rm;
 144     outputStream* log = Log(protectiondomain)::trace_stream();
 145     print_count(log);
 146   }
 147 }
 148 
 149 
 150 void Dictionary::do_unloading() {
 151   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 152 
 153   // The NULL class loader doesn't initiate loading classes from other class loaders
 154   if (loader_data() == ClassLoaderData::the_null_class_loader_data()) {
 155     return;
 156   }
 157 
 158   // Remove unloaded entries and classes from this dictionary
 159   DictionaryEntry* probe = NULL;
 160   for (int index = 0; index < table_size(); index++) {
 161     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
 162       probe = *p;
 163       InstanceKlass* ik = probe->klass();








 164       ClassLoaderData* k_def_class_loader_data = ik->class_loader_data();
 165 
 166       // If the klass that this loader initiated is dead,









 167       // (determined by checking the defining class loader)
 168       // remove this entry.

 169       if (k_def_class_loader_data->is_unloading()) {
 170         assert(k_def_class_loader_data != loader_data(),


 171                "cannot have live defining loader and unreachable klass");







 172         *p = probe->next();



 173         free_entry(probe);
 174         continue;
 175       }

 176       p = probe->next_addr();
 177     }
 178   }
 179 }
 180 














 181 void Dictionary::remove_classes_in_error_state() {
 182   assert(DumpSharedSpaces, "supported only when dumping");
 183   DictionaryEntry* probe = NULL;
 184   for (int index = 0; index < table_size(); index++) {
 185     for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
 186       probe = *p;
 187       InstanceKlass* ik = probe->klass();
 188       if (ik->is_in_error_state()) { // purge this entry
 189         *p = probe->next();



 190         free_entry(probe);
 191         ResourceMark rm;
 192         tty->print_cr("Preload Warning: Removed error class: %s", ik->external_name());
 193         continue;
 194       }
 195 
 196       p = probe->next_addr();
 197     }
 198   }
 199 }
 200 
 201 //   Just the classes from defining class loaders
 202 void Dictionary::classes_do(void f(InstanceKlass*)) {
 203   for (int index = 0; index < table_size(); index++) {
 204     for (DictionaryEntry* probe = bucket(index);
 205                           probe != NULL;
 206                           probe = probe->next()) {
 207       InstanceKlass* k = probe->klass();
 208       if (loader_data() == k->class_loader_data()) {
 209         f(k);
 210       }
 211     }
 212   }
 213 }
 214 
 215 // Added for initialize_itable_for_klass to handle exceptions
 216 //   Just the classes from defining class loaders
 217 void Dictionary::classes_do(void f(InstanceKlass*, TRAPS), TRAPS) {
 218   for (int index = 0; index < table_size(); index++) {
 219     for (DictionaryEntry* probe = bucket(index);
 220                           probe != NULL;
 221                           probe = probe->next()) {
 222       InstanceKlass* k = probe->klass();
 223       if (loader_data() == k->class_loader_data()) {
 224         f(k, CHECK);
 225       }
 226     }
 227   }
 228 }
 229 
 230 // All classes, and their class loaders, including initiating class loaders
 231 void Dictionary::all_entries_do(void f(InstanceKlass*, ClassLoaderData*)) {

 232   for (int index = 0; index < table_size(); index++) {
 233     for (DictionaryEntry* probe = bucket(index);
 234                           probe != NULL;
 235                           probe = probe->next()) {
 236       InstanceKlass* k = probe->klass();
 237       f(k, loader_data());
 238     }
 239   }
 240 }
 241 
























 242 
 243 // Add a loaded class to the system dictionary.
 244 // Readers of the SystemDictionary aren't always locked, so _buckets
 245 // is volatile. The store of the next field in the constructor is
 246 // also cast to volatile;  we do this to ensure store order is maintained
 247 // by the compilers.
 248 
 249 void Dictionary::add_klass(int index, unsigned int hash, Symbol* class_name,
 250                            InstanceKlass* obj) {
 251   assert_locked_or_safepoint(SystemDictionary_lock);
 252   assert(obj != NULL, "adding NULL obj");
 253   assert(obj->name() == class_name, "sanity check on name");

 254 
 255   DictionaryEntry* entry = new_entry(hash, obj);


 256   add_entry(index, entry);
 257 }
 258 
 259 
 260 // This routine does not lock the system dictionary.
 261 //
 262 // Since readers don't hold a lock, we must make sure that system
 263 // dictionary entries are only removed at a safepoint (when only one
 264 // thread is running), and are added to in a safe way (all links must
 265 // be updated in an MT-safe manner).
 266 //
 267 // Callers should be aware that an entry could be added just after
 268 // _buckets[index] is read here, so the caller will not see the new entry.
 269 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
 270                                        Symbol* class_name) {

 271   for (DictionaryEntry* entry = bucket(index);
 272                         entry != NULL;
 273                         entry = entry->next()) {
 274     if (entry->hash() == hash && entry->equals(class_name)) {
 275       if (!DumpSharedSpaces || SystemDictionaryShared::is_builtin(entry)) {
 276         return entry;
 277       }
 278     }
 279   }
 280   return NULL;
 281 }
 282 
 283 
 284 InstanceKlass* Dictionary::find(int index, unsigned int hash, Symbol* name,
 285                                 Handle protection_domain) {
 286   DictionaryEntry* entry = get_entry(index, hash, name);
 287   if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
 288     return entry->klass();
 289   } else {
 290     return NULL;
 291   }
 292 }
 293 
 294 
 295 InstanceKlass* Dictionary::find_class(int index, unsigned int hash,
 296                                       Symbol* name) {
 297   assert_locked_or_safepoint(SystemDictionary_lock);
 298   assert (index == index_for(name), "incorrect index?");
 299 
 300   DictionaryEntry* entry = get_entry(index, hash, name);
 301   return (entry != NULL) ? entry->klass() : NULL;
 302 }
 303 
 304 
 305 // Variant of find_class for shared classes.  No locking required, as
 306 // that table is static.
 307 
 308 InstanceKlass* Dictionary::find_shared_class(int index, unsigned int hash,
 309                                              Symbol* name) {
 310   assert (index == index_for(name), "incorrect index?");
 311 
 312   DictionaryEntry* entry = get_entry(index, hash, name);
 313   return (entry != NULL) ? entry->klass() : NULL;
 314 }
 315 
 316 
 317 void Dictionary::add_protection_domain(int index, unsigned int hash,
 318                                        InstanceKlass* klass,
 319                                        Handle protection_domain,
 320                                        TRAPS) {
 321   Symbol*  klass_name = klass->name();
 322   DictionaryEntry* entry = get_entry(index, hash, klass_name);
 323 
 324   assert(entry != NULL,"entry must be present, we just created it");
 325   assert(protection_domain() != NULL,
 326          "real protection domain should be present");
 327 
 328   entry->add_protection_domain(this, protection_domain);
 329 
 330   assert(entry->contains_protection_domain(protection_domain()),
 331          "now protection domain should be present");
 332 }
 333 
 334 
 335 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash,
 336                                             Symbol* name,

 337                                             Handle protection_domain) {
 338   DictionaryEntry* entry = get_entry(index, hash, name);
 339   return entry->is_valid_protection_domain(protection_domain);
 340 }
 341 
 342 
 343 void Dictionary::reorder_dictionary() {
 344 
 345   // Copy all the dictionary entries into a single master list.
 346 
 347   DictionaryEntry* master_list = NULL;
 348   for (int i = 0; i < table_size(); ++i) {
 349     DictionaryEntry* p = bucket(i);
 350     while (p != NULL) {
 351       DictionaryEntry* tmp;
 352       tmp = p->next();
 353       p->set_next(master_list);
 354       master_list = p;
 355       p = tmp;
 356     }
 357     set_entry(i, NULL);
 358   }
 359 
 360   // Add the dictionary entries back to the list in the correct buckets.
 361   while (master_list != NULL) {
 362     DictionaryEntry* p = master_list;
 363     master_list = master_list->next();
 364     p->set_next(NULL);
 365     Symbol* class_name = p->klass()->name();
 366     // Since the null class loader data isn't copied to the CDS archive,
 367     // compute the hash with NULL for loader data.
 368     unsigned int hash = compute_hash(class_name);
 369     int index = hash_to_index(hash);
 370     p->set_hash(hash);

 371     p->set_next(bucket(index));
 372     set_entry(index, p);
 373   }
 374 }
 375 
 376 
 377 SymbolPropertyTable::SymbolPropertyTable(int table_size)
 378   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry))
 379 {
 380 }
 381 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t,
 382                                          int number_of_entries)
 383   : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries)
 384 {
 385 }
 386 
 387 
 388 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash,
 389                                                      Symbol* sym,
 390                                                      intptr_t sym_mode) {


 431 }
 432 
 433 
 434 // ----------------------------------------------------------------------------
 435 
 436 void Dictionary::print(bool details) {
 437   ResourceMark rm;
 438 
 439   if (details) {
 440     tty->print_cr("Java system dictionary (table_size=%d, classes=%d)",
 441                    table_size(), number_of_entries());
 442     tty->print_cr("^ indicates that initiating loader is different from "
 443                   "defining loader");
 444   }
 445 
 446   for (int index = 0; index < table_size(); index++) {
 447     for (DictionaryEntry* probe = bucket(index);
 448                           probe != NULL;
 449                           probe = probe->next()) {
 450       Klass* e = probe->klass();

 451       bool is_defining_class =
 452          (loader_data() == e->class_loader_data());
 453       if (details) {
 454         tty->print("%4d: ", index);
 455       }
 456       tty->print("%s%s", ((!details) || is_defining_class) ? " " : "^",
 457                  e->external_name());
 458 
 459       if (details) {
 460         tty->print(", loader ");
 461         e->class_loader_data()->print_value();




 462       }
 463       tty->cr();
 464     }
 465   }





 466   tty->cr();
 467 }
 468 
 469 void DictionaryEntry::verify() {
 470   Klass* e = klass();

 471   guarantee(e->is_instance_klass(),
 472                           "Verify of system dictionary failed");
 473   e->verify();
 474   verify_protection_domain_set();
 475 }
 476 
 477 void Dictionary::verify() {
 478   guarantee(number_of_entries() >= 0, "Verify of system dictionary failed");
 479 
 480   ClassLoaderData* cld = loader_data();
 481   // class loader must be present;  a null class loader is the
 482   // boostrap loader
 483   guarantee(cld != NULL || DumpSharedSpaces ||
 484             cld->class_loader() == NULL ||
 485             cld->class_loader()->is_instance(),
 486             "checking type of class_loader");



 487 
 488   ResourceMark rm;
 489   
 490   stringStream tempst;
 491   tempst.print("System Dictionary for %s", cld->loader_name());
 492   verify_table<DictionaryEntry>(tempst.as_string());
 493 }
 494 
< prev index next >