duke@435: /* twisti@1573: * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // A symbolOop is a canonicalized string. duke@435: // All symbolOops reside in global symbolTable. duke@435: // See oopFactory::new_symbol for how to allocate a symbolOop duke@435: duke@435: class symbolOopDesc : public oopDesc { duke@435: friend class VMStructs; duke@435: private: duke@435: unsigned short _length; // number of UTF8 characters in the symbol duke@435: jbyte _body[1]; duke@435: duke@435: enum { duke@435: // max_symbol_length is constrained by type of _length duke@435: max_symbol_length = (1 << 16) -1 duke@435: }; duke@435: public: duke@435: duke@435: // Low-level access (used with care, since not GC-safe) duke@435: jbyte* base() { return &_body[0]; } duke@435: duke@435: duke@435: // Returns the largest size symbol we can safely hold. duke@435: static int max_length() { duke@435: return max_symbol_length; duke@435: } duke@435: duke@435: static int object_size(int length) { duke@435: int size = header_size() + (sizeof(unsigned short) + length + HeapWordSize - 1) / HeapWordSize; duke@435: return align_object_size(size); duke@435: } duke@435: duke@435: int object_size() { return object_size(utf8_length()); } duke@435: duke@435: int byte_at(int index) const { duke@435: assert(index >=0 && index < _length, "symbol index overflow"); duke@435: return ((symbolOopDesc*)this)->base()[index]; duke@435: } duke@435: duke@435: void byte_at_put(int index, int value) { duke@435: assert(index >=0 && index < _length, "symbol index overflow"); duke@435: ((symbolOopDesc*)this)->base()[index] = value; duke@435: } duke@435: duke@435: jbyte* bytes() { return base(); } duke@435: duke@435: int utf8_length() const { return _length; } duke@435: duke@435: void set_utf8_length(int len) { _length = len; } duke@435: twisti@1573: // Compares the symbol with a string. duke@435: bool equals(const char* str, int len) const; twisti@1573: bool equals(const char* str) const { return equals(str, (int) strlen(str)); } twisti@1573: twisti@1573: // Tests if the symbol starts with the given prefix. twisti@1573: bool starts_with(const char* prefix, int len) const; twisti@1573: bool starts_with(const char* prefix) const { twisti@1573: return starts_with(prefix, (int) strlen(prefix)); twisti@1573: } twisti@1573: twisti@1573: // Tests if the symbol starts with the given prefix. twisti@1573: int index_of_at(int i, const char* str, int len) const; twisti@1573: int index_of_at(int i, const char* str) const { twisti@1573: return index_of_at(i, str, (int) strlen(str)); twisti@1573: } duke@435: duke@435: // Three-way compare for sorting; returns -1/0/1 if receiver is than arg duke@435: // note that the ordering is not alfabetical duke@435: inline int fast_compare(symbolOop other) const; duke@435: duke@435: // Returns receiver converted to null-terminated UTF-8 string; string is duke@435: // allocated in resource area, or in the char buffer provided by caller. duke@435: char* as_C_string() const; duke@435: char* as_C_string(char* buf, int size) const; duke@435: // Use buf if needed buffer length is <= size. duke@435: char* as_C_string_flexible_buffer(Thread* t, char* buf, int size) const; duke@435: duke@435: duke@435: // Returns a null terminated utf8 string in a resource array duke@435: char* as_utf8() const { return as_C_string(); } duke@435: char* as_utf8_flexible_buffer(Thread* t, char* buf, int size) const { duke@435: return as_C_string_flexible_buffer(t, buf, size); duke@435: } duke@435: duke@435: jchar* as_unicode(int& length) const; duke@435: duke@435: // Treating this symbol as a class name, returns the Java name for the class. duke@435: // String is allocated in resource area if buffer is not provided. duke@435: // See Klass::external_name() duke@435: const char* as_klass_external_name() const; duke@435: const char* as_klass_external_name(char* buf, int size) const; duke@435: duke@435: bool object_is_parsable() const { duke@435: return (utf8_length() > 0 || (oop)this == Universe::emptySymbol()); duke@435: } duke@435: duke@435: // Printing duke@435: void print_symbol_on(outputStream* st = NULL); duke@435: }; duke@435: duke@435: duke@435: // Note: this comparison is used for vtable sorting only; it doesn't matter duke@435: // what order it defines, as long as it is a total, time-invariant order duke@435: // Since symbolOops are in permSpace, their relative order in memory never changes, duke@435: // so use address comparison for speed duke@435: int symbolOopDesc::fast_compare(symbolOop other) const { duke@435: return (((uintptr_t)this < (uintptr_t)other) ? -1 duke@435: : ((uintptr_t)this == (uintptr_t) other) ? 0 : 1); duke@435: }