85 // 86 // 3) When a Symbol K is created for temporary use, generally for substrings of 87 // an existing symbol or to create a new symbol, assign it to a 88 // TempNewSymbol. The SymbolTable methods new_symbol(), lookup() 89 // and probe() all potentially return a pointer to a new Symbol. 90 // The allocation (or lookup) of K increments the reference count for K 91 // and the destructor decrements the reference count. 92 // 93 // Another example of TempNewSymbol usage is parsed_name used in 94 // ClassFileParser::parseClassFile() where parsed_name is used in the cleanup 95 // after a failed attempt to load a class. Here parsed_name is a 96 // TempNewSymbol (passed in as a parameter) so the reference count on its symbol 97 // will be decremented when it goes out of scope. 98 99 100 // This cannot be inherited from ResourceObj because it cannot have a vtable. 101 // Since sometimes this is allocated from Metadata, pick a base allocation 102 // type without virtual functions. 103 class ClassLoaderData; 104 105 // We separate the fields in SymbolBase from Symbol::_body so that 106 // Symbol::size(int) can correctly calculate the space needed. 107 class SymbolBase : public MetaspaceObj { 108 public: 109 ATOMIC_SHORT_PAIR( 110 volatile short _refcount, // needs atomic operation 111 unsigned short _length // number of UTF8 characters in the symbol (does not need atomic op) 112 ); 113 int _identity_hash; 114 }; 115 116 class Symbol : private SymbolBase { 117 friend class VMStructs; 118 friend class SymbolTable; 119 friend class MoveSymbols; 120 private: 121 jbyte _body[1]; 122 123 enum { 124 // max_symbol_length is constrained by type of _length 125 max_symbol_length = (1 << 16) -1 126 }; 127 128 static int size(int length) { 129 size_t sz = heap_word_size(sizeof(SymbolBase) + (length > 0 ? length : 0)); 130 return align_object_size(sz); 131 } 132 133 void byte_at_put(int index, int value) { 134 assert(index >=0 && index < _length, "symbol index overflow"); 135 _body[index] = value; 136 } 137 138 Symbol(const u1* name, int length, int refcount); 139 void* operator new(size_t size, int len, TRAPS) throw(); 140 void* operator new(size_t size, int len, Arena* arena, TRAPS) throw(); 141 void* operator new(size_t size, int len, ClassLoaderData* loader_data, TRAPS) throw(); 142 143 void operator delete(void* p); 144 145 public: 146 // Low-level access (used with care, since not GC-safe) 147 const jbyte* base() const { return &_body[0]; } 148 149 int size() { return size(utf8_length()); } 150 151 // Returns the largest size symbol we can safely hold. 152 static int max_length() { return max_symbol_length; } 153 154 int identity_hash() { return _identity_hash; } 155 156 // For symbol table alternate hashing 157 unsigned int new_hash(juint seed); 158 159 // Reference counting. See comments above this class for when to use. 160 int refcount() const { return _refcount; } 161 void increment_refcount(); 162 void decrement_refcount(); 163 164 int byte_at(int index) const { 165 assert(index >=0 && index < _length, "symbol index overflow"); 166 return base()[index]; 167 } 168 169 const jbyte* bytes() const { return base(); } 170 171 int utf8_length() const { return _length; } 172 173 // Compares the symbol with a string. 174 bool equals(const char* str, int len) const; | 85 // 86 // 3) When a Symbol K is created for temporary use, generally for substrings of 87 // an existing symbol or to create a new symbol, assign it to a 88 // TempNewSymbol. The SymbolTable methods new_symbol(), lookup() 89 // and probe() all potentially return a pointer to a new Symbol. 90 // The allocation (or lookup) of K increments the reference count for K 91 // and the destructor decrements the reference count. 92 // 93 // Another example of TempNewSymbol usage is parsed_name used in 94 // ClassFileParser::parseClassFile() where parsed_name is used in the cleanup 95 // after a failed attempt to load a class. Here parsed_name is a 96 // TempNewSymbol (passed in as a parameter) so the reference count on its symbol 97 // will be decremented when it goes out of scope. 98 99 100 // This cannot be inherited from ResourceObj because it cannot have a vtable. 101 // Since sometimes this is allocated from Metadata, pick a base allocation 102 // type without virtual functions. 103 class ClassLoaderData; 104 105 class Symbol : public MetaspaceObj { 106 friend class VMStructs; 107 friend class SymbolTable; 108 friend class MoveSymbols; 109 110 private: 111 ATOMIC_SHORT_PAIR( 112 volatile short _refcount, // needs atomic operation 113 unsigned short _length // number of UTF8 characters in the symbol (does not need atomic op) 114 ); 115 short _identity_hash; 116 jbyte _body[2]; 117 118 enum { 119 // max_symbol_length is constrained by type of _length 120 max_symbol_length = (1 << 16) -1 121 }; 122 123 static int size(int length) { 124 size_t sz = heap_word_size(sizeof(Symbol) + (length > 2 ? length - 2 : 0)); 125 return align_object_size(sz); 126 } 127 128 void byte_at_put(int index, int value) { 129 assert(index >=0 && index < _length, "symbol index overflow"); 130 _body[index] = value; 131 } 132 133 Symbol(const u1* name, int length, int refcount); 134 void* operator new(size_t size, int len, TRAPS) throw(); 135 void* operator new(size_t size, int len, Arena* arena, TRAPS) throw(); 136 void* operator new(size_t size, int len, ClassLoaderData* loader_data, TRAPS) throw(); 137 138 void operator delete(void* p); 139 140 public: 141 // Low-level access (used with care, since not GC-safe) 142 const jbyte* base() const { return &_body[0]; } 143 144 int size() { return size(utf8_length()); } 145 146 // Returns the largest size symbol we can safely hold. 147 static int max_length() { return max_symbol_length; } 148 unsigned identity_hash() { 149 unsigned addr_bits = (unsigned)((uintptr_t)this >> (LogMinObjAlignmentInBytes + 3)); 150 return (unsigned)_identity_hash | 151 ((addr_bits ^ (_length << 8) ^ (( _body[0] << 8) | _body[1])) << 16); 152 } 153 154 // For symbol table alternate hashing 155 unsigned int new_hash(juint seed); 156 157 // Reference counting. See comments above this class for when to use. 158 int refcount() const { return _refcount; } 159 void increment_refcount(); 160 void decrement_refcount(); 161 162 int byte_at(int index) const { 163 assert(index >=0 && index < _length, "symbol index overflow"); 164 return base()[index]; 165 } 166 167 const jbyte* bytes() const { return base(); } 168 169 int utf8_length() const { return _length; } 170 171 // Compares the symbol with a string. 172 bool equals(const char* str, int len) const; |