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