src/share/classes/com/sun/tools/javac/util/UnsharedNameTable.java

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 113
eff38cc97183
child 554
9d9f26857129
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

jjg@113 1 /*
xdono@117 2 * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved.
jjg@113 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@113 4 *
jjg@113 5 * This code is free software; you can redistribute it and/or modify it
jjg@113 6 * under the terms of the GNU General Public License version 2 only, as
jjg@113 7 * published by the Free Software Foundation. Sun designates this
jjg@113 8 * particular file as subject to the "Classpath" exception as provided
jjg@113 9 * by Sun in the LICENSE file that accompanied this code.
jjg@113 10 *
jjg@113 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@113 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@113 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@113 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@113 15 * accompanied this code).
jjg@113 16 *
jjg@113 17 * You should have received a copy of the GNU General Public License version
jjg@113 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@113 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@113 20 *
jjg@113 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@113 22 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@113 23 * have any questions.
jjg@113 24 */
jjg@113 25
jjg@113 26 package com.sun.tools.javac.util;
jjg@113 27
jjg@113 28 import java.lang.ref.WeakReference;
jjg@113 29
jjg@113 30 /**
jjg@113 31 * Implementation of Name.Table that stores names in individual arrays
jjg@113 32 * using weak references. It is recommended for use when a single shared
jjg@113 33 * byte array is unsuitable.
jjg@113 34 *
jjg@113 35 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@113 36 * you write code that depends on this, you do so at your own risk.
jjg@113 37 * This code and its internal interfaces are subject to change or
jjg@113 38 * deletion without notice.</b>
jjg@113 39 */
jjg@113 40 public class UnsharedNameTable extends Name.Table {
jjg@113 41 static public Name.Table create(Names names) {
jjg@113 42 return new UnsharedNameTable(names);
jjg@113 43 }
jjg@113 44
jjg@113 45 static class HashEntry extends WeakReference<NameImpl> {
jjg@113 46 HashEntry next;
jjg@113 47 HashEntry(NameImpl referent) {
jjg@113 48 super(referent);
jjg@113 49 }
jjg@113 50 }
jjg@113 51
jjg@113 52 /** The hash table for names.
jjg@113 53 */
jjg@113 54 private HashEntry[] hashes = null;
jjg@113 55
jjg@113 56 /** The mask to be used for hashing
jjg@113 57 */
jjg@113 58 private int hashMask;
jjg@113 59
jjg@113 60 /** Index counter for names in this table.
jjg@113 61 */
jjg@113 62 public int index;
jjg@113 63
jjg@113 64 /** Allocator
jjg@113 65 * @param names The main name table
jjg@113 66 * @param hashSize the (constant) size to be used for the hash table
jjg@113 67 * needs to be a power of two.
jjg@113 68 */
jjg@113 69 public UnsharedNameTable(Names names, int hashSize) {
jjg@113 70 super(names);
jjg@113 71 hashMask = hashSize - 1;
jjg@113 72 hashes = new HashEntry[hashSize];
jjg@113 73 }
jjg@113 74
jjg@113 75 public UnsharedNameTable(Names names) {
jjg@113 76 this(names, 0x8000);
jjg@113 77 }
jjg@113 78
jjg@113 79
jjg@113 80 @Override
jjg@113 81 public Name fromChars(char[] cs, int start, int len) {
jjg@113 82 byte[] name = new byte[len * 3];
jjg@113 83 int nbytes = Convert.chars2utf(cs, start, name, 0, len);
jjg@113 84 return fromUtf(name, 0, nbytes);
jjg@113 85 }
jjg@113 86
jjg@113 87 @Override
jjg@113 88 public Name fromUtf(byte[] cs, int start, int len) {
jjg@113 89 int h = hashValue(cs, start, len) & hashMask;
jjg@113 90
jjg@113 91 HashEntry element = hashes[h];
jjg@113 92
jjg@113 93 NameImpl n = null;
jjg@113 94
jjg@113 95 HashEntry previousNonNullTableEntry = null;
jjg@113 96 HashEntry firstTableEntry = element;
jjg@113 97
jjg@113 98 while (element != null) {
jjg@113 99 if (element == null) {
jjg@113 100 break;
jjg@113 101 }
jjg@113 102
jjg@113 103 n = element.get();
jjg@113 104
jjg@113 105 if (n == null) {
jjg@113 106 if (firstTableEntry == element) {
jjg@113 107 hashes[h] = firstTableEntry = element.next;
jjg@113 108 }
jjg@113 109 else {
jjg@113 110 assert previousNonNullTableEntry != null : "previousNonNullTableEntry cannot be null here.";
jjg@113 111 previousNonNullTableEntry.next = element.next;
jjg@113 112 }
jjg@113 113 }
jjg@113 114 else {
jjg@113 115 if (n.getByteLength() == len && equals(n.bytes, 0, cs, start, len)) {
jjg@113 116 return n;
jjg@113 117 }
jjg@113 118 previousNonNullTableEntry = element;
jjg@113 119 }
jjg@113 120
jjg@113 121 element = element.next;
jjg@113 122 }
jjg@113 123
jjg@113 124 byte[] bytes = new byte[len];
jjg@113 125 System.arraycopy(cs, start, bytes, 0, len);
jjg@113 126 n = new NameImpl(this, bytes, index++);
jjg@113 127
jjg@113 128 System.arraycopy(cs, start, n.bytes, 0, len);
jjg@113 129
jjg@113 130 HashEntry newEntry = new HashEntry(n);
jjg@113 131
jjg@113 132 if (previousNonNullTableEntry == null) { // We are not the first name with that hashCode.
jjg@113 133 hashes[h] = newEntry;
jjg@113 134 }
jjg@113 135 else {
jjg@113 136 assert previousNonNullTableEntry.next == null : "previousNonNullTableEntry.next must be null.";
jjg@113 137 previousNonNullTableEntry.next = newEntry;
jjg@113 138 }
jjg@113 139
jjg@113 140 return n;
jjg@113 141 }
jjg@113 142
jjg@113 143 @Override
jjg@113 144 public void dispose() {
jjg@113 145 hashes = null;
jjg@113 146 }
jjg@113 147
jjg@113 148 static class NameImpl extends Name {
jjg@113 149 NameImpl(UnsharedNameTable table, byte[] bytes, int index) {
jjg@113 150 super(table);
jjg@113 151 this.bytes = bytes;
jjg@113 152 this.index = index;
jjg@113 153 }
jjg@113 154
jjg@113 155 final byte[] bytes;
jjg@113 156 final int index;
jjg@113 157
jjg@113 158 @Override
jjg@113 159 public int getIndex() {
jjg@113 160 return index;
jjg@113 161 }
jjg@113 162
jjg@113 163 @Override
jjg@113 164 public int getByteLength() {
jjg@113 165 return bytes.length;
jjg@113 166 }
jjg@113 167
jjg@113 168 @Override
jjg@113 169 public byte getByteAt(int i) {
jjg@113 170 return bytes[i];
jjg@113 171 }
jjg@113 172
jjg@113 173 @Override
jjg@113 174 public byte[] getByteArray() {
jjg@113 175 return bytes;
jjg@113 176 }
jjg@113 177
jjg@113 178 @Override
jjg@113 179 public int getByteOffset() {
jjg@113 180 return 0;
jjg@113 181 }
jjg@113 182
jjg@113 183 }
jjg@113 184
jjg@113 185 }

mercurial