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