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
53 /* fprintf (stderr, "Freeing: %p\n", x); */
56 if ((unsigned long) x > 0xbf000000) {
57 fprintf (stderr, "Looks bad!\n");
63 /* fprintf (stderr, "Done.\n"); */
68 void sfreeEventually (void *x)
72 ; /* should keep in a table */
78 ** all memory should be allocated from dimalloc
81 static long unsigned size_toLongUnsigned (size_t x)
83 long unsigned res = (long unsigned) x;
85 llassert ((size_t) res == x);
89 /*@out@*/ void *dimalloc (size_t size, const char *name, int line)
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 (message ("Out of memory. Allocating %w bytes at %s:%d.",
123 size_toLongUnsigned (size),
124 cstring_fromChars (name), line));
128 /*@-null@*/ /* null okay for size = 0 */
129 /* fprintf (stderr, "%s:%d: Allocating: [%p / %d]\n", name, line, ret, size); */
134 void *dicalloc (size_t num, size_t size, const char *name, int line)
136 void *ret = (void *) calloc (num, size);
141 (message ("Out of memory. Allocating %w bytes at %s:%d.",
142 size_toLongUnsigned (size),
143 cstring_fromChars (name), line));
149 void *direalloc (/*@out@*/ /*@null@*/ void *x, size_t size,
150 char *name, int line)
156 ret = (void *) dmalloc (size);
160 ret = (void *) realloc (x, size);
166 (message ("Out of memory. Allocating %w bytes at %s:%d.",
167 size_toLongUnsigned (size),
168 cstring_fromChars (name), line));
177 char *FormatInt (int i)
179 char temp[255]; /* assume the integer has at most 254 digits */
182 sprintf (temp, "%i", i);
183 outs = (char *) dmalloc (sizeof (*outs) * (1 + strlen (temp)));
191 bool firstWord (char *s, char *w)
193 llassert (s != NULL);
194 llassert (w != NULL);
196 for (; *w != '\0'; w++, s++)
198 if (*w != *s || *s == '\0')
205 void mstring_markFree (char *s)
210 char *mstring_spaces (int n)
218 ret = (char *) dmalloc (size_fromInt (n + 1));
221 for (i = 0; i < n; i++)
231 bool mstring_containsChar (const char *s, char c)
233 if (mstring_isDefined (s))
235 return (strchr (s, c) != NULL);
243 char *mstring_concat (const char *s1, const char *s2)
245 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
251 extern /*@only@*/ char *
252 mstring_concatFree (/*@only@*/ char *s1, /*@only@*/ char *s2)
254 /* like mstring_concat but deallocates old strings */
255 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
264 extern /*@only@*/ char *
265 mstring_concatFree1 (/*@only@*/ char *s1, const char *s2)
267 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
275 extern /*@only@*/ char *
276 mstring_append (/*@only@*/ char *s1, char c)
278 size_t l = strlen (s1);
281 s = (char *) dmalloc (sizeof (*s) * (l + 2));
291 char *mstring_copy (char *s1)
299 char *s = (char *) dmalloc ((strlen (s1) + 1) * sizeof (*s));
306 char *mstring_safePrint (char *s)
310 return ("<undefined>");
319 char *mstring_create (int n)
323 s = dmalloc (sizeof (*s) * (n + 1));
329 fputline (FILE *out, char *s)
333 check (fputs (s, out) != EOF);
336 check (fputc ('\n', out) == (int) '\n');
354 longUnsigned_fromInt (int x)
358 return (long unsigned) x;
361 size_t size_fromInt (int x)
363 size_t res = (size_t) x;
365 llassert ((int) res == x);
369 int size_toInt (size_t x)
373 llassert ((size_t) res == x);
377 long size_toLong (size_t x)
381 llassert ((size_t) res == x);
390 llassert ((x >= (int)'\0') && (x <= (int)'~'));
397 longUnsigned_toInt (long unsigned int x)
401 llassert ((long unsigned) res == x);
406 long_toInt (long int x)
410 /*@+ignorequals@*/ llassert (res == x); /*@=ignorequals@*/
416 bool mstring_equalPrefix (const char *c1, const char *c2)
418 llassert (c1 != NULL);
419 llassert (c2 != NULL);
421 if (strncmp(c1, c2, strlen(c2)) == 0)
431 bool mstring_equal (/*@null@*/ const char *s1, /*@null@*/ const char *s2)
445 return (strcmp(s1, s2) == 0);