jjg@113: /*
jjg@113: * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
jjg@113: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@113: *
jjg@113: * This code is free software; you can redistribute it and/or modify it
jjg@113: * under the terms of the GNU General Public License version 2 only, as
jjg@113: * published by the Free Software Foundation. Sun designates this
jjg@113: * particular file as subject to the "Classpath" exception as provided
jjg@113: * by Sun in the LICENSE file that accompanied this code.
jjg@113: *
jjg@113: * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@113: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@113: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@113: * version 2 for more details (a copy is included in the LICENSE file that
jjg@113: * accompanied this code).
jjg@113: *
jjg@113: * You should have received a copy of the GNU General Public License version
jjg@113: * 2 along with this work; if not, write to the Free Software Foundation,
jjg@113: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@113: *
jjg@113: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@113: * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@113: * have any questions.
jjg@113: */
jjg@113:
jjg@113: package com.sun.tools.javac.util;
jjg@113:
jjg@113: import java.lang.ref.WeakReference;
jjg@113:
jjg@113: /**
jjg@113: * Implementation of Name.Table that stores names in individual arrays
jjg@113: * using weak references. It is recommended for use when a single shared
jjg@113: * byte array is unsuitable.
jjg@113: *
jjg@113: *
This is NOT part of any API supported by Sun Microsystems. If
jjg@113: * you write code that depends on this, you do so at your own risk.
jjg@113: * This code and its internal interfaces are subject to change or
jjg@113: * deletion without notice.
jjg@113: */
jjg@113: public class UnsharedNameTable extends Name.Table {
jjg@113: static public Name.Table create(Names names) {
jjg@113: return new UnsharedNameTable(names);
jjg@113: }
jjg@113:
jjg@113: static class HashEntry extends WeakReference {
jjg@113: HashEntry next;
jjg@113: HashEntry(NameImpl referent) {
jjg@113: super(referent);
jjg@113: }
jjg@113: }
jjg@113:
jjg@113: /** The hash table for names.
jjg@113: */
jjg@113: private HashEntry[] hashes = null;
jjg@113:
jjg@113: /** The mask to be used for hashing
jjg@113: */
jjg@113: private int hashMask;
jjg@113:
jjg@113: /** Index counter for names in this table.
jjg@113: */
jjg@113: public int index;
jjg@113:
jjg@113: /** Allocator
jjg@113: * @param names The main name table
jjg@113: * @param hashSize the (constant) size to be used for the hash table
jjg@113: * needs to be a power of two.
jjg@113: */
jjg@113: public UnsharedNameTable(Names names, int hashSize) {
jjg@113: super(names);
jjg@113: hashMask = hashSize - 1;
jjg@113: hashes = new HashEntry[hashSize];
jjg@113: }
jjg@113:
jjg@113: public UnsharedNameTable(Names names) {
jjg@113: this(names, 0x8000);
jjg@113: }
jjg@113:
jjg@113:
jjg@113: @Override
jjg@113: public Name fromChars(char[] cs, int start, int len) {
jjg@113: byte[] name = new byte[len * 3];
jjg@113: int nbytes = Convert.chars2utf(cs, start, name, 0, len);
jjg@113: return fromUtf(name, 0, nbytes);
jjg@113: }
jjg@113:
jjg@113: @Override
jjg@113: public Name fromUtf(byte[] cs, int start, int len) {
jjg@113: int h = hashValue(cs, start, len) & hashMask;
jjg@113:
jjg@113: HashEntry element = hashes[h];
jjg@113:
jjg@113: NameImpl n = null;
jjg@113:
jjg@113: HashEntry previousNonNullTableEntry = null;
jjg@113: HashEntry firstTableEntry = element;
jjg@113:
jjg@113: while (element != null) {
jjg@113: if (element == null) {
jjg@113: break;
jjg@113: }
jjg@113:
jjg@113: n = element.get();
jjg@113:
jjg@113: if (n == null) {
jjg@113: if (firstTableEntry == element) {
jjg@113: hashes[h] = firstTableEntry = element.next;
jjg@113: }
jjg@113: else {
jjg@113: assert previousNonNullTableEntry != null : "previousNonNullTableEntry cannot be null here.";
jjg@113: previousNonNullTableEntry.next = element.next;
jjg@113: }
jjg@113: }
jjg@113: else {
jjg@113: if (n.getByteLength() == len && equals(n.bytes, 0, cs, start, len)) {
jjg@113: return n;
jjg@113: }
jjg@113: previousNonNullTableEntry = element;
jjg@113: }
jjg@113:
jjg@113: element = element.next;
jjg@113: }
jjg@113:
jjg@113: byte[] bytes = new byte[len];
jjg@113: System.arraycopy(cs, start, bytes, 0, len);
jjg@113: n = new NameImpl(this, bytes, index++);
jjg@113:
jjg@113: System.arraycopy(cs, start, n.bytes, 0, len);
jjg@113:
jjg@113: HashEntry newEntry = new HashEntry(n);
jjg@113:
jjg@113: if (previousNonNullTableEntry == null) { // We are not the first name with that hashCode.
jjg@113: hashes[h] = newEntry;
jjg@113: }
jjg@113: else {
jjg@113: assert previousNonNullTableEntry.next == null : "previousNonNullTableEntry.next must be null.";
jjg@113: previousNonNullTableEntry.next = newEntry;
jjg@113: }
jjg@113:
jjg@113: return n;
jjg@113: }
jjg@113:
jjg@113: @Override
jjg@113: public void dispose() {
jjg@113: hashes = null;
jjg@113: }
jjg@113:
jjg@113: static class NameImpl extends Name {
jjg@113: NameImpl(UnsharedNameTable table, byte[] bytes, int index) {
jjg@113: super(table);
jjg@113: this.bytes = bytes;
jjg@113: this.index = index;
jjg@113: }
jjg@113:
jjg@113: final byte[] bytes;
jjg@113: final int index;
jjg@113:
jjg@113: @Override
jjg@113: public int getIndex() {
jjg@113: return index;
jjg@113: }
jjg@113:
jjg@113: @Override
jjg@113: public int getByteLength() {
jjg@113: return bytes.length;
jjg@113: }
jjg@113:
jjg@113: @Override
jjg@113: public byte getByteAt(int i) {
jjg@113: return bytes[i];
jjg@113: }
jjg@113:
jjg@113: @Override
jjg@113: public byte[] getByteArray() {
jjg@113: return bytes;
jjg@113: }
jjg@113:
jjg@113: @Override
jjg@113: public int getByteOffset() {
jjg@113: return 0;
jjg@113: }
jjg@113:
jjg@113: }
jjg@113:
jjg@113: }