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