1 /*
2  * Copyright (c) 1997, 2020, 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_CLASSFILE_STRINGTABLE_HPP
26 #define SHARE_CLASSFILE_STRINGTABLE_HPP
27 
28 #include "memory/allocation.hpp"
29 #include "memory/padded.hpp"
30 #include "oops/oop.hpp"
31 #include "oops/weakHandle.hpp"
32 #include "utilities/tableStatistics.hpp"
33 
34 class CompactHashtableWriter;
35 class DumpedInternedStrings;
36 class JavaThread;
37 class SerializeClosure;
38 
39 class StringTable;
40 class StringTableConfig;
41 class StringTableCreateEntry;
42 
43 class StringTable : public CHeapObj<mtSymbol>{
44   friend class VMStructs;
45   friend class Symbol;
46   friend class StringTableConfig;
47   friend class StringTableCreateEntry;
48 
49   static volatile bool _has_work;
50 
51   // Set if one bucket is out of balance due to hash algorithm deficiency
52   static volatile bool _needs_rehashing;
53 
54   static OopStorage* _oop_storage;
55 
56   static void grow(JavaThread* jt);
57   static void clean_dead_entries(JavaThread* jt);
58 
59   static double get_load_factor();
60   static double get_dead_factor(size_t num_dead);
61 
62   // GC support
63 
64   // Callback for GC to notify of changes that might require cleaning or resize.
65   static void gc_notification(size_t num_dead);
66   static void trigger_concurrent_work();
67 
68   static size_t item_added();
69   static void item_removed();
70 
71   static oop intern(Handle string_or_null_h, const jchar* name, int len, TRAPS);
72   static oop do_intern(Handle string_or_null, const jchar* name, int len, uintx hash, TRAPS);
73   static oop do_lookup(const jchar* name, int len, uintx hash);
74 
75   static void print_table_statistics(outputStream* st, const char* table_name);
76 
77   static bool do_rehash();
78 
79  public:
80   static size_t table_size();
81   static TableStatistics get_table_statistics();
82 
83   static void create_table();
84 
85   static void do_concurrent_work(JavaThread* jt);
86   static bool has_work();
87 
88   // Probing
89   static oop lookup(Symbol* symbol);
90   static oop lookup(const jchar* chars, int length);
91 
92   // Interning
93   static oop intern(Symbol* symbol, TRAPS);
94   static oop intern(oop string, TRAPS);
95   static oop intern(const char *utf8_string, TRAPS);
96 
97   // Rehash the string table if it gets out of balance
98   static void rehash_table();
needs_rehashing()99   static bool needs_rehashing() { return _needs_rehashing; }
update_needs_rehash(bool rehash)100   static inline void update_needs_rehash(bool rehash) {
101     if (rehash) {
102       _needs_rehashing = true;
103     }
104   }
105 
106   // Sharing
107  private:
108   static oop lookup_shared(const jchar* name, int len, unsigned int hash) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
109  public:
110   static oop create_archived_string(oop s, Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
111   static void shared_oops_do(OopClosure* f) NOT_CDS_JAVA_HEAP_RETURN;
112   static void write_to_archive(const DumpedInternedStrings* dumped_interned_strings) NOT_CDS_JAVA_HEAP_RETURN;
113   static void serialize_shared_table_header(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
114 
115   // Jcmd
116   static void dump(outputStream* st, bool verbose=false);
117   // Debugging
118   static size_t verify_and_compare_entries();
119   static void verify();
120 };
121 
122 #endif // SHARE_CLASSFILE_STRINGTABLE_HPP
123