Home | History | Annotate | Line # | Download | only in lint1
mem1.c revision 1.37
      1 /*	$NetBSD: mem1.c,v 1.37 2021/03/27 12:32:19 rillig 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 #if HAVE_NBTOOL_CONFIG_H
     35 #include "nbtool_config.h"
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 #if defined(__RCSID) && !defined(lint)
     40 __RCSID("$NetBSD: mem1.c,v 1.37 2021/03/27 12:32:19 rillig Exp $");
     41 #endif
     42 
     43 #include <sys/types.h>
     44 #include <sys/param.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 
     49 #include "lint1.h"
     50 
     51 /*
     52  * Filenames allocated by record_filename are shared.
     53  */
     54 struct filename {
     55 	char	*fn_name;
     56 	size_t	fn_len;
     57 	int	fn_id;
     58 	struct	filename *fn_next;
     59 };
     60 
     61 static struct filename *filenames;	/* null-terminated array */
     62 
     63 /* Find the given filename, or return NULL. */
     64 static const struct filename *
     65 search_filename(const char *s, size_t len)
     66 {
     67 	const struct filename *fn;
     68 
     69 	for (fn = filenames; fn != NULL; fn = fn->fn_next) {
     70 		if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0)
     71 			break;
     72 	}
     73 	return fn;
     74 }
     75 
     76 struct filename_replacement {
     77 	char *orig;
     78 	size_t orig_len;
     79 	char *repl;
     80 	struct filename_replacement *next;
     81 };
     82 
     83 static struct filename_replacement *filename_replacements;
     84 
     85 void
     86 add_directory_replacement(char *arg)
     87 {
     88 	struct filename_replacement *r = xmalloc(sizeof *r);
     89 
     90 	char *sep = strchr(arg, '=');
     91 	if (sep == NULL)
     92 		err(1, "Bad replacement directory spec `%s'", arg);
     93 	*sep = '\0';
     94 
     95 	r->orig = arg;
     96 	r->orig_len = sep - arg;
     97 	r->repl = sep + 1;
     98 	r->next = filename_replacements;
     99 	filename_replacements = r;
    100 }
    101 
    102 const char *
    103 transform_filename(const char *name, size_t len)
    104 {
    105 	static char buf[MAXPATHLEN];
    106 	const struct filename_replacement *r;
    107 
    108 	for (r = filename_replacements; r != NULL; r = r->next)
    109 		if (r->orig_len < len &&
    110 		    memcmp(name, r->orig, r->orig_len) == 0)
    111 			break;
    112 	if (r == NULL)
    113 		return name;
    114 	snprintf(buf, sizeof buf, "%s%s", r->repl, name + r->orig_len);
    115 	return buf;
    116 }
    117 
    118 /*
    119  * Return a copy of the filename s with unlimited lifetime.
    120  * If the filename is new, write it to the output file.
    121  */
    122 const char *
    123 record_filename(const char *s, size_t slen)
    124 {
    125 	const struct filename *existing_fn;
    126 	struct filename *fn;
    127 
    128 	static	int	nxt_id = 0;
    129 
    130 	if (s == NULL)
    131 		return NULL;
    132 
    133 	if ((existing_fn = search_filename(s, slen)) != NULL)
    134 		return existing_fn->fn_name;
    135 
    136 	fn = xmalloc(sizeof(*fn));
    137 	/* Do not use strdup() because s is not NUL-terminated.*/
    138 	fn->fn_name = xmalloc(slen + 1);
    139 	(void)memcpy(fn->fn_name, s, slen);
    140 	fn->fn_name[slen] = '\0';
    141 	fn->fn_len = slen;
    142 	fn->fn_id = nxt_id++;
    143 	fn->fn_next = filenames;
    144 	filenames = fn;
    145 
    146 	/* Write the ID of this filename to the output file. */
    147 	outclr();
    148 	outint(fn->fn_id);
    149 	outchar('s');
    150 	outstrg(transform_filename(fn->fn_name, fn->fn_len));
    151 
    152 	return fn->fn_name;
    153 }
    154 
    155 /* Get the ID of a filename. */
    156 int
    157 get_filename_id(const char *s)
    158 {
    159 	const struct filename *fn;
    160 
    161 	if (s == NULL || (fn = search_filename(s, strlen(s))) == NULL)
    162 		return -1;
    163 	return fn->fn_id;
    164 }
    165 
    166 /*
    167  * Memory for declarations and other things which must be available
    168  * until the end of a block (or the end of the translation unit)
    169  * are associated with the level (mem_block_level) of the block (or with 0).
    170  * Because this memory is allocated in large blocks associated with
    171  * a given level it can be freed easily at the end of a block.
    172  */
    173 #define	ML_INC	((size_t)32)		/* Increment for length of *mblks */
    174 
    175 typedef struct mbl {
    176 	void	*blk;			/* beginning of memory block */
    177 	void	*ffree;			/* first free byte */
    178 	size_t	nfree;			/* # of free bytes */
    179 	size_t	size;			/* total size of memory block */
    180 	struct	mbl *nxt;		/* next block */
    181 } mbl_t;
    182 
    183 /*
    184  * Array of pointers to lists of memory blocks. mem_block_level is used as
    185  * index into this array.
    186  */
    187 static	mbl_t	**mblks;
    188 
    189 /* number of elements in *mblks */
    190 static	size_t	nmblks;
    191 
    192 /* free list for memory blocks */
    193 static	mbl_t	*frmblks;
    194 
    195 /* length of new allocated memory blocks */
    196 static	size_t	mblklen;
    197 
    198 static	void	*xgetblk(mbl_t **, size_t);
    199 static	void	xfreeblk(mbl_t **);
    200 static	mbl_t	*xnewblk(void);
    201 
    202 static mbl_t *
    203 xnewblk(void)
    204 {
    205 	mbl_t	*mb = xmalloc(sizeof *mb);
    206 
    207 	/* use mmap instead of malloc to avoid malloc's size overhead */
    208 	mb->blk = xmapalloc(mblklen);
    209 	mb->size = mblklen;
    210 
    211 	return mb;
    212 }
    213 
    214 /* Allocate new memory, initialized with zero. */
    215 static void *
    216 xgetblk(mbl_t **mbp, size_t s)
    217 {
    218 	mbl_t	*mb;
    219 	void	*p;
    220 	size_t	t = 0;
    221 
    222 	/*
    223 	 * If the first block of the list has not enough free space,
    224 	 * or there is no first block, get a new block. The new block
    225 	 * is taken from the free list or, if there is no block on the
    226 	 * free list, is allocated using xnewblk().
    227 	 *
    228 	 * If a new block is allocated it is initialized with zero.
    229 	 * Blocks taken from the free list are zero'd in xfreeblk().
    230 	 */
    231 
    232 	s = WORST_ALIGN(s);
    233 	if ((mb = *mbp) == NULL || mb->nfree < s) {
    234 		if ((mb = frmblks) == NULL || mb->size < s) {
    235 			if (s > mblklen) {
    236 				t = mblklen;
    237 				mblklen = s;
    238 			}
    239 			mb = xnewblk();
    240 #ifndef BLKDEBUG
    241 			(void)memset(mb->blk, 0, mb->size);
    242 #endif
    243 			if (t > 0)
    244 				mblklen = t;
    245 		} else {
    246 			frmblks = mb->nxt;
    247 		}
    248 		mb->ffree = mb->blk;
    249 		mb->nfree = mb->size;
    250 		mb->nxt = *mbp;
    251 		*mbp = mb;
    252 	}
    253 	p = mb->ffree;
    254 	mb->ffree = (char *)mb->ffree + s;
    255 	mb->nfree -= s;
    256 #ifdef BLKDEBUG
    257 	(void)memset(p, 0, s);
    258 #endif
    259 	return p;
    260 }
    261 
    262 /*
    263  * Move all blocks from list *fmbp to free list. For each block, set all
    264  * used memory to zero.
    265  */
    266 static void
    267 xfreeblk(mbl_t **fmbp)
    268 {
    269 	mbl_t	*mb;
    270 
    271 	while ((mb = *fmbp) != NULL) {
    272 		*fmbp = mb->nxt;
    273 		mb->nxt = frmblks;
    274 		frmblks = mb;
    275 		(void)memset(mb->blk, ZERO, mb->size - mb->nfree);
    276 	}
    277 }
    278 
    279 void
    280 initmem(void)
    281 {
    282 	int	pgsz;
    283 
    284 	pgsz = getpagesize();
    285 	mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz;
    286 
    287 	mblks = xcalloc(nmblks = ML_INC, sizeof *mblks);
    288 }
    289 
    290 
    291 /* Allocate memory associated with level l. */
    292 void *
    293 getlblk(size_t l, size_t s)
    294 {
    295 
    296 	while (l >= nmblks) {
    297 		mblks = xrealloc(mblks, (nmblks + ML_INC) * sizeof *mblks);
    298 		(void)memset(&mblks[nmblks], 0, ML_INC * sizeof *mblks);
    299 		nmblks += ML_INC;
    300 	}
    301 	return xgetblk(&mblks[l], s);
    302 }
    303 
    304 void *
    305 getblk(size_t s)
    306 {
    307 
    308 	return getlblk(mem_block_level, s);
    309 }
    310 
    311 /* Free all memory associated with level l. */
    312 void
    313 freelblk(int l)
    314 {
    315 
    316 	xfreeblk(&mblks[l]);
    317 }
    318 
    319 void
    320 freeblk(void)
    321 {
    322 
    323 	freelblk(mem_block_level);
    324 }
    325 
    326 static	mbl_t	*tmblk;
    327 
    328 /*
    329  * Return zero-initialized memory that is freed at the end of the current
    330  * expression.
    331  */
    332 void *
    333 tgetblk(size_t s)
    334 {
    335 
    336 	return xgetblk(&tmblk, s);
    337 }
    338 
    339 /* Return a freshly allocated tree node. */
    340 tnode_t *
    341 getnode(void)
    342 {
    343 	tnode_t *tn = tgetblk(sizeof *tn);
    344 	tn->tn_from_system_header = in_system_header;
    345 	return tn;
    346 }
    347 
    348 /* Free all memory which is allocated by the current expression. */
    349 void
    350 tfreeblk(void)
    351 {
    352 
    353 	xfreeblk(&tmblk);
    354 }
    355 
    356 /*
    357  * Save the memory which is used by the current expression. This memory
    358  * is not freed by the next tfreeblk() call. The pointer returned can be
    359  * used to restore the memory.
    360  */
    361 mbl_t *
    362 tsave(void)
    363 {
    364 	mbl_t	*tmem;
    365 
    366 	tmem = tmblk;
    367 	tmblk = NULL;
    368 	return tmem;
    369 }
    370 
    371 /*
    372  * Free all memory used for the current expression and the memory used
    373  * be a previous expression and saved by tsave(). The next call to
    374  * tfreeblk() frees the restored memory.
    375  */
    376 void
    377 trestor(mbl_t *tmem)
    378 {
    379 
    380 	tfreeblk();
    381 	if (tmblk != NULL) {
    382 		free(tmblk->blk);
    383 		free(tmblk);
    384 	}
    385 	tmblk = tmem;
    386 }
    387