src/share/vm/oops/symbol.cpp

Tue, 26 Nov 2013 14:35:38 +0100

author
sjohanss
date
Tue, 26 Nov 2013 14:35:38 +0100
changeset 6148
55a0da3d420b
parent 5614
9758d9f36299
child 6351
f9e35a9dc8c7
permissions
-rw-r--r--

8027675: Full collections with Serial slower in JDK 8 compared to 7u40
Summary: Reduced the number of calls to follow_class_loader and instead marked and pushed the klass holder directly. Also removed unneeded calls to adjust_klass.
Reviewed-by: coleenp, jmasa, mgerdin, tschatzl

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

mercurial