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