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