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