]> andersk Git - splint.git/blame - src/general.c
changed PLUS to BINARYOP_PLUS
[splint.git] / src / general.c
CommitLineData
616915dd 1/*
2** LCLint - annotation-assisted static program checker
28bf4b0b 3** Copyright (C) 1994-2001 University of Virginia,
616915dd 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.
19**
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
23*/
24/*
25** general.c
26*/
27
28# include "lclintMacros.nf"
29# include "basic.h"
30# undef malloc
31# undef realloc
32# undef calloc
33
34# ifdef USEDMALLOC
35# include "dmalloc.h"
36# endif
37
38# include "portab.h"
39
40/*
41** redefine undef'd memory ops
42*/
43
44# ifndef USEDMALLOC
616915dd 45
46/*@-mustdefine@*/
47
48void sfree (void *x)
49{
50 if (x != NULL)
51 {
28bf4b0b 52 /* fprintf (stderr, "Freeing: %p\n", x); */
53
616915dd 54 /*
28bf4b0b 55 if ((unsigned long) x > 0xbf000000) {
616915dd 56 fprintf (stderr, "Looks bad!\n");
57 }
58 */
28bf4b0b 59
616915dd 60 free (x);
28bf4b0b 61
616915dd 62 /* fprintf (stderr, "Done.\n"); */
63 }
64}
65# endif
66
67void sfreeEventually (void *x)
68{
69 if (x != NULL)
70 {
71 ; /* should keep in a table */
72 }
73/*@-mustfree@*/
74} /*@=mustfree@*/
75
76/*
77** all memory should be allocated from dimalloc
78*/
79
80static long unsigned size_toLongUnsigned (size_t x)
81{
82 long unsigned res = (long unsigned) x;
83
84 llassert ((size_t) res == x);
85 return res;
86}
87
28bf4b0b 88/*@out@*/ void *dimalloc (size_t size, const char *name, int line)
616915dd 89{
90 /*
91 static void *lastaddr = 0;
92 static int numallocs = 0;
93 static int numbad = 0;
94 */
95
96 /* was malloc, use calloc to initialize to zero */
97 void *ret = (void *) calloc (1, size);
98
99 /*
100 numallocs++;
101
102 if (ret < lastaddr)
103 {
104 numbad++;
105 fprintf (stderr, "Bad alloc: %d / %d\n", numbad, numallocs);
106 }
107
108 lastaddr = ret;
109 */
110
111 if (ret == NULL)
112 {
113 if (size == 0)
114 {
115 llbug (message ("Zero allocation at %q.",
116 fileloc_unparseRaw (cstring_fromChars (name), line)));
117 }
118 else
119 {
a779b61e 120 /* drl
121 fix this so message doesn't run out of
122 memory*/
123
124 llbuglit("Out of memory");
125
616915dd 126 llfatalerrorLoc
127 (message ("Out of memory. Allocating %w bytes at %s:%d.",
128 size_toLongUnsigned (size),
129 cstring_fromChars (name), line));
a779b61e 130
616915dd 131 }
132 }
133
134 /*@-null@*/ /* null okay for size = 0 */
28bf4b0b 135 /* fprintf (stderr, "%s:%d: Allocating: [%p / %d]\n", name, line, ret, size); */
616915dd 136 return ret;
137 /*@=null@*/
138}
139
28bf4b0b 140void *dicalloc (size_t num, size_t size, const char *name, int line)
616915dd 141{
142 void *ret = (void *) calloc (num, size);
143
144 if (ret == NULL)
145 {
146 llfatalerrorLoc
147 (message ("Out of memory. Allocating %w bytes at %s:%d.",
148 size_toLongUnsigned (size),
149 cstring_fromChars (name), line));
150 }
151
152 return ret;
153}
154
155void *direalloc (/*@out@*/ /*@null@*/ void *x, size_t size,
156 char *name, int line)
157{
158 void *ret;
159
160 if (x == NULL)
161 {
162 ret = (void *) dmalloc (size);
163 }
164 else
165 {
166 ret = (void *) realloc (x, size);
167 }
168
169 if (ret == NULL)
170 {
171 llfatalerrorLoc
172 (message ("Out of memory. Allocating %w bytes at %s:%d.",
173 size_toLongUnsigned (size),
174 cstring_fromChars (name), line));
175 }
176
177 return ret;
178}
179
180/*@=mustdefine@*/
181
182# ifndef NOLCL
183char *FormatInt (int i)
184{
185 char temp[255]; /* assume the integer has at most 254 digits */
186 char *outs;
187
188 sprintf (temp, "%i", i);
189 outs = (char *) dmalloc (sizeof (*outs) * (1 + strlen (temp)));
190 strcpy (outs, temp);
191
192 return (outs);
193}
194# endif
195
616915dd 196# ifndef NOLCL
197bool firstWord (char *s, char *w)
198{
199 llassert (s != NULL);
200 llassert (w != NULL);
201
202 for (; *w != '\0'; w++, s++)
203 {
204 if (*w != *s || *s == '\0')
205 return FALSE;
206 }
207 return TRUE;
208}
209# endif
210
616915dd 211void mstring_markFree (char *s)
212{
15b3d2b2 213 sfreeEventually (s);
616915dd 214}
215
616915dd 216char *mstring_spaces (int n)
217{
218 int i;
219 char *ret;
220 char *ptr;
221
222 llassert (n >= 0);
223
224 ret = (char *) dmalloc (size_fromInt (n + 1));
225 ptr = ret;
226
227 for (i = 0; i < n; i++)
228 {
229 *ptr++ = ' ';
230 }
231
232 *ptr = '\0';
233
234 return ret;
235}
28bf4b0b 236
237bool mstring_containsChar (const char *s, char c)
238{
239 if (mstring_isDefined (s))
240 {
241 return (strchr (s, c) != NULL);
242 }
243 else
244 {
245 return FALSE;
246 }
247}
616915dd 248
249char *mstring_concat (const char *s1, const char *s2)
250{
251 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
252 strcpy (s, s1);
253 strcat (s, s2);
254 return s;
255}
256
257extern /*@only@*/ char *
258mstring_concatFree (/*@only@*/ char *s1, /*@only@*/ char *s2)
259{
260 /* like mstring_concat but deallocates old strings */
261 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
262 strcpy (s, s1);
263 strcat (s, s2);
264
265 sfree (s1);
266 sfree (s2);
267 return s;
268}
269
270extern /*@only@*/ char *
271mstring_concatFree1 (/*@only@*/ char *s1, const char *s2)
272{
273 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
274 strcpy (s, s1);
275 strcat (s, s2);
276 sfree (s1);
277
278 return s;
279}
280
281extern /*@only@*/ char *
282mstring_append (/*@only@*/ char *s1, char c)
283{
284 size_t l = strlen (s1);
285 char *s;
286
287 s = (char *) dmalloc (sizeof (*s) * (l + 2));
288
289 strcpy (s, s1);
290 *(s + l) = c;
291 *(s + l + 1) = '\0';
292 sfree (s1);
293 return s;
294}
295
296extern
297char *mstring_copy (char *s1)
298{
299 if (s1 == NULL)
300 {
301 return NULL;
302 }
303 else
304 {
305 char *s = (char *) dmalloc ((strlen (s1) + 1) * sizeof (*s));
306 strcpy (s, s1);
307 return s;
308 }
309}
310
311extern
312char *mstring_safePrint (char *s)
313{
314 if (s == NULL)
315 {
316 return ("<undefined>");
317 }
318 else
319 {
320 return s;
321 }
322}
323
324extern
325char *mstring_create (int n)
326{
327 char *s;
328
329 s = dmalloc (sizeof (*s) * (n + 1));
330 *s = '\0';
331 return s;
332}
333
334void
335fputline (FILE *out, char *s)
336{
337 if (strlen (s) > 0)
338 {
339 check (fputs (s, out) != EOF);
340 }
341
342 check (fputc ('\n', out) == (int) '\n');
343}
344
345int int_log (int x)
346{
347 int ret = 1;
348
349 while (x > 10)
350 {
351 ret++;
352 x /= 10;
353 }
354
355 return ret;
356}
357
358/*@-czechfcns@*/
359long unsigned int
360longUnsigned_fromInt (int x)
361{
362 llassert (x >= 0);
363
364 return (long unsigned) x;
365}
366
367size_t size_fromInt (int x)
368{
369 size_t res = (size_t) x;
370
371 llassert ((int) res == x);
372 return res;
373}
374
375int size_toInt (size_t x)
376{
377 int res = (int) x;
378
379 llassert ((size_t) res == x);
380 return res;
381}
382
383long size_toLong (size_t x)
384{
385 long res = (long) x;
386
387 llassert ((size_t) res == x);
388 return res;
389}
390
391/*@=czechfcns@*/
392
393char
394char_fromInt (int x)
395{
396 llassert ((x >= (int)'\0') && (x <= (int)'~'));
397
398 return ((char) x);
399}
400
401/*@-czechfcns@*/
402int
403longUnsigned_toInt (long unsigned int x)
404{
405 int res = (int) x;
406
407 llassert ((long unsigned) res == x);
408 return res;
409}
410
411int
412long_toInt (long int x)
413{
414 int res = (int) x;
415
416 /*@+ignorequals@*/ llassert (res == x); /*@=ignorequals@*/
417 return res;
418}
419
420/*@+czechfcns@*/
421
422bool mstring_equalPrefix (const char *c1, const char *c2)
423{
424 llassert (c1 != NULL);
425 llassert (c2 != NULL);
426
427 if (strncmp(c1, c2, strlen(c2)) == 0)
428 {
429 return TRUE;
430 }
431 else
432 {
433 return FALSE;
434 }
435}
436
437bool mstring_equal (/*@null@*/ const char *s1, /*@null@*/ const char *s2)
438{
439 if (s1 == NULL)
440 {
441 return (s2 == NULL);
442 }
443 else
444 {
445 if (s2 == NULL)
446 {
447 return FALSE;
448 }
449 else
450 {
451 return (strcmp(s1, s2) == 0);
452 }
453 }
454}
455
This page took 0.110264 seconds and 5 git commands to generate.