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