src/share/classes/com/sun/tools/javac/util/SharedNameTable.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.SoftReference;
jjg@113 29
jjg@113 30 /**
jjg@113 31 * Implementation of Name.Table that stores all names in a single shared
jjg@113 32 * byte array, expanding it as needed. This avoids the overhead incurred
jjg@113 33 * by using an array of bytes for each name.
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 SharedNameTable extends Name.Table {
jjg@113 41 // maintain a freelist of recently used name tables for reuse.
jjg@113 42 private static List<SoftReference<SharedNameTable>> freelist = List.nil();
jjg@113 43
jjg@113 44 static public synchronized SharedNameTable create(Names names) {
jjg@113 45 while (freelist.nonEmpty()) {
jjg@113 46 SharedNameTable t = freelist.head.get();
jjg@113 47 freelist = freelist.tail;
jjg@113 48 if (t != null) {
jjg@113 49 return t;
jjg@113 50 }
jjg@113 51 }
jjg@113 52 return new SharedNameTable(names);
jjg@113 53 }
jjg@113 54
jjg@113 55 static private synchronized void dispose(SharedNameTable t) {
jjg@113 56 freelist = freelist.prepend(new SoftReference<SharedNameTable>(t));
jjg@113 57 }
jjg@113 58
jjg@113 59 /** The hash table for names.
jjg@113 60 */
jjg@113 61 private NameImpl[] hashes;
jjg@113 62
jjg@113 63 /** The shared byte array holding all encountered names.
jjg@113 64 */
jjg@113 65 public byte[] bytes;
jjg@113 66
jjg@113 67 /** The mask to be used for hashing
jjg@113 68 */
jjg@113 69 private int hashMask;
jjg@113 70
jjg@113 71 /** The number of filled bytes in `names'.
jjg@113 72 */
jjg@113 73 private int nc = 0;
jjg@113 74
jjg@113 75 /** Allocator
jjg@113 76 * @param names The main name table
jjg@113 77 * @param hashSize the (constant) size to be used for the hash table
jjg@113 78 * needs to be a power of two.
jjg@113 79 * @param nameSize the initial size of the name table.
jjg@113 80 */
jjg@113 81 public SharedNameTable(Names names, int hashSize, int nameSize) {
jjg@113 82 super(names);
jjg@113 83 hashMask = hashSize - 1;
jjg@113 84 hashes = new NameImpl[hashSize];
jjg@113 85 bytes = new byte[nameSize];
jjg@113 86
jjg@113 87 }
jjg@113 88
jjg@113 89 public SharedNameTable(Names names) {
jjg@113 90 this(names, 0x8000, 0x20000);
jjg@113 91 }
jjg@113 92
jjg@113 93 @Override
jjg@113 94 public Name fromChars(char[] cs, int start, int len) {
jjg@113 95 int nc = this.nc;
jjg@113 96 byte[] bytes = this.bytes;
jjg@113 97 while (nc + len * 3 >= bytes.length) {
jjg@113 98 // System.err.println("doubling name buffer of length " + names.length + " to fit " + len + " chars");//DEBUG
jjg@113 99 byte[] newnames = new byte[bytes.length * 2];
jjg@113 100 System.arraycopy(bytes, 0, newnames, 0, bytes.length);
jjg@113 101 bytes = this.bytes = newnames;
jjg@113 102 }
jjg@113 103 int nbytes = Convert.chars2utf(cs, start, bytes, nc, len) - nc;
jjg@113 104 int h = hashValue(bytes, nc, nbytes) & hashMask;
jjg@113 105 NameImpl n = hashes[h];
jjg@113 106 while (n != null &&
jjg@113 107 (n.getByteLength() != nbytes ||
jjg@113 108 !equals(bytes, n.index, bytes, nc, nbytes))) {
jjg@113 109 n = n.next;
jjg@113 110 }
jjg@113 111 if (n == null) {
jjg@113 112 n = new NameImpl(this);
jjg@113 113 n.index = nc;
jjg@113 114 n.length = nbytes;
jjg@113 115 n.next = hashes[h];
jjg@113 116 hashes[h] = n;
jjg@113 117 this.nc = nc + nbytes;
jjg@113 118 if (nbytes == 0) {
jjg@113 119 this.nc++;
jjg@113 120 }
jjg@113 121 }
jjg@113 122 return n;
jjg@113 123 }
jjg@113 124
jjg@113 125 @Override
jjg@113 126 public Name fromUtf(byte[] cs, int start, int len) {
jjg@113 127 int h = hashValue(cs, start, len) & hashMask;
jjg@113 128 NameImpl n = hashes[h];
jjg@113 129 byte[] names = this.bytes;
jjg@113 130 while (n != null &&
jjg@113 131 (n.getByteLength() != len || !equals(names, n.index, cs, start, len))) {
jjg@113 132 n = n.next;
jjg@113 133 }
jjg@113 134 if (n == null) {
jjg@113 135 int nc = this.nc;
jjg@113 136 while (nc + len > names.length) {
jjg@113 137 // System.err.println("doubling name buffer of length + " + names.length + " to fit " + len + " bytes");//DEBUG
jjg@113 138 byte[] newnames = new byte[names.length * 2];
jjg@113 139 System.arraycopy(names, 0, newnames, 0, names.length);
jjg@113 140 names = this.bytes = newnames;
jjg@113 141 }
jjg@113 142 System.arraycopy(cs, start, names, nc, len);
jjg@113 143 n = new NameImpl(this);
jjg@113 144 n.index = nc;
jjg@113 145 n.length = len;
jjg@113 146 n.next = hashes[h];
jjg@113 147 hashes[h] = n;
jjg@113 148 this.nc = nc + len;
jjg@113 149 if (len == 0) {
jjg@113 150 this.nc++;
jjg@113 151 }
jjg@113 152 }
jjg@113 153 return n;
jjg@113 154 }
jjg@113 155
jjg@113 156 @Override
jjg@113 157 public void dispose() {
jjg@113 158 dispose(this);
jjg@113 159 }
jjg@113 160
jjg@113 161 static class NameImpl extends Name {
jjg@113 162 /** The next name occupying the same hash bucket.
jjg@113 163 */
jjg@113 164 NameImpl next;
jjg@113 165
jjg@113 166 /** The index where the bytes of this name are stored in the global name
jjg@113 167 * buffer `byte'.
jjg@113 168 */
jjg@113 169 int index;
jjg@113 170
jjg@113 171 /** The number of bytes in this name.
jjg@113 172 */
jjg@113 173 int length;
jjg@113 174
jjg@113 175 NameImpl(SharedNameTable table) {
jjg@113 176 super(table);
jjg@113 177 }
jjg@113 178
jjg@113 179 @Override
jjg@113 180 public int getIndex() {
jjg@113 181 return index;
jjg@113 182 }
jjg@113 183
jjg@113 184 @Override
jjg@113 185 public int getByteLength() {
jjg@113 186 return length;
jjg@113 187 }
jjg@113 188
jjg@113 189 @Override
jjg@113 190 public byte getByteAt(int i) {
jjg@113 191 return getByteArray()[index + i];
jjg@113 192 }
jjg@113 193
jjg@113 194 @Override
jjg@113 195 public byte[] getByteArray() {
jjg@113 196 return ((SharedNameTable) table).bytes;
jjg@113 197 }
jjg@113 198
jjg@113 199 @Override
jjg@113 200 public int getByteOffset() {
jjg@113 201 return index;
jjg@113 202 }
jjg@113 203
jjg@113 204 /** Return the hash value of this name.
jjg@113 205 */
jjg@113 206 public int hashCode() {
jjg@113 207 return index;
jjg@113 208 }
jjg@113 209
jjg@113 210 /** Is this name equal to other?
jjg@113 211 */
jjg@113 212 public boolean equals(Object other) {
jjg@113 213 if (other instanceof Name)
jjg@113 214 return
jjg@113 215 table == ((Name)other).table && index == ((Name) other).getIndex();
jjg@113 216 else return false;
jjg@113 217 }
jjg@113 218
jjg@113 219 }
jjg@113 220
jjg@113 221 }

mercurial