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

sRefTable.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 ** sRefTable.c
00026 **
00027 ** based on table_template.c
00028 **
00029 ** where T has T_equal (or change this) and T_unparse
00030 */
00031 
00032 # include "lclintMacros.nf"
00033 # include "basic.h"
00034 # include "sRefTable.h"
00035 
00036 static /*@notnull@*/ /*@only@*/ sRefTable
00037 sRefTable_new (void)
00038 {
00039   sRefTable s = (sRefTable) dmalloc (sizeof (*s));
00040 
00041   s->entries = 0;
00042   s->nspace = sRefTableBASESIZE;
00043   s->elements = (sRef *) dmalloc (sizeof (*s->elements) * sRefTableBASESIZE);
00044 
00045   return (s);
00046 }
00047 
00048 static void
00049 sRefTable_grow (/*@notnull@*/ sRefTable s)
00050 {
00051   int i;
00052   sRef *newelements; 
00053 
00054   s->nspace = sRefTableBASESIZE;
00055   newelements = (sRef *) dmalloc (sizeof (*newelements) * (s->entries + s->nspace));
00056 
00057   for (i = 0; i < s->entries; i++)
00058     {
00059       newelements[i] = s->elements[i];
00060     }
00061 
00062   sfree (s->elements);
00063   s->elements = newelements;
00064 }
00065 
00066 sRefTable
00067 sRefTable_add (sRefTable s, /*@owned@*/ sRef el)
00068 {
00069   if (sRefTable_isNull (s))
00070     {
00071       s = sRefTable_new ();
00072     }
00073 
00074   if (s->nspace <= 0)
00075     {
00076       sRefTable_grow (s);
00077     }
00078 
00079   s->nspace--;
00080   
00081   llassert (s->elements != NULL);
00082   s->elements[s->entries] = el;
00083   
00084   s->entries++;
00085 
00086   return s;
00087 }
00088 
00089 void
00090 sRefTable_clear (sRefTable s)
00091 {
00092   if (sRefTable_isDefined (s))
00093     {
00094       int i;
00095       
00096       for (i = 0; i < s->entries; i++)
00097         {
00098                   sRef_free (s->elements[i]); 
00099         }
00100 
00101       s->nspace += s->entries;
00102       s->entries = 0;
00103     }
00104 }
00105 
00106 static int sRefTable_size (sRefTable s)
00107 {
00108   if (sRefTable_isNull (s)) return 0;
00109   return s->entries;
00110 }
00111 
00112 /*@only@*/ cstring
00113 sRefTable_unparse (sRefTable s)
00114 {
00115   int i;
00116   cstring st = cstring_undefined;
00117 
00118   if (sRefTable_isDefined (s))
00119     {
00120       for (i = 0; i < sRefTable_size (s); i++)
00121         {
00122           if (i == 0)
00123             st = message ("%4d. %q\n", i, sRef_unparse (s->elements[i]));
00124           else
00125             st = message ("%q%4d. %q\n", st, i, sRef_unparse (s->elements[i]));
00126         }
00127     }
00128   return st;
00129 }
00130 
00131 void
00132 sRefTable_free (/*@only@*/ sRefTable s)
00133 {
00134   if (sRefTable_isDefined (s))
00135     {
00136       int i;
00137 
00138       for (i = 0; i < s->entries; i++)
00139         {
00140           sRef_free (s->elements[i]);
00141         }
00142 
00143       sfree (s->elements); 
00144       sfree (s);
00145     }
00146 }
00147 

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