Home | History | Annotate | Line # | Download | only in lint1
mem1.c revision 1.60
      1  1.60    rillig /*	$NetBSD: mem1.c,v 1.60 2022/02/27 08:31:26 rillig Exp $	*/
      2   1.2       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1994, 1995 Jochen Pohl
      5   1.1       cgd  * All Rights Reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *      This product includes software developed by Jochen Pohl for
     18   1.1       cgd  *	The NetBSD Project.
     19   1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20   1.1       cgd  *    derived from this software without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1       cgd  */
     33   1.1       cgd 
     34  1.11       jmc #if HAVE_NBTOOL_CONFIG_H
     35  1.11       jmc #include "nbtool_config.h"
     36  1.11       jmc #endif
     37  1.11       jmc 
     38   1.3  christos #include <sys/cdefs.h>
     39   1.7        tv #if defined(__RCSID) && !defined(lint)
     40  1.60    rillig __RCSID("$NetBSD: mem1.c,v 1.60 2022/02/27 08:31:26 rillig Exp $");
     41   1.1       cgd #endif
     42   1.1       cgd 
     43   1.1       cgd #include <sys/param.h>
     44   1.1       cgd #include <stdlib.h>
     45   1.1       cgd #include <string.h>
     46   1.1       cgd 
     47   1.1       cgd #include "lint1.h"
     48   1.1       cgd 
     49   1.1       cgd /*
     50  1.44    rillig  * Filenames allocated by record_filename are shared and have unlimited
     51  1.44    rillig  * lifetime.
     52   1.1       cgd  */
     53  1.32    rillig struct filename {
     54  1.44    rillig 	const char *fn_name;
     55   1.1       cgd 	size_t	fn_len;
     56   1.1       cgd 	int	fn_id;
     57  1.32    rillig 	struct	filename *fn_next;
     58  1.32    rillig };
     59   1.1       cgd 
     60  1.34    rillig static struct filename *filenames;	/* null-terminated array */
     61   1.1       cgd 
     62  1.26    rillig /* Find the given filename, or return NULL. */
     63  1.33    rillig static const struct filename *
     64  1.33    rillig search_filename(const char *s, size_t len)
     65   1.1       cgd {
     66  1.34    rillig 	const struct filename *fn;
     67   1.1       cgd 
     68  1.34    rillig 	for (fn = filenames; fn != NULL; fn = fn->fn_next) {
     69   1.1       cgd 		if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0)
     70   1.1       cgd 			break;
     71   1.1       cgd 	}
     72  1.20    rillig 	return fn;
     73   1.1       cgd }
     74   1.1       cgd 
     75  1.32    rillig struct filename_replacement {
     76  1.44    rillig 	const char *orig;
     77  1.35    rillig 	size_t orig_len;
     78  1.44    rillig 	const char *repl;
     79  1.44    rillig 	const struct filename_replacement *next;
     80  1.18  christos };
     81  1.18  christos 
     82  1.34    rillig static struct filename_replacement *filename_replacements;
     83  1.18  christos 
     84  1.18  christos void
     85  1.30    rillig add_directory_replacement(char *arg)
     86  1.18  christos {
     87  1.43    rillig 	struct filename_replacement *r = xmalloc(sizeof(*r));
     88  1.19    rillig 
     89  1.36    rillig 	char *sep = strchr(arg, '=');
     90  1.36    rillig 	if (sep == NULL)
     91  1.36    rillig 		err(1, "Bad replacement directory spec `%s'", arg);
     92  1.36    rillig 	*sep = '\0';
     93  1.36    rillig 
     94  1.18  christos 	r->orig = arg;
     95  1.36    rillig 	r->orig_len = sep - arg;
     96  1.36    rillig 	r->repl = sep + 1;
     97  1.34    rillig 	r->next = filename_replacements;
     98  1.34    rillig 	filename_replacements = r;
     99  1.18  christos }
    100  1.18  christos 
    101  1.18  christos const char *
    102  1.37    rillig transform_filename(const char *name, size_t len)
    103  1.18  christos {
    104  1.18  christos 	static char buf[MAXPATHLEN];
    105  1.34    rillig 	const struct filename_replacement *r;
    106  1.18  christos 
    107  1.34    rillig 	for (r = filename_replacements; r != NULL; r = r->next)
    108  1.35    rillig 		if (r->orig_len < len &&
    109  1.35    rillig 		    memcmp(name, r->orig, r->orig_len) == 0)
    110  1.18  christos 			break;
    111  1.18  christos 	if (r == NULL)
    112  1.18  christos 		return name;
    113  1.51    rillig 	(void)snprintf(buf, sizeof(buf), "%s%s", r->repl, name + r->orig_len);
    114  1.18  christos 	return buf;
    115  1.18  christos }
    116  1.18  christos 
    117  1.39    rillig static int
    118  1.39    rillig next_filename_id(void)
    119  1.39    rillig {
    120  1.39    rillig 	static int next_id = 0;
    121  1.39    rillig 
    122  1.39    rillig 	return next_id++;
    123  1.39    rillig }
    124  1.39    rillig 
    125  1.29    rillig /*
    126  1.29    rillig  * Return a copy of the filename s with unlimited lifetime.
    127  1.37    rillig  * If the filename is new, write it to the output file.
    128  1.29    rillig  */
    129   1.1       cgd const char *
    130  1.37    rillig record_filename(const char *s, size_t slen)
    131   1.1       cgd {
    132  1.33    rillig 	const struct filename *existing_fn;
    133  1.32    rillig 	struct filename *fn;
    134  1.44    rillig 	char *name;
    135   1.1       cgd 
    136   1.1       cgd 	if (s == NULL)
    137  1.20    rillig 		return NULL;
    138   1.1       cgd 
    139  1.36    rillig 	if ((existing_fn = search_filename(s, slen)) != NULL)
    140  1.33    rillig 		return existing_fn->fn_name;
    141  1.31    rillig 
    142  1.44    rillig 	/* Do not use strdup() because s is not NUL-terminated.*/
    143  1.44    rillig 	name = xmalloc(slen + 1);
    144  1.44    rillig 	(void)memcpy(name, s, slen);
    145  1.44    rillig 	name[slen] = '\0';
    146  1.44    rillig 
    147  1.36    rillig 	fn = xmalloc(sizeof(*fn));
    148  1.44    rillig 	fn->fn_name = name;
    149  1.36    rillig 	fn->fn_len = slen;
    150  1.39    rillig 	fn->fn_id = next_filename_id();
    151  1.34    rillig 	fn->fn_next = filenames;
    152  1.34    rillig 	filenames = fn;
    153  1.31    rillig 
    154  1.37    rillig 	/* Write the ID of this filename to the output file. */
    155  1.31    rillig 	outclr();
    156  1.31    rillig 	outint(fn->fn_id);
    157  1.31    rillig 	outchar('s');
    158  1.37    rillig 	outstrg(transform_filename(fn->fn_name, fn->fn_len));
    159  1.31    rillig 
    160  1.20    rillig 	return fn->fn_name;
    161   1.1       cgd }
    162   1.1       cgd 
    163  1.26    rillig /* Get the ID of a filename. */
    164   1.1       cgd int
    165  1.37    rillig get_filename_id(const char *s)
    166   1.1       cgd {
    167  1.33    rillig 	const struct filename *fn;
    168   1.1       cgd 
    169  1.33    rillig 	if (s == NULL || (fn = search_filename(s, strlen(s))) == NULL)
    170  1.20    rillig 		return -1;
    171  1.20    rillig 	return fn->fn_id;
    172   1.1       cgd }
    173   1.1       cgd 
    174   1.1       cgd /*
    175  1.59    rillig  * Memory for declarations and other things that must be available
    176   1.1       cgd  * until the end of a block (or the end of the translation unit)
    177  1.42    rillig  * is associated with the corresponding mem_block_level, which may be 0.
    178  1.13       wiz  * Because this memory is allocated in large blocks associated with
    179   1.1       cgd  * a given level it can be freed easily at the end of a block.
    180   1.1       cgd  */
    181  1.38    rillig typedef struct memory_block {
    182  1.42    rillig 	void	*start;			/* beginning of memory block */
    183  1.42    rillig 	void	*first_free;		/* first free byte */
    184   1.1       cgd 	size_t	nfree;			/* # of free bytes */
    185  1.42    rillig 	struct	memory_block *next;
    186  1.38    rillig } memory_block;
    187   1.1       cgd 
    188   1.1       cgd 
    189  1.59    rillig static	size_t	mblk_size;	/* size of newly allocated memory blocks */
    190   1.1       cgd 
    191  1.59    rillig /* Array of lists of memory blocks, indexed by mem_block_level. */
    192  1.59    rillig static	memory_block	**mblks;
    193  1.59    rillig static	size_t	nmblks;		/* number of elements in *mblks */
    194  1.59    rillig #define	ML_INC	((size_t)32)	/* Increment for length of *mblks */
    195   1.1       cgd 
    196   1.1       cgd 
    197  1.22    rillig /* Allocate new memory, initialized with zero. */
    198   1.1       cgd static void *
    199  1.38    rillig xgetblk(memory_block **mbp, size_t s)
    200   1.1       cgd {
    201  1.38    rillig 	memory_block	*mb;
    202   1.1       cgd 	void	*p;
    203   1.1       cgd 
    204  1.59    rillig 	s = WORST_ALIGN(s);
    205  1.22    rillig 
    206   1.1       cgd 	if ((mb = *mbp) == NULL || mb->nfree < s) {
    207  1.59    rillig 		size_t block_size = s > mblk_size ? s : mblk_size;
    208  1.59    rillig 		mb = xmalloc(sizeof(*mb));
    209  1.59    rillig 		mb->start = xmalloc(block_size);
    210  1.42    rillig 		mb->first_free = mb->start;
    211  1.59    rillig 		mb->nfree = block_size;
    212  1.42    rillig 		mb->next = *mbp;
    213   1.1       cgd 		*mbp = mb;
    214   1.1       cgd 	}
    215  1.59    rillig 
    216  1.42    rillig 	p = mb->first_free;
    217  1.42    rillig 	mb->first_free = (char *)mb->first_free + s;
    218   1.1       cgd 	mb->nfree -= s;
    219  1.17  christos 	(void)memset(p, 0, s);
    220  1.20    rillig 	return p;
    221   1.1       cgd }
    222   1.1       cgd 
    223  1.58    rillig /* Free all blocks from list *fmbp. */
    224   1.1       cgd static void
    225  1.38    rillig xfreeblk(memory_block **fmbp)
    226   1.1       cgd {
    227  1.38    rillig 	memory_block	*mb;
    228   1.1       cgd 
    229   1.1       cgd 	while ((mb = *fmbp) != NULL) {
    230  1.42    rillig 		*fmbp = mb->next;
    231  1.58    rillig 		free(mb);
    232   1.1       cgd 	}
    233   1.1       cgd }
    234   1.1       cgd 
    235   1.1       cgd void
    236   1.5     lukem initmem(void)
    237   1.1       cgd {
    238   1.1       cgd 
    239  1.59    rillig 	mblk_size = mem_block_size();
    240  1.43    rillig 	mblks = xcalloc(nmblks = ML_INC, sizeof(*mblks));
    241   1.1       cgd }
    242   1.1       cgd 
    243   1.5     lukem 
    244  1.48    rillig /* Allocate memory associated with level l, initialized with zero. */
    245   1.1       cgd void *
    246  1.60    rillig level_zero_alloc(size_t l, size_t s)
    247   1.1       cgd {
    248   1.5     lukem 
    249   1.1       cgd 	while (l >= nmblks) {
    250  1.43    rillig 		mblks = xrealloc(mblks, (nmblks + ML_INC) * sizeof(*mblks));
    251  1.43    rillig 		(void)memset(&mblks[nmblks], 0, ML_INC * sizeof(*mblks));
    252   1.1       cgd 		nmblks += ML_INC;
    253   1.1       cgd 	}
    254  1.20    rillig 	return xgetblk(&mblks[l], s);
    255   1.1       cgd }
    256   1.1       cgd 
    257  1.60    rillig /* Allocate memory that is freed at the end of the current block. */
    258   1.1       cgd void *
    259  1.60    rillig block_zero_alloc(size_t s)
    260   1.1       cgd {
    261   1.5     lukem 
    262  1.60    rillig 	return level_zero_alloc(mem_block_level, s);
    263   1.1       cgd }
    264   1.1       cgd 
    265   1.1       cgd void
    266  1.60    rillig level_free_all(size_t level)
    267   1.1       cgd {
    268   1.5     lukem 
    269  1.60    rillig 	xfreeblk(&mblks[level]);
    270   1.1       cgd }
    271   1.1       cgd 
    272   1.1       cgd 
    273  1.38    rillig static	memory_block	*tmblk;
    274  1.26    rillig 
    275  1.60    rillig /* Allocate memory that is freed at the end of the current expression. */
    276   1.1       cgd void *
    277  1.60    rillig expr_zero_alloc(size_t s)
    278   1.1       cgd {
    279   1.5     lukem 
    280  1.20    rillig 	return xgetblk(&tmblk, s);
    281   1.1       cgd }
    282   1.1       cgd 
    283  1.45    rillig static bool
    284  1.45    rillig str_endswith(const char *haystack, const char *needle)
    285  1.45    rillig {
    286  1.45    rillig 	size_t hlen = strlen(haystack);
    287  1.45    rillig 	size_t nlen = strlen(needle);
    288  1.45    rillig 
    289  1.45    rillig 	return nlen <= hlen &&
    290  1.45    rillig 	       memcmp(haystack + hlen - nlen, needle, nlen) == 0;
    291  1.45    rillig }
    292  1.45    rillig 
    293  1.41    rillig /*
    294  1.41    rillig  * Return a freshly allocated tree node that is freed at the end of the
    295  1.41    rillig  * current expression.
    296  1.56    rillig  *
    297  1.56    rillig  * The node records whether it comes from a system file, which makes strict
    298  1.56    rillig  * bool mode less restrictive.
    299  1.41    rillig  */
    300   1.1       cgd tnode_t *
    301  1.60    rillig expr_alloc_tnode(void)
    302   1.1       cgd {
    303  1.60    rillig 	tnode_t *tn = expr_zero_alloc(sizeof(*tn));
    304  1.45    rillig 	/*
    305  1.45    rillig 	 * files named *.c that are different from the main translation unit
    306  1.45    rillig 	 * typically contain generated code that cannot be influenced, such
    307  1.45    rillig 	 * as a flex lexer or a yacc parser.
    308  1.45    rillig 	 */
    309  1.55    rillig 	tn->tn_sys = in_system_header ||
    310  1.55    rillig 		     (curr_pos.p_file != csrc_pos.p_file &&
    311  1.55    rillig 		      str_endswith(curr_pos.p_file, ".c"));
    312  1.25    rillig 	return tn;
    313   1.1       cgd }
    314   1.1       cgd 
    315  1.26    rillig /* Free all memory which is allocated by the current expression. */
    316   1.1       cgd void
    317  1.41    rillig expr_free_all(void)
    318   1.1       cgd {
    319   1.5     lukem 
    320   1.1       cgd 	xfreeblk(&tmblk);
    321   1.1       cgd }
    322   1.1       cgd 
    323   1.1       cgd /*
    324   1.1       cgd  * Save the memory which is used by the current expression. This memory
    325  1.41    rillig  * is not freed by the next expr_free_all() call. The pointer returned can be
    326   1.1       cgd  * used to restore the memory.
    327   1.1       cgd  */
    328  1.38    rillig memory_block *
    329  1.41    rillig expr_save_memory(void)
    330   1.1       cgd {
    331  1.38    rillig 	memory_block	*tmem;
    332   1.1       cgd 
    333   1.1       cgd 	tmem = tmblk;
    334   1.1       cgd 	tmblk = NULL;
    335  1.20    rillig 	return tmem;
    336   1.1       cgd }
    337   1.1       cgd 
    338   1.1       cgd /*
    339  1.47    rillig  * Free all memory used for the current expression and restore the memory used
    340  1.47    rillig  * by a previous expression and saved by expr_save_memory(). The next call to
    341  1.41    rillig  * expr_free_all() frees the restored memory.
    342   1.1       cgd  */
    343   1.1       cgd void
    344  1.41    rillig expr_restore_memory(memory_block *tmem)
    345   1.1       cgd {
    346   1.5     lukem 
    347  1.41    rillig 	expr_free_all();
    348   1.1       cgd 	if (tmblk != NULL) {
    349  1.42    rillig 		free(tmblk->start);
    350   1.1       cgd 		free(tmblk);
    351   1.1       cgd 	}
    352   1.1       cgd 	tmblk = tmem;
    353   1.1       cgd }
    354