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: splint@cs.virginia.edu
21 ** To report a bug: splint-bug@cs.virginia.edu
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 llbug (message ("Zero allocation at %q.",
117 fileloc_unparseRaw (cstring_fromChars (name), line)));
122 fix this so message doesn't run out of
125 llbuglit("Out of memory");
128 (message ("Out of memory. Allocating %w bytes at %s:%d.",
129 size_toLongUnsigned (size),
130 cstring_fromChars (name), line));
135 /*@-null@*/ /* null okay for size = 0 */
136 /* fprintf (stderr, "%s:%d: Allocating: [%p / %d]\n", name, line, ret, size); */
141 void *dicalloc (size_t num, size_t size, const char *name, int line)
143 void *ret = (void *) calloc (num, size);
148 (message ("Out of memory. Allocating %w bytes at %s:%d.",
149 size_toLongUnsigned (size),
150 cstring_fromChars (name), line));
156 void *direalloc (/*@out@*/ /*@null@*/ void *x, size_t size,
157 char *name, int line)
163 ret = (void *) dmalloc (size);
167 ret = (void *) realloc (x, size);
173 (message ("Out of memory. Allocating %w bytes at %s:%d.",
174 size_toLongUnsigned (size),
175 cstring_fromChars (name), line));
184 char *FormatInt (int i)
186 char temp[255]; /* assume the integer has at most 254 digits */
189 sprintf (temp, "%i", i);
190 outs = (char *) dmalloc (sizeof (*outs) * (1 + strlen (temp)));
198 bool firstWord (char *s, char *w)
200 llassert (s != NULL);
201 llassert (w != NULL);
203 for (; *w != '\0'; w++, s++)
205 if (*w != *s || *s == '\0')
212 void mstring_markFree (char *s)
217 char *mstring_spaces (int n)
225 ret = (char *) dmalloc (size_fromInt (n + 1));
228 for (i = 0; i < n; i++)
238 bool mstring_containsChar (const char *s, char c)
240 if (mstring_isDefined (s))
242 return (strchr (s, c) != NULL);
250 bool mstring_containsString (const char *s, const char *c)
252 if (mstring_isDefined (s))
254 return (strstr (s, c) != NULL);
262 char *mstring_concat (const char *s1, const char *s2)
264 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
270 extern /*@only@*/ char *
271 mstring_concatFree (/*@only@*/ char *s1, /*@only@*/ char *s2)
273 /* like mstring_concat but deallocates old strings */
274 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
283 extern /*@only@*/ char *
284 mstring_concatFree1 (/*@only@*/ char *s1, const char *s2)
286 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
294 extern /*@only@*/ char *
295 mstring_append (/*@only@*/ char *s1, char c)
297 size_t l = strlen (s1);
300 s = (char *) dmalloc (sizeof (*s) * (l + 2));
310 char *mstring_copy (char *s1) /*@ensures maxRead(result) == maxRead(s1) /\ maxSet(result) == maxSet(s1) @*/
318 char *s = (char *) dmalloc ((strlen (s1) + 1) * sizeof (*s));
325 char *mstring_safePrint (char *s)
329 return ("<undefined>");
338 char *mstring_create (int n)
342 s = dmalloc (sizeof (*s) * (n + 1));
348 fputline (FILE *out, char *s)
352 check (fputs (s, out) != EOF);
355 check (fputc ('\n', out) == (int) '\n');
358 unsigned int int_toNonNegative (int x) /*@*/
379 longUnsigned_fromInt (int x)
383 return (long unsigned) x;
386 size_t size_fromInt (int x) /*@ensures result==x@*/
388 size_t res = (size_t) x;
390 llassert ((int) res == x);
394 int size_toInt (size_t x)
398 llassert ((size_t) res == x);
402 long size_toLong (size_t x)
406 llassert ((size_t) res == x);
416 ** evans 2001-09-28 - changed assertion in response to Anthony Giorgio's comment
417 ** that the old assertions failed for EBCDIC character set. Now we just check
418 ** that the result is equal.
422 llassert ((int) res == x);
428 longUnsigned_toInt (long unsigned int x)
432 llassert ((long unsigned) res == x);
437 long_toInt (long int x)
441 /*@+ignorequals@*/ llassert (res == x); /*@=ignorequals@*/
447 bool mstring_equalPrefix (const char *c1, const char *c2)
449 llassert (c1 != NULL);
450 llassert (c2 != NULL);
452 if (strncmp(c1, c2, strlen(c2)) == 0)
462 bool mstring_equal (/*@null@*/ const char *s1, /*@null@*/ const char *s2)
476 return (strcmp(s1, s2) == 0);