Remove __P macros from src/usr.bin and src/usr.sbin.
[dragonfly.git] / usr.bin / xlint / lint1 / mem1.c
1 /*      $NetBSD: mem1.c,v 1.2 1995/07/03 21:24:25 cgd 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  * $NetBSD: mem1.c,v 1.2 1995/07/03 21:24:25 cgd Exp $
34  * $DragonFly: src/usr.bin/xlint/lint1/mem1.c,v 1.4 2003/11/03 19:31:34 eirikn Exp $
35  */
36
37 #include <sys/types.h>
38 #include <sys/mman.h>
39 #include <sys/param.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <err.h>
44
45 #include "lint1.h"
46
47 /*
48  * Filenames allocated by fnalloc() and fnnalloc() are shared.
49  */
50 typedef struct fn {
51         char    *fn_name;
52         size_t  fn_len;
53         int     fn_id;
54         struct  fn *fn_nxt;
55 } fn_t;
56
57 static  fn_t    *fnames;
58
59 static  fn_t    *srchfn(const char *, size_t);
60
61 /*
62  * Look for a Filename of length l.
63  */
64 static fn_t *
65 srchfn(s, len)
66         const   char *s;
67         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  */
81 const char *
82 fnalloc(s)
83         const   char *s;
84 {
85         return (s != NULL ? fnnalloc(s, strlen(s)) : NULL);
86 }
87
88 const char *
89 fnnalloc(s, len)
90         const   char *s;
91         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(s)
124         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
142 typedef 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  */
154 static  mbl_t   **mblks;
155
156 /* number of elements in *mblks */
157 static  size_t  nmblks;
158
159 /* free list for memory blocks */
160 static  mbl_t   *frmblks;
161
162 /* length of new allocated memory blocks */
163 static  size_t  mblklen;
164
165 static  void    *xgetblk(mbl_t **, size_t);
166 static  void    xfreeblk(mbl_t **);
167 static  mbl_t   *xnewblk(void);
168
169 static mbl_t *
170 xnewblk()
171 {
172         mbl_t   *mb;
173         int     prot, flags;
174
175         mb = xmalloc(sizeof (mbl_t));
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         if (ALIGN((u_long)mb->blk) != (u_long)mb->blk)
185                 errx(1, "mapped address is not aligned");
186
187         mb->size = mblklen;
188
189         return (mb);
190 }
191
192 /*
193  * Allocate new memory. If the first block of the list has not enough
194  * free space, or there is no first block, get a new block. The new
195  * block is taken from the free list or, if there is no block on the
196  * free list, is allocated using xnewblk(). If a new block is allocated
197  * it is initialized with zero. Blocks taken from the free list are
198  * zero'd in xfreeblk().
199  */
200 static void *
201 xgetblk(mbp, s)
202         mbl_t   **mbp;
203         size_t  s;
204 {
205         mbl_t   *mb;
206         void    *p;
207
208         s = ALIGN(s);
209         if ((mb = *mbp) == NULL || mb->nfree < s) {
210                 if ((mb = frmblks) == NULL) {
211                         mb = xnewblk();
212                         (void)memset(mb->blk, 0, mb->size);
213                 } else {
214                         frmblks = mb->nxt;
215                 }
216                 mb->ffree = mb->blk;
217                 mb->nfree = mb->size;;
218                 mb->nxt = *mbp;
219                 *mbp = mb;
220         }
221         p = mb->ffree;
222         mb->ffree = (char *)mb->ffree + s;
223         mb->nfree -= s;
224         return (p);
225 }
226
227 /*
228  * Move all blocks from list *fmbp to free list. For each block, set all
229  * used memory to zero.
230  */
231 static void
232 xfreeblk(fmbp)
233         mbl_t   **fmbp;
234 {
235         mbl_t   *mb;
236
237         while ((mb = *fmbp) != NULL) {
238                 *fmbp = mb->nxt;
239                 mb->nxt = frmblks;
240                 frmblks = mb;
241                 (void)memset(mb->blk, 0, mb->size - mb->nfree);
242         }
243 }
244
245 void
246 initmem()
247 {
248         int     pgsz;
249
250         pgsz = getpagesize();
251         mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
252
253         mblks = xcalloc(nmblks = ML_INC, sizeof (mbl_t *));
254 }
255
256         
257 /*
258  * Allocate memory associated with level l.
259  */
260 void *
261 getlblk(l, s)
262         int     l;
263         size_t  s;
264 {
265         while (l >= nmblks) {
266                 mblks = xrealloc(mblks, (nmblks + ML_INC) * sizeof (mbl_t *));
267                 (void)memset(&mblks[nmblks], 0, ML_INC * sizeof (mbl_t *));
268                 nmblks += ML_INC;
269         }
270         return (xgetblk(&mblks[l], s));
271 }
272
273 void *
274 getblk(s)
275         size_t  s;
276 {
277         return (getlblk(mblklev, s));
278 }
279
280 /*
281  * Free all memory associated with level l.
282  */
283 void
284 freelblk(l)
285         int     l;
286 {
287         xfreeblk(&mblks[l]);
288 }
289
290 void
291 freeblk()
292 {
293         freelblk(mblklev);
294 }
295
296 /*
297  * tgetblk() returns memory which is associated with the current
298  * expression.
299  */
300 static  mbl_t   *tmblk;
301
302 void *
303 tgetblk(s)
304         size_t  s;
305 {
306         return (xgetblk(&tmblk, s));
307 }
308
309 /*
310  * Get memory for a new tree node.
311  */
312 tnode_t *
313 getnode()
314 {
315         return (tgetblk(sizeof (tnode_t)));
316 }
317
318 /*
319  * Free all memory which is allocated by the the current expression.
320  */
321 void
322 tfreeblk()
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  */
332 mbl_t *
333 tsave()
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  */
347 void
348 trestor(tmem)
349         mbl_t   *tmem;
350 {
351         tfreeblk();
352         if (tmblk != NULL) {
353                 free(tmblk->blk);
354                 free(tmblk);
355         }
356         tmblk = tmem;
357 }