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

typeIdSet.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 ** typeIdSet.c
00026 */
00027 
00028 # include "lclintMacros.nf"
00029 # include "basic.h"
00030 
00031 /*@constant int TISTABLEBASESIZE;@*/
00032 # define TISTABLEBASESIZE LARGEBASESIZE
00033 
00034 static int tistableentries = 0;
00035 static int tistablefree = 0;
00036 typedef /*@only@*/ usymIdSet o_usymIdSet;
00037 static /*@only@*/ o_usymIdSet *tistable;
00038 static void tistable_addDirectEntry (/*@only@*/ usymIdSet p_s) 
00039    /*@modifies tistable, tistableentries, tistablefree@*/;
00040 
00041 void typeIdSet_initMod (void)
00042    /*@globals undef tistable;@*/
00043    /*@modifies tistable, tistablefree;@*/
00044 {
00045   llassert (tistableentries == 0 && tistablefree == 0);
00046 
00047   tistablefree = TISTABLEBASESIZE;
00048   tistable = (usymIdSet *) dmalloc (sizeof (tistable) * tistablefree);
00049   tistable[0] = usymIdSet_undefined;
00050   tistableentries = 1;
00051   tistablefree--;
00052 }
00053 
00054 void typeIdSet_destroyMod (void)
00055    /*@globals killed tistable, tistableentries@*/
00056 {
00057   int i;
00058 
00059   for (i = 0; i < tistableentries; i++)
00060     {
00061       usymIdSet_free (tistable[i]);
00062     }
00063 
00064   sfree (tistable);
00065   tistableentries = 0;
00066 }
00067 
00068 void typeIdSet_dumpTable (FILE *fout)
00069 {
00070   int i;
00071 
00072   /*
00073   ** Don't dump 0th entry
00074   */
00075 
00076   for (i = 1; i < tistableentries; i++)
00077     {
00078       cstring s = usymIdSet_dump (tistable[i]);
00079 
00080       fprintf (fout, "%s\n", cstring_toCharsSafe (s));
00081       cstring_free (s);
00082     }
00083 }
00084 
00085 static /*@unused@*/ void tistable_printOut (void)
00086 {
00087   int i;
00088 
00089   /*
00090   ** Don't dump 0th entry
00091   */
00092 
00093   for (i = 1; i < tistableentries; i++)
00094     {
00095       cstring s = usymIdSet_unparse (tistable[i]);
00096 
00097       fprintf (g_msgstream, "%d: %s\n", i, cstring_toCharsSafe (s));
00098       cstring_free (s);
00099     }
00100 }
00101 
00102 void typeIdSet_loadTable (FILE *fin)
00103 {
00104   char *s = mstring_create (MAX_DUMP_LINE_LENGTH);  
00105   char *os = s;
00106 
00107   llassert (tistableentries == 1);
00108 
00109   s = fgets (s, MAX_DUMP_LINE_LENGTH, fin);
00110 
00111   while (s != NULL && *s != ';')
00112     {
00113       usymIdSet u = usymIdSet_undump (&s);
00114       
00115       llassert (*s == '\0' || *s == '\n');
00116       
00117       tistable_addDirectEntry (u);
00118       s = fgets (os, MAX_DUMP_LINE_LENGTH, fin);
00119     }
00120 }
00121   
00122 static void tistable_grow (void)
00123 {
00124   o_usymIdSet *oldtable = tistable;
00125   int newsize = tistableentries + TISTABLEBASESIZE;
00126   int i;
00127 
00128   llassert (tistablefree == 0);
00129 
00130   tistable = (usymIdSet *) dmalloc (sizeof (tistable) * newsize);
00131 
00132   for (i = 0; i < tistableentries; i++)
00133     {
00134       tistable[i] = oldtable[i];
00135     }
00136 
00137   tistablefree = TISTABLEBASESIZE;
00138   sfree (oldtable);
00139 } 
00140 
00141 static void tistable_addDirectEntry (/*@only@*/ usymIdSet s)
00142 {
00143   if (tistablefree == 0)
00144     {
00145       tistable_grow ();
00146     }
00147 
00148   tistable[tistableentries] = s;
00149   tistableentries++;
00150   tistablefree--;
00151 }
00152 
00153 static int tistable_addEntry (/*@only@*/ usymIdSet s)
00154 {
00155   int i;
00156 
00157   
00158   for (i = 0; i < tistableentries; i++)
00159     {
00160       if (usymIdSet_compare (tistable[i], s) == 0)
00161         {
00162           /*@access usymIdSet@*/
00163           llassert (i == 0 || s != tistable[i]);
00164           /*@noaccess usymIdSet@*/
00165 
00166           usymIdSet_free (s);
00167                   return i;
00168         }
00169     }
00170 
00171   tistable_addDirectEntry (s);
00172   return (tistableentries - 1);
00173 }
00174   
00175 static /*@observer@*/ usymIdSet tistable_fetch (typeIdSet t)
00176    /*@globals tistableentries, tistable@*/
00177    /*@modifies nothing;@*/
00178 {
00179   llassert (t >= 0 && t < tistableentries);
00180 
00181   return tistable[t];
00182 }
00183 
00184 typeIdSet typeIdSet_emptySet (void)
00185 {
00186   if (tistableentries == 0)
00187     {
00188       int val = tistable_addEntry (usymIdSet_new ());
00189 
00190       llassert (val == 0);
00191     }
00192 
00193   llassert (usymIdSet_isUndefined (tistable[0]));
00194   return 0;
00195 }
00196     
00197 bool typeIdSet_member (typeIdSet t, typeId el)
00198 {
00199   usymIdSet u = tistable_fetch (t);
00200 
00201   return usymIdSet_member (u, el);
00202 }
00203 
00204 bool typeIdSet_isEmpty (typeIdSet t)
00205 {
00206   return (t == 0);
00207 }
00208 
00209 typeIdSet typeIdSet_single (typeId t)
00210 {
00211   return (tistable_addEntry (usymIdSet_single (t)));
00212 }
00213 
00214 typeIdSet typeIdSet_singleOpt (typeId t)
00215 {
00216   if (typeId_isValid (t))
00217     {
00218       return (tistable_addEntry (usymIdSet_single (t)));
00219     }
00220   else
00221     {
00222       return typeIdSet_empty;
00223     }
00224 }
00225 
00226 typeIdSet typeIdSet_insert (typeIdSet t, typeId el)
00227 {
00228   usymIdSet u = tistable_fetch (t);
00229 
00230   if (usymIdSet_member (u, el))
00231     {
00232       return t;
00233     }
00234   else
00235     {
00236       return (tistable_addEntry (usymIdSet_add (u, el)));
00237     }
00238 }
00239 
00240 typeIdSet typeIdSet_removeFresh (typeIdSet t, typeId el)
00241 {
00242   return (tistable_addEntry (usymIdSet_removeFresh (tistable_fetch (t), el)));
00243 }
00244 
00245 cstring typeIdSet_unparse (typeIdSet t)
00246 {
00247   return (usymIdSet_unparse (tistable_fetch (t)));
00248 }
00249 
00250 int typeIdSet_compare (typeIdSet t1, typeIdSet t2)
00251 {
00252   return (int_compare (t1, t2));
00253 }
00254 
00255 typeIdSet typeIdSet_subtract (typeIdSet s, typeIdSet t)
00256 {
00257   if (typeIdSet_isEmpty (t))
00258     {
00259       return s;
00260     }
00261   else
00262     {
00263       return (tistable_addEntry (usymIdSet_subtract (tistable_fetch (s),
00264                                                      tistable_fetch (t))));
00265     }
00266 }
00267 
00268 cstring typeIdSet_dump (typeIdSet t)
00269 {
00270   return (message ("%d", t));
00271 }
00272 
00273 typeIdSet typeIdSet_undump (char **s)
00274 {
00275   int i;
00276 
00277   
00278   i = getInt (s);
00279 
00280   llassert (i >= 0 && i < tistableentries);
00281   return (typeIdSet) i;
00282 }
00283 
00284 typeIdSet typeIdSet_union (typeIdSet t1, typeIdSet t2)
00285 {
00286   if (t1 == typeIdSet_undefined)
00287     {
00288       return t2;
00289     }
00290   else if (t2 == typeIdSet_undefined)
00291     {
00292       return t1;
00293     }
00294   else
00295     {
00296       return (tistable_addEntry (usymIdSet_newUnion (tistable_fetch (t1),
00297                                                      tistable_fetch (t2))));
00298     }
00299 }

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