duke@435: /* coleenp@3682: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: coleenp@2497: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "oops/oop.inline.hpp" coleenp@2497: #include "oops/symbol.hpp" coleenp@2497: #include "runtime/os.hpp" coleenp@2497: #include "memory/allocation.inline.hpp" duke@435: coleenp@3682: Symbol::Symbol(const u1* name, int length, int refcount) : _refcount(refcount), _length(length) { coleenp@2497: _identity_hash = os::random(); coleenp@2497: for (int i = 0; i < _length; i++) { coleenp@2497: byte_at_put(i, name[i]); coleenp@2497: } coleenp@2497: } coleenp@2497: coleenp@3682: void* Symbol::operator new(size_t sz, int len, TRAPS) { coleenp@3682: int alloc_size = object_size(len)*HeapWordSize; coleenp@3682: address res = (address) AllocateHeap(alloc_size, "symbol"); coleenp@3682: DEBUG_ONLY(set_allocation_type(res, ResourceObj::C_HEAP);) coleenp@3682: return res; coleenp@3682: } coleenp@3682: coleenp@3682: void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) { coleenp@3682: int alloc_size = object_size(len)*HeapWordSize; coleenp@3682: address res = (address)arena->Amalloc(alloc_size); coleenp@3682: DEBUG_ONLY(set_allocation_type(res, ResourceObj::ARENA);) coleenp@3682: return res; coleenp@2497: } twisti@1573: twisti@1573: // ------------------------------------------------------------------ coleenp@2497: // Symbol::equals twisti@1573: // twisti@1573: // Compares the symbol with a string of the given length. coleenp@2497: bool Symbol::equals(const char* str, int len) const { duke@435: int l = utf8_length(); duke@435: if (l != len) return false; duke@435: while (l-- > 0) { duke@435: if (str[l] != (char) byte_at(l)) duke@435: return false; duke@435: } duke@435: assert(l == -1, "we should be at the beginning"); duke@435: return true; duke@435: } duke@435: twisti@1573: twisti@1573: // ------------------------------------------------------------------ coleenp@2497: // Symbol::starts_with twisti@1573: // twisti@1573: // Tests if the symbol starts with the specified prefix of the given twisti@1573: // length. coleenp@2497: bool Symbol::starts_with(const char* prefix, int len) const { twisti@1573: if (len > utf8_length()) return false; twisti@1573: while (len-- > 0) { twisti@1573: if (prefix[len] != (char) byte_at(len)) twisti@1573: return false; twisti@1573: } twisti@1573: assert(len == -1, "we should be at the beginning"); twisti@1573: return true; twisti@1573: } twisti@1573: twisti@1573: twisti@1573: // ------------------------------------------------------------------ coleenp@2497: // Symbol::index_of twisti@1573: // twisti@1573: // Finds if the given string is a substring of this symbol's utf8 bytes. twisti@1573: // Return -1 on failure. Otherwise return the first index where str occurs. coleenp@2497: int Symbol::index_of_at(int i, const char* str, int len) const { twisti@1573: assert(i >= 0 && i <= utf8_length(), "oob"); twisti@1573: if (len <= 0) return 0; twisti@1573: char first_char = str[0]; coleenp@2497: address bytes = (address) ((Symbol*)this)->base(); twisti@1573: address limit = bytes + utf8_length() - len; // inclusive limit twisti@1573: address scan = bytes + i; twisti@1573: if (scan > limit) twisti@1573: return -1; twisti@1573: for (;;) { twisti@1573: scan = (address) memchr(scan, first_char, (limit + 1 - scan)); twisti@1573: if (scan == NULL) twisti@1573: return -1; // not found twisti@1573: assert(scan >= bytes+i && scan <= limit, "scan oob"); twisti@1573: if (memcmp(scan, str, len) == 0) twisti@1573: return (int)(scan - bytes); twisti@1573: } twisti@1573: } twisti@1573: twisti@1573: coleenp@2497: char* Symbol::as_C_string(char* buf, int size) const { duke@435: if (size > 0) { duke@435: int len = MIN2(size - 1, utf8_length()); duke@435: for (int i = 0; i < len; i++) { duke@435: buf[i] = byte_at(i); duke@435: } duke@435: buf[len] = '\0'; duke@435: } duke@435: return buf; duke@435: } duke@435: coleenp@2497: char* Symbol::as_C_string() const { duke@435: int len = utf8_length(); duke@435: char* str = NEW_RESOURCE_ARRAY(char, len + 1); duke@435: return as_C_string(str, len + 1); duke@435: } duke@435: coleenp@2497: char* Symbol::as_C_string_flexible_buffer(Thread* t, duke@435: char* buf, int size) const { duke@435: char* str; duke@435: int len = utf8_length(); duke@435: int buf_len = len + 1; duke@435: if (size < buf_len) { duke@435: str = NEW_RESOURCE_ARRAY(char, buf_len); duke@435: } else { duke@435: str = buf; duke@435: } duke@435: return as_C_string(str, buf_len); duke@435: } duke@435: coleenp@2497: void Symbol::print_symbol_on(outputStream* st) const { duke@435: st = st ? st : tty; never@657: int length = UTF8::unicode_length((const char*)bytes(), utf8_length()); never@657: const char *ptr = (const char *)bytes(); never@657: jchar value; never@657: for (int index = 0; index < length; index++) { never@657: ptr = UTF8::next(ptr, &value); never@657: if (value >= 32 && value < 127 || value == '\'' || value == '\\') { never@657: st->put(value); never@657: } else { never@657: st->print("\\u%04x", value); never@657: } never@657: } duke@435: } duke@435: coleenp@2497: jchar* Symbol::as_unicode(int& length) const { coleenp@2497: Symbol* this_ptr = (Symbol*)this; duke@435: length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length()); duke@435: jchar* result = NEW_RESOURCE_ARRAY(jchar, length); duke@435: if (length > 0) { duke@435: UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length); duke@435: } duke@435: return result; duke@435: } duke@435: coleenp@2497: const char* Symbol::as_klass_external_name(char* buf, int size) const { duke@435: if (size > 0) { duke@435: char* str = as_C_string(buf, size); duke@435: int length = (int)strlen(str); duke@435: // Turn all '/'s into '.'s (also for array klasses) duke@435: for (int index = 0; index < length; index++) { duke@435: if (str[index] == '/') { duke@435: str[index] = '.'; duke@435: } duke@435: } duke@435: return str; duke@435: } else { duke@435: return buf; duke@435: } duke@435: } duke@435: coleenp@2497: const char* Symbol::as_klass_external_name() const { duke@435: char* str = as_C_string(); duke@435: int length = (int)strlen(str); duke@435: // Turn all '/'s into '.'s (also for array klasses) duke@435: for (int index = 0; index < length; index++) { duke@435: if (str[index] == '/') { duke@435: str[index] = '.'; duke@435: } duke@435: } duke@435: return str; duke@435: } coleenp@2497: coleenp@2497: coleenp@2497: void Symbol::print_on(outputStream* st) const { coleenp@2497: if (this == NULL) { coleenp@2497: st->print_cr("NULL"); coleenp@2497: } else { coleenp@2497: st->print("Symbol: '"); coleenp@2497: print_symbol_on(st); coleenp@2497: st->print("'"); coleenp@2497: st->print(" count %d", refcount()); coleenp@2497: } coleenp@2497: } coleenp@2497: coleenp@2497: // The print_value functions are present in all builds, to support the coleenp@2497: // disassembler and error reporting. coleenp@2497: void Symbol::print_value_on(outputStream* st) const { coleenp@2497: if (this == NULL) { coleenp@2497: st->print("NULL"); coleenp@2497: } else { coleenp@2497: st->print("'"); coleenp@2497: for (int i = 0; i < utf8_length(); i++) { coleenp@2497: st->print("%c", byte_at(i)); coleenp@2497: } coleenp@2497: st->print("'"); coleenp@2497: } coleenp@2497: } coleenp@2497: coleenp@3682: // SymbolTable prints this in its statistics coleenp@2497: NOT_PRODUCT(int Symbol::_total_count = 0;)