1 /* $NetBSD: mem1.c,v 1.2 1995/07/03 21:24:25 cgd Exp $ */
4 * Copyright (c) 1994, 1995 Jochen Pohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
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.6 2004/07/07 12:24:00 asmodai Exp $
37 #include <sys/types.h>
39 #include <sys/param.h>
48 * Filenames allocated by fnalloc() and fnnalloc() are shared.
59 static fn_t *srchfn(const char *, size_t);
62 * Look for a Filename of length l.
65 srchfn(const char *s, size_t len)
69 for (fn = fnames; fn != NULL; fn = fn->fn_nxt) {
70 if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0)
77 * Return a shared string for filename s.
80 fnalloc(const char *s)
82 return (s != NULL ? fnnalloc(s, strlen(s)) : NULL);
86 fnnalloc(const char *s, size_t len)
90 static int nxt_id = 0;
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';
102 fn->fn_id = nxt_id++;
105 /* Write id of this filename to the output file. */
109 outstrg(fn->fn_name);
111 return (fn->fn_name);
115 * Get id of a filename.
118 getfnid(const char *s)
122 if (s == NULL || (fn = srchfn(s, strlen(s))) == NULL)
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.
134 #define ML_INC ((size_t)32) /* Increment for length of *mblks */
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 */
145 * Array of pointers to lists of memory blocks. mblklev is used as
146 * index into this array.
148 static mbl_t **mblks;
150 /* number of elements in *mblks */
151 static size_t nmblks;
153 /* free list for memory blocks */
154 static mbl_t *frmblks;
156 /* length of new allocated memory blocks */
157 static size_t mblklen;
159 static void *xgetblk(mbl_t **, size_t);
160 static void xfreeblk(mbl_t **);
161 static mbl_t *xnewblk(void);
169 mb = xmalloc(sizeof (mbl_t));
171 /* use mmap instead of malloc to avoid malloc's size overhead */
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");
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().
195 xgetblk(mbl_t **mbp, size_t s)
201 if ((mb = *mbp) == NULL || mb->nfree < s) {
202 if ((mb = frmblks) == NULL) {
204 (void)memset(mb->blk, 0, mb->size);
209 mb->nfree = mb->size;;
214 mb->ffree = (char *)mb->ffree + s;
220 * Move all blocks from list *fmbp to free list. For each block, set all
221 * used memory to zero.
224 xfreeblk(mbl_t **fmbp)
228 while ((mb = *fmbp) != NULL) {
232 (void)memset(mb->blk, 0, mb->size - mb->nfree);
241 pgsz = getpagesize();
242 mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
244 mblks = xcalloc(nmblks = ML_INC, sizeof (mbl_t *));
249 * Allocate memory associated with level l.
252 getlblk(int l, size_t s)
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 *));
259 return (xgetblk(&mblks[l], s));
265 return (getlblk(mblklev, s));
269 * Free all memory associated with level l.
284 * tgetblk() returns memory which is associated with the current
292 return (xgetblk(&tmblk, s));
296 * Get memory for a new tree node.
301 return (tgetblk(sizeof (tnode_t)));
305 * Free all memory which is allocated by the the current expression.
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.
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.