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