1 /* $NetBSD: mem1.c,v 1.7 2002/01/31 19:36:54 tv 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.
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 $");
38 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
42 #include <sys/param.h>
51 * Filenames allocated by fnalloc() and fnnalloc() are shared.
62 static fn_t *srchfn(const char *, size_t);
65 * Look for a Filename of length l.
68 srchfn(const char *s, size_t len)
72 for (fn = fnames; fn != NULL; fn = fn->fn_nxt) {
73 if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0)
80 * Return a shared string for filename s.
83 fnalloc(const char *s)
86 return (s != NULL ? fnnalloc(s, strlen(s)) : NULL);
90 fnnalloc(const char *s, size_t len)
94 static int nxt_id = 0;
99 if ((fn = srchfn(s, len)) == NULL) {
100 if ((fn = malloc(sizeof (fn_t))) == NULL)
102 /* Do not used strdup() because string is not NUL-terminated.*/
103 if ((fn->fn_name = malloc(len + 1)) == NULL)
105 (void)memcpy(fn->fn_name, s, len);
106 fn->fn_name[len] = '\0';
108 fn->fn_id = nxt_id++;
111 /* Write id of this filename to the output file. */
115 outstrg(fn->fn_name);
117 return (fn->fn_name);
121 * Get id of a filename.
124 getfnid(const char *s)
128 if (s == NULL || (fn = srchfn(s, strlen(s))) == NULL)
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.
140 #define ML_INC ((size_t)32) /* Increment for length of *mblks */
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 */
151 * Array of pointers to lists of memory blocks. mblklev is used as
152 * index into this array.
154 static mbl_t **mblks;
156 /* number of elements in *mblks */
157 static size_t nmblks;
159 /* free list for memory blocks */
160 static mbl_t *frmblks;
162 /* length of new allocated memory blocks */
163 static size_t mblklen;
165 static void *xgetblk(mbl_t **, size_t);
166 static void xfreeblk(mbl_t **);
167 static mbl_t *xnewblk(void);
175 if ((mb = malloc(sizeof (mbl_t))) == NULL)
178 /* use mmap instead of malloc to avoid malloc's size overhead */
180 prot = PROT_READ | PROT_WRITE;
181 flags = MAP_ANON | MAP_PRIVATE;
182 mb->blk = mmap(NULL, mblklen, prot, flags, -1, (off_t)0);
183 if (mb->blk == (void *)MAP_FAILED)
184 err(1, "can't map memory");
192 * Allocate new memory. If the first block of the list has not enough
193 * free space, or there is no first block, get a new block. The new
194 * block is taken from the free list or, if there is no block on the
195 * free list, is allocated using xnewblk(). If a new block is allocated
196 * it is initialized with zero. Blocks taken from the free list are
197 * zero'd in xfreeblk().
200 xgetblk(mbl_t **mbp, size_t s)
207 if ((mb = *mbp) == NULL || mb->nfree < s) {
208 if ((mb = frmblks) == NULL) {
216 (void)memset(mb->blk, 0, mb->size);
221 mb->nfree = mb->size;
226 mb->ffree = (char *)mb->ffree + s;
232 * Move all blocks from list *fmbp to free list. For each block, set all
233 * used memory to zero.
236 xfreeblk(mbl_t **fmbp)
240 while ((mb = *fmbp) != NULL) {
244 (void)memset(mb->blk, 0, mb->size - mb->nfree);
253 pgsz = getpagesize();
254 mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
256 if ((mblks = calloc(nmblks = ML_INC, sizeof (mbl_t *))) == NULL)
262 * Allocate memory associated with level l.
265 getlblk(int l, size_t s)
268 while (l >= nmblks) {
269 if ((mblks = realloc(mblks, (nmblks + ML_INC) *
270 sizeof (mbl_t *))) == NULL)
272 (void)memset(&mblks[nmblks], 0, ML_INC * sizeof (mbl_t *));
275 return (xgetblk(&mblks[l], s));
282 return (getlblk(mblklev, s));
286 * Free all memory associated with level l.
303 * tgetblk() returns memory which is associated with the current
312 return (xgetblk(&tmblk, s));
316 * Get memory for a new tree node.
322 return (tgetblk(sizeof (tnode_t)));
326 * Free all memory which is allocated by the current expression.
336 * Save the memory which is used by the current expression. This memory
337 * is not freed by the next tfreeblk() call. The pointer returned can be
338 * used to restore the memory.
351 * Free all memory used for the current expression and the memory used
352 * be a previous expression and saved by tsave(). The next call to
353 * tfreeblk() frees the restored memory.