src/share/vm/oops/symbolOop.cpp

Thu, 13 Jan 2011 22:15:41 -0800

author
never
date
Thu, 13 Jan 2011 22:15:41 -0800
changeset 2462
8012aa3ccede
parent 2314
f95d63e2154a
permissions
-rw-r--r--

4926272: methodOopDesc::method_from_bcp is unsafe
Reviewed-by: coleenp, jrose, kvn, dcubed

duke@435 1 /*
stefank@2314 2 * Copyright (c) 1997, 2010, 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
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "oops/oop.inline.hpp"
stefank@2314 27 #include "oops/symbolOop.hpp"
duke@435 28
twisti@1573 29
twisti@1573 30 // ------------------------------------------------------------------
twisti@1573 31 // symbolOopDesc::equals
twisti@1573 32 //
twisti@1573 33 // Compares the symbol with a string of the given length.
duke@435 34 bool symbolOopDesc::equals(const char* str, int len) const {
duke@435 35 int l = utf8_length();
duke@435 36 if (l != len) return false;
duke@435 37 while (l-- > 0) {
duke@435 38 if (str[l] != (char) byte_at(l))
duke@435 39 return false;
duke@435 40 }
duke@435 41 assert(l == -1, "we should be at the beginning");
duke@435 42 return true;
duke@435 43 }
duke@435 44
twisti@1573 45
twisti@1573 46 // ------------------------------------------------------------------
twisti@1573 47 // symbolOopDesc::starts_with
twisti@1573 48 //
twisti@1573 49 // Tests if the symbol starts with the specified prefix of the given
twisti@1573 50 // length.
twisti@1573 51 bool symbolOopDesc::starts_with(const char* prefix, int len) const {
twisti@1573 52 if (len > utf8_length()) return false;
twisti@1573 53 while (len-- > 0) {
twisti@1573 54 if (prefix[len] != (char) byte_at(len))
twisti@1573 55 return false;
twisti@1573 56 }
twisti@1573 57 assert(len == -1, "we should be at the beginning");
twisti@1573 58 return true;
twisti@1573 59 }
twisti@1573 60
twisti@1573 61
twisti@1573 62 // ------------------------------------------------------------------
twisti@1573 63 // symbolOopDesc::index_of
twisti@1573 64 //
twisti@1573 65 // Finds if the given string is a substring of this symbol's utf8 bytes.
twisti@1573 66 // Return -1 on failure. Otherwise return the first index where str occurs.
twisti@1573 67 int symbolOopDesc::index_of_at(int i, const char* str, int len) const {
twisti@1573 68 assert(i >= 0 && i <= utf8_length(), "oob");
twisti@1573 69 if (len <= 0) return 0;
twisti@1573 70 char first_char = str[0];
twisti@1573 71 address bytes = (address) ((symbolOopDesc*)this)->base();
twisti@1573 72 address limit = bytes + utf8_length() - len; // inclusive limit
twisti@1573 73 address scan = bytes + i;
twisti@1573 74 if (scan > limit)
twisti@1573 75 return -1;
twisti@1573 76 for (;;) {
twisti@1573 77 scan = (address) memchr(scan, first_char, (limit + 1 - scan));
twisti@1573 78 if (scan == NULL)
twisti@1573 79 return -1; // not found
twisti@1573 80 assert(scan >= bytes+i && scan <= limit, "scan oob");
twisti@1573 81 if (memcmp(scan, str, len) == 0)
twisti@1573 82 return (int)(scan - bytes);
twisti@1573 83 }
twisti@1573 84 }
twisti@1573 85
twisti@1573 86
duke@435 87 char* symbolOopDesc::as_C_string(char* buf, int size) const {
duke@435 88 if (size > 0) {
duke@435 89 int len = MIN2(size - 1, utf8_length());
duke@435 90 for (int i = 0; i < len; i++) {
duke@435 91 buf[i] = byte_at(i);
duke@435 92 }
duke@435 93 buf[len] = '\0';
duke@435 94 }
duke@435 95 return buf;
duke@435 96 }
duke@435 97
duke@435 98 char* symbolOopDesc::as_C_string() const {
duke@435 99 int len = utf8_length();
duke@435 100 char* str = NEW_RESOURCE_ARRAY(char, len + 1);
duke@435 101 return as_C_string(str, len + 1);
duke@435 102 }
duke@435 103
duke@435 104 char* symbolOopDesc::as_C_string_flexible_buffer(Thread* t,
duke@435 105 char* buf, int size) const {
duke@435 106 char* str;
duke@435 107 int len = utf8_length();
duke@435 108 int buf_len = len + 1;
duke@435 109 if (size < buf_len) {
duke@435 110 str = NEW_RESOURCE_ARRAY(char, buf_len);
duke@435 111 } else {
duke@435 112 str = buf;
duke@435 113 }
duke@435 114 return as_C_string(str, buf_len);
duke@435 115 }
duke@435 116
duke@435 117 void symbolOopDesc::print_symbol_on(outputStream* st) {
duke@435 118 st = st ? st : tty;
never@657 119 int length = UTF8::unicode_length((const char*)bytes(), utf8_length());
never@657 120 const char *ptr = (const char *)bytes();
never@657 121 jchar value;
never@657 122 for (int index = 0; index < length; index++) {
never@657 123 ptr = UTF8::next(ptr, &value);
never@657 124 if (value >= 32 && value < 127 || value == '\'' || value == '\\') {
never@657 125 st->put(value);
never@657 126 } else {
never@657 127 st->print("\\u%04x", value);
never@657 128 }
never@657 129 }
duke@435 130 }
duke@435 131
duke@435 132 jchar* symbolOopDesc::as_unicode(int& length) const {
duke@435 133 symbolOopDesc* this_ptr = (symbolOopDesc*)this;
duke@435 134 length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
duke@435 135 jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
duke@435 136 if (length > 0) {
duke@435 137 UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
duke@435 138 }
duke@435 139 return result;
duke@435 140 }
duke@435 141
duke@435 142 const char* symbolOopDesc::as_klass_external_name(char* buf, int size) const {
duke@435 143 if (size > 0) {
duke@435 144 char* str = as_C_string(buf, size);
duke@435 145 int length = (int)strlen(str);
duke@435 146 // Turn all '/'s into '.'s (also for array klasses)
duke@435 147 for (int index = 0; index < length; index++) {
duke@435 148 if (str[index] == '/') {
duke@435 149 str[index] = '.';
duke@435 150 }
duke@435 151 }
duke@435 152 return str;
duke@435 153 } else {
duke@435 154 return buf;
duke@435 155 }
duke@435 156 }
duke@435 157
duke@435 158 const char* symbolOopDesc::as_klass_external_name() const {
duke@435 159 char* str = as_C_string();
duke@435 160 int length = (int)strlen(str);
duke@435 161 // Turn all '/'s into '.'s (also for array klasses)
duke@435 162 for (int index = 0; index < length; index++) {
duke@435 163 if (str[index] == '/') {
duke@435 164 str[index] = '.';
duke@435 165 }
duke@435 166 }
duke@435 167 return str;
duke@435 168 }

mercurial