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