stringTable.hpp revision 10941:857efca82258
1/*
2 * Copyright (c) 1997, 2016, 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
31template <class T, class N> class CompactHashtable;
32class CompactStringTableWriter;
33class FileMapInfo;
34class SerializeClosure;
35
36class StringTable : public RehashableHashtable<oop, mtSymbol> {
37  friend class VMStructs;
38  friend class Symbol;
39
40private:
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);
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  StringTable() : RehashableHashtable<oop, mtSymbol>((int)StringTableSize,
69                              sizeof (HashtableEntry<oop, mtSymbol>)) {}
70
71  StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
72    : RehashableHashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t,
73                     number_of_entries) {}
74public:
75  // The string table
76  static StringTable* the_table() { return _the_table; }
77
78  // Size of one bucket in the string table.  Used when checking for rollover.
79  static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); }
80
81  static void create_table() {
82    assert(_the_table == NULL, "One string table allowed.");
83    _the_table = new StringTable();
84  }
85
86  // GC support
87  //   Delete pointers to otherwise-unreachable objects.
88  static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f) {
89    int processed = 0;
90    int removed = 0;
91    unlink_or_oops_do(cl, f, &processed, &removed);
92  }
93  static void unlink(BoolObjectClosure* cl) {
94    int processed = 0;
95    int removed = 0;
96    unlink_or_oops_do(cl, NULL, &processed, &removed);
97  }
98  static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed);
99  static void unlink(BoolObjectClosure* cl, int* processed, int* removed) {
100    unlink_or_oops_do(cl, NULL, processed, removed);
101  }
102  // Serially invoke "f->do_oop" on the locations of all oops in the table.
103  static void oops_do(OopClosure* f);
104
105  // Possibly parallel versions of the above
106  static void possibly_parallel_unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed);
107  static void possibly_parallel_unlink(BoolObjectClosure* cl, int* processed, int* removed) {
108    possibly_parallel_unlink_or_oops_do(cl, NULL, processed, removed);
109  }
110  static void possibly_parallel_oops_do(OopClosure* f);
111
112  // Hashing algorithm, used as the hash value used by the
113  //     StringTable for bucket selection and comparison (stored in the
114  //     HashtableEntry structures).  This is used in the String.intern() method.
115  static unsigned int hash_string(const jchar* s, int len);
116  static unsigned int hash_string(oop string);
117
118  // Internal test.
119  static void test_alt_hash() PRODUCT_RETURN;
120
121  // Probing
122  static oop lookup(Symbol* symbol);
123  static oop lookup(jchar* chars, int length);
124
125  // Interning
126  static oop intern(Symbol* symbol, TRAPS);
127  static oop intern(oop string, TRAPS);
128  static oop intern(const char *utf8_string, TRAPS);
129
130  // Debugging
131  static void verify();
132  static void dump(outputStream* st, bool verbose=false);
133
134  enum VerifyMesgModes {
135    _verify_quietly    = 0,
136    _verify_with_mesgs = 1
137  };
138
139  enum VerifyRetTypes {
140    _verify_pass          = 0,
141    _verify_fail_continue = 1,
142    _verify_fail_done     = 2
143  };
144
145  static VerifyRetTypes compare_entries(int bkt1, int e_cnt1,
146                                        HashtableEntry<oop, mtSymbol>* e_ptr1,
147                                        int bkt2, int e_cnt2,
148                                        HashtableEntry<oop, mtSymbol>* e_ptr2);
149  static VerifyRetTypes verify_entry(int bkt, int e_cnt,
150                                     HashtableEntry<oop, mtSymbol>* e_ptr,
151                                     VerifyMesgModes mesg_mode);
152  static int verify_and_compare_entries();
153
154  // Sharing
155  static void ignore_shared_strings(bool v) { _ignore_shared_strings = v; }
156  static bool shared_string_ignored()       { return _ignore_shared_strings; }
157  static void shared_oops_do(OopClosure* f);
158  static bool copy_shared_string(GrowableArray<MemRegion> *string_space,
159                                 CompactStringTableWriter* ch_table);
160  static void serialize(SerializeClosure* soc, GrowableArray<MemRegion> *string_space,
161                        size_t* space_size);
162  static void reverse() {
163    the_table()->Hashtable<oop, mtSymbol>::reverse();
164  }
165
166  // Rehash the symbol table if it gets out of balance
167  static void rehash_table();
168  static bool needs_rehashing() { return _needs_rehashing; }
169
170  // Parallel chunked scanning
171  static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; }
172  static int parallel_claimed_index() { return _parallel_claimed_idx; }
173};
174#endif // SHARE_VM_CLASSFILE_STRINGTABLE_HPP
175