2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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
28 # include "splintMacros.nf"
41 ** redefine undef'd memory ops
52 /* fprintf (stderr, "Freeing: %p\n", x); */
55 if ((unsigned long) x > 0xbf000000) {
56 fprintf (stderr, "Looks bad!\n");
62 /* fprintf (stderr, "Done.\n"); */
67 void sfreeEventually (void *x)
71 ; /* should keep in a table */
77 ** all memory should be allocated from dimalloc
80 static long unsigned size_toLongUnsigned (size_t x)
82 long unsigned res = (long unsigned) x;
84 llassert ((size_t) res == x);
88 /*@out@*/ void *dimalloc (size_t size, const char *name, int line)
89 /*@ensures maxSet(result) == (size - 1); @*/
92 static void *lastaddr = 0;
93 static int numallocs = 0;
94 static int numbad = 0;
97 /* was malloc, use calloc to initialize to zero */
98 void *ret = (void *) calloc (1, size);
106 fprintf (stderr, "Bad alloc: %d / %d\n", numbad, numallocs);
116 llcontbug (message ("Zero allocation at %q.",
117 fileloc_unparseRaw (cstring_fromChars (name), line)));
121 ** Return some allocated storage...hope we get lucky.
124 return dimalloc (16, name, line);
129 fix this so message doesn't run out of
132 llbuglit("Out of memory");
135 (message ("Out of memory. Allocating %w bytes at %s:%d.",
136 size_toLongUnsigned (size),
137 cstring_fromChars (name), line));
142 /*@-null@*/ /* null okay for size = 0 */
143 /* fprintf (stderr, "%s:%d: Allocating: [%p / %d]\n", name, line, ret, size); */
148 void *dicalloc (size_t num, size_t size, const char *name, int line)
150 void *ret = (void *) calloc (num, size);
155 (message ("Out of memory. Allocating %w bytes at %s:%d.",
156 size_toLongUnsigned (size),
157 cstring_fromChars (name), line));
163 void *direalloc (/*@out@*/ /*@null@*/ void *x, size_t size,
164 char *name, int line)
170 ret = (void *) dmalloc (size);
174 ret = (void *) realloc (x, size);
180 (message ("Out of memory. Allocating %w bytes at %s:%d.",
181 size_toLongUnsigned (size),
182 cstring_fromChars (name), line));
190 char *FormatInt (int i)
192 char temp[255]; /* assume the integer has at most 254 digits */
194 int sres = snprintf (temp, 255, "%i", i);
195 check (sres >= 0 && sres <= 255);
196 outs = (char *) dmalloc (sizeof (*outs) * (1 + strlen (temp)));
202 bool firstWord (char *s, char *w)
204 llassert (s != NULL);
205 llassert (w != NULL);
207 for (; *w != '\0'; w++, s++)
209 if (*w != *s || *s == '\0')
215 void mstring_markFree (char *s)
220 char *mstring_spaces (int n)
228 ret = (char *) dmalloc (size_fromInt (n + 1));
231 for (i = 0; i < n; i++)
241 bool mstring_containsChar (const char *s, char c)
243 if (mstring_isDefined (s))
245 return (strchr (s, c) != NULL);
253 bool mstring_containsString (const char *s, const char *c)
255 if (mstring_isDefined (s))
257 return (strstr (s, c) != NULL);
265 char *mstring_concat (const char *s1, const char *s2)
267 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
273 extern /*@only@*/ char *
274 mstring_concatFree (/*@only@*/ char *s1, /*@only@*/ char *s2)
276 /* like mstring_concat but deallocates old strings */
277 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
286 extern /*@only@*/ char *
287 mstring_concatFree1 (/*@only@*/ char *s1, const char *s2)
289 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
297 extern /*@only@*/ char *
298 mstring_append (/*@only@*/ char *s1, char c)
300 size_t l = strlen (s1);
303 s = (char *) dmalloc (sizeof (*s) * (l + 2));
313 char *mstring_copy (char *s1) /*@ensures maxRead(result) == maxRead(s1) /\ maxSet(result) == maxSet(s1) @*/
321 char *s = (char *) dmalloc ((strlen (s1) + 1) * sizeof (*s));
328 char *mstring_safePrint (char *s)
332 return ("<undefined>");
341 char *mstring_create (size_t n)
345 s = dmalloc (sizeof (*s) * (n + 1));
351 fputline (FILE *out, char *s)
355 check (fputs (s, out) != EOF);
358 check (fputc ('\n', out) == (int) '\n');
361 unsigned int int_toNonNegative (int x) /*@*/
382 longUnsigned_fromInt (int x)
386 return (long unsigned) x;
389 size_t size_fromInt (int x) /*@ensures result==x@*/
391 size_t res = (size_t) x;
393 llassert ((int) res == x);
397 int size_toInt (size_t x)
401 llassert ((size_t) res == x);
405 long size_toLong (size_t x)
409 llassert ((size_t) res == x);
419 ** evans 2001-09-28 - changed assertion in response to Anthony Giorgio's comment
420 ** that the old assertions failed for EBCDIC character set. Now we just check
421 ** that the result is equal.
425 llassert ((int) res == x);
431 longUnsigned_toInt (long unsigned int x)
435 llassert ((long unsigned) res == x);
440 long_toInt (long int x)
444 /*@+ignorequals@*/ llassert (res == x); /*@=ignorequals@*/
450 bool mstring_equalPrefix (const char *c1, const char *c2)
452 llassert (c1 != NULL);
453 llassert (c2 != NULL);
455 if (strncmp(c1, c2, strlen(c2)) == 0)
465 bool mstring_equal (/*@null@*/ const char *s1, /*@null@*/ const char *s2)
479 return (strcmp(s1, s2) == 0);