annotate src/share/vm/oops/symbol.cpp @ 7745:6dc8db7164d2

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