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