Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

mapping.c

Go to the documentation of this file.
00001 /*
00002 ** LCLint - annotation-assisted static program checker
00003 ** Copyright (C) 1994-2000 University of Virginia,
00004 **         Massachusetts Institute of Technology
00005 **
00006 ** This program is free software; you can redistribute it and/or modify it
00007 ** under the terms of the GNU General Public License as published by the
00008 ** Free Software Foundation; either version 2 of the License, or (at your
00009 ** option) any later version.
00010 ** 
00011 ** This program is distributed in the hope that it will be useful, but
00012 ** WITHOUT ANY WARRANTY; without even the implied warranty of
00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 ** General Public License for more details.
00015 ** 
00016 ** The GNU General Public License is available from http://www.gnu.org/ or
00017 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00018 ** MA 02111-1307, USA.
00019 **
00020 ** For information on lclint: lclint-request@cs.virginia.edu
00021 ** To report a bug: lclint-bug@cs.virginia.edu
00022 ** For more information: http://lclint.cs.virginia.edu
00023 */
00024 /*
00025 ** mapping.c
00026 **
00027 ** Module for lsymbol maps.
00028 **
00029 **  AUTHOR:
00030 **      Yang Meng Tan,
00031 **         Massachusetts Institute of Technology
00032 */
00033 
00034 # include "lclintMacros.nf"
00035 # include "llbasic.h"
00036 
00037 /*@constant int MAPPING_SIZE; @*/
00038 # define MAPPING_SIZE 127
00039 
00040 /* use lower-order bits by masking out higher order bits */
00041 
00042 /*@-macrofcndecl@*/
00043 # define MMASH(key)  ((unsigned int) ((key) & MAPPING_SIZE))
00044 /*@=macrofcndecl@*/
00045 
00046 static void mappair_free (/*@null@*/ /*@only@*/ mappair *p)
00047 {
00048   if (p == NULL) 
00049     {
00050       return;
00051     }
00052   else
00053     {
00054       mappair_free (p->next);
00055       sfree (p);
00056     }
00057 }
00058 
00059 void mapping_free (/*@only@*/ mapping *m)
00060 {
00061   int i;
00062 
00063   for (i = 0; i <= MAPPING_SIZE; i++)
00064     {
00065       mappair_free (m->buckets[i]);
00066     }
00067   
00068   sfree (m->buckets);
00069   sfree (m);
00070 }
00071 
00072 /*@only@*/ mapping *
00073 mapping_create (void)
00074 {
00075   int i;
00076   mapping *t = (mapping *) dmalloc (sizeof (*t));
00077 
00078   t->buckets = (mappair **) dmalloc ((MAPPING_SIZE + 1) * sizeof (*t->buckets));
00079   t->count = 0;
00080 
00081   for (i = 0; i <= MAPPING_SIZE; i++)
00082     {
00083       t->buckets[i] = (mappair *) 0;
00084     }
00085 
00086   return t;
00087 }
00088 
00089 lsymbol
00090 mapping_find (mapping * t, lsymbol domain)
00091 {
00092   mappair *entry;
00093   unsigned int key;
00094 
00095   key = MMASH (domain);
00096   entry = t->buckets[key];
00097   for (; entry != NULL; entry = entry->next)
00098     {
00099       if (entry->domain == domain)
00100         return entry->range;
00101     }
00102 
00103   return lsymbol_undefined;
00104 }
00105 
00106 void
00107 mapping_bind (mapping *t, lsymbol domain, lsymbol range)
00108 {
00109   /* add the binding (domain -> range) to t */
00110   /* can assume that the binding is a new one in m, so no need
00111      to check. */
00112   mappair *entry;
00113   mappair *newentry = (mappair *) dmalloc (sizeof (*newentry));
00114   unsigned int key;
00115 
00116   key = MMASH (domain);
00117   /*@-deparrays@*/ entry = t->buckets[key]; /*@=deparrays@*/
00118   newentry->domain = domain;
00119   newentry->range = range;
00120   newentry->next = entry;
00121 
00122   t->buckets[key] = newentry; 
00123   t->count++;
00124 }

Generated at Fri Nov 3 18:57:43 2000 for LCLint by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000