src/share/vm/oops/symbol.hpp

Thu, 04 Apr 2019 17:56:29 +0800

author
aoqi
date
Thu, 04 Apr 2019 17:56:29 +0800
changeset 9572
624a0741915c
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

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

mercurial