mem1.c revision 91592
1169695Skan/*	$NetBSD: mem1.c,v 1.6 2002/01/29 02:43:39 tv Exp $	*/
2169695Skan
3169695Skan/*
4169695Skan * Copyright (c) 1994, 1995 Jochen Pohl
5169695Skan * All Rights Reserved.
6169695Skan *
7169695Skan * Redistribution and use in source and binary forms, with or without
8169695Skan * modification, are permitted provided that the following conditions
9169695Skan * are met:
10169695Skan * 1. Redistributions of source code must retain the above copyright
11169695Skan *    notice, this list of conditions and the following disclaimer.
12169695Skan * 2. Redistributions in binary form must reproduce the above copyright
13169695Skan *    notice, this list of conditions and the following disclaimer in the
14169695Skan *    documentation and/or other materials provided with the distribution.
15169695Skan * 3. All advertising materials mentioning features or use of this software
16169695Skan *    must display the following acknowledgement:
17169695Skan *      This product includes software developed by Jochen Pohl for
18169695Skan *	The NetBSD Project.
19169695Skan * 4. The name of the author may not be used to endorse or promote products
20169695Skan *    derived from this software without specific prior written permission.
21169695Skan *
22169695Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23169695Skan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24169695Skan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25169695Skan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26169695Skan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27169695Skan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28169695Skan * 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.6 2002/01/29 02:43:39 tv Exp $");
37#endif
38__FBSDID("$FreeBSD: head/usr.bin/xlint/lint1/mem1.c 91592 2002-03-03 15:12:50Z 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
204	s = ALIGN(s);
205	if ((mb = *mbp) == NULL || mb->nfree < s) {
206		if ((mb = frmblks) == NULL) {
207			mb = xnewblk();
208			(void)memset(mb->blk, 0, mb->size);
209		} else {
210			frmblks = mb->nxt;
211		}
212		mb->ffree = mb->blk;
213		mb->nfree = mb->size;;
214		mb->nxt = *mbp;
215		*mbp = mb;
216	}
217	p = mb->ffree;
218	mb->ffree = (char *)mb->ffree + s;
219	mb->nfree -= s;
220	return (p);
221}
222
223/*
224 * Move all blocks from list *fmbp to free list. For each block, set all
225 * used memory to zero.
226 */
227static void
228xfreeblk(mbl_t **fmbp)
229{
230	mbl_t	*mb;
231
232	while ((mb = *fmbp) != NULL) {
233		*fmbp = mb->nxt;
234		mb->nxt = frmblks;
235		frmblks = mb;
236		(void)memset(mb->blk, 0, mb->size - mb->nfree);
237	}
238}
239
240void
241initmem(void)
242{
243	int	pgsz;
244
245	pgsz = getpagesize();
246	mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
247
248	if ((mblks = calloc(nmblks = ML_INC, sizeof (mbl_t *))) == NULL)
249		nomem();
250}
251
252
253/*
254 * Allocate memory associated with level l.
255 */
256void *
257getlblk(int l, size_t s)
258{
259
260	while (l >= nmblks) {
261		if ((mblks = realloc(mblks, (nmblks + ML_INC) *
262		    sizeof (mbl_t *))) == NULL)
263			nomem();
264		(void)memset(&mblks[nmblks], 0, ML_INC * sizeof (mbl_t *));
265		nmblks += ML_INC;
266	}
267	return (xgetblk(&mblks[l], s));
268}
269
270void *
271getblk(size_t s)
272{
273
274	return (getlblk(mblklev, s));
275}
276
277/*
278 * Free all memory associated with level l.
279 */
280void
281freelblk(int l)
282{
283
284	xfreeblk(&mblks[l]);
285}
286
287void
288freeblk(void)
289{
290
291	freelblk(mblklev);
292}
293
294/*
295 * tgetblk() returns memory which is associated with the current
296 * expression.
297 */
298static	mbl_t	*tmblk;
299
300void *
301tgetblk(size_t s)
302{
303
304	return (xgetblk(&tmblk, s));
305}
306
307/*
308 * Get memory for a new tree node.
309 */
310tnode_t *
311getnode(void)
312{
313
314	return (tgetblk(sizeof (tnode_t)));
315}
316
317/*
318 * Free all memory which is allocated by the current expression.
319 */
320void
321tfreeblk(void)
322{
323
324	xfreeblk(&tmblk);
325}
326
327/*
328 * Save the memory which is used by the current expression. This memory
329 * is not freed by the next tfreeblk() call. The pointer returned can be
330 * used to restore the memory.
331 */
332mbl_t *
333tsave(void)
334{
335	mbl_t	*tmem;
336
337	tmem = tmblk;
338	tmblk = NULL;
339	return (tmem);
340}
341
342/*
343 * Free all memory used for the current expression and the memory used
344 * be a previous expression and saved by tsave(). The next call to
345 * tfreeblk() frees the restored memory.
346 */
347void
348trestor(mbl_t *tmem)
349{
350
351	tfreeblk();
352	if (tmblk != NULL) {
353		free(tmblk->blk);
354		free(tmblk);
355	}
356	tmblk = tmem;
357}
358