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