79 // unloaded. If a Symbol* is copied out of the ConstantPool into Symbol* X, 80 // the Symbol* in the ConstantPool will in general out live X so the reference 81 // counting on X can be elided. 82 // 83 // For cases where the scope of A is not greater than the scope of B, 84 // the reference counting is explicitly done. See ciSymbol, 85 // ResolutionErrorEntry and ClassVerifier for examples. 86 // 87 // 3) When a Symbol K is created for temporary use, generally for substrings of 88 // an existing symbol or to create a new symbol, assign it to a 89 // TempNewSymbol. The SymbolTable methods new_symbol(), lookup() 90 // and probe() all potentially return a pointer to a new Symbol. 91 // The allocation (or lookup) of K increments the reference count for K 92 // and the destructor decrements the reference count. 93 // 94 // This cannot be inherited from ResourceObj because it cannot have a vtable. 95 // Since sometimes this is allocated from Metadata, pick a base allocation 96 // type without virtual functions. 97 class ClassLoaderData; 98 99 // Set _refcount to PERM_REFCOUNT to prevent the Symbol from being GC'ed. 100 #ifndef PERM_REFCOUNT 101 #define PERM_REFCOUNT -1 102 #endif 103 104 class Symbol : public MetaspaceObj { 105 friend class VMStructs; 106 friend class SymbolTable; 107 friend class MoveSymbols; 108 109 private: 110 ATOMIC_SHORT_PAIR( 111 volatile short _refcount, // needs atomic operation 112 unsigned short _length // number of UTF8 characters in the symbol (does not need atomic op) 113 ); 114 short _identity_hash; 115 jbyte _body[2]; 116 117 enum { 118 // max_symbol_length is constrained by type of _length 119 max_symbol_length = (1 << 16) -1 120 }; 121 122 static int byte_size(int length) { 123 // minimum number of natural words needed to hold these bits (no non-heap version) 124 return (int)(sizeof(Symbol) + (length > 2 ? length - 2 : 0)); 125 } 126 static int size(int length) { 127 // minimum number of natural words needed to hold these bits (no non-heap version) 128 return (int)heap_word_size(byte_size(length)); 129 } 130 131 void byte_at_put(int index, int value) { 132 assert(index >=0 && index < _length, "symbol index overflow"); 133 _body[index] = value; 134 } 135 136 Symbol(const u1* name, int length, int refcount); 137 void* operator new(size_t size, int len, TRAPS) throw(); 138 void* operator new(size_t size, int len, Arena* arena, TRAPS) throw(); 139 void* operator new(size_t size, int len, ClassLoaderData* loader_data, TRAPS) throw(); 140 141 void operator delete(void* p); 142 143 public: 144 // Low-level access (used with care, since not GC-safe) 145 const jbyte* base() const { return &_body[0]; } 146 147 int size() { return size(utf8_length()); } 148 int byte_size() { return byte_size(utf8_length()); } 149 150 // Symbols should be stored in the read-only region of CDS archive. 151 static bool is_read_only_by_default() { return true; } 152 153 // Returns the largest size symbol we can safely hold. 154 static int max_length() { return max_symbol_length; } 155 unsigned identity_hash() const { 156 unsigned addr_bits = (unsigned)((uintptr_t)this >> (LogMinObjAlignmentInBytes + 3)); 157 return ((unsigned)_identity_hash & 0xffff) | 158 ((addr_bits ^ (_length << 8) ^ (( _body[0] << 8) | _body[1])) << 16); 159 } 160 161 // For symbol table alternate hashing 162 unsigned int new_hash(juint seed); 163 164 // Reference counting. See comments above this class for when to use. 165 int refcount() const { return _refcount; } 166 void increment_refcount(); 167 void decrement_refcount(); 168 bool is_permanent() { 169 return (_refcount == PERM_REFCOUNT); 170 } 171 172 int byte_at(int index) const { 173 assert(index >=0 && index < _length, "symbol index overflow"); 174 return base()[index]; 175 } 176 177 const jbyte* bytes() const { return base(); } 178 179 int utf8_length() const { return _length; } 180 181 // Compares the symbol with a string. 182 bool equals(const char* str, int len) const { 183 int l = utf8_length(); 184 if (l != len) return false; 185 while (l-- > 0) { 186 if (str[l] != (char) byte_at(l)) 187 return false; 188 } 189 assert(l == -1, "we should be at the beginning"); 190 return true; 191 } 192 bool equals(const char* str) const { return equals(str, (int) strlen(str)); } 193 194 // Tests if the symbol starts with the given prefix. 195 bool starts_with(const char* prefix, int len) const; 196 bool starts_with(const char* prefix) const { 197 return starts_with(prefix, (int) strlen(prefix)); 198 } 199 | 79 // unloaded. If a Symbol* is copied out of the ConstantPool into Symbol* X, 80 // the Symbol* in the ConstantPool will in general out live X so the reference 81 // counting on X can be elided. 82 // 83 // For cases where the scope of A is not greater than the scope of B, 84 // the reference counting is explicitly done. See ciSymbol, 85 // ResolutionErrorEntry and ClassVerifier for examples. 86 // 87 // 3) When a Symbol K is created for temporary use, generally for substrings of 88 // an existing symbol or to create a new symbol, assign it to a 89 // TempNewSymbol. The SymbolTable methods new_symbol(), lookup() 90 // and probe() all potentially return a pointer to a new Symbol. 91 // The allocation (or lookup) of K increments the reference count for K 92 // and the destructor decrements the reference count. 93 // 94 // This cannot be inherited from ResourceObj because it cannot have a vtable. 95 // Since sometimes this is allocated from Metadata, pick a base allocation 96 // type without virtual functions. 97 class ClassLoaderData; 98 99 // Set _refcount to PERM_REFCOUNT to prevent the Symbol from being freed. 100 #ifndef PERM_REFCOUNT 101 #define PERM_REFCOUNT ((1 << 16) - 1) 102 #endif 103 104 class Symbol : public MetaspaceObj { 105 friend class VMStructs; 106 friend class SymbolTable; 107 friend class MoveSymbols; 108 109 private: 110 111 // This is an int because it needs atomic operation on the refcount. Mask length 112 // in high half word. length is the number of UTF8 characters in the symbol 113 volatile uint32_t _length_and_refcount; 114 short _identity_hash; 115 jbyte _body[2]; 116 117 enum { 118 // max_symbol_length must fit into the top 16 bits of _length_and_refcount 119 max_symbol_length = (1 << 16) -1 120 }; 121 122 static int byte_size(int length) { 123 // minimum number of natural words needed to hold these bits (no non-heap version) 124 return (int)(sizeof(Symbol) + (length > 2 ? length - 2 : 0)); 125 } 126 static int size(int length) { 127 // minimum number of natural words needed to hold these bits (no non-heap version) 128 return (int)heap_word_size(byte_size(length)); 129 } 130 131 void byte_at_put(int index, int value) { 132 assert(index >=0 && index < length(), "symbol index overflow"); 133 _body[index] = value; 134 } 135 136 Symbol(const u1* name, int length, int refcount); 137 void* operator new(size_t size, int len, TRAPS) throw(); 138 void* operator new(size_t size, int len, Arena* arena, TRAPS) throw(); 139 void* operator new(size_t size, int len, ClassLoaderData* loader_data, TRAPS) throw(); 140 141 void operator delete(void* p); 142 143 static int extract_length(uint32_t value) { return value >> 16; } 144 static int extract_refcount(uint32_t value) { return value & 0xffff; } 145 static uint32_t pack_length_and_refcount(int length, int refcount); 146 147 int length() const { return extract_length(_length_and_refcount); } 148 149 public: 150 // Low-level access (used with care, since not GC-safe) 151 const jbyte* base() const { return &_body[0]; } 152 153 int size() { return size(utf8_length()); } 154 int byte_size() { return byte_size(utf8_length()); } 155 156 // Symbols should be stored in the read-only region of CDS archive. 157 static bool is_read_only_by_default() { return true; } 158 159 // Returns the largest size symbol we can safely hold. 160 static int max_length() { return max_symbol_length; } 161 unsigned identity_hash() const { 162 unsigned addr_bits = (unsigned)((uintptr_t)this >> (LogMinObjAlignmentInBytes + 3)); 163 return ((unsigned)_identity_hash & 0xffff) | 164 ((addr_bits ^ (length() << 8) ^ (( _body[0] << 8) | _body[1])) << 16); 165 } 166 167 // For symbol table alternate hashing 168 unsigned int new_hash(juint seed); 169 170 // Reference counting. See comments above this class for when to use. 171 int refcount() const { return extract_refcount(_length_and_refcount); } 172 bool try_increment_refcount(); 173 void increment_refcount(); 174 void decrement_refcount(); 175 bool is_permanent() { 176 return (refcount() == PERM_REFCOUNT); 177 } 178 179 int byte_at(int index) const { 180 assert(index >=0 && index < length(), "symbol index overflow"); 181 return base()[index]; 182 } 183 184 const jbyte* bytes() const { return base(); } 185 186 int utf8_length() const { return length(); } 187 188 // Compares the symbol with a string. 189 bool equals(const char* str, int len) const { 190 int l = utf8_length(); 191 if (l != len) return false; 192 while (l-- > 0) { 193 if (str[l] != (char) byte_at(l)) 194 return false; 195 } 196 assert(l == -1, "we should be at the beginning"); 197 return true; 198 } 199 bool equals(const char* str) const { return equals(str, (int) strlen(str)); } 200 201 // Tests if the symbol starts with the given prefix. 202 bool starts_with(const char* prefix, int len) const; 203 bool starts_with(const char* prefix) const { 204 return starts_with(prefix, (int) strlen(prefix)); 205 } 206 |