mem1.c revision 100364
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 $");
39
40#include <sys/types.h>
41#include <sys/mman.h>
42#include <sys/param.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}
365