hotspot/src/share/vm/classfile/symbolTable.cpp
author ikrylov
Wed, 01 Dec 2010 18:26:32 -0500
changeset 7405 e6fc8d3926f8
parent 7397 5b173b4ca846
child 8076 96d498ec7ae1
permissions -rw-r--r--
6348631: remove the use of the HPI library from Hotspot Summary: move functions from hpi library to hotspot, communicate with licensees and open source community, check jdk for dependency, file CCC request Reviewed-by: coleenp, acorn, dsamersoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2332
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2332
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2332
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "classfile/javaClasses.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "classfile/symbolTable.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "classfile/systemDictionary.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "gc_interface/collectedHeap.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "memory/filemap.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
#include "memory/gcLocker.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
#include "oops/oop.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    33
#include "oops/oop.inline2.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    34
#include "oops/symbolKlass.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    35
#include "runtime/mutexLocker.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    36
#include "utilities/hashtable.inline.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// --------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
SymbolTable* SymbolTable::_the_table = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
// Lookup a symbol in a bucket.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
symbolOop SymbolTable::lookup(int index, const char* name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
                              int len, unsigned int hash) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  for (HashtableEntry* e = bucket(index); e != NULL; e = e->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    if (e->hash() == hash) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
      symbolOop sym = symbolOop(e->literal());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
      if (sym->equals(name, len)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
        return sym;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
// We take care not to be blocking while holding the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
// SymbolTable_lock. Otherwise, the system might deadlock, since the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
// symboltable is used during compilation (VM_thread) The lock free
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// synchronization is simplified by the fact that we do not delete
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
// entries in the symbol table during normal execution (only during
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
// safepoints).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
symbolOop SymbolTable::lookup(const char* name, int len, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  unsigned int hashValue = hash_symbol(name, len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  int index = the_table()->hash_to_index(hashValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  symbolOop s = the_table()->lookup(index, name, len, hashValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  // Found
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  if (s != NULL) return s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  // Otherwise, add to symbol to table
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  return the_table()->basic_add(index, (u1*)name, len, hashValue, CHECK_NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
symbolOop SymbolTable::lookup(symbolHandle sym, int begin, int end, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  char* buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  int index, len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  unsigned int hashValue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  char* name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    debug_only(No_Safepoint_Verifier nsv;)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    name = (char*)sym->base() + begin;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    len = end - begin;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    hashValue = hash_symbol(name, len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    index = the_table()->hash_to_index(hashValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    symbolOop s = the_table()->lookup(index, name, len, hashValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    // Found
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    if (s != NULL) return s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  // Otherwise, add to symbol to table. Copy to a C string first.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  char stack_buf[128];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  ResourceMark rm(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  if (len <= 128) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    buffer = stack_buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  for (int i=0; i<len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    buffer[i] = name[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  // Make sure there is no safepoint in the code above since name can't move.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  // We can't include the code in No_Safepoint_Verifier because of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  // ResourceMark.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  return the_table()->basic_add(index, (u1*)buffer, len, hashValue, CHECK_NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
symbolOop SymbolTable::lookup_only(const char* name, int len,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
                                   unsigned int& hash) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  hash = hash_symbol(name, len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  int index = the_table()->hash_to_index(hash);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  return the_table()->lookup(index, name, len, hash);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
2332
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   122
// Suggestion: Push unicode-based lookup all the way into the hashing
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   123
// and probing logic, so there is no need for convert_to_utf8 until
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   124
// an actual new symbolOop is created.
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   125
symbolOop SymbolTable::lookup_unicode(const jchar* name, int utf16_length, TRAPS) {
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   126
  int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   127
  char stack_buf[128];
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   128
  if (utf8_length < (int) sizeof(stack_buf)) {
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   129
    char* chars = stack_buf;
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   130
    UNICODE::convert_to_utf8(name, utf16_length, chars);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   131
    return lookup(chars, utf8_length, THREAD);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   132
  } else {
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   133
    ResourceMark rm(THREAD);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   134
    char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   135
    UNICODE::convert_to_utf8(name, utf16_length, chars);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   136
    return lookup(chars, utf8_length, THREAD);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   137
  }
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   138
}
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   139
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   140
symbolOop SymbolTable::lookup_only_unicode(const jchar* name, int utf16_length,
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   141
                                           unsigned int& hash) {
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   142
  int utf8_length = UNICODE::utf8_length((jchar*) name, utf16_length);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   143
  char stack_buf[128];
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   144
  if (utf8_length < (int) sizeof(stack_buf)) {
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   145
    char* chars = stack_buf;
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   146
    UNICODE::convert_to_utf8(name, utf16_length, chars);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   147
    return lookup_only(chars, utf8_length, hash);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   148
  } else {
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   149
    ResourceMark rm;
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   150
    char* chars = NEW_RESOURCE_ARRAY(char, utf8_length + 1);;
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   151
    UNICODE::convert_to_utf8(name, utf16_length, chars);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   152
    return lookup_only(chars, utf8_length, hash);
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   153
  }
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   154
}
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 2131
diff changeset
   155
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
void SymbolTable::add(constantPoolHandle cp, int names_count,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
                      const char** names, int* lengths, int* cp_indices,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
                      unsigned int* hashValues, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  SymbolTable* table = the_table();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  bool added = table->basic_add(cp, names_count, names, lengths,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
                                cp_indices, hashValues, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  if (!added) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    // do it the hard way
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    for (int i=0; i<names_count; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
      int index = table->hash_to_index(hashValues[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
      symbolOop sym = table->basic_add(index, (u1*)names[i], lengths[i],
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
                                       hashValues[i], CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
      cp->symbol_at_put(cp_indices[i], sym);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
symbolOop SymbolTable::basic_add(int index, u1 *name, int len,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
                                 unsigned int hashValue, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
         "proposed name of symbol must be stable");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  // We assume that lookup() has been called already, that it failed,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  // and symbol was not found.  We create the symbol here.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  symbolKlass* sk  = (symbolKlass*) Universe::symbolKlassObj()->klass_part();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  symbolOop s_oop = sk->allocate_symbol(name, len, CHECK_NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  symbolHandle sym (THREAD, s_oop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  // Allocation must be done before grapping the SymbolTable_lock lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  MutexLocker ml(SymbolTable_lock, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  assert(sym->equals((char*)name, len), "symbol must be properly initialized");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  // Since look-up was done lock-free, we need to check if another
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  // thread beat us in the race to insert the symbol.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  symbolOop test = lookup(index, (char*)name, len, hashValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  if (test != NULL) {
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 1
diff changeset
   194
    // A race occurred and another thread introduced the symbol, this one
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    // will be dropped and collected.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    return test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  HashtableEntry* entry = new_entry(hashValue, sym());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  add_entry(index, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  return sym();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
bool SymbolTable::basic_add(constantPoolHandle cp, int names_count,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
                            const char** names, int* lengths,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
                            int* cp_indices, unsigned int* hashValues,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
                            TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  symbolKlass* sk  = (symbolKlass*) Universe::symbolKlassObj()->klass_part();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  symbolOop sym_oops[symbol_alloc_batch_size];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  bool allocated = sk->allocate_symbols(names_count, names, lengths,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
                                        sym_oops, CHECK_false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  if (!allocated) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  symbolHandle syms[symbol_alloc_batch_size];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  for (i=0; i<names_count; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    syms[i] = symbolHandle(THREAD, sym_oops[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  // Allocation must be done before grabbing the SymbolTable_lock lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  MutexLocker ml(SymbolTable_lock, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  for (i=0; i<names_count; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    assert(syms[i]->equals(names[i], lengths[i]), "symbol must be properly initialized");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    // Since look-up was done lock-free, we need to check if another
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
    // thread beat us in the race to insert the symbol.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    int index = hash_to_index(hashValues[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    symbolOop test = lookup(index, names[i], lengths[i], hashValues[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    if (test != NULL) {
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 1
diff changeset
   231
      // A race occurred and another thread introduced the symbol, this one
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      // will be dropped and collected. Use test instead.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
      cp->symbol_at_put(cp_indices[i], test);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
      symbolOop sym = syms[i]();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
      HashtableEntry* entry = new_entry(hashValues[i], sym);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
      add_entry(index, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      cp->symbol_at_put(cp_indices[i], sym);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
void SymbolTable::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  for (int i = 0; i < the_table()->table_size(); ++i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
    HashtableEntry* p = the_table()->bucket(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    for ( ; p != NULL; p = p->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      symbolOop s = symbolOop(p->literal());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
      guarantee(s != NULL, "symbol is NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
      s->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
      guarantee(s->is_perm(), "symbol not in permspace");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
      unsigned int h = hash_symbol((char*)s->bytes(), s->utf8_length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
      guarantee(p->hash() == h, "broken hash in symbol table entry");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      guarantee(the_table()->hash_to_index(h) == i,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
                "wrong index in symbol table");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
//---------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
// Non-product code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
void SymbolTable::print_histogram() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  MutexLocker ml(SymbolTable_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  const int results_length = 100;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  int results[results_length];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  int i,j;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  // initialize results to zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  for (j = 0; j < results_length; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    results[j] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  int total = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  int max_symbols = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  int out_of_range = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  for (i = 0; i < the_table()->table_size(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    HashtableEntry* p = the_table()->bucket(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
    for ( ; p != NULL; p = p->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
      int counter = symbolOop(p->literal())->utf8_length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
      total += counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
      if (counter < results_length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
        results[counter]++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
        out_of_range++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
      max_symbols = MAX2(max_symbols, counter);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  tty->print_cr("Symbol Table:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  tty->print_cr("%8s %5d", "Total  ", total);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  tty->print_cr("%8s %5d", "Maximum", max_symbols);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  tty->print_cr("%8s %3.2f", "Average",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
          ((float) total / (float) the_table()->table_size()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  tty->print_cr("%s", "Histogram:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  tty->print_cr(" %s %29s", "Length", "Number chains that length");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  for (i = 0; i < results_length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    if (results[i] > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
      tty->print_cr("%6d %10d", i, results[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  int line_length = 70;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  tty->print_cr("%s %30s", " Length", "Number chains that length");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  for (i = 0; i < results_length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
    if (results[i] > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
      tty->print("%4d", i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
      for (j = 0; (j < results[i]) && (j < line_length);  j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
        tty->print("%1s", "*");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
      if (j == line_length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
        tty->print("%1s", "+");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
      tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  tty->print_cr(" %s %d: %d\n", "Number chains longer than",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
                    results_length, out_of_range);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
#endif // PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
// --------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
class StableMemoryChecker : public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
  enum { _bufsize = wordSize*4 };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  address _region;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  jint    _size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  u1      _save_buf[_bufsize];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  int sample(u1* save_buf) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
    if (_size <= _bufsize) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
      memcpy(save_buf, _region, _size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
      return _size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
      // copy head and tail
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
      memcpy(&save_buf[0],          _region,                      _bufsize/2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
      memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
      return (_bufsize/2)*2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  StableMemoryChecker(const void* region, jint size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    _region = (address) region;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
    _size   = size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    sample(_save_buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  bool verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    u1 check_buf[sizeof(_save_buf)];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    int check_size = sample(check_buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    return (0 == memcmp(_save_buf, check_buf, check_size));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  void set_region(const void* region) { _region = (address) region; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
// --------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
// Compute the hash value for a java.lang.String object which would
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
// contain the characters passed in. This hash value is used for at
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
// least two purposes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
// (a) As the hash value used by the StringTable for bucket selection
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
//     and comparison (stored in the HashtableEntry structures).  This
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
//     is used in the String.intern() method.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
// (b) As the hash value used by the String object itself, in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
//     String.hashCode().  This value is normally calculate in Java code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
//     in the String.hashCode method(), but is precomputed for String
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
//     objects in the shared archive file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
//     For this reason, THIS ALGORITHM MUST MATCH String.hashCode().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
int StringTable::hash_string(jchar* s, int len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
  unsigned h = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  while (len-- > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    h = 31*h + (unsigned) *s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    s++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  return h;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
StringTable* StringTable::_the_table = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
oop StringTable::lookup(int index, jchar* name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
                        int len, unsigned int hash) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
  for (HashtableEntry* l = bucket(index); l != NULL; l = l->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
    if (l->hash() == hash) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
      if (java_lang_String::equals(l->literal(), name, len)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
        return l->literal();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
oop StringTable::basic_add(int index, Handle string_or_null, jchar* name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
                           int len, unsigned int hashValue, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  debug_only(StableMemoryChecker smc(name, len * sizeof(name[0])));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
         "proposed name of symbol must be stable");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  Handle string;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  // try to reuse the string if possible
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  if (!string_or_null.is_null() && string_or_null()->is_perm()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
    string = string_or_null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
    string = java_lang_String::create_tenured_from_unicode(name, len, CHECK_NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  // Allocation must be done before grapping the SymbolTable_lock lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  MutexLocker ml(StringTable_lock, THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  assert(java_lang_String::equals(string(), name, len),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
         "string must be properly initialized");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  // Since look-up was done lock-free, we need to check if another
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  // thread beat us in the race to insert the symbol.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  if (test != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
    // Entry already added
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
    return test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  HashtableEntry* entry = new_entry(hashValue, string());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
  add_entry(index, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  return string();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
oop StringTable::lookup(symbolOop symbol) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  int length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
  jchar* chars = symbol->as_unicode(length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  unsigned int hashValue = hash_string(chars, length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  int index = the_table()->hash_to_index(hashValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  return the_table()->lookup(index, chars, length, hashValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
oop StringTable::intern(Handle string_or_null, jchar* name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
                        int len, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
  unsigned int hashValue = hash_string(name, len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  int index = the_table()->hash_to_index(hashValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  oop string = the_table()->lookup(index, name, len, hashValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  // Found
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  if (string != NULL) return string;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  // Otherwise, add to symbol to table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  return the_table()->basic_add(index, string_or_null, name, len,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
                                hashValue, CHECK_NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
oop StringTable::intern(symbolOop symbol, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  if (symbol == NULL) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  ResourceMark rm(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  int length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  jchar* chars = symbol->as_unicode(length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  Handle string;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  oop result = intern(string, chars, length, CHECK_NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
oop StringTable::intern(oop string, TRAPS)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  if (string == NULL) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  ResourceMark rm(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  int length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  Handle h_string (THREAD, string);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
  jchar* chars = java_lang_String::as_unicode_string(string, length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
  oop result = intern(h_string, chars, length, CHECK_NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
oop StringTable::intern(const char* utf8_string, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
  if (utf8_string == NULL) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
  ResourceMark rm(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
  int length = UTF8::unicode_length(utf8_string);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  jchar* chars = NEW_RESOURCE_ARRAY(jchar, length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  UTF8::convert_to_unicode(utf8_string, chars, length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  Handle string;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  oop result = intern(string, chars, length, CHECK_NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
void StringTable::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  for (int i = 0; i < the_table()->table_size(); ++i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
    HashtableEntry* p = the_table()->bucket(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
    for ( ; p != NULL; p = p->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
      oop s = p->literal();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
      guarantee(s != NULL, "interned string is NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
      guarantee(s->is_perm(), "interned string not in permspace");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
      int length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
      jchar* chars = java_lang_String::as_unicode_string(s, length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
      unsigned int h = hash_string(chars, length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
      guarantee(p->hash() == h, "broken hash in string table entry");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
      guarantee(the_table()->hash_to_index(h) == i,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
                "wrong index in string table");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
}