1 /*
   2  * Copyright (c) 2000, 2018, 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 package sun.jvm.hotspot.oops;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.runtime.*;
  31 import sun.jvm.hotspot.types.*;
  32 import sun.jvm.hotspot.utilities.*;
  33 
  34 // A Symbol is a canonicalized string.
  35 // All Symbols reside in global symbolTable.
  36 
  37 public class Symbol extends VMObject {
  38   static {
  39     VM.registerVMInitializedObserver(new Observer() {
  40         public void update(Observable o, Object data) {
  41           initialize(VM.getVM().getTypeDataBase());
  42         }
  43       });
  44   }
  45 
  46   private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
  47     Type type  = db.lookupType("Symbol");
  48     length     = type.getCIntegerField("_length_and_refcount");
  49     baseOffset = type.getField("_body").getOffset();
  50     idHash = type.getCIntegerField("_identity_hash");
  51   }
  52 
  53   // Format:
  54   //   [header]
  55   //   [klass ]
  56   //   [length] byte size of uft8 string
  57   //   ..body..
  58 
  59   public static Symbol create(Address addr) {
  60     if (addr == null) {
  61       return null;
  62     }
  63     return new Symbol(addr);
  64   }
  65 
  66   Symbol(Address addr) {
  67     super(addr);
  68   }
  69 
  70   public boolean isSymbol()            { return true; }
  71 
  72   private static long baseOffset; // tells where the array part starts
  73 
  74   // Fields
  75   private static CIntegerField length;
  76 
  77   // Accessors for declared fields
  78   public long getLength() {
  79     long i = length.getValue(this.addr);
  80     return (i >> 16) & 0xffff;
  81   }
  82 
  83   public byte getByteAt(long index) {
  84     return addr.getJByteAt(baseOffset + index);
  85   }
  86   // _identity_hash is a short
  87   private static CIntegerField idHash;
  88 
  89   public int identityHash() {
  90     long addr_value = getAddress().asLongValue();
  91     int  addr_bits = (int)(addr_value >> (VM.getVM().getLogMinObjAlignmentInBytes() + 3));
  92     int  length = (int)getLength();
  93     int  byte0 = getByteAt(0);
  94     int  byte1 = getByteAt(1);
  95     int  id_hash = (int)(0xffff & idHash.getValue(this.addr));
  96     return id_hash |
  97            ((addr_bits ^ (length << 8) ^ ((byte0 << 8) | byte1)) << 16);
  98   }
  99 
 100   public boolean equals(byte[] modUTF8Chars) {
 101     int l = (int) getLength();
 102     if (l != modUTF8Chars.length) return false;
 103     while (l-- > 0) {
 104       if (modUTF8Chars[l] != getByteAt(l)) return false;
 105     }
 106     if (Assert.ASSERTS_ENABLED) {
 107       Assert.that(l == -1, "we should be at the beginning");
 108     }
 109     return true;
 110   }
 111 
 112   public boolean equals(String string) {
 113     return asString().equals(string);
 114   }
 115 
 116   public byte[] asByteArray() {
 117     int length = (int) getLength();
 118     byte [] result = new byte [length];
 119     for (int index = 0; index < length; index++) {
 120       result[index] = getByteAt(index);
 121     }
 122     return result;
 123   }
 124 
 125   public String asString() {
 126     // Decode the byte array and return the string.
 127     try {
 128       return readModifiedUTF8(asByteArray());
 129     } catch(Exception e) {
 130       System.err.println(addr);
 131       e.printStackTrace();
 132       return null;
 133     }
 134   }
 135 
 136   public boolean startsWith(String str) {
 137     return asString().startsWith(str);
 138   }
 139 
 140   public void printValueOn(PrintStream tty) {
 141     tty.print("#" + asString());
 142   }
 143 
 144   /** Note: this comparison is used for vtable sorting only; it
 145       doesn't matter what order it defines, as long as it is a total,
 146       time-invariant order Since Symbol* are in C_HEAP, their
 147       relative order in memory never changes, so use address
 148       comparison for speed. */
 149   public long fastCompare(Symbol other) {
 150     return addr.minus(other.addr);
 151   }
 152 
 153   private static String readModifiedUTF8(byte[] buf) throws IOException {
 154     final int len = buf.length;
 155     byte[] tmp = new byte[len + 2];
 156     // write modified UTF-8 length as short in big endian
 157     tmp[0] = (byte) ((len >>> 8) & 0xFF);
 158     tmp[1] = (byte) ((len >>> 0) & 0xFF);
 159     // copy the data
 160     System.arraycopy(buf, 0, tmp, 2, len);
 161     DataInputStream dis = new DataInputStream(new ByteArrayInputStream(tmp));
 162     return dis.readUTF();
 163   }
 164 }