Some clean-up of memory routines (+ some regression fixes).
[splint-patched.git] / src / sRefList.c
blobba277aad351024b974da85f77ccb04823677190f
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.
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
25 ** sRefList.c (from slist_template.c)
28 # include "splintMacros.nf"
29 # include "basic.h"
31 sRefList
32 sRefList_new (void)
34 return sRefList_undefined;
37 static /*@only@*/ /*@notnull@*/ sRefList
38 sRefList_newEmpty (void)
40 sRefList s = (sRefList) dmalloc (sizeof (*s));
42 s->nelements = 0;
43 s->nspace = sRefListBASESIZE;
44 s->elements = (sRef *) dmalloc (sizeof (*s->elements) * sRefListBASESIZE);
46 return (s);
49 /*@only@*/ sRefList sRefList_single (sRef el)
51 sRefList res = sRefList_newEmpty ();
52 res = sRefList_add (res, el);
53 return res;
56 static void
57 sRefList_grow (/*@notnull@*/ sRefList s)
59 int i;
60 sRef *oldelements = s->elements;
62 s->nspace += sRefListBASESIZE;
64 s->elements = (sRef *) dmalloc (sizeof (*s->elements) * (s->nelements + s->nspace));
66 for (i = 0; i < s->nelements; i++)
68 s->elements[i] = oldelements[i];
71 sfree (oldelements);
74 /*@notnull@*/ sRefList sRefList_add (sRefList s, sRef el)
76 if (sRefList_isUndefined (s))
78 s = sRefList_newEmpty ();
81 if (s->nspace <= 0)
82 sRefList_grow (s);
84 s->nspace--;
85 s->elements[s->nelements] = el;
86 s->nelements++;
88 return (s);
91 sRefList sRefList_copy (sRefList s)
93 sRefList t = sRefList_new ();
95 sRefList_elements (s, current)
97 t = sRefList_add (t, sRef_copy (current));
98 } end_sRefList_elements;
100 return t;
103 /*@only@*/ cstring
104 sRefList_unparse (sRefList s)
106 int i;
107 cstring st = cstring_undefined;
109 if (sRefList_isDefined (s))
111 for (i = 0; i < sRefList_size (s); i++)
113 if (i == 0)
115 st = message ("%q%q ", st, sRef_unparse (s->elements[i]));
117 else
118 st = message ("%q%q ", st, sRef_unparse (s->elements[i]));
122 return st;
125 int sRefList_size (sRefList s)
127 if (sRefList_isUndefined (s)) return 0;
128 return s->nelements;
131 void
132 sRefList_free (/*@only@*/ sRefList s)
134 if (sRefList_isDefined (s))
136 sfree (s->elements);
137 sfree (s);