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

Tue, 25 Sep 2012 13:06:58 -0700

author
jjg
date
Tue, 25 Sep 2012 13:06:58 -0700
changeset 1339
0e5899f09dab
parent 581
f2fdd52e4e87
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
Reviewed-by: mcimadamore, jjg
Contributed-by: vicenterz@yahoo.es

jjg@113 1 /*
jjg@1339 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@113 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * 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@581 35 * <p><b>This is NOT part of any supported API.
jjg@581 36 * If 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@1339 96 byte[] bytes = this.bytes = ArrayUtils.ensureCapacity(this.bytes, nc + len * 3);
jjg@113 97 int nbytes = Convert.chars2utf(cs, start, bytes, nc, len) - nc;
jjg@113 98 int h = hashValue(bytes, nc, nbytes) & hashMask;
jjg@113 99 NameImpl n = hashes[h];
jjg@113 100 while (n != null &&
jjg@113 101 (n.getByteLength() != nbytes ||
jjg@113 102 !equals(bytes, n.index, bytes, nc, nbytes))) {
jjg@113 103 n = n.next;
jjg@113 104 }
jjg@113 105 if (n == null) {
jjg@113 106 n = new NameImpl(this);
jjg@113 107 n.index = nc;
jjg@113 108 n.length = nbytes;
jjg@113 109 n.next = hashes[h];
jjg@113 110 hashes[h] = n;
jjg@113 111 this.nc = nc + nbytes;
jjg@113 112 if (nbytes == 0) {
jjg@113 113 this.nc++;
jjg@113 114 }
jjg@113 115 }
jjg@113 116 return n;
jjg@113 117 }
jjg@113 118
jjg@113 119 @Override
jjg@113 120 public Name fromUtf(byte[] cs, int start, int len) {
jjg@113 121 int h = hashValue(cs, start, len) & hashMask;
jjg@113 122 NameImpl n = hashes[h];
jjg@113 123 byte[] names = this.bytes;
jjg@113 124 while (n != null &&
jjg@113 125 (n.getByteLength() != len || !equals(names, n.index, cs, start, len))) {
jjg@113 126 n = n.next;
jjg@113 127 }
jjg@113 128 if (n == null) {
jjg@113 129 int nc = this.nc;
jjg@1339 130 names = this.bytes = ArrayUtils.ensureCapacity(names, nc + len);
jjg@113 131 System.arraycopy(cs, start, names, nc, len);
jjg@113 132 n = new NameImpl(this);
jjg@113 133 n.index = nc;
jjg@113 134 n.length = len;
jjg@113 135 n.next = hashes[h];
jjg@113 136 hashes[h] = n;
jjg@113 137 this.nc = nc + len;
jjg@113 138 if (len == 0) {
jjg@113 139 this.nc++;
jjg@113 140 }
jjg@113 141 }
jjg@113 142 return n;
jjg@113 143 }
jjg@113 144
jjg@113 145 @Override
jjg@113 146 public void dispose() {
jjg@113 147 dispose(this);
jjg@113 148 }
jjg@113 149
jjg@113 150 static class NameImpl extends Name {
jjg@113 151 /** The next name occupying the same hash bucket.
jjg@113 152 */
jjg@113 153 NameImpl next;
jjg@113 154
jjg@113 155 /** The index where the bytes of this name are stored in the global name
jjg@113 156 * buffer `byte'.
jjg@113 157 */
jjg@113 158 int index;
jjg@113 159
jjg@113 160 /** The number of bytes in this name.
jjg@113 161 */
jjg@113 162 int length;
jjg@113 163
jjg@113 164 NameImpl(SharedNameTable table) {
jjg@113 165 super(table);
jjg@113 166 }
jjg@113 167
jjg@113 168 @Override
jjg@113 169 public int getIndex() {
jjg@113 170 return index;
jjg@113 171 }
jjg@113 172
jjg@113 173 @Override
jjg@113 174 public int getByteLength() {
jjg@113 175 return length;
jjg@113 176 }
jjg@113 177
jjg@113 178 @Override
jjg@113 179 public byte getByteAt(int i) {
jjg@113 180 return getByteArray()[index + i];
jjg@113 181 }
jjg@113 182
jjg@113 183 @Override
jjg@113 184 public byte[] getByteArray() {
jjg@113 185 return ((SharedNameTable) table).bytes;
jjg@113 186 }
jjg@113 187
jjg@113 188 @Override
jjg@113 189 public int getByteOffset() {
jjg@113 190 return index;
jjg@113 191 }
jjg@113 192
jjg@113 193 /** Return the hash value of this name.
jjg@113 194 */
jjg@113 195 public int hashCode() {
jjg@113 196 return index;
jjg@113 197 }
jjg@113 198
jjg@113 199 /** Is this name equal to other?
jjg@113 200 */
jjg@113 201 public boolean equals(Object other) {
jjg@113 202 if (other instanceof Name)
jjg@113 203 return
jjg@113 204 table == ((Name)other).table && index == ((Name) other).getIndex();
jjg@113 205 else return false;
jjg@113 206 }
jjg@113 207
jjg@113 208 }
jjg@113 209
jjg@113 210 }

mercurial