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