mem1.c revision 1.33 1 /* $NetBSD: mem1.c,v 1.33 2021/03/27 12:10:41 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.33 2021/03/27 12:10:41 rillig Exp $");
41 #endif
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #include "lint1.h"
50
51 /*
52 * Filenames allocated by fnalloc() and fnnalloc() are shared.
53 */
54 struct filename {
55 char *fn_name;
56 size_t fn_len;
57 int fn_id;
58 struct filename *fn_next;
59 };
60
61 static struct filename *fnames;
62
63 /* Find the given filename, or return NULL. */
64 static const struct filename *
65 search_filename(const char *s, size_t len)
66 {
67 struct filename *fn;
68
69 for (fn = fnames; fn != NULL; fn = fn->fn_next) {
70 if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0)
71 break;
72 }
73 return fn;
74 }
75
76 struct filename_replacement {
77 char *orig;
78 char *repl;
79 size_t len;
80 struct filename_replacement *next;
81 };
82
83 static struct filename_replacement *replist;
84
85 void
86 add_directory_replacement(char *arg)
87 {
88 struct filename_replacement *r = xmalloc(sizeof *r);
89
90 r->orig = arg;
91 if ((r->repl = strchr(arg, '=')) == NULL)
92 err(1, "Bad replacement directory spec `%s'", arg);
93 r->len = r->repl - r->orig;
94 *(r->repl)++ = '\0';
95 if (replist == NULL) {
96 r->next = NULL;
97 } else
98 r->next = replist;
99 replist = r;
100 }
101
102 const char *
103 fnxform(const char *name, size_t len)
104 {
105 static char buf[MAXPATHLEN];
106 struct filename_replacement *r;
107
108 for (r = replist; r != NULL; r = r->next)
109 if (r->len < len && memcmp(name, r->orig, r->len) == 0)
110 break;
111 if (r == NULL)
112 return name;
113 snprintf(buf, sizeof buf, "%s%s", r->repl, name + r->len);
114 return buf;
115 }
116
117 /*
118 * Return a copy of the filename s with unlimited lifetime.
119 * If the filename is new, it is written to the output file.
120 */
121 const char *
122 fnnalloc(const char *s, size_t len)
123 {
124 const struct filename *existing_fn;
125 struct filename *fn;
126
127 static int nxt_id = 0;
128
129 if (s == NULL)
130 return NULL;
131
132 if ((existing_fn = search_filename(s, len)) != NULL)
133 return existing_fn->fn_name;
134
135 fn = xmalloc(sizeof *fn);
136 /* Do not use strdup() because s is not NUL-terminated.*/
137 fn->fn_name = xmalloc(len + 1);
138 (void)memcpy(fn->fn_name, s, len);
139 fn->fn_name[len] = '\0';
140 fn->fn_len = len;
141 fn->fn_id = nxt_id++;
142 fn->fn_next = fnames;
143 fnames = fn;
144
145 /* Write id of this filename to the output file. */
146 outclr();
147 outint(fn->fn_id);
148 outchar('s');
149 outstrg(fnxform(fn->fn_name, fn->fn_len));
150
151 return fn->fn_name;
152 }
153
154 /* Get the ID of a filename. */
155 int
156 getfnid(const char *s)
157 {
158 const struct filename *fn;
159
160 if (s == NULL || (fn = search_filename(s, strlen(s))) == NULL)
161 return -1;
162 return fn->fn_id;
163 }
164
165 /*
166 * Memory for declarations and other things which must be available
167 * until the end of a block (or the end of the translation unit)
168 * are associated with the level (mem_block_level) of the block (or with 0).
169 * Because this memory is allocated in large blocks associated with
170 * a given level it can be freed easily at the end of a block.
171 */
172 #define ML_INC ((size_t)32) /* Increment for length of *mblks */
173
174 typedef struct mbl {
175 void *blk; /* beginning of memory block */
176 void *ffree; /* first free byte */
177 size_t nfree; /* # of free bytes */
178 size_t size; /* total size of memory block */
179 struct mbl *nxt; /* next block */
180 } mbl_t;
181
182 /*
183 * Array of pointers to lists of memory blocks. mem_block_level is used as
184 * index into this array.
185 */
186 static mbl_t **mblks;
187
188 /* number of elements in *mblks */
189 static size_t nmblks;
190
191 /* free list for memory blocks */
192 static mbl_t *frmblks;
193
194 /* length of new allocated memory blocks */
195 static size_t mblklen;
196
197 static void *xgetblk(mbl_t **, size_t);
198 static void xfreeblk(mbl_t **);
199 static mbl_t *xnewblk(void);
200
201 static mbl_t *
202 xnewblk(void)
203 {
204 mbl_t *mb = xmalloc(sizeof *mb);
205
206 /* use mmap instead of malloc to avoid malloc's size overhead */
207 mb->blk = xmapalloc(mblklen);
208 mb->size = mblklen;
209
210 return mb;
211 }
212
213 /* Allocate new memory, initialized with zero. */
214 static void *
215 xgetblk(mbl_t **mbp, size_t s)
216 {
217 mbl_t *mb;
218 void *p;
219 size_t t = 0;
220
221 /*
222 * If the first block of the list has not enough free space,
223 * or there is no first block, get a new block. The new block
224 * is taken from the free list or, if there is no block on the
225 * free list, is allocated using xnewblk().
226 *
227 * If a new block is allocated it is initialized with zero.
228 * Blocks taken from the free list are zero'd in xfreeblk().
229 */
230
231 s = WORST_ALIGN(s);
232 if ((mb = *mbp) == NULL || mb->nfree < s) {
233 if ((mb = frmblks) == NULL || mb->size < s) {
234 if (s > mblklen) {
235 t = mblklen;
236 mblklen = s;
237 }
238 mb = xnewblk();
239 #ifndef BLKDEBUG
240 (void)memset(mb->blk, 0, mb->size);
241 #endif
242 if (t > 0)
243 mblklen = t;
244 } else {
245 frmblks = mb->nxt;
246 }
247 mb->ffree = mb->blk;
248 mb->nfree = mb->size;
249 mb->nxt = *mbp;
250 *mbp = mb;
251 }
252 p = mb->ffree;
253 mb->ffree = (char *)mb->ffree + s;
254 mb->nfree -= s;
255 #ifdef BLKDEBUG
256 (void)memset(p, 0, s);
257 #endif
258 return p;
259 }
260
261 /*
262 * Move all blocks from list *fmbp to free list. For each block, set all
263 * used memory to zero.
264 */
265 static void
266 xfreeblk(mbl_t **fmbp)
267 {
268 mbl_t *mb;
269
270 while ((mb = *fmbp) != NULL) {
271 *fmbp = mb->nxt;
272 mb->nxt = frmblks;
273 frmblks = mb;
274 (void)memset(mb->blk, ZERO, mb->size - mb->nfree);
275 }
276 }
277
278 void
279 initmem(void)
280 {
281 int pgsz;
282
283 pgsz = getpagesize();
284 mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
285
286 mblks = xcalloc(nmblks = ML_INC, sizeof *mblks);
287 }
288
289
290 /* Allocate memory associated with level l. */
291 void *
292 getlblk(size_t l, size_t s)
293 {
294
295 while (l >= nmblks) {
296 mblks = xrealloc(mblks, (nmblks + ML_INC) * sizeof *mblks);
297 (void)memset(&mblks[nmblks], 0, ML_INC * sizeof *mblks);
298 nmblks += ML_INC;
299 }
300 return xgetblk(&mblks[l], s);
301 }
302
303 void *
304 getblk(size_t s)
305 {
306
307 return getlblk(mem_block_level, s);
308 }
309
310 /* Free all memory associated with level l. */
311 void
312 freelblk(int l)
313 {
314
315 xfreeblk(&mblks[l]);
316 }
317
318 void
319 freeblk(void)
320 {
321
322 freelblk(mem_block_level);
323 }
324
325 static mbl_t *tmblk;
326
327 /*
328 * Return zero-initialized memory that is freed at the end of the current
329 * expression.
330 */
331 void *
332 tgetblk(size_t s)
333 {
334
335 return xgetblk(&tmblk, s);
336 }
337
338 /* Return a freshly allocated tree node. */
339 tnode_t *
340 getnode(void)
341 {
342 tnode_t *tn = tgetblk(sizeof *tn);
343 tn->tn_from_system_header = in_system_header;
344 return tn;
345 }
346
347 /* Free all memory which is allocated by the current expression. */
348 void
349 tfreeblk(void)
350 {
351
352 xfreeblk(&tmblk);
353 }
354
355 /*
356 * Save the memory which is used by the current expression. This memory
357 * is not freed by the next tfreeblk() call. The pointer returned can be
358 * used to restore the memory.
359 */
360 mbl_t *
361 tsave(void)
362 {
363 mbl_t *tmem;
364
365 tmem = tmblk;
366 tmblk = NULL;
367 return tmem;
368 }
369
370 /*
371 * Free all memory used for the current expression and the memory used
372 * be a previous expression and saved by tsave(). The next call to
373 * tfreeblk() frees the restored memory.
374 */
375 void
376 trestor(mbl_t *tmem)
377 {
378
379 tfreeblk();
380 if (tmblk != NULL) {
381 free(tmblk->blk);
382 free(tmblk);
383 }
384 tmblk = tmem;
385 }
386