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

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

mercurial