1 /* 2 * Copyright (c) 1997, 2017, 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_STRINGTABLE_HPP 26 #define SHARE_VM_CLASSFILE_STRINGTABLE_HPP 27 28 #include "memory/allocation.inline.hpp" 29 #include "utilities/hashtable.hpp" 30 31 template <class T, class N> class CompactHashtable; 32 class CompactStringTableWriter; 33 class FileMapInfo; 34 class SerializeClosure; 35 36 class StringTable : public RehashableHashtable<oop, mtSymbol> { 37 friend class VMStructs; 38 friend class Symbol; 39 40 private: 41 // The string table 42 static StringTable* _the_table; 43 44 // Shared string table 45 static CompactHashtable<oop, char> _shared_table; 46 static bool _ignore_shared_strings; 47 48 // Set if one bucket is out of balance due to hash algorithm deficiency 49 static bool _needs_rehashing; 50 51 // Claimed high water mark for parallel chunked scanning 52 static volatile int _parallel_claimed_idx; 53 54 static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS); 55 oop basic_add(int index, Handle string_or_null, jchar* name, int len, 56 unsigned int hashValue, TRAPS); 57 58 oop lookup_in_main_table(int index, jchar* chars, int length, unsigned int hashValue); 59 static oop lookup_shared(jchar* name, int len, unsigned int hash); 60 61 // Apply the give oop closure to the entries to the buckets 62 // in the range [start_idx, end_idx). 63 static void buckets_oops_do(OopClosure* f, int start_idx, int end_idx); 64 // Unlink or apply the give oop closure to the entries to the buckets 65 // in the range [start_idx, end_idx). 66 static void buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, int* processed, int* removed); 67 68 // Hashing algorithm, used as the hash value used by the 69 // StringTable for bucket selection and comparison (stored in the 70 // HashtableEntry structures). This is used in the String.intern() method. 71 static unsigned int hash_string(const jchar* s, int len); 72 static unsigned int hash_string(oop string); 73 static unsigned int alt_hash_string(const jchar* s, int len); 74 75 StringTable() : RehashableHashtable<oop, mtSymbol>((int)StringTableSize, 76 sizeof (HashtableEntry<oop, mtSymbol>)) {} 77 78 StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries) 79 : RehashableHashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t, 80 number_of_entries) {} 81 public: 82 // The string table 83 static StringTable* the_table() { return _the_table; } 84 85 // Size of one bucket in the string table. Used when checking for rollover. 86 static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); } 87 88 static void create_table() { 89 assert(_the_table == NULL, "One string table allowed."); 90 _the_table = new StringTable(); 91 } 92 93 // GC support 94 // Delete pointers to otherwise-unreachable objects. 95 static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f) { 96 int processed = 0; 97 int removed = 0; 98 unlink_or_oops_do(cl, f, &processed, &removed); 99 } 100 static void unlink(BoolObjectClosure* cl) { 101 int processed = 0; 102 int removed = 0; 103 unlink_or_oops_do(cl, NULL, &processed, &removed); 104 } 105 static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed); 106 static void unlink(BoolObjectClosure* cl, int* processed, int* removed) { 107 unlink_or_oops_do(cl, NULL, processed, removed); 108 } 109 // Serially invoke "f->do_oop" on the locations of all oops in the table. 110 static void oops_do(OopClosure* f); 111 112 // Possibly parallel versions of the above 113 static void possibly_parallel_unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed); 114 static void possibly_parallel_unlink(BoolObjectClosure* cl, int* processed, int* removed) { 115 possibly_parallel_unlink_or_oops_do(cl, NULL, processed, removed); 116 } 117 static void possibly_parallel_oops_do(OopClosure* f); 118 119 // Internal test. 120 static void test_alt_hash() PRODUCT_RETURN; 121 122 // Probing 123 static oop lookup(Symbol* symbol); 124 static oop lookup(jchar* chars, int length); 125 126 // Interning 127 static oop intern(Symbol* symbol, TRAPS); 128 static oop intern(oop string, TRAPS); 129 static oop intern(const char *utf8_string, TRAPS); 130 131 // Debugging 132 static void verify(); 133 static void dump(outputStream* st, bool verbose=false); 134 135 enum VerifyMesgModes { 136 _verify_quietly = 0, 137 _verify_with_mesgs = 1 138 }; 139 140 enum VerifyRetTypes { 141 _verify_pass = 0, 142 _verify_fail_continue = 1, 143 _verify_fail_done = 2 144 }; 145 146 static VerifyRetTypes compare_entries(int bkt1, int e_cnt1, 147 HashtableEntry<oop, mtSymbol>* e_ptr1, 148 int bkt2, int e_cnt2, 149 HashtableEntry<oop, mtSymbol>* e_ptr2); 150 static VerifyRetTypes verify_entry(int bkt, int e_cnt, 151 HashtableEntry<oop, mtSymbol>* e_ptr, 152 VerifyMesgModes mesg_mode); 153 static int verify_and_compare_entries(); 154 155 // Sharing 156 static void ignore_shared_strings(bool v) { _ignore_shared_strings = v; } 157 static bool shared_string_ignored() { return _ignore_shared_strings; } 158 static void shared_oops_do(OopClosure* f); 159 static bool copy_shared_string(GrowableArray<MemRegion> *string_space, 160 CompactStringTableWriter* ch_table); 161 static void serialize(SerializeClosure* soc, GrowableArray<MemRegion> *string_space, 162 size_t* space_size); 163 static void reverse() { 164 the_table()->Hashtable<oop, mtSymbol>::reverse(); 165 } 166 167 // Rehash the symbol table if it gets out of balance 168 static void rehash_table(); 169 static bool needs_rehashing() { return _needs_rehashing; } 170 171 // Parallel chunked scanning 172 static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; } 173 static int parallel_claimed_index() { return _parallel_claimed_idx; } 174 }; 175 #endif // SHARE_VM_CLASSFILE_STRINGTABLE_HPP