]>
Commit | Line | Data |
---|---|---|
28bf4b0b | 1 | /* |
2 | ** LCLint - annotation-assisted static program checker | |
3 | ** Copyright (C) 1994-2001 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 lclint: lclint-request@cs.virginia.edu | |
21 | ** To report a bug: lclint-bug@cs.virginia.edu | |
22 | ** For more information: http://lclint.cs.virginia.edu | |
23 | */ | |
24 | /* | |
25 | ** mtLoseReferenceList.c | |
26 | ** | |
27 | ** based on list_template.c | |
28 | ** | |
29 | ** where T has T_equal (or change this) and T_unparse | |
30 | */ | |
31 | ||
32 | # include "lclintMacros.nf" | |
33 | # include "basic.h" | |
34 | # include "mtgrammar.h" | |
35 | ||
36 | mtLoseReferenceList | |
37 | mtLoseReferenceList_new () | |
38 | { | |
39 | return mtLoseReferenceList_undefined; | |
40 | } | |
41 | ||
42 | static /*@notnull@*/ mtLoseReferenceList | |
43 | mtLoseReferenceList_newEmpty (void) | |
44 | { | |
45 | mtLoseReferenceList s = (mtLoseReferenceList) dmalloc (sizeof (*s)); | |
46 | ||
47 | s->nelements = 0; | |
48 | s->nspace = mtLoseReferenceListBASESIZE; | |
49 | s->elements = (mtLoseReference *) dmalloc (sizeof (*s->elements) * mtLoseReferenceListBASESIZE); | |
50 | ||
51 | return (s); | |
52 | } | |
53 | ||
54 | static void | |
55 | mtLoseReferenceList_grow (/*@notnull@*/ mtLoseReferenceList s) | |
56 | { | |
57 | int i; | |
58 | mtLoseReference *newelements; | |
59 | ||
60 | s->nspace += mtLoseReferenceListBASESIZE; | |
61 | ||
62 | newelements = (mtLoseReference *) dmalloc (sizeof (*newelements) * (s->nelements + s->nspace)); | |
63 | ||
64 | if (newelements == (mtLoseReference *) 0) | |
65 | { | |
66 | llfatalerror (cstring_makeLiteral ("mtLoseReferenceList_grow: out of memory!")); | |
67 | } | |
68 | ||
69 | for (i = 0; i < s->nelements; i++) | |
70 | { | |
71 | newelements[i] = s->elements[i]; | |
72 | } | |
73 | ||
74 | sfree (s->elements); | |
75 | s->elements = newelements; | |
76 | } | |
77 | ||
78 | mtLoseReferenceList mtLoseReferenceList_single (/*@keep@*/ mtLoseReference el) | |
79 | { | |
80 | mtLoseReferenceList s = mtLoseReferenceList_new (); | |
81 | s = mtLoseReferenceList_add (s, el); | |
82 | return s; | |
83 | } | |
84 | ||
85 | mtLoseReferenceList mtLoseReferenceList_add (mtLoseReferenceList s, /*@keep@*/ mtLoseReference el) | |
86 | { | |
87 | if (!mtLoseReferenceList_isDefined (s)) | |
88 | { | |
89 | s = mtLoseReferenceList_newEmpty (); | |
90 | } | |
91 | ||
92 | if (s->nspace <= 0) | |
93 | { | |
94 | mtLoseReferenceList_grow (s); | |
95 | } | |
96 | ||
97 | s->nspace--; | |
98 | /*@i32@*/ s->elements[s->nelements] = el; | |
99 | s->nelements++; | |
100 | ||
101 | /*@i32@*/ return s; | |
102 | } | |
103 | ||
104 | mtLoseReferenceList mtLoseReferenceList_prepend (mtLoseReferenceList s, /*@keep@*/ mtLoseReference el) | |
105 | { | |
106 | int i; | |
107 | ||
108 | if (!mtLoseReferenceList_isDefined (s)) | |
109 | { | |
110 | /*@i32@*/ return mtLoseReferenceList_single (el); | |
111 | } | |
112 | ||
113 | if (s->nspace <= 0) | |
114 | { | |
115 | mtLoseReferenceList_grow (s); | |
116 | } | |
117 | ||
118 | s->nspace--; | |
119 | ||
120 | for (i = s->nelements; i > 0; i--) | |
121 | { | |
122 | s->elements[i] = s->elements [i - 1]; | |
123 | } | |
124 | ||
125 | /*@i32@*/ s->elements[0] = el; | |
126 | s->nelements++; | |
127 | ||
128 | /*@i32@*/ return s; | |
129 | } | |
130 | ||
131 | cstring | |
132 | mtLoseReferenceList_unparse (mtLoseReferenceList s) | |
133 | { | |
134 | return mtLoseReferenceList_unparseSep (s, cstring_makeLiteralTemp (" ")); | |
135 | } | |
136 | ||
137 | cstring | |
138 | mtLoseReferenceList_unparseSep (mtLoseReferenceList s, cstring sep) | |
139 | { | |
140 | cstring st = cstring_undefined; | |
141 | ||
142 | if (mtLoseReferenceList_isDefined (s)) | |
143 | { | |
144 | int i; | |
145 | ||
146 | for (i = 0; i < s->nelements; i++) | |
147 | { | |
148 | if (i == 0) | |
149 | { | |
150 | st = mtLoseReference_unparse (s->elements[i]); | |
151 | } | |
152 | else | |
153 | st = message ("%q%s%q", st, sep, mtLoseReference_unparse (s->elements[i])); | |
154 | } | |
155 | } | |
156 | ||
157 | return st; | |
158 | } | |
159 | ||
160 | void | |
161 | mtLoseReferenceList_free (mtLoseReferenceList s) | |
162 | { | |
163 | if (mtLoseReferenceList_isDefined (s)) | |
164 | { | |
165 | int i; | |
166 | ||
167 | for (i = 0; i < s->nelements; i++) { | |
168 | mtLoseReference_free (s->elements[i]); | |
169 | } | |
170 | ||
171 | sfree (s->elements); | |
172 | sfree (s); | |
173 | } | |
174 | } | |
175 |