Fri, 23 Mar 2012 11:16:05 -0400
7150058: Allocate symbols from null boot loader to an arena for NMT
Summary: Move symbol allocation to an arena so NMT doesn't have to track them at startup.
Reviewed-by: never, kamg, zgu
duke@435 | 1 | /* |
coleenp@3682 | 2 | * Copyright (c) 1997, 2012, 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" |
stefank@2314 | 27 | #include "oops/oop.inline.hpp" |
coleenp@2497 | 28 | #include "oops/symbol.hpp" |
coleenp@2497 | 29 | #include "runtime/os.hpp" |
coleenp@2497 | 30 | #include "memory/allocation.inline.hpp" |
duke@435 | 31 | |
coleenp@3682 | 32 | Symbol::Symbol(const u1* name, int length, int refcount) : _refcount(refcount), _length(length) { |
coleenp@2497 | 33 | _identity_hash = os::random(); |
coleenp@2497 | 34 | for (int i = 0; i < _length; i++) { |
coleenp@2497 | 35 | byte_at_put(i, name[i]); |
coleenp@2497 | 36 | } |
coleenp@2497 | 37 | } |
coleenp@2497 | 38 | |
coleenp@3682 | 39 | void* Symbol::operator new(size_t sz, int len, TRAPS) { |
coleenp@3682 | 40 | int alloc_size = object_size(len)*HeapWordSize; |
coleenp@3682 | 41 | address res = (address) AllocateHeap(alloc_size, "symbol"); |
coleenp@3682 | 42 | DEBUG_ONLY(set_allocation_type(res, ResourceObj::C_HEAP);) |
coleenp@3682 | 43 | return res; |
coleenp@3682 | 44 | } |
coleenp@3682 | 45 | |
coleenp@3682 | 46 | void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) { |
coleenp@3682 | 47 | int alloc_size = object_size(len)*HeapWordSize; |
coleenp@3682 | 48 | address res = (address)arena->Amalloc(alloc_size); |
coleenp@3682 | 49 | DEBUG_ONLY(set_allocation_type(res, ResourceObj::ARENA);) |
coleenp@3682 | 50 | return res; |
coleenp@2497 | 51 | } |
twisti@1573 | 52 | |
twisti@1573 | 53 | // ------------------------------------------------------------------ |
coleenp@2497 | 54 | // Symbol::equals |
twisti@1573 | 55 | // |
twisti@1573 | 56 | // Compares the symbol with a string of the given length. |
coleenp@2497 | 57 | bool Symbol::equals(const char* str, int len) const { |
duke@435 | 58 | int l = utf8_length(); |
duke@435 | 59 | if (l != len) return false; |
duke@435 | 60 | while (l-- > 0) { |
duke@435 | 61 | if (str[l] != (char) byte_at(l)) |
duke@435 | 62 | return false; |
duke@435 | 63 | } |
duke@435 | 64 | assert(l == -1, "we should be at the beginning"); |
duke@435 | 65 | return true; |
duke@435 | 66 | } |
duke@435 | 67 | |
twisti@1573 | 68 | |
twisti@1573 | 69 | // ------------------------------------------------------------------ |
coleenp@2497 | 70 | // Symbol::starts_with |
twisti@1573 | 71 | // |
twisti@1573 | 72 | // Tests if the symbol starts with the specified prefix of the given |
twisti@1573 | 73 | // length. |
coleenp@2497 | 74 | bool Symbol::starts_with(const char* prefix, int len) const { |
twisti@1573 | 75 | if (len > utf8_length()) return false; |
twisti@1573 | 76 | while (len-- > 0) { |
twisti@1573 | 77 | if (prefix[len] != (char) byte_at(len)) |
twisti@1573 | 78 | return false; |
twisti@1573 | 79 | } |
twisti@1573 | 80 | assert(len == -1, "we should be at the beginning"); |
twisti@1573 | 81 | return true; |
twisti@1573 | 82 | } |
twisti@1573 | 83 | |
twisti@1573 | 84 | |
twisti@1573 | 85 | // ------------------------------------------------------------------ |
coleenp@2497 | 86 | // Symbol::index_of |
twisti@1573 | 87 | // |
twisti@1573 | 88 | // Finds if the given string is a substring of this symbol's utf8 bytes. |
twisti@1573 | 89 | // Return -1 on failure. Otherwise return the first index where str occurs. |
coleenp@2497 | 90 | int Symbol::index_of_at(int i, const char* str, int len) const { |
twisti@1573 | 91 | assert(i >= 0 && i <= utf8_length(), "oob"); |
twisti@1573 | 92 | if (len <= 0) return 0; |
twisti@1573 | 93 | char first_char = str[0]; |
coleenp@2497 | 94 | address bytes = (address) ((Symbol*)this)->base(); |
twisti@1573 | 95 | address limit = bytes + utf8_length() - len; // inclusive limit |
twisti@1573 | 96 | address scan = bytes + i; |
twisti@1573 | 97 | if (scan > limit) |
twisti@1573 | 98 | return -1; |
twisti@1573 | 99 | for (;;) { |
twisti@1573 | 100 | scan = (address) memchr(scan, first_char, (limit + 1 - scan)); |
twisti@1573 | 101 | if (scan == NULL) |
twisti@1573 | 102 | return -1; // not found |
twisti@1573 | 103 | assert(scan >= bytes+i && scan <= limit, "scan oob"); |
twisti@1573 | 104 | if (memcmp(scan, str, len) == 0) |
twisti@1573 | 105 | return (int)(scan - bytes); |
twisti@1573 | 106 | } |
twisti@1573 | 107 | } |
twisti@1573 | 108 | |
twisti@1573 | 109 | |
coleenp@2497 | 110 | char* Symbol::as_C_string(char* buf, int size) const { |
duke@435 | 111 | if (size > 0) { |
duke@435 | 112 | int len = MIN2(size - 1, utf8_length()); |
duke@435 | 113 | for (int i = 0; i < len; i++) { |
duke@435 | 114 | buf[i] = byte_at(i); |
duke@435 | 115 | } |
duke@435 | 116 | buf[len] = '\0'; |
duke@435 | 117 | } |
duke@435 | 118 | return buf; |
duke@435 | 119 | } |
duke@435 | 120 | |
coleenp@2497 | 121 | char* Symbol::as_C_string() const { |
duke@435 | 122 | int len = utf8_length(); |
duke@435 | 123 | char* str = NEW_RESOURCE_ARRAY(char, len + 1); |
duke@435 | 124 | return as_C_string(str, len + 1); |
duke@435 | 125 | } |
duke@435 | 126 | |
coleenp@2497 | 127 | char* Symbol::as_C_string_flexible_buffer(Thread* t, |
duke@435 | 128 | char* buf, int size) const { |
duke@435 | 129 | char* str; |
duke@435 | 130 | int len = utf8_length(); |
duke@435 | 131 | int buf_len = len + 1; |
duke@435 | 132 | if (size < buf_len) { |
duke@435 | 133 | str = NEW_RESOURCE_ARRAY(char, buf_len); |
duke@435 | 134 | } else { |
duke@435 | 135 | str = buf; |
duke@435 | 136 | } |
duke@435 | 137 | return as_C_string(str, buf_len); |
duke@435 | 138 | } |
duke@435 | 139 | |
coleenp@2497 | 140 | void Symbol::print_symbol_on(outputStream* st) const { |
duke@435 | 141 | st = st ? st : tty; |
never@657 | 142 | int length = UTF8::unicode_length((const char*)bytes(), utf8_length()); |
never@657 | 143 | const char *ptr = (const char *)bytes(); |
never@657 | 144 | jchar value; |
never@657 | 145 | for (int index = 0; index < length; index++) { |
never@657 | 146 | ptr = UTF8::next(ptr, &value); |
never@657 | 147 | if (value >= 32 && value < 127 || value == '\'' || value == '\\') { |
never@657 | 148 | st->put(value); |
never@657 | 149 | } else { |
never@657 | 150 | st->print("\\u%04x", value); |
never@657 | 151 | } |
never@657 | 152 | } |
duke@435 | 153 | } |
duke@435 | 154 | |
coleenp@2497 | 155 | jchar* Symbol::as_unicode(int& length) const { |
coleenp@2497 | 156 | Symbol* this_ptr = (Symbol*)this; |
duke@435 | 157 | length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length()); |
duke@435 | 158 | jchar* result = NEW_RESOURCE_ARRAY(jchar, length); |
duke@435 | 159 | if (length > 0) { |
duke@435 | 160 | UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length); |
duke@435 | 161 | } |
duke@435 | 162 | return result; |
duke@435 | 163 | } |
duke@435 | 164 | |
coleenp@2497 | 165 | const char* Symbol::as_klass_external_name(char* buf, int size) const { |
duke@435 | 166 | if (size > 0) { |
duke@435 | 167 | char* str = as_C_string(buf, size); |
duke@435 | 168 | int length = (int)strlen(str); |
duke@435 | 169 | // Turn all '/'s into '.'s (also for array klasses) |
duke@435 | 170 | for (int index = 0; index < length; index++) { |
duke@435 | 171 | if (str[index] == '/') { |
duke@435 | 172 | str[index] = '.'; |
duke@435 | 173 | } |
duke@435 | 174 | } |
duke@435 | 175 | return str; |
duke@435 | 176 | } else { |
duke@435 | 177 | return buf; |
duke@435 | 178 | } |
duke@435 | 179 | } |
duke@435 | 180 | |
coleenp@2497 | 181 | const char* Symbol::as_klass_external_name() const { |
duke@435 | 182 | char* str = as_C_string(); |
duke@435 | 183 | int length = (int)strlen(str); |
duke@435 | 184 | // Turn all '/'s into '.'s (also for array klasses) |
duke@435 | 185 | for (int index = 0; index < length; index++) { |
duke@435 | 186 | if (str[index] == '/') { |
duke@435 | 187 | str[index] = '.'; |
duke@435 | 188 | } |
duke@435 | 189 | } |
duke@435 | 190 | return str; |
duke@435 | 191 | } |
coleenp@2497 | 192 | |
coleenp@2497 | 193 | |
coleenp@2497 | 194 | void Symbol::print_on(outputStream* st) const { |
coleenp@2497 | 195 | if (this == NULL) { |
coleenp@2497 | 196 | st->print_cr("NULL"); |
coleenp@2497 | 197 | } else { |
coleenp@2497 | 198 | st->print("Symbol: '"); |
coleenp@2497 | 199 | print_symbol_on(st); |
coleenp@2497 | 200 | st->print("'"); |
coleenp@2497 | 201 | st->print(" count %d", refcount()); |
coleenp@2497 | 202 | } |
coleenp@2497 | 203 | } |
coleenp@2497 | 204 | |
coleenp@2497 | 205 | // The print_value functions are present in all builds, to support the |
coleenp@2497 | 206 | // disassembler and error reporting. |
coleenp@2497 | 207 | void Symbol::print_value_on(outputStream* st) const { |
coleenp@2497 | 208 | if (this == NULL) { |
coleenp@2497 | 209 | st->print("NULL"); |
coleenp@2497 | 210 | } else { |
coleenp@2497 | 211 | st->print("'"); |
coleenp@2497 | 212 | for (int i = 0; i < utf8_length(); i++) { |
coleenp@2497 | 213 | st->print("%c", byte_at(i)); |
coleenp@2497 | 214 | } |
coleenp@2497 | 215 | st->print("'"); |
coleenp@2497 | 216 | } |
coleenp@2497 | 217 | } |
coleenp@2497 | 218 | |
coleenp@3682 | 219 | // SymbolTable prints this in its statistics |
coleenp@2497 | 220 | NOT_PRODUCT(int Symbol::_total_count = 0;) |