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
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
37Symbol::Symbol(const u1* name, int length, int refcount) {
38  _refcount = refcount;
39  _length = length;
40  _identity_hash = (short)os::random();
41  for (int i = 0; i < _length; i++) {
42    byte_at_put(i, name[i]);
43  }
44}
45
46void* Symbol::operator new(size_t sz, int len, TRAPS) throw() {
47  int alloc_size = size(len)*wordSize;
48  address res = (address) AllocateHeap(alloc_size, mtSymbol);
49  return res;
50}
51
52void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) throw() {
53  int alloc_size = size(len)*wordSize;
54  address res = (address)arena->Amalloc_4(alloc_size);
55  return res;
56}
57
58void Symbol::operator delete(void *p) {
59  assert(((Symbol*)p)->refcount() == 0, "should not call this");
60  FreeHeap(p);
61}
62
63// ------------------------------------------------------------------
64// Symbol::starts_with
65//
66// Tests if the symbol starts with the specified prefix of the given
67// length.
68bool Symbol::starts_with(const char* prefix, int len) const {
69  if (len > utf8_length()) return false;
70  while (len-- > 0) {
71    if (prefix[len] != (char) byte_at(len))
72      return false;
73  }
74  assert(len == -1, "we should be at the beginning");
75  return true;
76}
77
78
79// ------------------------------------------------------------------
80// Symbol::index_of
81//
82// Finds if the given string is a substring of this symbol's utf8 bytes.
83// Return -1 on failure.  Otherwise return the first index where str occurs.
84int Symbol::index_of_at(int i, const char* str, int len) const {
85  assert(i >= 0 && i <= utf8_length(), "oob");
86  if (len <= 0)  return 0;
87  char first_char = str[0];
88  address bytes = (address) ((Symbol*)this)->base();
89  address limit = bytes + utf8_length() - len;  // inclusive limit
90  address scan = bytes + i;
91  if (scan > limit)
92    return -1;
93  for (; scan <= limit; scan++) {
94    scan = (address) memchr(scan, first_char, (limit + 1 - scan));
95    if (scan == NULL)
96      return -1;  // not found
97    assert(scan >= bytes+i && scan <= limit, "scan oob");
98    if (memcmp(scan, str, len) == 0)
99      return (int)(scan - bytes);
100  }
101  return -1;
102}
103
104
105char* Symbol::as_C_string(char* buf, int size) const {
106  if (size > 0) {
107    int len = MIN2(size - 1, utf8_length());
108    for (int i = 0; i < len; i++) {
109      buf[i] = byte_at(i);
110    }
111    buf[len] = '\0';
112  }
113  return buf;
114}
115
116char* Symbol::as_C_string() const {
117  int len = utf8_length();
118  char* str = NEW_RESOURCE_ARRAY(char, len + 1);
119  return as_C_string(str, len + 1);
120}
121
122char* Symbol::as_C_string_flexible_buffer(Thread* t,
123                                                 char* buf, int size) const {
124  char* str;
125  int len = utf8_length();
126  int buf_len = len + 1;
127  if (size < buf_len) {
128    str = NEW_RESOURCE_ARRAY(char, buf_len);
129  } else {
130    str = buf;
131  }
132  return as_C_string(str, buf_len);
133}
134
135void Symbol::print_utf8_on(outputStream* st) const {
136  st->print("%s", as_C_string());
137}
138
139void Symbol::print_symbol_on(outputStream* st) const {
140  char *s;
141  st = st ? st : tty;
142  {
143    // ResourceMark may not affect st->print(). If st is a string
144    // stream it could resize, using the same resource arena.
145    ResourceMark rm;
146    s = as_quoted_ascii();
147    s = os::strdup(s);
148  }
149  if (s == NULL) {
150    st->print("(null)");
151  } else {
152    st->print("%s", s);
153    os::free(s);
154  }
155}
156
157char* Symbol::as_quoted_ascii() const {
158  const char *ptr = (const char *)&_body[0];
159  int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length());
160  char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1);
161  UTF8::as_quoted_ascii(ptr, utf8_length(), result, quoted_length + 1);
162  return result;
163}
164
165jchar* Symbol::as_unicode(int& length) const {
166  Symbol* this_ptr = (Symbol*)this;
167  length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
168  jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
169  if (length > 0) {
170    UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
171  }
172  return result;
173}
174
175const char* Symbol::as_klass_external_name(char* buf, int size) const {
176  if (size > 0) {
177    char* str    = as_C_string(buf, size);
178    int   length = (int)strlen(str);
179    // Turn all '/'s into '.'s (also for array klasses)
180    for (int index = 0; index < length; index++) {
181      if (str[index] == '/') {
182        str[index] = '.';
183      }
184    }
185    return str;
186  } else {
187    return buf;
188  }
189}
190
191const char* Symbol::as_klass_external_name() const {
192  char* str    = as_C_string();
193  int   length = (int)strlen(str);
194  // Turn all '/'s into '.'s (also for array klasses)
195  for (int index = 0; index < length; index++) {
196    if (str[index] == '/') {
197      str[index] = '.';
198    }
199  }
200  return str;
201}
202
203// Alternate hashing for unbalanced symbol tables.
204unsigned int Symbol::new_hash(juint seed) {
205  ResourceMark rm;
206  // Use alternate hashing algorithm on this symbol.
207  return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length());
208}
209
210void Symbol::increment_refcount() {
211  // Only increment the refcount if non-negative.  If negative either
212  // overflow has occurred or it is a permanent symbol in a read only
213  // shared archive.
214  if (_refcount >= 0) { // not a permanent symbol
215    Atomic::inc(&_refcount);
216    NOT_PRODUCT(Atomic::inc(&_total_count);)
217  }
218}
219
220void Symbol::decrement_refcount() {
221  if (_refcount >= 0) { // not a permanent symbol
222    short new_value = Atomic::add(short(-1), &_refcount);
223#ifdef ASSERT
224    if (new_value == -1) { // we have transitioned from 0 -> -1
225      print();
226      assert(false, "reference count underflow for symbol");
227    }
228#endif
229    (void)new_value;
230  }
231}
232
233void Symbol::metaspace_pointers_do(MetaspaceClosure* it) {
234  if (log_is_enabled(Trace, cds)) {
235    LogStream trace_stream(Log(cds)::trace());
236    trace_stream.print("Iter(Symbol): %p ", this);
237    print_value_on(&trace_stream);
238    trace_stream.cr();
239  }
240}
241
242void Symbol::print_on(outputStream* st) const {
243  if (this == NULL) {
244    st->print_cr("NULL");
245  } else {
246    st->print("Symbol: '");
247    print_symbol_on(st);
248    st->print("'");
249    st->print(" count %d", refcount());
250  }
251}
252
253// The print_value functions are present in all builds, to support the
254// disassembler and error reporting.
255void Symbol::print_value_on(outputStream* st) const {
256  if (this == NULL) {
257    st->print("NULL");
258  } else {
259    st->print("'");
260    for (int i = 0; i < utf8_length(); i++) {
261      st->print("%c", byte_at(i));
262    }
263    st->print("'");
264  }
265}
266
267// SymbolTable prints this in its statistics
268NOT_PRODUCT(int Symbol::_total_count = 0;)
269