]> andersk Git - splint.git/blob - src/sRefTable.c
Pushed back constraintResolve.c to the previous version.
[splint.git] / src / sRefTable.c
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
10 ** 
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ** General Public License for more details.
15 ** 
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
19 **
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
23 */
24 /*
25 ** sRefTable.c
26 **
27 ** based on table_template.c
28 **
29 ** where T has T_equal (or change this) and T_unparse
30 */
31
32 # include "splintMacros.nf"
33 # include "basic.h"
34 # include "sRefTable.h"
35
36 static /*@notnull@*/ /*@only@*/ sRefTable
37 sRefTable_new (void)
38 {
39   sRefTable s = (sRefTable) dmalloc (sizeof (*s));
40
41   s->entries = 0;
42   s->nspace = sRefTableBASESIZE;
43   s->elements = (sRef *) dmalloc (sizeof (*s->elements) * sRefTableBASESIZE);
44
45   return (s);
46 }
47
48 static void
49 sRefTable_grow (/*@notnull@*/ sRefTable s)
50 {
51   int i;
52   sRef *newelements; 
53
54   s->nspace = sRefTableBASESIZE;
55   newelements = (sRef *) dmalloc (sizeof (*newelements) * (s->entries + s->nspace));
56
57   for (i = 0; i < s->entries; i++)
58     {
59       newelements[i] = s->elements[i];
60     }
61
62   sfree (s->elements);
63   s->elements = newelements;
64 }
65
66 sRefTable
67 sRefTable_add (sRefTable s, /*@owned@*/ sRef el)
68 {
69   if (sRefTable_isNull (s))
70     {
71       s = sRefTable_new ();
72     }
73
74   if (s->nspace <= 0)
75     {
76       sRefTable_grow (s);
77     }
78
79   s->nspace--;
80   
81   llassert (s->elements != NULL);
82   s->elements[s->entries] = el;
83   DPRINTF (("Adding to sRef table: [%p]", el));
84   
85   s->entries++;
86   return s;
87 }
88
89 void
90 sRefTable_clear (sRefTable s)
91 {
92 # ifdef DEBUGSPLINT
93   usymtab_checkAllValid ();
94 # endif
95
96   if (sRefTable_isDefined (s))
97     {
98       int i;
99       
100       for (i = 0; i < s->entries; i++)
101         {
102           DPRINTF (("Table clear: [%p] %s", s->elements[i], sRef_unparseDebug (s->elements[i])));
103           /* sRef_checkValid (s->elements[i]); */
104           sRef_free (s->elements[i]); 
105         }
106       
107       s->nspace += s->entries;
108       s->entries = 0;
109     }
110
111 # ifdef DEBUGSPLINT
112   usymtab_checkAllValid ();
113 # endif
114 }
115
116 static int sRefTable_size (sRefTable s)
117 {
118   if (sRefTable_isNull (s)) return 0;
119   return s->entries;
120 }
121
122 /*@only@*/ cstring
123 sRefTable_unparse (sRefTable s)
124 {
125   int i;
126   cstring st = cstring_undefined;
127
128   if (sRefTable_isDefined (s))
129     {
130       for (i = 0; i < sRefTable_size (s); i++)
131         {
132           if (i == 0)
133             st = message ("%4d. %q\n", i, sRef_unparse (s->elements[i]));
134           else
135             st = message ("%q%4d. %q\n", st, i, sRef_unparse (s->elements[i]));
136         }
137     }
138   return st;
139 }
140
141 void
142 sRefTable_free (/*@only@*/ sRefTable s)
143 {
144   if (sRefTable_isDefined (s))
145     {
146       int i;
147
148       for (i = 0; i < s->entries; i++)
149         {
150           DPRINTF (("Table free: [%p] %s", s->elements[i], sRef_unparse (s->elements[i])));
151           sRef_free (s->elements[i]);
152         }
153
154       sfree (s->elements); 
155       sfree (s);
156     }
157 }
158
This page took 0.078823 seconds and 5 git commands to generate.