]> andersk Git - splint.git/blame - src/sRefTable.c
Fixed -help <mode> bug.
[splint.git] / src / sRefTable.c
CommitLineData
616915dd 1/*
11db3170 2** Splint - annotation-assisted static program checker
c59f5181 3** Copyright (C) 1994-2003 University of Virginia,
616915dd 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**
155af98d 20** For information on splint: info@splint.org
21** To report a bug: splint-bug@splint.org
11db3170 22** For more information: http://www.splint.org
616915dd 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
1b8ae690 32# include "splintMacros.nf"
616915dd 33# include "basic.h"
34# include "sRefTable.h"
35
36static /*@notnull@*/ /*@only@*/ sRefTable
37sRefTable_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
48static void
49sRefTable_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
66sRefTable
67sRefTable_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;
28bf4b0b 83 DPRINTF (("Adding to sRef table: [%p]", el));
616915dd 84
85 s->entries++;
616915dd 86 return s;
87}
88
89void
90sRefTable_clear (sRefTable s)
91{
6483a926 92# ifdef DEBUGSPLINT
93 usymtab_checkAllValid ();
94# endif
95
616915dd 96 if (sRefTable_isDefined (s))
97 {
98 int i;
99
100 for (i = 0; i < s->entries; i++)
101 {
28bf4b0b 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]);
616915dd 105 }
28bf4b0b 106
616915dd 107 s->nspace += s->entries;
108 s->entries = 0;
109 }
6483a926 110
111# ifdef DEBUGSPLINT
112 usymtab_checkAllValid ();
113# endif
616915dd 114}
115
116static int sRefTable_size (sRefTable s)
117{
118 if (sRefTable_isNull (s)) return 0;
119 return s->entries;
120}
121
122/*@only@*/ cstring
123sRefTable_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
141void
142sRefTable_free (/*@only@*/ sRefTable s)
143{
144 if (sRefTable_isDefined (s))
145 {
146 int i;
147
148 for (i = 0; i < s->entries; i++)
149 {
28bf4b0b 150 DPRINTF (("Table free: [%p] %s", s->elements[i], sRef_unparse (s->elements[i])));
616915dd 151 sRef_free (s->elements[i]);
152 }
153
154 sfree (s->elements);
155 sfree (s);
156 }
157}
158
This page took 0.093157 seconds and 5 git commands to generate.