1 /*
   2  * Copyright (c) 1997, 2016, 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 
  26 #include "precompiled.hpp"
  27 #include "classfile/altHashing.hpp"
  28 #include "classfile/classLoaderData.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/symbol.hpp"
  32 #include "runtime/atomic.inline.hpp"
  33 #include "runtime/os.hpp"
  34 
  35 Symbol::Symbol(const u1* name, int length, int refcount) {
  36   _refcount = refcount;
  37   _length = length;
  38   _identity_hash = (short)os::random();
  39   for (int i = 0; i < _length; i++) {
  40     byte_at_put(i, name[i]);
  41   }
  42 }
  43 
  44 void* Symbol::operator new(size_t sz, int len, TRAPS) throw() {
  45   int alloc_size = size(len)*wordSize;
  46   address res = (address) AllocateHeap(alloc_size, mtSymbol);
  47   return res;
  48 }
  49 
  50 void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) throw() {
  51   int alloc_size = size(len)*wordSize;
  52   address res = (address)arena->Amalloc_4(alloc_size);
  53   return res;
  54 }
  55 
  56 void* Symbol::operator new(size_t sz, int len, ClassLoaderData* loader_data, TRAPS) throw() {
  57   address res;
  58   res = (address) Metaspace::allocate(loader_data, size(len), true,
  59                                       MetaspaceObj::SymbolType, CHECK_NULL);
  60   return res;
  61 }
  62 
  63 void Symbol::operator delete(void *p) {
  64   assert(((Symbol*)p)->refcount() == 0, "should not call this");
  65   FreeHeap(p);
  66 }
  67 
  68 // ------------------------------------------------------------------
  69 // Symbol::equals
  70 //
  71 // Compares the symbol with a string of the given length.
  72 bool Symbol::equals(const char* str, int len) const {
  73   int l = utf8_length();
  74   if (l != len) return false;
  75   while (l-- > 0) {
  76     if (str[l] != (char) byte_at(l))
  77       return false;
  78   }
  79   assert(l == -1, "we should be at the beginning");
  80   return true;
  81 }
  82 
  83 
  84 // ------------------------------------------------------------------
  85 // Symbol::starts_with
  86 //
  87 // Tests if the symbol starts with the specified prefix of the given
  88 // length.
  89 bool Symbol::starts_with(const char* prefix, int len) const {
  90   if (len > utf8_length()) return false;
  91   while (len-- > 0) {
  92     if (prefix[len] != (char) byte_at(len))
  93       return false;
  94   }
  95   assert(len == -1, "we should be at the beginning");
  96   return true;
  97 }
  98 
  99 
 100 // ------------------------------------------------------------------
 101 // Symbol::index_of
 102 //
 103 // Finds if the given string is a substring of this symbol's utf8 bytes.
 104 // Return -1 on failure.  Otherwise return the first index where str occurs.
 105 int Symbol::index_of_at(int i, const char* str, int len) const {
 106   assert(i >= 0 && i <= utf8_length(), "oob");
 107   if (len <= 0)  return 0;
 108   char first_char = str[0];
 109   address bytes = (address) ((Symbol*)this)->base();
 110   address limit = bytes + utf8_length() - len;  // inclusive limit
 111   address scan = bytes + i;
 112   if (scan > limit)
 113     return -1;
 114   for (; scan <= limit; scan++) {
 115     scan = (address) memchr(scan, first_char, (limit + 1 - scan));
 116     if (scan == NULL)
 117       return -1;  // not found
 118     assert(scan >= bytes+i && scan <= limit, "scan oob");
 119     if (memcmp(scan, str, len) == 0)
 120       return (int)(scan - bytes);
 121   }
 122   return -1;
 123 }
 124 
 125 
 126 char* Symbol::as_C_string(char* buf, int size) const {
 127   if (size > 0) {
 128     int len = MIN2(size - 1, utf8_length());
 129     for (int i = 0; i < len; i++) {
 130       buf[i] = byte_at(i);
 131     }
 132     buf[len] = '\0';
 133   }
 134   return buf;
 135 }
 136 
 137 char* Symbol::as_C_string() const {
 138   int len = utf8_length();
 139   char* str = NEW_RESOURCE_ARRAY(char, len + 1);
 140   return as_C_string(str, len + 1);
 141 }
 142 
 143 char* Symbol::as_C_string_flexible_buffer(Thread* t,
 144                                                  char* buf, int size) const {
 145   char* str;
 146   int len = utf8_length();
 147   int buf_len = len + 1;
 148   if (size < buf_len) {
 149     str = NEW_RESOURCE_ARRAY(char, buf_len);
 150   } else {
 151     str = buf;
 152   }
 153   return as_C_string(str, buf_len);
 154 }
 155 
 156 void Symbol::print_utf8_on(outputStream* st) const {
 157   st->print("%s", as_C_string());
 158 }
 159 
 160 void Symbol::print_symbol_on(outputStream* st) const {
 161   char *s;
 162   st = st ? st : tty;
 163   {
 164     // ResourceMark may not affect st->print(). If st is a string
 165     // stream it could resize, using the same resource arena.
 166     ResourceMark rm;
 167     s = as_quoted_ascii();
 168     s = os::strdup(s);
 169   }
 170   st->print("%s", s);
 171   os::free(s);
 172 }
 173 
 174 char* Symbol::as_quoted_ascii() const {
 175   const char *ptr = (const char *)&_body[0];
 176   int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length());
 177   char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1);
 178   UTF8::as_quoted_ascii(ptr, utf8_length(), result, quoted_length + 1);
 179   return result;
 180 }
 181 
 182 jchar* Symbol::as_unicode(int& length) const {
 183   Symbol* this_ptr = (Symbol*)this;
 184   length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
 185   jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
 186   if (length > 0) {
 187     UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
 188   }
 189   return result;
 190 }
 191 
 192 const char* Symbol::as_klass_external_name(char* buf, int size) const {
 193   if (size > 0) {
 194     char* str    = as_C_string(buf, size);
 195     int   length = (int)strlen(str);
 196     // Turn all '/'s into '.'s (also for array klasses)
 197     for (int index = 0; index < length; index++) {
 198       if (str[index] == '/') {
 199         str[index] = '.';
 200       }
 201     }
 202     return str;
 203   } else {
 204     return buf;
 205   }
 206 }
 207 
 208 const char* Symbol::as_klass_external_name() const {
 209   char* str    = as_C_string();
 210   int   length = (int)strlen(str);
 211   // Turn all '/'s into '.'s (also for array klasses)
 212   for (int index = 0; index < length; index++) {
 213     if (str[index] == '/') {
 214       str[index] = '.';
 215     }
 216   }
 217   return str;
 218 }
 219 
 220 // Alternate hashing for unbalanced symbol tables.
 221 unsigned int Symbol::new_hash(juint seed) {
 222   ResourceMark rm;
 223   // Use alternate hashing algorithm on this symbol.
 224   return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length());
 225 }
 226 
 227 void Symbol::increment_refcount() {
 228   // Only increment the refcount if positive.  If negative either
 229   // overflow has occurred or it is a permanent symbol in a read only
 230   // shared archive.
 231   if (_refcount >= 0) {
 232     Atomic::inc(&_refcount);
 233     NOT_PRODUCT(Atomic::inc(&_total_count);)
 234   }
 235 }
 236 
 237 void Symbol::decrement_refcount() {
 238   if (_refcount >= 0) {
 239     Atomic::dec(&_refcount);
 240 #ifdef ASSERT
 241     if (_refcount < 0) {
 242       print();
 243       assert(false, "reference count underflow for symbol");
 244     }
 245 #endif
 246   }
 247 }
 248 
 249 void Symbol::print_on(outputStream* st) const {
 250   if (this == NULL) {
 251     st->print_cr("NULL");
 252   } else {
 253     st->print("Symbol: '");
 254     print_symbol_on(st);
 255     st->print("'");
 256     st->print(" count %d", refcount());
 257   }
 258 }
 259 
 260 // The print_value functions are present in all builds, to support the
 261 // disassembler and error reporting.
 262 void Symbol::print_value_on(outputStream* st) const {
 263   if (this == NULL) {
 264     st->print("NULL");
 265   } else {
 266     st->print("'");
 267     for (int i = 0; i < utf8_length(); i++) {
 268       st->print("%c", byte_at(i));
 269     }
 270     st->print("'");
 271   }
 272 }
 273 
 274 // SymbolTable prints this in its statistics
 275 NOT_PRODUCT(int Symbol::_total_count = 0;)