1 /*
  2  * Copyright (c) 1997, 2025, 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/allStatic.hpp"
 29 #include "memory/padded.hpp"
 30 #include "oops/oop.hpp"
 31 #include "oops/oopHandle.hpp"
 32 #include "oops/weakHandle.hpp"
 33 #include "utilities/tableStatistics.hpp"
 34 
 35 class CompactHashtableWriter;
 36 class JavaThread;
 37 class SerializeClosure;
 38 
 39 class StringTableConfig;
 40 
 41 class StringTable : AllStatic {
 42   friend class StringTableConfig;
 43   class VerifyCompStrings;
 44   static volatile bool _has_work;
 45 
 46   // Set if one bucket is out of balance due to hash algorithm deficiency
 47   static volatile bool _needs_rehashing;
 48 
 49   static OopStorage* _oop_storage;
 50 
 51   static void grow(JavaThread* jt);
 52   static void clean_dead_entries(JavaThread* jt);
 53 
 54   static double get_load_factor();
 55   static double get_dead_factor(size_t num_dead);
 56 
 57 public:
 58   typedef struct StringWrapperInternal StringWrapper;
 59 
 60   // Unnamed int needed to fit CompactHashtable's equals type signature
 61   static bool wrapped_string_equals(oop java_string, const StringWrapper& wrapped_str, int = 0);
 62 
 63 private:
 64   static const char* get_symbol_utf8(const StringWrapper& symbol_str);
 65   static unsigned int hash_wrapped_string(const StringWrapper& wrapped_str);
 66   static const jchar* to_unicode(const StringWrapper& wrapped_str, int &len, TRAPS);
 67   static Handle handle_from_wrapped_string(const StringWrapper& wrapped_str, TRAPS);
 68 
 69   // GC support
 70 
 71   // Callback for GC to notify of changes that might require cleaning or resize.
 72   static void gc_notification(size_t num_dead);
 73   static void trigger_concurrent_work();
 74 
 75   static void item_added();
 76   static void item_removed();
 77   static size_t items_count_acquire();
 78 
 79   static oop intern(const StringWrapper& name, TRAPS);
 80   static oop do_intern(const StringWrapper& name, uintx hash, TRAPS);
 81   static oop do_lookup(const StringWrapper& name, uintx hash);
 82 
 83   static void print_table_statistics(outputStream* st);
 84 
 85  public:
 86   static size_t table_size();
 87   static TableStatistics get_table_statistics();
 88 
 89   static void create_table();
 90 
 91   static void do_concurrent_work(JavaThread* jt);
 92   static bool has_work();
 93 
 94   // Probing
 95   static oop lookup(Symbol* symbol);
 96   static oop lookup(const jchar* chars, int length);
 97 
 98   // Interning
 99   static oop intern(Symbol* symbol, TRAPS);
100   static oop intern(oop string, TRAPS);
101   static oop intern(const char* utf8_string, TRAPS);
102 
103   // Rehash the string table if it gets out of balance
104 private:
105   static bool should_grow();
106   static bool maybe_rehash_table();
107 public:
108   static void rehash_table();
109   static bool needs_rehashing() { return _needs_rehashing; }
110   static inline void update_needs_rehash(bool rehash);
111 
112   // Sharing
113 #if INCLUDE_CDS_JAVA_HEAP
114   static inline oop read_string_from_compact_hashtable(address base_address, u4 index);
115 
116 private:
117   static bool _is_two_dimensional_shared_strings_array;
118   static OopHandle _shared_strings_array;
119   static int _shared_strings_array_root_index;
120 
121   // All the shared strings are referenced through _shared_strings_array to keep them alive.
122   // Each shared string is stored as a 32-bit index in ::_shared_table. The index
123   // is interpreted in two ways:
124   //
125   // [1] _is_two_dimensional_shared_strings_array = false: _shared_strings_array is an Object[].
126   //     Each shared string is stored as _shared_strings_array[index]
127   //
128   // [2] _is_two_dimensional_shared_strings_array = true: _shared_strings_array is an Object[][]
129   //     This happens when there are too many elements in the shared table. We store them
130   //     using two levels of objArrays, such that none of the arrays are too big for
131   //     ArchiveHeapWriter::is_too_large_to_archive(). In this case, the index is splited into two
132   //     parts. Each shared string is stored as _shared_strings_array[primary_index][secondary_index]:
133   //
134   //           [bits 31 .. 14][ bits 13 .. 0  ]
135   //            primary_index  secondary_index
136   const static int _secondary_array_index_bits = 14;
137   const static int _secondary_array_max_length = 1 << _secondary_array_index_bits;
138   const static int _secondary_array_index_mask = _secondary_array_max_length - 1;
139 
140   // make sure _secondary_array_index_bits is not too big
141   static void verify_secondary_array_index_bits() PRODUCT_RETURN;
142 #endif // INCLUDE_CDS_JAVA_HEAP
143 
144  private:
145   static oop lookup_shared(const StringWrapper& name, unsigned int hash) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
146  public:
147   static oop lookup_shared(const jchar* name, int len) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
148   static size_t shared_entry_count() NOT_CDS_JAVA_HEAP_RETURN_(0);
149   static void allocate_shared_strings_array(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
150   static oop init_shared_strings_array() NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
151   static void write_shared_table() NOT_CDS_JAVA_HEAP_RETURN;
152   static void set_shared_strings_array_index(int root_index) NOT_CDS_JAVA_HEAP_RETURN;
153   static void serialize_shared_table_header(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
154 
155   // Jcmd
156   static void dump(outputStream* st, bool verbose=false);
157   // Debugging
158   static size_t verify_and_compare_entries();
159   static void verify();
160 };
161 
162 #endif // SHARE_CLASSFILE_STRINGTABLE_HPP