1 /* 2 * Copyright (c) 1997, 2013, 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_SYMBOL_HPP 26 #define SHARE_VM_OOPS_SYMBOL_HPP 27 28 #include "utilities/utf8.hpp" 29 #include "memory/allocation.hpp" 30 31 // A Symbol is a canonicalized string. 32 // All Symbols reside in global SymbolTable and are reference counted. 33 34 // Reference counting 35 // 36 // All Symbols are allocated and added to the SymbolTable. 37 // When a class is unloaded, the reference counts of the Symbol pointers in 38 // the ConstantPool and in InstanceKlass (see release_C_heap_structures) are 39 // decremented. When the reference count for a Symbol goes to 0, the garbage 40 // collector can free the Symbol and remove it from the SymbolTable. 41 // 42 // 0) Symbols need to be reference counted when a pointer to the Symbol is 43 // saved in persistent storage. This does not include the pointer 44 // in the SymbolTable bucket (the _literal field in HashtableEntry) 45 // that points to the Symbol. All other stores of a Symbol* 46 // to a field of a persistent variable (e.g., the _name filed in 47 // FieldAccessInfo or _ptr in a CPSlot) is reference counted. 48 // 49 // 1) The lookup of a "name" in the SymbolTable either creates a Symbol F for 50 // "name" and returns a pointer to F or finds a pre-existing Symbol F for 51 // "name" and returns a pointer to it. In both cases the reference count for F 52 // is incremented under the assumption that a pointer to F will be created from 53 // the return value. Thus the increment of the reference count is on the lookup 54 // and not on the assignment to the new Symbol*. That is 55 // Symbol* G = lookup() 56 // ^ increment on lookup() 57 // and not 58 // Symbol* G = lookup() 59 // ^ increment on assignmnet 60 // The reference count must be decremented manually when the copy of the 61 // pointer G is destroyed. 62 // 63 // 2) For a local Symbol* A that is a copy of an existing Symbol* B, the 64 // reference counting is elided when the scope of B is greater than the scope 65 // of A. For example, in the code fragment 66 // below "klass" is passed as a parameter to the method. Symbol* "kn" 67 // is a copy of the name in "klass". 68 // 69 // Symbol* kn = klass->name(); 70 // unsigned int d_hash = dictionary()->compute_hash(kn, class_loader); 71 // 72 // The scope of "klass" is greater than the scope of "kn" so the reference 73 // counting for "kn" is elided. 74 // 75 // Symbol* copied from ConstantPool entries are good candidates for reference 76 // counting elision. The ConstantPool entries for a class C exist until C is 77 // unloaded. If a Symbol* is copied out of the ConstantPool into Symbol* X, 78 // the Symbol* in the ConstantPool will in general out live X so the reference 79 // counting on X can be elided. 80 // 81 // For cases where the scope of A is not greater than the scope of B, 82 // the reference counting is explicitly done. See ciSymbol, 83 // ResolutionErrorEntry and ClassVerifier for examples. 84 // 85 // 3) When a Symbol K is created for temporary use, generally for substrings of 86 // an existing symbol or to create a new symbol, assign it to a 87 // TempNewSymbol. The SymbolTable methods new_symbol(), lookup() 88 // and probe() all potentially return a pointer to a new Symbol. 89 // The allocation (or lookup) of K increments the reference count for K 90 // and the destructor decrements the reference count. 91 // 92 // Another example of TempNewSymbol usage is parsed_name used in 93 // ClassFileParser::parseClassFile() where parsed_name is used in the cleanup 94 // after a failed attempt to load a class. Here parsed_name is a 95 // TempNewSymbol (passed in as a parameter) so the reference count on its symbol 96 // will be decremented when it goes out of scope. 97 98 99 // This cannot be inherited from ResourceObj because it cannot have a vtable. 100 // Since sometimes this is allocated from Metadata, pick a base allocation 101 // type without virtual functions. 102 class ClassLoaderData; 103 104 class Symbol : public MetaspaceObj { 105 friend class VMStructs; 106 friend class SymbolTable; 107 friend class MoveSymbols; 108 private: 109 volatile int _refcount; 110 int _identity_hash; 111 unsigned short _length; // number of UTF8 characters in the symbol 112 jbyte _body[1]; 113 114 enum { 115 // max_symbol_length is constrained by type of _length 116 max_symbol_length = (1 << 16) -1 117 }; 118 119 static int size(int length) { 120 size_t sz = heap_word_size(sizeof(Symbol) + (length > 0 ? length - 1 : 0)); 121 return align_object_size(sz); 122 } 123 124 void byte_at_put(int index, int value) { 125 assert(index >=0 && index < _length, "symbol index overflow"); 126 _body[index] = value; 127 } 128 129 Symbol(const u1* name, int length, int refcount); 130 void* operator new(size_t size, int len, TRAPS); 131 void* operator new(size_t size, int len, Arena* arena, TRAPS); 132 void* operator new(size_t size, int len, ClassLoaderData* loader_data, TRAPS); 133 134 void operator delete(void* p); 135 136 public: 137 // Low-level access (used with care, since not GC-safe) 138 const jbyte* base() const { return &_body[0]; } 139 140 int size() { return size(utf8_length()); } 141 142 // Returns the largest size symbol we can safely hold. 143 static int max_length() { return max_symbol_length; } 144 145 int identity_hash() { return _identity_hash; } 146 147 // For symbol table alternate hashing 148 unsigned int new_hash(jint seed); 149 150 // Reference counting. See comments above this class for when to use. 151 int refcount() const { return _refcount; } 152 void increment_refcount(); 153 void decrement_refcount(); 154 155 int byte_at(int index) const { 156 assert(index >=0 && index < _length, "symbol index overflow"); 157 return base()[index]; 158 } 159 160 const jbyte* bytes() const { return base(); } 161 162 int utf8_length() const { return _length; } 163 164 // Compares the symbol with a string. 165 bool equals(const char* str, int len) const; 166 bool equals(const char* str) const { return equals(str, (int) strlen(str)); } 167 168 // Tests if the symbol starts with the given prefix. 169 bool starts_with(const char* prefix, int len) const; 170 bool starts_with(const char* prefix) const { 171 return starts_with(prefix, (int) strlen(prefix)); 172 } 173 174 // Tests if the symbol starts with the given prefix. 175 int index_of_at(int i, const char* str, int len) const; 176 int index_of_at(int i, const char* str) const { 177 return index_of_at(i, str, (int) strlen(str)); 178 } 179 180 // Three-way compare for sorting; returns -1/0/1 if receiver is </==/> than arg 181 // note that the ordering is not alfabetical 182 inline int fast_compare(Symbol* other) const; 183 184 // Returns receiver converted to null-terminated UTF-8 string; string is 185 // allocated in resource area, or in the char buffer provided by caller. 186 char* as_C_string() const; 187 char* as_C_string(char* buf, int size) const; 188 // Use buf if needed buffer length is <= size. 189 char* as_C_string_flexible_buffer(Thread* t, char* buf, int size) const; 190 191 // Returns an escaped form of a Java string. 192 char* as_quoted_ascii() const; 193 194 // Returns a null terminated utf8 string in a resource array 195 char* as_utf8() const { return as_C_string(); } 196 char* as_utf8_flexible_buffer(Thread* t, char* buf, int size) const { 197 return as_C_string_flexible_buffer(t, buf, size); 198 } 199 200 jchar* as_unicode(int& length) const; 201 202 // Treating this symbol as a class name, returns the Java name for the class. 203 // String is allocated in resource area if buffer is not provided. 204 // See Klass::external_name() 205 const char* as_klass_external_name() const; 206 const char* as_klass_external_name(char* buf, int size) const; 207 208 // Printing 209 void print_symbol_on(outputStream* st = NULL) const; 210 void print_on(outputStream* st) const; // First level print 211 void print_value_on(outputStream* st) const; // Second level print. 212 213 // printing on default output stream 214 void print() { print_on(tty); } 215 void print_value() { print_value_on(tty); } 216 217 #ifndef PRODUCT 218 // Empty constructor to create a dummy symbol object on stack 219 // only for getting its vtable pointer. 220 Symbol() { } 221 222 static int _total_count; 223 #endif 224 }; 225 226 // Note: this comparison is used for vtable sorting only; it doesn't matter 227 // what order it defines, as long as it is a total, time-invariant order 228 // Since Symbol*s are in C_HEAP, their relative order in memory never changes, 229 // so use address comparison for speed 230 int Symbol::fast_compare(Symbol* other) const { 231 return (((uintptr_t)this < (uintptr_t)other) ? -1 232 : ((uintptr_t)this == (uintptr_t) other) ? 0 : 1); 233 } 234 #endif // SHARE_VM_OOPS_SYMBOL_HPP