Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

psymbol.c

Go to the documentation of this file.
00001 /*****************************************************************************\
00002  *  Copyright  1994  The Board of Trustees of the University of Illinois.
00003  *  All rights reserved.
00004  *
00005  *  psymbol.c, part of the preprocessor for the IMPACT Meta-Description 
00006  *  Language 
00007  *  John C. Gyllenhaal, Wen-mei Hwu, and The IMPACT Research Group
00008  *  Coordinated Science Laboratory
00009  *  University of Illinois at Urbana-Champaign
00010  *
00011  *  The IMPACT Research Group may be contacted at impact@crhc.uiuc.edu.
00012  *
00013  *  psymbol.c (part of the preprocessor for the IMPACT Meta-Description 
00014  *  Language software), including both binary and source 
00015  *  (hereafter, Software) is copyrighted by The Board of Trustees of 
00016  *  the University of Illinois (UI), and ownership remains with the UI.
00017  *
00018  *  The UI grants you (hereafter, Licensee) a license to use the Software for
00019  *  academic, research and internal business purposes only, without a fee.
00020  *  Licensee may distribute the Software to third parties provided that the
00021  *  copyright notice and this statement appears on all copies and that no
00022  *  charge is associated with such copies.
00023  *
00024  *  Licensee may make derivative works.  However, if Licensee distributes
00025  *  any derivative work based on or derived from the Software, then Licensee
00026  *  will clearly notify users that such derivative work is a modified
00027  *  version and not the original Software distributed by the UI.
00028  *
00029  *  Any Licensee wishing to make commercial use of the Software should contact
00030  *  the UI, c/o the Research and Technology Management Office [rtmo@uiuc.edu],
00031  *  to negotiate an appropriate license for such commercial use.  Commercial
00032  *  use includes (1) integration of all or part of the source code into a
00033  *  product for sale or license by or on behalf of Licensee to third parties,
00034  *  or (2) distribution of the Software to third parties that need it to
00035  *  utilize a commercial product sold or licensed by or on behalf of Licensee.
00036  *
00037  *  UI MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
00038  *  ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
00039  *  THE UI SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY THE USERS OF THIS
00040  *  SOFTWARE.
00041  *
00042  *  By using or copying this Software, Licensee agrees to abide by the
00043  *  copyright law and all other applicable laws of the U.S. including, but
00044  *  not limited to, export control laws, and the terms of this license.
00045  *  UI shall have the right to terminate this license immediately by written
00046  *  notice upon Licensee's breach of, or non-compliance with, any of its terms.
00047  *  Licensee may be held legally responsible for any copyright infringement
00048  *  that is caused or encouraged by Licensee's failure to abide by the terms
00049  *  of this license.
00050  *
00051  *  Form approved by University Counsel, M.A.R., 05/10/93
00052 \*****************************************************************************/
00053 /*****************************************************************************\
00054  *      File:   psymbol.c
00055  * 
00056  *      Description: Symbol table routines for the IMPACT Meta-Description 
00057  *                   Language preprocessor
00058  * 
00059  *      Creation Date:  October 1994
00060  * 
00061  *      Authors: John C. Gyllenhaal and Wen-mei Hwu
00062  *
00063  *      Revisions:
00064  *  
00065 \*****************************************************************************/
00066 
00067 #include <string.h>
00068 #include "psymbol.h"
00069 
00070 L_Alloc_Pool *Psymbol_Table_pool = NULL;
00071 L_Alloc_Pool *Psymbol_pool = NULL;
00072 
00073 /* Create Psymbol_Table */
00074 Psymbol_Table *create_Psymbol_Table (char *name)
00075 {
00076     Psymbol_Table *table;
00077     int i;
00078 
00079     if (Psymbol_Table_pool == NULL)
00080     {
00081         Psymbol_Table_pool = L_create_alloc_pool ("Psymbol_Table", 
00082                                                   sizeof (Psymbol_Table), 16);
00083     }
00084     table = (Psymbol_Table *) L_alloc (Psymbol_Table_pool);
00085 
00086     /* Initialize fields */
00087     table->name = strdup (name);
00088     for (i=0; i < PSYMBOL_HASH_SIZE; i++)
00089         table->hash[i] = NULL;
00090     table->head = NULL;
00091     table->tail = NULL;
00092     table->count = 0;
00093 
00094     return (table);
00095 }
00096 
00097 /* Free Psymbol table */
00098 void free_Psymbol_Table (Psymbol_Table *table, void (*free_routine)(void *))
00099 {
00100     Psymbol *symbol, *next_symbol;
00101 
00102     /* Free all the symbols in the symbol table */
00103     for (symbol = table->head; symbol != NULL; symbol = next_symbol)
00104     {
00105         /* Get next symbol before deleting this one */
00106         next_symbol = symbol->next_symbol;
00107 
00108         /* Free symbol's contents */
00109         free (symbol->name);
00110         if (free_routine != NULL)
00111             free_routine (symbol->data);
00112 
00113         L_free (Psymbol_pool, symbol);
00114     }
00115 
00116     /* Free symbol table itself */
00117     free (table->name);
00118     
00119     L_free (Psymbol_Table_pool, table);
00120 }
00121 
00122 /* Hashes a Psymbol name, 
00123  * returning a number between 0 and PSYMBOL_HASH_SIZE-1
00124  */
00125 int hash_Psymbol_name (char *name)
00126 {
00127     int hash;
00128     char *ptr;
00129 
00130     hash = 0;
00131 
00132     /* Sum all the characters, shift hash left 1 with each character */
00133     for (ptr = name; *ptr != 0; ptr++)
00134     {
00135         hash = hash << 1;
00136         hash += *ptr;
00137     }
00138 
00139     /* Mask out unused bits */
00140     hash = hash & (PSYMBOL_HASH_SIZE -1);
00141     
00142     return (hash);
00143 }
00144 
00145 /* Low level add routine, data is not copied, it is just pointed to */
00146 void add_Psymbol (Psymbol_Table *table, char *name, void *data)
00147 {
00148     int hash_val;
00149     Psymbol *symbol;
00150 
00151     /* Create symbol */
00152     if (Psymbol_pool == NULL)
00153     {
00154         Psymbol_pool = L_create_alloc_pool ("Psymbol", sizeof (Psymbol), 32);
00155     }
00156     symbol = (Psymbol *) L_alloc (Psymbol_pool);
00157 
00158     /* Initialize fields */
00159     symbol->name = strdup (name);
00160     symbol->data = data;
00161     symbol->table = table;
00162 
00163     /* Add to list of symbols */
00164     symbol->next_symbol = NULL;
00165     symbol->prev_symbol = table->tail;
00166 
00167     if (table->tail == NULL)
00168         table->head = symbol;
00169     else
00170         table->tail->next_symbol = symbol;
00171     table->tail = symbol;
00172     
00173     /* Add to hash table of symbol names */
00174     hash_val = hash_Psymbol_name (name);
00175     symbol->next_hash = table->hash[hash_val];
00176     symbol->prev_hash = NULL;
00177 
00178     if (table->hash[hash_val] != NULL)
00179         table->hash[hash_val]->prev_hash = symbol;
00180     table->hash[hash_val] = symbol;
00181 
00182     /* Increment table's symbol count */
00183     table->count++;
00184 }
00185 
00186 /* low-level find symbol routines,
00187  * returns symbol, or NULL if not found
00188  */
00189 Psymbol *find_Psymbol (Psymbol_Table *table, char *name)
00190 {
00191     Psymbol *symbol;
00192     int hash_val;
00193 
00194     /* Search for name in hash table */
00195     hash_val = hash_Psymbol_name (name);
00196     for (symbol = table->hash[hash_val]; symbol != NULL; 
00197          symbol = symbol->next_hash)
00198     {
00199         /* Stop when match found */
00200         if (strcmp(symbol->name, name) == 0)
00201             break;
00202     }
00203     
00204     /* Return symbol or NULL if not found */
00205     return (symbol);
00206 }
00207 
00208 /* Low-level delete symbol routine. */
00209 void delete_Psymbol (Psymbol *symbol, void (*free_routine)(void *))
00210 {
00211     Psymbol_Table *table;
00212     int hash_val;
00213 
00214     /* Get the table the symbol is from */
00215     table = symbol->table;
00216 
00217     /* hash symbol name to find in symbol table */
00218     hash_val = hash_Psymbol_name (symbol->name);
00219     
00220     /* Remove symbol from hash table */
00221     if (symbol->prev_hash == NULL)
00222         table->hash[hash_val] = symbol->next_hash;
00223     else
00224         symbol->prev_hash->next_hash = symbol->next_hash;
00225 
00226     if (symbol->next_hash != NULL)
00227         symbol->next_hash->prev_hash = symbol->prev_hash;
00228 
00229     /* Remove symbol from symbol list */
00230     if (symbol->prev_symbol == NULL)
00231         table->head = symbol->next_symbol;
00232     else
00233         symbol->prev_symbol->next_symbol = symbol->next_symbol;
00234 
00235     if (symbol->next_symbol == NULL)
00236         table->tail = symbol->prev_symbol;
00237     else
00238         symbol->next_symbol->prev_symbol = symbol->prev_symbol;
00239     
00240     /* Free symbols contents */
00241     free (symbol->name);
00242     if (free_routine != NULL)
00243         free_routine (symbol->data);
00244 
00245     L_free (Psymbol_pool, symbol);
00246 
00247     /* Decrement table symbol count */
00248     table->count --;
00249 }
00250    
00251 
00252 /* Debug routine, prints Psymbol table's hash table */
00253 void print_Psymbol_hash_table (FILE *out, Psymbol_Table *table)
00254 {
00255     int i;
00256     Psymbol *symbol;
00257 
00258     fprintf (out, "%s symbol table has %i entries:\n", table->name,
00259              table->count);
00260 
00261     /* For each hash table list */
00262     for (i = 0; i < PSYMBOL_HASH_SIZE; i++)
00263     {
00264         /* Print out only those hash table lines with symbols in them */
00265         if (table->hash[i] != NULL)
00266         {
00267             fprintf (out, "%4i:", i);
00268             
00269             for (symbol = table->hash[i]; symbol != NULL;
00270                  symbol = symbol->next_hash)
00271             {
00272                 fprintf (out, " %s", symbol->name);
00273             }
00274 
00275             fprintf (out, "\n");            
00276         }
00277     }
00278 }

Generated on Mon Jul 21 20:28:50 2003 for TINKER LEGO DOC by doxygen 1.3.2