Home | History | Annotate | Line # | Download | only in lint1
mem1.c revision 1.63
      1 /*	$NetBSD: mem1.c,v 1.63 2022/05/20 21:18:55 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)
     40 __RCSID("$NetBSD: mem1.c,v 1.63 2022/05/20 21:18:55 rillig Exp $");
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 
     47 #include "lint1.h"
     48 
     49 /*
     50  * Filenames allocated by record_filename are shared and have unlimited
     51  * lifetime.
     52  */
     53 struct filename {
     54 	const char *fn_name;
     55 	size_t	fn_len;
     56 	int	fn_id;
     57 	struct	filename *fn_next;
     58 };
     59 
     60 static struct filename *filenames;	/* null-terminated array */
     61 static int next_filename_id;
     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 	const char *orig;
     78 	size_t orig_len;
     79 	const char *repl;
     80 	const 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 	(void)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 	char *name;
    128 
    129 	if ((existing_fn = search_filename(s, slen)) != NULL)
    130 		return existing_fn->fn_name;
    131 
    132 	name = xmalloc(slen + 1);
    133 	(void)memcpy(name, s, slen);
    134 	name[slen] = '\0';
    135 
    136 	fn = xmalloc(sizeof(*fn));
    137 	fn->fn_name = name;
    138 	fn->fn_len = slen;
    139 	fn->fn_id = next_filename_id++;
    140 	fn->fn_next = filenames;
    141 	filenames = fn;
    142 
    143 	/* Write the ID of this filename to the output file. */
    144 	outclr();
    145 	outint(fn->fn_id);
    146 	outchar('s');
    147 	outstrg(transform_filename(fn->fn_name, fn->fn_len));
    148 
    149 	return fn->fn_name;
    150 }
    151 
    152 /* Get the ID of a filename. */
    153 int
    154 get_filename_id(const char *s)
    155 {
    156 	const struct filename *fn;
    157 
    158 	if (s == NULL || (fn = search_filename(s, strlen(s))) == NULL)
    159 		return -1;
    160 	return fn->fn_id;
    161 }
    162 
    163 /*
    164  * Memory for declarations and other things that must be available
    165  * until the end of a block (or the end of the translation unit)
    166  * is associated with the corresponding mem_block_level, which may be 0.
    167  * Because this memory is allocated in large blocks associated with
    168  * a given level it can be freed easily at the end of a block.
    169  */
    170 typedef struct memory_block {
    171 	void	*start;			/* beginning of memory block */
    172 	void	*first_free;		/* first free byte */
    173 	size_t	nfree;			/* # of free bytes */
    174 	struct	memory_block *next;
    175 } memory_block;
    176 
    177 
    178 static	size_t	mblk_size;	/* size of newly allocated memory blocks */
    179 
    180 /* Array of lists of memory blocks, indexed by mem_block_level. */
    181 static	memory_block	**mblks;
    182 static	size_t	nmblks;		/* number of elements in *mblks */
    183 #define	ML_INC	((size_t)32)	/* Increment for length of *mblks */
    184 
    185 
    186 /* Allocate new memory, initialized with zero. */
    187 static void *
    188 xgetblk(memory_block **mbp, size_t s)
    189 {
    190 	memory_block	*mb;
    191 	void	*p;
    192 
    193 	size_t worst_align = 2 * sizeof(long) - 1;
    194 	s = (s + worst_align) & ~worst_align;
    195 
    196 	if ((mb = *mbp) == NULL || mb->nfree < s) {
    197 		size_t block_size = s > mblk_size ? s : mblk_size;
    198 		mb = xmalloc(sizeof(*mb));
    199 		mb->start = xmalloc(block_size);
    200 		mb->first_free = mb->start;
    201 		mb->nfree = block_size;
    202 		mb->next = *mbp;
    203 		*mbp = mb;
    204 	}
    205 
    206 	p = mb->first_free;
    207 	mb->first_free = (char *)mb->first_free + s;
    208 	mb->nfree -= s;
    209 	(void)memset(p, 0, s);
    210 	return p;
    211 }
    212 
    213 /* Free all blocks from list *fmbp. */
    214 static void
    215 xfreeblk(memory_block **fmbp)
    216 {
    217 	memory_block	*mb;
    218 
    219 	while ((mb = *fmbp) != NULL) {
    220 		*fmbp = mb->next;
    221 		free(mb);
    222 	}
    223 }
    224 
    225 void
    226 initmem(void)
    227 {
    228 
    229 	mblk_size = mem_block_size();
    230 	mblks = xcalloc(nmblks = ML_INC, sizeof(*mblks));
    231 }
    232 
    233 
    234 /* Allocate memory associated with level l, initialized with zero. */
    235 void *
    236 level_zero_alloc(size_t l, size_t s)
    237 {
    238 
    239 	while (l >= nmblks) {
    240 		mblks = xrealloc(mblks, (nmblks + ML_INC) * sizeof(*mblks));
    241 		(void)memset(&mblks[nmblks], 0, ML_INC * sizeof(*mblks));
    242 		nmblks += ML_INC;
    243 	}
    244 	return xgetblk(&mblks[l], s);
    245 }
    246 
    247 /* Allocate memory that is freed at the end of the current block. */
    248 void *
    249 block_zero_alloc(size_t s)
    250 {
    251 
    252 	return level_zero_alloc(mem_block_level, s);
    253 }
    254 
    255 void
    256 level_free_all(size_t level)
    257 {
    258 
    259 	xfreeblk(&mblks[level]);
    260 }
    261 
    262 
    263 static	memory_block	*tmblk;
    264 
    265 /* Allocate memory that is freed at the end of the current expression. */
    266 void *
    267 expr_zero_alloc(size_t s)
    268 {
    269 
    270 	return xgetblk(&tmblk, s);
    271 }
    272 
    273 static bool
    274 str_endswith(const char *haystack, const char *needle)
    275 {
    276 	size_t hlen = strlen(haystack);
    277 	size_t nlen = strlen(needle);
    278 
    279 	return nlen <= hlen &&
    280 	       memcmp(haystack + hlen - nlen, needle, nlen) == 0;
    281 }
    282 
    283 /*
    284  * Return a freshly allocated tree node that is freed at the end of the
    285  * current expression.
    286  *
    287  * The node records whether it comes from a system file, which makes strict
    288  * bool mode less restrictive.
    289  */
    290 tnode_t *
    291 expr_alloc_tnode(void)
    292 {
    293 	tnode_t *tn = expr_zero_alloc(sizeof(*tn));
    294 	/*
    295 	 * files named *.c that are different from the main translation unit
    296 	 * typically contain generated code that cannot be influenced, such
    297 	 * as a flex lexer or a yacc parser.
    298 	 */
    299 	tn->tn_sys = in_system_header ||
    300 		     (curr_pos.p_file != csrc_pos.p_file &&
    301 		      str_endswith(curr_pos.p_file, ".c"));
    302 	return tn;
    303 }
    304 
    305 /* Free all memory which is allocated by the current expression. */
    306 void
    307 expr_free_all(void)
    308 {
    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 expr_free_all() call. The pointer returned can be
    316  * used to restore the memory.
    317  */
    318 memory_block *
    319 expr_save_memory(void)
    320 {
    321 	memory_block	*tmem;
    322 
    323 	tmem = tmblk;
    324 	tmblk = NULL;
    325 	return tmem;
    326 }
    327 
    328 /*
    329  * Free all memory used for the current expression and restore the memory used
    330  * by a previous expression and saved by expr_save_memory(). The next call to
    331  * expr_free_all() frees the restored memory.
    332  */
    333 void
    334 expr_restore_memory(memory_block *tmem)
    335 {
    336 
    337 	expr_free_all();
    338 	if (tmblk != NULL) {
    339 		free(tmblk->start);
    340 		free(tmblk);
    341 	}
    342 	tmblk = tmem;
    343 }
    344