src/share/vm/oops/symbol.hpp

Print this page




  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


  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()); }




  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 #include "runtime/atomic.hpp"
  31 
  32 // A Symbol is a canonicalized string.
  33 // All Symbols reside in global SymbolTable and are reference counted.
  34 
  35 // Reference counting
  36 //
  37 // All Symbols are allocated and added to the SymbolTable.
  38 // When a class is unloaded, the reference counts of the Symbol pointers in
  39 // the ConstantPool and in InstanceKlass (see release_C_heap_structures) are
  40 // decremented.  When the reference count for a Symbol goes to 0, the garbage
  41 // collector can free the Symbol and remove it from the SymbolTable.
  42 //
  43 // 0) Symbols need to be reference counted when a pointer to the Symbol is
  44 // saved in persistent storage.  This does not include the pointer
  45 // in the SymbolTable bucket (the _literal field in HashtableEntry)
  46 // that points to the Symbol.  All other stores of a Symbol*
  47 // to a field of a persistent variable (e.g., the _name filed in
  48 // FieldAccessInfo or _ptr in a CPSlot) is reference counted.
  49 //
  50 // 1) The lookup of a "name" in the SymbolTable either creates a Symbol F for


  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   friend class VMStructs;
 109   friend class SymbolTable;
 110   friend class MoveSymbols;
 111  public:
 112   ATOMIC_SHORT_PAIR(
 113     volatile short _refcount,  // needs atomic operation
 114     unsigned short _length     // number of UTF8 characters in the symbol (does not need atomic op)
 115   );  
 116   int            _identity_hash;
 117 };
 118 
 119 class Symbol : private SymbolBase {
 120   friend class VMStructs;
 121   friend class SymbolTable;
 122   friend class MoveSymbols;
 123  private:
 124   jbyte _body[1];
 125 
 126   enum {
 127     // max_symbol_length is constrained by type of _length
 128     max_symbol_length = (1 << 16) -1
 129   };
 130 
 131   static int size(int length) {
 132     size_t sz = heap_word_size(sizeof(SymbolBase) + (length > 0 ? length : 0));
 133     return align_object_size(sz);
 134   }
 135 
 136   void byte_at_put(int index, int value) {
 137     assert(index >=0 && index < _length, "symbol index overflow");
 138     _body[index] = value;
 139   }
 140 
 141   Symbol(const u1* name, int length, int refcount);
 142   void* operator new(size_t size, int len, TRAPS);
 143   void* operator new(size_t size, int len, Arena* arena, TRAPS);
 144   void* operator new(size_t size, int len, ClassLoaderData* loader_data, TRAPS);
 145 
 146   void  operator delete(void* p);
 147 
 148  public:
 149   // Low-level access (used with care, since not GC-safe)
 150   const jbyte* base() const { return &_body[0]; }
 151 
 152   int size()                { return size(utf8_length()); }


TPATH=src/share/vm/oops WDIR=/scratch/iklam/jdk/sym2b/webrev RTOP=../../../..