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

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

mercurial