src/share/vm/oops/symbol.cpp

Thu, 04 Apr 2019 17:56:29 +0800

author
aoqi
date
Thu, 04 Apr 2019 17:56:29 +0800
changeset 9572
624a0741915c
parent 6876
710a3c8b516e
child 10015
eb7ce841ccec
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25
aoqi@0 26 #include "precompiled.hpp"
aoqi@0 27 #include "classfile/altHashing.hpp"
aoqi@0 28 #include "classfile/classLoaderData.hpp"
aoqi@0 29 #include "oops/symbol.hpp"
aoqi@0 30 #include "runtime/atomic.inline.hpp"
aoqi@0 31 #include "runtime/os.hpp"
aoqi@0 32 #include "memory/allocation.inline.hpp"
aoqi@0 33 #include "memory/resourceArea.hpp"
aoqi@0 34
aoqi@0 35 Symbol::Symbol(const u1* name, int length, int refcount) {
aoqi@0 36 _refcount = refcount;
aoqi@0 37 _length = length;
aoqi@0 38 _identity_hash = os::random();
aoqi@0 39 for (int i = 0; i < _length; i++) {
aoqi@0 40 byte_at_put(i, name[i]);
aoqi@0 41 }
aoqi@0 42 }
aoqi@0 43
aoqi@0 44 void* Symbol::operator new(size_t sz, int len, TRAPS) throw() {
aoqi@0 45 int alloc_size = size(len)*HeapWordSize;
aoqi@0 46 address res = (address) AllocateHeap(alloc_size, mtSymbol);
aoqi@0 47 return res;
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) throw() {
aoqi@0 51 int alloc_size = size(len)*HeapWordSize;
aoqi@0 52 address res = (address)arena->Amalloc(alloc_size);
aoqi@0 53 return res;
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 void* Symbol::operator new(size_t sz, int len, ClassLoaderData* loader_data, TRAPS) throw() {
aoqi@0 57 address res;
aoqi@0 58 int alloc_size = size(len)*HeapWordSize;
aoqi@0 59 res = (address) Metaspace::allocate(loader_data, size(len), true,
aoqi@0 60 MetaspaceObj::SymbolType, CHECK_NULL);
aoqi@0 61 return res;
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 void Symbol::operator delete(void *p) {
aoqi@0 65 assert(((Symbol*)p)->refcount() == 0, "should not call this");
aoqi@0 66 FreeHeap(p);
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 // ------------------------------------------------------------------
aoqi@0 70 // Symbol::equals
aoqi@0 71 //
aoqi@0 72 // Compares the symbol with a string of the given length.
aoqi@0 73 bool Symbol::equals(const char* str, int len) const {
aoqi@0 74 int l = utf8_length();
aoqi@0 75 if (l != len) return false;
aoqi@0 76 while (l-- > 0) {
aoqi@0 77 if (str[l] != (char) byte_at(l))
aoqi@0 78 return false;
aoqi@0 79 }
aoqi@0 80 assert(l == -1, "we should be at the beginning");
aoqi@0 81 return true;
aoqi@0 82 }
aoqi@0 83
aoqi@0 84
aoqi@0 85 // ------------------------------------------------------------------
aoqi@0 86 // Symbol::starts_with
aoqi@0 87 //
aoqi@0 88 // Tests if the symbol starts with the specified prefix of the given
aoqi@0 89 // length.
aoqi@0 90 bool Symbol::starts_with(const char* prefix, int len) const {
aoqi@0 91 if (len > utf8_length()) return false;
aoqi@0 92 while (len-- > 0) {
aoqi@0 93 if (prefix[len] != (char) byte_at(len))
aoqi@0 94 return false;
aoqi@0 95 }
aoqi@0 96 assert(len == -1, "we should be at the beginning");
aoqi@0 97 return true;
aoqi@0 98 }
aoqi@0 99
aoqi@0 100
aoqi@0 101 // ------------------------------------------------------------------
aoqi@0 102 // Symbol::index_of
aoqi@0 103 //
aoqi@0 104 // Finds if the given string is a substring of this symbol's utf8 bytes.
aoqi@0 105 // Return -1 on failure. Otherwise return the first index where str occurs.
aoqi@0 106 int Symbol::index_of_at(int i, const char* str, int len) const {
aoqi@0 107 assert(i >= 0 && i <= utf8_length(), "oob");
aoqi@0 108 if (len <= 0) return 0;
aoqi@0 109 char first_char = str[0];
aoqi@0 110 address bytes = (address) ((Symbol*)this)->base();
aoqi@0 111 address limit = bytes + utf8_length() - len; // inclusive limit
aoqi@0 112 address scan = bytes + i;
aoqi@0 113 if (scan > limit)
aoqi@0 114 return -1;
aoqi@0 115 for (; scan <= limit; scan++) {
aoqi@0 116 scan = (address) memchr(scan, first_char, (limit + 1 - scan));
aoqi@0 117 if (scan == NULL)
aoqi@0 118 return -1; // not found
aoqi@0 119 assert(scan >= bytes+i && scan <= limit, "scan oob");
aoqi@0 120 if (memcmp(scan, str, len) == 0)
aoqi@0 121 return (int)(scan - bytes);
aoqi@0 122 }
aoqi@0 123 return -1;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126
aoqi@0 127 char* Symbol::as_C_string(char* buf, int size) const {
aoqi@0 128 if (size > 0) {
aoqi@0 129 int len = MIN2(size - 1, utf8_length());
aoqi@0 130 for (int i = 0; i < len; i++) {
aoqi@0 131 buf[i] = byte_at(i);
aoqi@0 132 }
aoqi@0 133 buf[len] = '\0';
aoqi@0 134 }
aoqi@0 135 return buf;
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 char* Symbol::as_C_string() const {
aoqi@0 139 int len = utf8_length();
aoqi@0 140 char* str = NEW_RESOURCE_ARRAY(char, len + 1);
aoqi@0 141 return as_C_string(str, len + 1);
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 char* Symbol::as_C_string_flexible_buffer(Thread* t,
aoqi@0 145 char* buf, int size) const {
aoqi@0 146 char* str;
aoqi@0 147 int len = utf8_length();
aoqi@0 148 int buf_len = len + 1;
aoqi@0 149 if (size < buf_len) {
aoqi@0 150 str = NEW_RESOURCE_ARRAY(char, buf_len);
aoqi@0 151 } else {
aoqi@0 152 str = buf;
aoqi@0 153 }
aoqi@0 154 return as_C_string(str, buf_len);
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 void Symbol::print_symbol_on(outputStream* st) const {
aoqi@0 158 ResourceMark rm;
aoqi@0 159 st = st ? st : tty;
aoqi@0 160 st->print("%s", as_quoted_ascii());
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 char* Symbol::as_quoted_ascii() const {
aoqi@0 164 const char *ptr = (const char *)&_body[0];
aoqi@0 165 int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length());
aoqi@0 166 char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1);
aoqi@0 167 UTF8::as_quoted_ascii(ptr, utf8_length(), result, quoted_length + 1);
aoqi@0 168 return result;
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 jchar* Symbol::as_unicode(int& length) const {
aoqi@0 172 Symbol* this_ptr = (Symbol*)this;
aoqi@0 173 length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
aoqi@0 174 jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
aoqi@0 175 if (length > 0) {
aoqi@0 176 UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
aoqi@0 177 }
aoqi@0 178 return result;
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 const char* Symbol::as_klass_external_name(char* buf, int size) const {
aoqi@0 182 if (size > 0) {
aoqi@0 183 char* str = as_C_string(buf, size);
aoqi@0 184 int length = (int)strlen(str);
aoqi@0 185 // Turn all '/'s into '.'s (also for array klasses)
aoqi@0 186 for (int index = 0; index < length; index++) {
aoqi@0 187 if (str[index] == '/') {
aoqi@0 188 str[index] = '.';
aoqi@0 189 }
aoqi@0 190 }
aoqi@0 191 return str;
aoqi@0 192 } else {
aoqi@0 193 return buf;
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 const char* Symbol::as_klass_external_name() const {
aoqi@0 198 char* str = as_C_string();
aoqi@0 199 int length = (int)strlen(str);
aoqi@0 200 // Turn all '/'s into '.'s (also for array klasses)
aoqi@0 201 for (int index = 0; index < length; index++) {
aoqi@0 202 if (str[index] == '/') {
aoqi@0 203 str[index] = '.';
aoqi@0 204 }
aoqi@0 205 }
aoqi@0 206 return str;
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 // Alternate hashing for unbalanced symbol tables.
aoqi@0 210 unsigned int Symbol::new_hash(juint seed) {
aoqi@0 211 ResourceMark rm;
aoqi@0 212 // Use alternate hashing algorithm on this symbol.
aoqi@0 213 return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length());
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 void Symbol::increment_refcount() {
aoqi@0 217 // Only increment the refcount if positive. If negative either
aoqi@0 218 // overflow has occurred or it is a permanent symbol in a read only
aoqi@0 219 // shared archive.
aoqi@0 220 if (_refcount >= 0) {
aoqi@0 221 Atomic::inc(&_refcount);
aoqi@0 222 NOT_PRODUCT(Atomic::inc(&_total_count);)
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 void Symbol::decrement_refcount() {
aoqi@0 227 if (_refcount >= 0) {
aoqi@0 228 Atomic::dec(&_refcount);
aoqi@0 229 #ifdef ASSERT
aoqi@0 230 if (_refcount < 0) {
aoqi@0 231 print();
aoqi@0 232 assert(false, "reference count underflow for symbol");
aoqi@0 233 }
aoqi@0 234 #endif
aoqi@0 235 }
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 void Symbol::print_on(outputStream* st) const {
aoqi@0 239 if (this == NULL) {
aoqi@0 240 st->print_cr("NULL");
aoqi@0 241 } else {
aoqi@0 242 st->print("Symbol: '");
aoqi@0 243 print_symbol_on(st);
aoqi@0 244 st->print("'");
aoqi@0 245 st->print(" count %d", refcount());
aoqi@0 246 }
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 // The print_value functions are present in all builds, to support the
aoqi@0 250 // disassembler and error reporting.
aoqi@0 251 void Symbol::print_value_on(outputStream* st) const {
aoqi@0 252 if (this == NULL) {
aoqi@0 253 st->print("NULL");
aoqi@0 254 } else {
aoqi@0 255 st->print("'");
aoqi@0 256 for (int i = 0; i < utf8_length(); i++) {
aoqi@0 257 st->print("%c", byte_at(i));
aoqi@0 258 }
aoqi@0 259 st->print("'");
aoqi@0 260 }
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 // SymbolTable prints this in its statistics
aoqi@0 264 NOT_PRODUCT(int Symbol::_total_count = 0;)

mercurial