1 /* 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_OOPS_SYMBOLOOP_HPP 26 #define SHARE_VM_OOPS_SYMBOLOOP_HPP 27 28 #include "oops/typeArrayOop.hpp" 29 #include "utilities/utf8.hpp" 30 31 // A symbolOop is a canonicalized string. 32 // All symbolOops reside in global symbolTable. 33 // See oopFactory::new_symbol for how to allocate a symbolOop 34 35 class symbolOopDesc : public oopDesc { 36 friend class VMStructs; 37 private: 38 unsigned short _length; // number of UTF8 characters in the symbol 39 jbyte _body[1]; 40 41 enum { 42 // max_symbol_length is constrained by type of _length 43 max_symbol_length = (1 << 16) -1 44 }; 45 public: 46 47 // Low-level access (used with care, since not GC-safe) 48 jbyte* base() { return &_body[0]; } 49 50 51 // Returns the largest size symbol we can safely hold. 52 static int max_length() { 53 return max_symbol_length; 54 } 55 56 static int object_size(int length) { 57 int size = header_size() + (sizeof(unsigned short) + length + HeapWordSize - 1) / HeapWordSize; 58 return align_object_size(size); 59 } 60 61 int object_size() { return object_size(utf8_length()); } 62 63 int byte_at(int index) const { 64 assert(index >=0 && index < _length, "symbol index overflow"); 65 return ((symbolOopDesc*)this)->base()[index]; 66 } 67 68 void byte_at_put(int index, int value) { 69 assert(index >=0 && index < _length, "symbol index overflow"); 70 ((symbolOopDesc*)this)->base()[index] = value; 71 } 72 73 jbyte* bytes() { return base(); } 74 75 int utf8_length() const { return _length; } 76 77 void set_utf8_length(int len) { _length = len; } 78 79 // Compares the symbol with a string. 80 bool equals(const char* str, int len) const; 81 bool equals(const char* str) const { return equals(str, (int) strlen(str)); } 82 83 // Tests if the symbol starts with the given prefix. 84 bool starts_with(const char* prefix, int len) const; 85 bool starts_with(const char* prefix) const { 86 return starts_with(prefix, (int) strlen(prefix)); 87 } 88 89 // Tests if the symbol starts with the given prefix. 90 int index_of_at(int i, const char* str, int len) const; 91 int index_of_at(int i, const char* str) const { 92 return index_of_at(i, str, (int) strlen(str)); 93 } 94 95 // Three-way compare for sorting; returns -1/0/1 if receiver is </==/> than arg 96 // note that the ordering is not alfabetical 97 inline int fast_compare(symbolOop other) const; 98 99 // Returns receiver converted to null-terminated UTF-8 string; string is 100 // allocated in resource area, or in the char buffer provided by caller. 101 char* as_C_string() const; 102 char* as_C_string(char* buf, int size) const; 103 // Use buf if needed buffer length is <= size. 104 char* as_C_string_flexible_buffer(Thread* t, char* buf, int size) const; 105 106 107 // Returns a null terminated utf8 string in a resource array 108 char* as_utf8() const { return as_C_string(); } 109 char* as_utf8_flexible_buffer(Thread* t, char* buf, int size) const { 110 return as_C_string_flexible_buffer(t, buf, size); 111 } 112 113 jchar* as_unicode(int& length) const; 114 115 // Treating this symbol as a class name, returns the Java name for the class. 116 // String is allocated in resource area if buffer is not provided. 117 // See Klass::external_name() 118 const char* as_klass_external_name() const; 119 const char* as_klass_external_name(char* buf, int size) const; 120 121 bool object_is_parsable() const { 122 return (utf8_length() > 0 || (oop)this == Universe::emptySymbol()); 123 } 124 125 // Printing 126 void print_symbol_on(outputStream* st = NULL); 127 }; 128 129 130 // Note: this comparison is used for vtable sorting only; it doesn't matter 131 // what order it defines, as long as it is a total, time-invariant order 132 // Since symbolOops are in permSpace, their relative order in memory never changes, 133 // so use address comparison for speed 134 int symbolOopDesc::fast_compare(symbolOop other) const { 135 return (((uintptr_t)this < (uintptr_t)other) ? -1 136 : ((uintptr_t)this == (uintptr_t) other) ? 0 : 1); 137 } 138 139 #endif // SHARE_VM_OOPS_SYMBOLOOP_HPP