src/share/vm/oops/symbolOop.hpp

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 #ifndef SHARE_VM_OOPS_SYMBOLOOP_HPP
stefank@2314 26 #define SHARE_VM_OOPS_SYMBOLOOP_HPP
stefank@2314 27
stefank@2314 28 #include "oops/typeArrayOop.hpp"
stefank@2314 29 #include "utilities/utf8.hpp"
stefank@2314 30
duke@435 31 // A symbolOop is a canonicalized string.
duke@435 32 // All symbolOops reside in global symbolTable.
duke@435 33 // See oopFactory::new_symbol for how to allocate a symbolOop
duke@435 34
duke@435 35 class symbolOopDesc : public oopDesc {
duke@435 36 friend class VMStructs;
duke@435 37 private:
duke@435 38 unsigned short _length; // number of UTF8 characters in the symbol
duke@435 39 jbyte _body[1];
duke@435 40
duke@435 41 enum {
duke@435 42 // max_symbol_length is constrained by type of _length
duke@435 43 max_symbol_length = (1 << 16) -1
duke@435 44 };
duke@435 45 public:
duke@435 46
duke@435 47 // Low-level access (used with care, since not GC-safe)
duke@435 48 jbyte* base() { return &_body[0]; }
duke@435 49
duke@435 50
duke@435 51 // Returns the largest size symbol we can safely hold.
duke@435 52 static int max_length() {
duke@435 53 return max_symbol_length;
duke@435 54 }
duke@435 55
duke@435 56 static int object_size(int length) {
duke@435 57 int size = header_size() + (sizeof(unsigned short) + length + HeapWordSize - 1) / HeapWordSize;
duke@435 58 return align_object_size(size);
duke@435 59 }
duke@435 60
duke@435 61 int object_size() { return object_size(utf8_length()); }
duke@435 62
duke@435 63 int byte_at(int index) const {
duke@435 64 assert(index >=0 && index < _length, "symbol index overflow");
duke@435 65 return ((symbolOopDesc*)this)->base()[index];
duke@435 66 }
duke@435 67
duke@435 68 void byte_at_put(int index, int value) {
duke@435 69 assert(index >=0 && index < _length, "symbol index overflow");
duke@435 70 ((symbolOopDesc*)this)->base()[index] = value;
duke@435 71 }
duke@435 72
duke@435 73 jbyte* bytes() { return base(); }
duke@435 74
duke@435 75 int utf8_length() const { return _length; }
duke@435 76
duke@435 77 void set_utf8_length(int len) { _length = len; }
duke@435 78
twisti@1573 79 // Compares the symbol with a string.
duke@435 80 bool equals(const char* str, int len) const;
twisti@1573 81 bool equals(const char* str) const { return equals(str, (int) strlen(str)); }
twisti@1573 82
twisti@1573 83 // Tests if the symbol starts with the given prefix.
twisti@1573 84 bool starts_with(const char* prefix, int len) const;
twisti@1573 85 bool starts_with(const char* prefix) const {
twisti@1573 86 return starts_with(prefix, (int) strlen(prefix));
twisti@1573 87 }
twisti@1573 88
twisti@1573 89 // Tests if the symbol starts with the given prefix.
twisti@1573 90 int index_of_at(int i, const char* str, int len) const;
twisti@1573 91 int index_of_at(int i, const char* str) const {
twisti@1573 92 return index_of_at(i, str, (int) strlen(str));
twisti@1573 93 }
duke@435 94
duke@435 95 // Three-way compare for sorting; returns -1/0/1 if receiver is </==/> than arg
duke@435 96 // note that the ordering is not alfabetical
duke@435 97 inline int fast_compare(symbolOop other) const;
duke@435 98
duke@435 99 // Returns receiver converted to null-terminated UTF-8 string; string is
duke@435 100 // allocated in resource area, or in the char buffer provided by caller.
duke@435 101 char* as_C_string() const;
duke@435 102 char* as_C_string(char* buf, int size) const;
duke@435 103 // Use buf if needed buffer length is <= size.
duke@435 104 char* as_C_string_flexible_buffer(Thread* t, char* buf, int size) const;
duke@435 105
duke@435 106
duke@435 107 // Returns a null terminated utf8 string in a resource array
duke@435 108 char* as_utf8() const { return as_C_string(); }
duke@435 109 char* as_utf8_flexible_buffer(Thread* t, char* buf, int size) const {
duke@435 110 return as_C_string_flexible_buffer(t, buf, size);
duke@435 111 }
duke@435 112
duke@435 113 jchar* as_unicode(int& length) const;
duke@435 114
duke@435 115 // Treating this symbol as a class name, returns the Java name for the class.
duke@435 116 // String is allocated in resource area if buffer is not provided.
duke@435 117 // See Klass::external_name()
duke@435 118 const char* as_klass_external_name() const;
duke@435 119 const char* as_klass_external_name(char* buf, int size) const;
duke@435 120
duke@435 121 bool object_is_parsable() const {
duke@435 122 return (utf8_length() > 0 || (oop)this == Universe::emptySymbol());
duke@435 123 }
duke@435 124
duke@435 125 // Printing
duke@435 126 void print_symbol_on(outputStream* st = NULL);
duke@435 127 };
duke@435 128
duke@435 129
duke@435 130 // Note: this comparison is used for vtable sorting only; it doesn't matter
duke@435 131 // what order it defines, as long as it is a total, time-invariant order
duke@435 132 // Since symbolOops are in permSpace, their relative order in memory never changes,
duke@435 133 // so use address comparison for speed
duke@435 134 int symbolOopDesc::fast_compare(symbolOop other) const {
duke@435 135 return (((uintptr_t)this < (uintptr_t)other) ? -1
duke@435 136 : ((uintptr_t)this == (uintptr_t) other) ? 0 : 1);
duke@435 137 }
stefank@2314 138
stefank@2314 139 #endif // SHARE_VM_OOPS_SYMBOLOOP_HPP

mercurial