0
|
1 /*
|
|
2 * Copyright 1997-2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 * have any 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
|
|
76 // Three-way compare for sorting; returns -1/0/1 if receiver is </==/> than arg
|
|
77 // note that the ordering is not alfabetical
|
|
78 inline int fast_compare(symbolOop other) const;
|
|
79
|
|
80 // Returns receiver converted to null-terminated UTF-8 string; string is
|
|
81 // allocated in resource area, or in the char buffer provided by caller.
|
|
82 char* as_C_string() const;
|
|
83 char* as_C_string(char* buf, int size) const;
|
|
84 // Use buf if needed buffer length is <= size.
|
|
85 char* as_C_string_flexible_buffer(Thread* t, char* buf, int size) const;
|
|
86
|
|
87
|
|
88 // Returns a null terminated utf8 string in a resource array
|
|
89 char* as_utf8() const { return as_C_string(); }
|
|
90 char* as_utf8_flexible_buffer(Thread* t, char* buf, int size) const {
|
|
91 return as_C_string_flexible_buffer(t, buf, size);
|
|
92 }
|
|
93
|
|
94 jchar* as_unicode(int& length) const;
|
|
95
|
|
96 // Treating this symbol as a class name, returns the Java name for the class.
|
|
97 // String is allocated in resource area if buffer is not provided.
|
|
98 // See Klass::external_name()
|
|
99 const char* as_klass_external_name() const;
|
|
100 const char* as_klass_external_name(char* buf, int size) const;
|
|
101
|
|
102 bool object_is_parsable() const {
|
|
103 return (utf8_length() > 0 || (oop)this == Universe::emptySymbol());
|
|
104 }
|
|
105
|
|
106 // Printing
|
|
107 void print_symbol_on(outputStream* st = NULL);
|
|
108 };
|
|
109
|
|
110
|
|
111 // Note: this comparison is used for vtable sorting only; it doesn't matter
|
|
112 // what order it defines, as long as it is a total, time-invariant order
|
|
113 // Since symbolOops are in permSpace, their relative order in memory never changes,
|
|
114 // so use address comparison for speed
|
|
115 int symbolOopDesc::fast_compare(symbolOop other) const {
|
|
116 return (((uintptr_t)this < (uintptr_t)other) ? -1
|
|
117 : ((uintptr_t)this == (uintptr_t) other) ? 0 : 1);
|
|
118 }
|