]> andersk Git - splint.git/blame - src/sRefTable.c
Renamed lclintMacros.nf splintMacros.nf
[splint.git] / src / sRefTable.c
CommitLineData
616915dd 1/*
11db3170 2** Splint - annotation-assisted static program checker
77d37419 3** Copyright (C) 1994-2002 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**
1b8ae690 20** For information on splint: splint@cs.virginia.edu
21** To report a bug: splint-bug@cs.virginia.edu
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{
92 if (sRefTable_isDefined (s))
93 {
94 int i;
95
96 for (i = 0; i < s->entries; i++)
97 {
28bf4b0b 98 DPRINTF (("Table clear: [%p] %s", s->elements[i], sRef_unparseDebug (s->elements[i])));
99 /* sRef_checkValid (s->elements[i]); */
100 sRef_free (s->elements[i]);
616915dd 101 }
28bf4b0b 102
616915dd 103 s->nspace += s->entries;
104 s->entries = 0;
105 }
106}
107
108static int sRefTable_size (sRefTable s)
109{
110 if (sRefTable_isNull (s)) return 0;
111 return s->entries;
112}
113
114/*@only@*/ cstring
115sRefTable_unparse (sRefTable s)
116{
117 int i;
118 cstring st = cstring_undefined;
119
120 if (sRefTable_isDefined (s))
121 {
122 for (i = 0; i < sRefTable_size (s); i++)
123 {
124 if (i == 0)
125 st = message ("%4d. %q\n", i, sRef_unparse (s->elements[i]));
126 else
127 st = message ("%q%4d. %q\n", st, i, sRef_unparse (s->elements[i]));
128 }
129 }
130 return st;
131}
132
133void
134sRefTable_free (/*@only@*/ sRefTable s)
135{
136 if (sRefTable_isDefined (s))
137 {
138 int i;
139
140 for (i = 0; i < s->entries; i++)
141 {
28bf4b0b 142 DPRINTF (("Table free: [%p] %s", s->elements[i], sRef_unparse (s->elements[i])));
616915dd 143 sRef_free (s->elements[i]);
144 }
145
146 sfree (s->elements);
147 sfree (s);
148 }
149}
150
This page took 0.589818 seconds and 5 git commands to generate.