1 /*
   2  * Copyright (c) 2017, 2018, 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 #ifndef SHARE_VM_CLASSFILE_PROTECTIONDOMAINCACHE_HPP
  26 #define SHARE_VM_CLASSFILE_PROTECTIONDOMAINCACHE_HPP
  27 
  28 #include "oops/oop.hpp"
  29 #include "oops/weakHandle.hpp"
  30 #include "memory/iterator.hpp"
  31 #include "utilities/hashtable.hpp"
  32 
  33 // This class caches the approved protection domains that can access loaded classes.
  34 // Dictionary entry pd_set point to entries in this hashtable.   Please refer
  35 // to dictionary.hpp pd_set for more information about how protection domain entries
  36 // are used.
  37 // This table is walked during GC, rather than the class loader data graph dictionaries.
  38 class ProtectionDomainCacheEntry : public HashtableEntry<ClassLoaderWeakHandle, mtClass> {
  39   friend class VMStructs;
  40  public:
  41   oop object();
  42   oop object_no_keepalive();
  43 
  44   ProtectionDomainCacheEntry* next() {
  45     return (ProtectionDomainCacheEntry*)HashtableEntry<ClassLoaderWeakHandle, mtClass>::next();
  46   }
  47 
  48   ProtectionDomainCacheEntry** next_addr() {
  49     return (ProtectionDomainCacheEntry**)HashtableEntry<ClassLoaderWeakHandle, mtClass>::next_addr();
  50   }
  51 
  52   void verify();
  53 };
  54 
  55 // The ProtectionDomainCacheTable contains all protection domain oops. The
  56 // dictionary entries reference its entries instead of having references to oops
  57 // directly.
  58 // This is used to speed up system dictionary iteration: the oops in the
  59 // protection domain are the only ones referring the Java heap. So when there is
  60 // need to update these, instead of going over every entry of the system dictionary,
  61 // we only need to iterate over this set.
  62 // The amount of different protection domains used is typically magnitudes smaller
  63 // than the number of system dictionary entries (loaded classes).
  64 class ProtectionDomainCacheTable : public Hashtable<ClassLoaderWeakHandle, mtClass> {
  65   friend class VMStructs;
  66 private:
  67   ProtectionDomainCacheEntry* bucket(int i) const {
  68     return (ProtectionDomainCacheEntry*) Hashtable<ClassLoaderWeakHandle, mtClass>::bucket(i);
  69   }
  70 
  71   // The following method is not MT-safe and must be done under lock.
  72   ProtectionDomainCacheEntry** bucket_addr(int i) {
  73     return (ProtectionDomainCacheEntry**) Hashtable<ClassLoaderWeakHandle, mtClass>::bucket_addr(i);
  74   }
  75 
  76   ProtectionDomainCacheEntry* new_entry(unsigned int hash, ClassLoaderWeakHandle protection_domain) {
  77     ProtectionDomainCacheEntry* entry = (ProtectionDomainCacheEntry*)
  78       Hashtable<ClassLoaderWeakHandle, mtClass>::new_entry(hash, protection_domain);
  79     return entry;
  80   }
  81 
  82   static unsigned int compute_hash(Handle protection_domain);
  83 
  84   int index_for(Handle protection_domain);
  85   ProtectionDomainCacheEntry* add_entry(int index, unsigned int hash, Handle protection_domain);
  86   ProtectionDomainCacheEntry* find_entry(int index, Handle protection_domain);
  87 
  88   bool _dead_entries;
  89 
  90 public:
  91   ProtectionDomainCacheTable(int table_size);
  92   ProtectionDomainCacheEntry* get(Handle protection_domain);
  93 
  94   void unlink();
  95 
  96   void print_on(outputStream* st) const;
  97   void verify();
  98 
  99   bool has_work() { return _dead_entries; }
 100   void trigger_cleanup();
 101 };
 102 
 103 
 104 class ProtectionDomainEntry :public CHeapObj<mtClass> {
 105   friend class VMStructs;
 106  public:
 107   ProtectionDomainEntry* _next;
 108   ProtectionDomainCacheEntry* _pd_cache;
 109 
 110   ProtectionDomainEntry(ProtectionDomainCacheEntry* pd_cache, ProtectionDomainEntry* next) {
 111     _pd_cache = pd_cache;
 112     _next     = next;
 113   }
 114 
 115   ProtectionDomainEntry* next() { return _next; }
 116   void set_next(ProtectionDomainEntry* entry) { _next = entry; }
 117   oop object();
 118   oop object_no_keepalive();
 119 };
 120 #endif // SHARE_VM_CLASSFILE_PROTECTIONDOMAINCACHE_HPP