symbol.cpp 7.5 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25

26
#include "precompiled.hpp"
27 28
#include "classfile/altHashing.hpp"
#include "classfile/classLoaderData.hpp"
29
#include "oops/symbol.hpp"
30
#include "runtime/atomic.inline.hpp"
31 32
#include "runtime/os.hpp"
#include "memory/allocation.inline.hpp"
33
#include "memory/resourceArea.hpp"
34

35
Symbol::Symbol(const u1* name, int length, int refcount) : _refcount(refcount), _length(length) {
36 37 38 39 40
  _identity_hash = os::random();
  for (int i = 0; i < _length; i++) {
    byte_at_put(i, name[i]);
  }
}
D
duke 已提交
41

42
void* Symbol::operator new(size_t sz, int len, TRAPS) {
43
  int alloc_size = size(len)*HeapWordSize;
Z
zgu 已提交
44
  address res = (address) AllocateHeap(alloc_size, mtSymbol);
45 46 47 48
  return res;
}

void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) {
49
  int alloc_size = size(len)*HeapWordSize;
50 51
  address res = (address)arena->Amalloc(alloc_size);
  return res;
52
}
53

54 55 56 57 58 59 60 61 62 63 64 65 66
void* Symbol::operator new(size_t sz, int len, ClassLoaderData* loader_data, TRAPS) {
  address res;
  int alloc_size = size(len)*HeapWordSize;
  res = (address) Metaspace::allocate(loader_data, size(len), true,
                                      Metaspace::NonClassType, CHECK_NULL);
  return res;
}

void Symbol::operator delete(void *p) {
  assert(((Symbol*)p)->refcount() == 0, "should not call this");
  FreeHeap(p);
}

67
// ------------------------------------------------------------------
68
// Symbol::equals
69 70
//
// Compares the symbol with a string of the given length.
71
bool Symbol::equals(const char* str, int len) const {
D
duke 已提交
72 73 74 75 76 77 78 79 80 81
  int l = utf8_length();
  if (l != len) return false;
  while (l-- > 0) {
    if (str[l] != (char) byte_at(l))
      return false;
  }
  assert(l == -1, "we should be at the beginning");
  return true;
}

82 83

// ------------------------------------------------------------------
84
// Symbol::starts_with
85 86 87
//
// Tests if the symbol starts with the specified prefix of the given
// length.
88
bool Symbol::starts_with(const char* prefix, int len) const {
89 90 91 92 93 94 95 96 97 98 99
  if (len > utf8_length()) return false;
  while (len-- > 0) {
    if (prefix[len] != (char) byte_at(len))
      return false;
  }
  assert(len == -1, "we should be at the beginning");
  return true;
}


// ------------------------------------------------------------------
100
// Symbol::index_of
101 102 103
//
// Finds if the given string is a substring of this symbol's utf8 bytes.
// Return -1 on failure.  Otherwise return the first index where str occurs.
104
int Symbol::index_of_at(int i, const char* str, int len) const {
105 106 107
  assert(i >= 0 && i <= utf8_length(), "oob");
  if (len <= 0)  return 0;
  char first_char = str[0];
108
  address bytes = (address) ((Symbol*)this)->base();
109 110 111 112
  address limit = bytes + utf8_length() - len;  // inclusive limit
  address scan = bytes + i;
  if (scan > limit)
    return -1;
113
  for (; scan <= limit; scan++) {
114 115 116 117 118 119 120
    scan = (address) memchr(scan, first_char, (limit + 1 - scan));
    if (scan == NULL)
      return -1;  // not found
    assert(scan >= bytes+i && scan <= limit, "scan oob");
    if (memcmp(scan, str, len) == 0)
      return (int)(scan - bytes);
  }
121
  return -1;
122 123 124
}


125
char* Symbol::as_C_string(char* buf, int size) const {
D
duke 已提交
126 127 128 129 130 131 132 133 134 135
  if (size > 0) {
    int len = MIN2(size - 1, utf8_length());
    for (int i = 0; i < len; i++) {
      buf[i] = byte_at(i);
    }
    buf[len] = '\0';
  }
  return buf;
}

136
char* Symbol::as_C_string() const {
D
duke 已提交
137 138 139 140 141
  int len = utf8_length();
  char* str = NEW_RESOURCE_ARRAY(char, len + 1);
  return as_C_string(str, len + 1);
}

142
char* Symbol::as_C_string_flexible_buffer(Thread* t,
D
duke 已提交
143 144 145 146 147 148 149 150 151 152 153 154
                                                 char* buf, int size) const {
  char* str;
  int len = utf8_length();
  int buf_len = len + 1;
  if (size < buf_len) {
    str = NEW_RESOURCE_ARRAY(char, buf_len);
  } else {
    str = buf;
  }
  return as_C_string(str, buf_len);
}

155
void Symbol::print_symbol_on(outputStream* st) const {
156
  ResourceMark rm;
D
duke 已提交
157
  st = st ? st : tty;
158 159 160 161 162 163 164 165 166
  st->print("%s", as_quoted_ascii());
}

char* Symbol::as_quoted_ascii() const {
  const char *ptr = (const char *)&_body[0];
  int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length());
  char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1);
  UTF8::as_quoted_ascii(ptr, result, quoted_length + 1);
  return result;
D
duke 已提交
167 168
}

169 170
jchar* Symbol::as_unicode(int& length) const {
  Symbol* this_ptr = (Symbol*)this;
D
duke 已提交
171 172 173 174 175 176 177 178
  length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
  jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
  if (length > 0) {
    UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
  }
  return result;
}

179
const char* Symbol::as_klass_external_name(char* buf, int size) const {
D
duke 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  if (size > 0) {
    char* str    = as_C_string(buf, size);
    int   length = (int)strlen(str);
    // Turn all '/'s into '.'s (also for array klasses)
    for (int index = 0; index < length; index++) {
      if (str[index] == '/') {
        str[index] = '.';
      }
    }
    return str;
  } else {
    return buf;
  }
}

195
const char* Symbol::as_klass_external_name() const {
D
duke 已提交
196 197 198 199 200 201 202 203 204 205
  char* str    = as_C_string();
  int   length = (int)strlen(str);
  // Turn all '/'s into '.'s (also for array klasses)
  for (int index = 0; index < length; index++) {
    if (str[index] == '/') {
      str[index] = '.';
    }
  }
  return str;
}
206

207 208 209 210 211 212
// Alternate hashing for unbalanced symbol tables.
unsigned int Symbol::new_hash(jint seed) {
  ResourceMark rm;
  // Use alternate hashing algorithm on this symbol.
  return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length());
}
213

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
void Symbol::increment_refcount() {
  // Only increment the refcount if positive.  If negative either
  // overflow has occurred or it is a permanent symbol in a read only
  // shared archive.
  if (_refcount >= 0) {
    Atomic::inc(&_refcount);
    NOT_PRODUCT(Atomic::inc(&_total_count);)
  }
}

void Symbol::decrement_refcount() {
  if (_refcount >= 0) {
    Atomic::dec(&_refcount);
#ifdef ASSERT
    if (_refcount < 0) {
      print();
      assert(false, "reference count underflow for symbol");
    }
#endif
  }
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
void Symbol::print_on(outputStream* st) const {
  if (this == NULL) {
    st->print_cr("NULL");
  } else {
    st->print("Symbol: '");
    print_symbol_on(st);
    st->print("'");
    st->print(" count %d", refcount());
  }
}

// The print_value functions are present in all builds, to support the
// disassembler and error reporting.
void Symbol::print_value_on(outputStream* st) const {
  if (this == NULL) {
    st->print("NULL");
  } else {
    st->print("'");
    for (int i = 0; i < utf8_length(); i++) {
      st->print("%c", byte_at(i));
    }
    st->print("'");
  }
}

261
// SymbolTable prints this in its statistics
262
NOT_PRODUCT(int Symbol::_total_count = 0;)