symbolTable.hpp

Go to the documentation of this file.
00001 /* Copyright 1994, 1995 LongView Technologies L.L.C. $Revision: 1.8 $ */
00002 /* Copyright (c) 2006, Sun Microsystems, Inc.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
00006 following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
00010           disclaimer in the documentation and/or other materials provided with the distribution.
00011     * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 
00012           from this software without specific prior written permission.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00020 
00021 
00022 */
00023 
00024 
00025 // The symbol table (Memory->symbol_table) holds all canonical symbols. 
00026 // It is implemented as an open hash table with a fixed number of buckets.
00027 // A bucket (symbolTableEntry) is a union containing either:
00028 //   NULL             => bucket is empty.
00029 //   symbolOop        => bucket has one element.
00030 //   symbolTableLink* => bucket has more than one element.
00031 
00032 // SPACE HACK:
00033 //  - symbolTableLinks are allocated in blocks to reduce the malloc overhead.
00034 
00035 const int symbol_table_size = 20011;
00036 
00037 int hash(char* name, int len);
00038 
00039 struct symbolTableLink {
00040   // instance variable
00041   symbolOop symbol;
00042   symbolTableLink* next;
00043   
00044   // memory operations
00045   bool verify(int i);
00046 };
00047 
00048 struct symbolTableEntry {
00049   void* symbol_or_link;
00050   bool is_empty()  { return symbol_or_link == NULL; }
00051   bool is_symbol() { return oop(symbol_or_link)->is_mem(); }
00052   void clear()     { symbol_or_link = NULL; }
00053 
00054   symbolOop get_symbol() {
00055     return symbolOop(symbol_or_link); }
00056   void set_symbol(symbolOop s) {
00057     symbol_or_link = (void*) s; }
00058 
00059   symbolTableLink* get_link() {
00060     return (symbolTableLink*) symbol_or_link; }
00061   void set_link(symbolTableLink* l) {
00062     symbol_or_link = (void*) l; }
00063 
00064   // memory operations
00065   bool verify(int i);
00066   void deallocate();
00067   
00068   int length();
00069 };
00070 
00071 class symbolTable: public CHeapObj {
00072  private:
00073   // instance variables
00074   symbolTableEntry buckets[symbol_table_size];
00075   symbolTableLink* free_list;
00076   symbolTableLink* first_free_link;
00077   symbolTableLink* end_block;
00078  public:
00079   // constructor
00080   symbolTable();
00081 
00082   // operations
00083   symbolOop lookup(char* name, int len);
00084 
00085   // Used in bootstrap for checking
00086   bool is_present(symbolOop sym);
00087  protected:
00088   void add_symbol(symbolOop s); // Only used by bootstrap
00089 
00090   symbolOop basic_add(char *name, int len, int hashValue);
00091   symbolOop basic_add(symbolOop s, int hashValue);
00092   symbolTableEntry* bucketFor(int hashValue) {
00093     assert(hashValue % symbol_table_size >= 0, "must be positive");
00094     return &buckets[hashValue % symbol_table_size]; }
00095   symbolTableEntry* firstBucket() { return &buckets[0]; }
00096   symbolTableEntry* lastBucket()  { return &buckets[symbol_table_size-1]; }
00097  public:
00098   void add(symbolOop s);
00099   
00100   // memory operations
00101   void follow_used_symbols(); // Used during phase1 of garbage collection
00102 
00103   void switch_pointers(oop from, oop to);
00104   void relocate();
00105   void verify();
00106 
00107   // memory management for symbolTableLinks
00108   symbolTableLink* new_link(symbolOop s, symbolTableLink* n = NULL);
00109   void             delete_link(symbolTableLink* l);
00110 
00111   // histogram
00112   void print_histogram();
00113 
00114   friend bootstrap;
00115 };

Generated on Mon Oct 9 13:37:28 2006 for Strongtalk VM by  doxygen 1.4.7