2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 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 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
28 # include "lclintMacros.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)
91 static void *lastaddr = 0;
92 static int numallocs = 0;
93 static int numbad = 0;
96 /* was malloc, use calloc to initialize to zero */
97 void *ret = (void *) calloc (1, size);
105 fprintf (stderr, "Bad alloc: %d / %d\n", numbad, numallocs);
115 llbug (message ("Zero allocation at %q.",
116 fileloc_unparseRaw (cstring_fromChars (name), line)));
121 fix this so message doesn't run out of
124 llbuglit("Out of memory");
127 (message ("Out of memory. Allocating %w bytes at %s:%d.",
128 size_toLongUnsigned (size),
129 cstring_fromChars (name), line));
134 /*@-null@*/ /* null okay for size = 0 */
135 /* fprintf (stderr, "%s:%d: Allocating: [%p / %d]\n", name, line, ret, size); */
140 void *dicalloc (size_t num, size_t size, const char *name, int line)
142 void *ret = (void *) calloc (num, size);
147 (message ("Out of memory. Allocating %w bytes at %s:%d.",
148 size_toLongUnsigned (size),
149 cstring_fromChars (name), line));
155 void *direalloc (/*@out@*/ /*@null@*/ void *x, size_t size,
156 char *name, int line)
162 ret = (void *) dmalloc (size);
166 ret = (void *) realloc (x, size);
172 (message ("Out of memory. Allocating %w bytes at %s:%d.",
173 size_toLongUnsigned (size),
174 cstring_fromChars (name), line));
183 char *FormatInt (int i)
185 char temp[255]; /* assume the integer has at most 254 digits */
188 sprintf (temp, "%i", i);
189 outs = (char *) dmalloc (sizeof (*outs) * (1 + strlen (temp)));
197 bool firstWord (char *s, char *w)
199 llassert (s != NULL);
200 llassert (w != NULL);
202 for (; *w != '\0'; w++, s++)
204 if (*w != *s || *s == '\0')
211 void mstring_markFree (char *s)
216 char *mstring_spaces (int n)
224 ret = (char *) dmalloc (size_fromInt (n + 1));
227 for (i = 0; i < n; i++)
237 bool mstring_containsChar (const char *s, char c)
239 if (mstring_isDefined (s))
241 return (strchr (s, c) != NULL);
249 char *mstring_concat (const char *s1, const char *s2)
251 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
257 extern /*@only@*/ char *
258 mstring_concatFree (/*@only@*/ char *s1, /*@only@*/ char *s2)
260 /* like mstring_concat but deallocates old strings */
261 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
270 extern /*@only@*/ char *
271 mstring_concatFree1 (/*@only@*/ char *s1, const char *s2)
273 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
281 extern /*@only@*/ char *
282 mstring_append (/*@only@*/ char *s1, char c)
284 size_t l = strlen (s1);
287 s = (char *) dmalloc (sizeof (*s) * (l + 2));
297 char *mstring_copy (char *s1)
305 char *s = (char *) dmalloc ((strlen (s1) + 1) * sizeof (*s));
312 char *mstring_safePrint (char *s)
316 return ("<undefined>");
325 char *mstring_create (int n)
329 s = dmalloc (sizeof (*s) * (n + 1));
335 fputline (FILE *out, char *s)
339 check (fputs (s, out) != EOF);
342 check (fputc ('\n', out) == (int) '\n');
360 longUnsigned_fromInt (int x)
364 return (long unsigned) x;
367 size_t size_fromInt (int x)
369 size_t res = (size_t) x;
371 llassert ((int) res == x);
375 int size_toInt (size_t x)
379 llassert ((size_t) res == x);
383 long size_toLong (size_t x)
387 llassert ((size_t) res == x);
396 llassert ((x >= (int)'\0') && (x <= (int)'~'));
403 longUnsigned_toInt (long unsigned int x)
407 llassert ((long unsigned) res == x);
412 long_toInt (long int x)
416 /*@+ignorequals@*/ llassert (res == x); /*@=ignorequals@*/
422 bool mstring_equalPrefix (const char *c1, const char *c2)
424 llassert (c1 != NULL);
425 llassert (c2 != NULL);
427 if (strncmp(c1, c2, strlen(c2)) == 0)
437 bool mstring_equal (/*@null@*/ const char *s1, /*@null@*/ const char *s2)
451 return (strcmp(s1, s2) == 0);