mem1.c revision 1.55 1 /* $NetBSD: mem1.c,v 1.55 2021/11/16 21:01:05 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
18 * The NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: mem1.c,v 1.55 2021/11/16 21:01:05 rillig Exp $");
41 #endif
42
43 #include <sys/param.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "lint1.h"
48
49 /*
50 * Filenames allocated by record_filename are shared and have unlimited
51 * lifetime.
52 */
53 struct filename {
54 const char *fn_name;
55 size_t fn_len;
56 int fn_id;
57 struct filename *fn_next;
58 };
59
60 static struct filename *filenames; /* null-terminated array */
61
62 /* Find the given filename, or return NULL. */
63 static const struct filename *
64 search_filename(const char *s, size_t len)
65 {
66 const struct filename *fn;
67
68 for (fn = filenames; fn != NULL; fn = fn->fn_next) {
69 if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0)
70 break;
71 }
72 return fn;
73 }
74
75 struct filename_replacement {
76 const char *orig;
77 size_t orig_len;
78 const char *repl;
79 const struct filename_replacement *next;
80 };
81
82 static struct filename_replacement *filename_replacements;
83
84 void
85 add_directory_replacement(char *arg)
86 {
87 struct filename_replacement *r = xmalloc(sizeof(*r));
88
89 char *sep = strchr(arg, '=');
90 if (sep == NULL)
91 err(1, "Bad replacement directory spec `%s'", arg);
92 *sep = '\0';
93
94 r->orig = arg;
95 r->orig_len = sep - arg;
96 r->repl = sep + 1;
97 r->next = filename_replacements;
98 filename_replacements = r;
99 }
100
101 const char *
102 transform_filename(const char *name, size_t len)
103 {
104 static char buf[MAXPATHLEN];
105 const struct filename_replacement *r;
106
107 for (r = filename_replacements; r != NULL; r = r->next)
108 if (r->orig_len < len &&
109 memcmp(name, r->orig, r->orig_len) == 0)
110 break;
111 if (r == NULL)
112 return name;
113 (void)snprintf(buf, sizeof(buf), "%s%s", r->repl, name + r->orig_len);
114 return buf;
115 }
116
117 static int
118 next_filename_id(void)
119 {
120 static int next_id = 0;
121
122 return next_id++;
123 }
124
125 /*
126 * Return a copy of the filename s with unlimited lifetime.
127 * If the filename is new, write it to the output file.
128 */
129 const char *
130 record_filename(const char *s, size_t slen)
131 {
132 const struct filename *existing_fn;
133 struct filename *fn;
134 char *name;
135
136 if (s == NULL)
137 return NULL;
138
139 if ((existing_fn = search_filename(s, slen)) != NULL)
140 return existing_fn->fn_name;
141
142 /* Do not use strdup() because s is not NUL-terminated.*/
143 name = xmalloc(slen + 1);
144 (void)memcpy(name, s, slen);
145 name[slen] = '\0';
146
147 fn = xmalloc(sizeof(*fn));
148 fn->fn_name = name;
149 fn->fn_len = slen;
150 fn->fn_id = next_filename_id();
151 fn->fn_next = filenames;
152 filenames = fn;
153
154 /* Write the ID of this filename to the output file. */
155 outclr();
156 outint(fn->fn_id);
157 outchar('s');
158 outstrg(transform_filename(fn->fn_name, fn->fn_len));
159
160 return fn->fn_name;
161 }
162
163 /* Get the ID of a filename. */
164 int
165 get_filename_id(const char *s)
166 {
167 const struct filename *fn;
168
169 if (s == NULL || (fn = search_filename(s, strlen(s))) == NULL)
170 return -1;
171 return fn->fn_id;
172 }
173
174 /*
175 * Memory for declarations and other things which must be available
176 * until the end of a block (or the end of the translation unit)
177 * is associated with the corresponding mem_block_level, which may be 0.
178 * Because this memory is allocated in large blocks associated with
179 * a given level it can be freed easily at the end of a block.
180 */
181 #define ML_INC ((size_t)32) /* Increment for length of *mblks */
182
183 typedef struct memory_block {
184 void *start; /* beginning of memory block */
185 void *first_free; /* first free byte */
186 size_t nfree; /* # of free bytes */
187 size_t size; /* total size of memory block */
188 struct memory_block *next;
189 } memory_block;
190
191 /*
192 * Array of pointers to lists of memory blocks. mem_block_level is used as
193 * index into this array.
194 */
195 static memory_block **mblks;
196
197 /* number of elements in *mblks */
198 static size_t nmblks;
199
200 /* free list for memory blocks */
201 static memory_block *frmblks;
202
203 /* length of new allocated memory blocks */
204 static size_t mblklen;
205
206
207 static memory_block *
208 xnewblk(void)
209 {
210 memory_block *mb = xmalloc(sizeof(*mb));
211
212 mb->start = xmalloc(mblklen);
213 mb->size = mblklen;
214
215 return mb;
216 }
217
218 /* Allocate new memory, initialized with zero. */
219 static void *
220 xgetblk(memory_block **mbp, size_t s)
221 {
222 memory_block *mb;
223 void *p;
224 size_t t = 0;
225
226 /*
227 * If the first block of the list has not enough free space,
228 * or there is no first block, get a new block. The new block
229 * is taken from the free list or, if there is no block on the
230 * free list, is allocated using xnewblk().
231 *
232 * If a new block is allocated it is initialized with zero.
233 * Blocks taken from the free list are zero'd in xfreeblk().
234 */
235
236 s = WORST_ALIGN(s);
237 if ((mb = *mbp) == NULL || mb->nfree < s) {
238 if ((mb = frmblks) == NULL || mb->size < s) {
239 if (s > mblklen) {
240 t = mblklen;
241 mblklen = s;
242 }
243 mb = xnewblk();
244 #ifndef BLKDEBUG
245 (void)memset(mb->start, 0, mb->size);
246 #endif
247 if (t > 0)
248 mblklen = t;
249 } else {
250 frmblks = mb->next;
251 }
252 mb->first_free = mb->start;
253 mb->nfree = mb->size;
254 mb->next = *mbp;
255 *mbp = mb;
256 }
257 p = mb->first_free;
258 mb->first_free = (char *)mb->first_free + s;
259 mb->nfree -= s;
260 #ifdef BLKDEBUG
261 (void)memset(p, 0, s);
262 #endif
263 return p;
264 }
265
266 /*
267 * Move all blocks from list *fmbp to free list. For each block, set all
268 * used memory to zero.
269 */
270 static void
271 xfreeblk(memory_block **fmbp)
272 {
273 memory_block *mb;
274
275 while ((mb = *fmbp) != NULL) {
276 *fmbp = mb->next;
277 mb->next = frmblks;
278 frmblks = mb;
279 (void)memset(mb->start, ZERO, mb->size - mb->nfree);
280 }
281 }
282
283 void
284 initmem(void)
285 {
286
287 mblklen = mem_block_size();
288 mblks = xcalloc(nmblks = ML_INC, sizeof(*mblks));
289 }
290
291
292 /* Allocate memory associated with level l, initialized with zero. */
293 void *
294 getlblk(size_t l, size_t s)
295 {
296
297 while (l >= nmblks) {
298 mblks = xrealloc(mblks, (nmblks + ML_INC) * sizeof(*mblks));
299 (void)memset(&mblks[nmblks], 0, ML_INC * sizeof(*mblks));
300 nmblks += ML_INC;
301 }
302 return xgetblk(&mblks[l], s);
303 }
304
305 /*
306 * Return allocated memory for the current mem_block_level, initialized with
307 * zero.
308 */
309 void *
310 getblk(size_t s)
311 {
312
313 return getlblk(mem_block_level, s);
314 }
315
316 /* Free all memory associated with level l. */
317 void
318 freelblk(int l)
319 {
320
321 xfreeblk(&mblks[l]);
322 }
323
324 void
325 freeblk(void)
326 {
327
328 freelblk(mem_block_level);
329 }
330
331 static memory_block *tmblk;
332
333 /*
334 * Return zero-initialized memory that is freed at the end of the current
335 * expression.
336 */
337 void *
338 expr_zalloc(size_t s)
339 {
340
341 return xgetblk(&tmblk, s);
342 }
343
344 static bool
345 str_endswith(const char *haystack, const char *needle)
346 {
347 size_t hlen = strlen(haystack);
348 size_t nlen = strlen(needle);
349
350 return nlen <= hlen &&
351 memcmp(haystack + hlen - nlen, needle, nlen) == 0;
352 }
353
354 /*
355 * Return a freshly allocated tree node that is freed at the end of the
356 * current expression.
357 */
358 tnode_t *
359 expr_zalloc_tnode(void)
360 {
361 tnode_t *tn = expr_zalloc(sizeof(*tn));
362 /*
363 * files named *.c that are different from the main translation unit
364 * typically contain generated code that cannot be influenced, such
365 * as a flex lexer or a yacc parser.
366 */
367 tn->tn_sys = in_system_header ||
368 (curr_pos.p_file != csrc_pos.p_file &&
369 str_endswith(curr_pos.p_file, ".c"));
370 return tn;
371 }
372
373 /* Free all memory which is allocated by the current expression. */
374 void
375 expr_free_all(void)
376 {
377
378 xfreeblk(&tmblk);
379 }
380
381 /*
382 * Save the memory which is used by the current expression. This memory
383 * is not freed by the next expr_free_all() call. The pointer returned can be
384 * used to restore the memory.
385 */
386 memory_block *
387 expr_save_memory(void)
388 {
389 memory_block *tmem;
390
391 tmem = tmblk;
392 tmblk = NULL;
393 return tmem;
394 }
395
396 /*
397 * Free all memory used for the current expression and restore the memory used
398 * by a previous expression and saved by expr_save_memory(). The next call to
399 * expr_free_all() frees the restored memory.
400 */
401 void
402 expr_restore_memory(memory_block *tmem)
403 {
404
405 expr_free_all();
406 if (tmblk != NULL) {
407 free(tmblk->start);
408 free(tmblk);
409 }
410 tmblk = tmem;
411 }
412