1 /*
2 * Copyright (c) 1997, 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
26 #include "precompiled.hpp"
27 #include "classfile/altHashing.hpp"
28 #include "classfile/classLoaderData.hpp"
29 #include "logging/log.hpp"
30 #include "logging/logStream.hpp"
31 #include "memory/allocation.inline.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "oops/symbol.hpp"
34 #include "runtime/atomic.hpp"
35 #include "runtime/os.hpp"
36
37 uint32_t Symbol::pack_length_and_refcount(int length, int refcount) {
38 STATIC_ASSERT(max_symbol_length == ((1 << 16) - 1));
39 STATIC_ASSERT(PERM_REFCOUNT == ((1 << 16) - 1));
40 assert(length >= 0, "negative length");
41 assert(length <= max_symbol_length, "too long symbol");
42 assert(refcount >= 0, "negative refcount");
43 assert(refcount <= PERM_REFCOUNT, "invalid refcount");
44 uint32_t hi = length;
45 uint32_t lo = refcount;
46 return (hi << 16) | lo;
47 }
48
49 Symbol::Symbol(const u1* name, int length, int refcount) {
50 _length_and_refcount = pack_length_and_refcount(length, refcount);
51 _identity_hash = (short)os::random();
52 for (int i = 0; i < length; i++) {
53 byte_at_put(i, name[i]);
54 }
55 }
56
57 void* Symbol::operator new(size_t sz, int len, TRAPS) throw() {
58 int alloc_size = size(len)*wordSize;
59 address res = (address) AllocateHeap(alloc_size, mtSymbol);
60 return res;
61 }
62
63 void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) throw() {
64 int alloc_size = size(len)*wordSize;
65 address res = (address)arena->Amalloc_4(alloc_size);
66 return res;
67 }
68
69 void Symbol::operator delete(void *p) {
70 assert(((Symbol*)p)->refcount() == 0, "should not call this");
71 FreeHeap(p);
72 }
73
74 // ------------------------------------------------------------------
75 // Symbol::starts_with
76 //
77 // Tests if the symbol starts with the specified prefix of the given
78 // length.
79 bool Symbol::starts_with(const char* prefix, int len) const {
80 if (len > utf8_length()) return false;
81 while (len-- > 0) {
82 if (prefix[len] != (char) byte_at(len))
83 return false;
84 }
85 assert(len == -1, "we should be at the beginning");
86 return true;
87 }
88
89
90 // ------------------------------------------------------------------
91 // Symbol::index_of
92 //
93 // Finds if the given string is a substring of this symbol's utf8 bytes.
94 // Return -1 on failure. Otherwise return the first index where str occurs.
95 int Symbol::index_of_at(int i, const char* str, int len) const {
96 assert(i >= 0 && i <= utf8_length(), "oob");
97 if (len <= 0) return 0;
98 char first_char = str[0];
99 address bytes = (address) ((Symbol*)this)->base();
100 address limit = bytes + utf8_length() - len; // inclusive limit
101 address scan = bytes + i;
102 if (scan > limit)
103 return -1;
104 for (; scan <= limit; scan++) {
105 scan = (address) memchr(scan, first_char, (limit + 1 - scan));
106 if (scan == NULL)
107 return -1; // not found
108 assert(scan >= bytes+i && scan <= limit, "scan oob");
109 if (memcmp(scan, str, len) == 0)
110 return (int)(scan - bytes);
111 }
112 return -1;
113 }
114
115
116 char* Symbol::as_C_string(char* buf, int size) const {
117 if (size > 0) {
118 int len = MIN2(size - 1, utf8_length());
119 for (int i = 0; i < len; i++) {
120 buf[i] = byte_at(i);
121 }
122 buf[len] = '\0';
123 }
124 return buf;
125 }
126
127 char* Symbol::as_C_string() const {
128 int len = utf8_length();
129 char* str = NEW_RESOURCE_ARRAY(char, len + 1);
130 return as_C_string(str, len + 1);
131 }
132
133 char* Symbol::as_C_string_flexible_buffer(Thread* t,
134 char* buf, int size) const {
135 char* str;
136 int len = utf8_length();
137 int buf_len = len + 1;
138 if (size < buf_len) {
139 str = NEW_RESOURCE_ARRAY(char, buf_len);
140 } else {
141 str = buf;
142 }
143 return as_C_string(str, buf_len);
144 }
145
146 void Symbol::print_utf8_on(outputStream* st) const {
147 st->print("%s", as_C_string());
148 }
149
150 void Symbol::print_symbol_on(outputStream* st) const {
151 char *s;
152 st = st ? st : tty;
153 {
154 // ResourceMark may not affect st->print(). If st is a string
155 // stream it could resize, using the same resource arena.
156 ResourceMark rm;
157 s = as_quoted_ascii();
158 s = os::strdup(s);
159 }
160 if (s == NULL) {
161 st->print("(null)");
162 } else {
163 st->print("%s", s);
164 os::free(s);
165 }
166 }
167
168 char* Symbol::as_quoted_ascii() const {
169 const char *ptr = (const char *)&_body[0];
170 int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length());
171 char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1);
172 UTF8::as_quoted_ascii(ptr, utf8_length(), result, quoted_length + 1);
173 return result;
174 }
175
176 jchar* Symbol::as_unicode(int& length) const {
177 Symbol* this_ptr = (Symbol*)this;
178 length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
179 jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
180 if (length > 0) {
181 UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
182 }
183 return result;
184 }
185
186 const char* Symbol::as_klass_external_name(char* buf, int size) const {
187 if (size > 0) {
188 char* str = as_C_string(buf, size);
189 int length = (int)strlen(str);
190 // Turn all '/'s into '.'s (also for array klasses)
191 for (int index = 0; index < length; index++) {
192 if (str[index] == '/') {
193 str[index] = '.';
194 }
195 }
196 return str;
197 } else {
198 return buf;
199 }
200 }
201
202 const char* Symbol::as_klass_external_name() const {
203 char* str = as_C_string();
204 int length = (int)strlen(str);
205 // Turn all '/'s into '.'s (also for array klasses)
206 for (int index = 0; index < length; index++) {
207 if (str[index] == '/') {
208 str[index] = '.';
209 }
210 }
211 return str;
212 }
213
214 // Alternate hashing for unbalanced symbol tables.
215 unsigned int Symbol::new_hash(juint seed) {
216 ResourceMark rm;
217 // Use alternate hashing algorithm on this symbol.
218 return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length());
219 }
220
221 // Increment refcount while checking for zero. If the Symbol's refcount becomes zero
222 // a thread could be concurrently removing the Symbol. This is used during SymbolTable
223 // lookup to avoid reviving a dead Symbol.
224 bool Symbol::try_increment_refcount() {
225 uint32_t found = _length_and_refcount;
226 while (true) {
227 uint32_t old_value = found;
228 int refc = extract_refcount(old_value);
229 if (refc == PERM_REFCOUNT) {
230 return true; // sticky max or created permanent
231 } else if (refc == 0) {
232 return false; // dead, can't revive.
233 } else {
234 found = Atomic::cmpxchg(old_value + 1, &_length_and_refcount, old_value);
235 if (found == old_value) {
236 return true; // successfully updated.
237 }
238 // refcount changed, try again.
239 }
240 }
241 }
242
243 // The increment_refcount() is called when not doing lookup. It is assumed that you
244 // have a symbol with a non-zero refcount and it can't become zero while referenced by
245 // this caller.
246 void Symbol::increment_refcount() {
247 if (!try_increment_refcount()) {
248 #ifdef ASSERT
249 print();
250 fatal("refcount has gone to zero");
251 #endif
252 }
253 #ifndef PRODUCT
254 if (refcount() != PERM_REFCOUNT) { // not a permanent symbol
255 NOT_PRODUCT(Atomic::inc(&_total_count);)
256 }
257 #endif
258 }
259
260 // Decrement refcount potentially while racing increment, so we need
261 // to check the value after attempting to decrement so that if another
262 // thread increments to PERM_REFCOUNT the value is not decremented.
263 void Symbol::decrement_refcount() {
264 uint32_t found = _length_and_refcount;
265 while (true) {
266 uint32_t old_value = found;
267 int refc = extract_refcount(old_value);
268 if (refc == PERM_REFCOUNT) {
269 return; // refcount is permanent, permanent is sticky
270 } else if (refc == 0) {
271 #ifdef ASSERT
272 print();
273 fatal("refcount underflow");
274 #endif
275 return;
276 } else {
277 found = Atomic::cmpxchg(old_value - 1, &_length_and_refcount, old_value);
278 if (found == old_value) {
279 return; // successfully updated.
280 }
281 // refcount changed, try again.
282 }
283 }
284 }
285
286 void Symbol::metaspace_pointers_do(MetaspaceClosure* it) {
287 if (log_is_enabled(Trace, cds)) {
288 LogStream trace_stream(Log(cds)::trace());
289 trace_stream.print("Iter(Symbol): %p ", this);
290 print_value_on(&trace_stream);
291 trace_stream.cr();
292 }
293 }
294
295 void Symbol::print_on(outputStream* st) const {
296 if (this == NULL) {
297 st->print_cr("NULL");
298 } else {
299 st->print("Symbol: '");
300 print_symbol_on(st);
301 st->print("'");
302 st->print(" count %d", refcount());
303 }
304 }
305
306 // The print_value functions are present in all builds, to support the
307 // disassembler and error reporting.
308 void Symbol::print_value_on(outputStream* st) const {
309 if (this == NULL) {
310 st->print("NULL");
311 } else {
312 st->print("'");
313 for (int i = 0; i < utf8_length(); i++) {
314 st->print("%c", byte_at(i));
315 }
316 st->print("'");
317 }
318 }
319
320 // SymbolTable prints this in its statistics
321 NOT_PRODUCT(int Symbol::_total_count = 0;)
--- EOF ---