Home | History | Annotate | Line # | Download | only in lint1
mem1.c revision 1.73
      1  1.73    rillig /*	$NetBSD: mem1.c,v 1.73 2023/07/30 08:58:54 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.68    rillig  *	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.63    rillig #if defined(__RCSID)
     40  1.73    rillig __RCSID("$NetBSD: mem1.c,v 1.73 2023/07/30 08:58:54 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.61    rillig static int next_filename_id;
     62   1.1       cgd 
     63  1.26    rillig /* Find the given filename, or return NULL. */
     64  1.33    rillig static const struct filename *
     65  1.33    rillig search_filename(const char *s, size_t len)
     66   1.1       cgd {
     67  1.34    rillig 	const struct filename *fn;
     68   1.1       cgd 
     69  1.34    rillig 	for (fn = filenames; fn != NULL; fn = fn->fn_next) {
     70   1.1       cgd 		if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0)
     71   1.1       cgd 			break;
     72   1.1       cgd 	}
     73  1.20    rillig 	return fn;
     74   1.1       cgd }
     75   1.1       cgd 
     76  1.32    rillig struct filename_replacement {
     77  1.44    rillig 	const char *orig;
     78  1.35    rillig 	size_t orig_len;
     79  1.44    rillig 	const char *repl;
     80  1.44    rillig 	const struct filename_replacement *next;
     81  1.18  christos };
     82  1.18  christos 
     83  1.34    rillig static struct filename_replacement *filename_replacements;
     84  1.18  christos 
     85  1.18  christos void
     86  1.30    rillig add_directory_replacement(char *arg)
     87  1.18  christos {
     88  1.43    rillig 	struct filename_replacement *r = xmalloc(sizeof(*r));
     89  1.19    rillig 
     90  1.36    rillig 	char *sep = strchr(arg, '=');
     91  1.36    rillig 	if (sep == NULL)
     92  1.36    rillig 		err(1, "Bad replacement directory spec `%s'", arg);
     93  1.36    rillig 	*sep = '\0';
     94  1.36    rillig 
     95  1.18  christos 	r->orig = arg;
     96  1.67    rillig 	r->orig_len = (size_t)(sep - arg);
     97  1.36    rillig 	r->repl = sep + 1;
     98  1.34    rillig 	r->next = filename_replacements;
     99  1.34    rillig 	filename_replacements = r;
    100  1.18  christos }
    101  1.18  christos 
    102  1.18  christos const char *
    103  1.37    rillig transform_filename(const char *name, size_t len)
    104  1.18  christos {
    105  1.18  christos 	static char buf[MAXPATHLEN];
    106  1.34    rillig 	const struct filename_replacement *r;
    107  1.18  christos 
    108  1.34    rillig 	for (r = filename_replacements; r != NULL; r = r->next)
    109  1.35    rillig 		if (r->orig_len < len &&
    110  1.35    rillig 		    memcmp(name, r->orig, r->orig_len) == 0)
    111  1.18  christos 			break;
    112  1.18  christos 	if (r == NULL)
    113  1.18  christos 		return name;
    114  1.51    rillig 	(void)snprintf(buf, sizeof(buf), "%s%s", r->repl, name + r->orig_len);
    115  1.18  christos 	return buf;
    116  1.18  christos }
    117  1.18  christos 
    118  1.29    rillig /*
    119  1.29    rillig  * Return a copy of the filename s with unlimited lifetime.
    120  1.37    rillig  * If the filename is new, write it to the output file.
    121  1.29    rillig  */
    122   1.1       cgd const char *
    123  1.37    rillig record_filename(const char *s, size_t slen)
    124   1.1       cgd {
    125   1.1       cgd 
    126  1.65    rillig 	const struct filename *existing_fn = search_filename(s, slen);
    127  1.65    rillig 	if (existing_fn != NULL)
    128  1.33    rillig 		return existing_fn->fn_name;
    129  1.31    rillig 
    130  1.65    rillig 	char *name = xmalloc(slen + 1);
    131  1.44    rillig 	(void)memcpy(name, s, slen);
    132  1.44    rillig 	name[slen] = '\0';
    133  1.44    rillig 
    134  1.65    rillig 	struct filename *fn = xmalloc(sizeof(*fn));
    135  1.44    rillig 	fn->fn_name = name;
    136  1.36    rillig 	fn->fn_len = slen;
    137  1.61    rillig 	fn->fn_id = next_filename_id++;
    138  1.34    rillig 	fn->fn_next = filenames;
    139  1.34    rillig 	filenames = fn;
    140  1.31    rillig 
    141  1.37    rillig 	/* Write the ID of this filename to the output file. */
    142  1.31    rillig 	outclr();
    143  1.31    rillig 	outint(fn->fn_id);
    144  1.31    rillig 	outchar('s');
    145  1.37    rillig 	outstrg(transform_filename(fn->fn_name, fn->fn_len));
    146  1.31    rillig 
    147  1.20    rillig 	return fn->fn_name;
    148   1.1       cgd }
    149   1.1       cgd 
    150  1.26    rillig /* Get the ID of a filename. */
    151   1.1       cgd int
    152  1.37    rillig get_filename_id(const char *s)
    153   1.1       cgd {
    154  1.33    rillig 	const struct filename *fn;
    155   1.1       cgd 
    156  1.33    rillig 	if (s == NULL || (fn = search_filename(s, strlen(s))) == NULL)
    157  1.20    rillig 		return -1;
    158  1.20    rillig 	return fn->fn_id;
    159   1.1       cgd }
    160   1.1       cgd 
    161  1.64    rillig typedef struct memory_pools {
    162  1.64    rillig 	struct memory_pool *pools;
    163  1.64    rillig 	size_t	cap;
    164  1.64    rillig } memory_pools;
    165   1.1       cgd 
    166  1.65    rillig /* Array of memory pools, indexed by mem_block_level. */
    167  1.64    rillig static memory_pools mpools;
    168   1.1       cgd 
    169  1.64    rillig /* The pool for the current expression is independent of any block level. */
    170  1.64    rillig static memory_pool expr_pool;
    171   1.1       cgd 
    172  1.64    rillig static void
    173  1.70    rillig mpool_add(memory_pool *pool, struct memory_pool_item item)
    174  1.64    rillig {
    175   1.1       cgd 
    176  1.64    rillig 	if (pool->len >= pool->cap) {
    177  1.64    rillig 		pool->cap = 2 * pool->len + 16;
    178  1.64    rillig 		pool->items = xrealloc(pool->items,
    179  1.64    rillig 		    sizeof(*pool->items) * pool->cap);
    180  1.64    rillig 	}
    181  1.64    rillig 	pool->items[pool->len++] = item;
    182  1.64    rillig }
    183   1.1       cgd 
    184  1.73    rillig #ifdef DEBUG_MEM
    185  1.73    rillig static void
    186  1.73    rillig debug_memory_pool_item(const struct memory_pool_item *item)
    187  1.73    rillig {
    188  1.73    rillig 	void *p = item->p;
    189  1.73    rillig 	size_t size = item->size;
    190  1.73    rillig 	const char *descr = item->descr;
    191  1.73    rillig 
    192  1.73    rillig 	if (strcmp(descr, "string") == 0) {
    193  1.73    rillig 		const char *str = p;
    194  1.73    rillig 		debug_step("%s: freeing string '%s'", __func__, str);
    195  1.73    rillig 	} else if (strcmp(descr, "sym") == 0) {
    196  1.73    rillig 		const sym_t *sym = p;
    197  1.73    rillig 		debug_step("%s: freeing symbol '%s'", __func__, sym->s_name);
    198  1.73    rillig 	} else if (strcmp(descr, "type") == 0) {
    199  1.73    rillig 		const type_t *tp = p;
    200  1.73    rillig 		debug_step("%s: freeing type '%s'", __func__, type_name(tp));
    201  1.73    rillig 	} else if (strcmp(descr, "tnode") == 0) {
    202  1.73    rillig 		const tnode_t *tn = p;
    203  1.73    rillig 		debug_step("%s: freeing node '%s' with type '%s'",
    204  1.73    rillig 		    __func__, op_name(tn->tn_op), type_name(tn->tn_type));
    205  1.73    rillig 	} else
    206  1.73    rillig 		debug_step("%s: freeing '%s' with %zu bytes",
    207  1.73    rillig 		    __func__, descr, size);
    208  1.73    rillig }
    209  1.73    rillig #endif
    210  1.73    rillig 
    211  1.64    rillig static void
    212  1.64    rillig mpool_free(memory_pool *pool)
    213   1.1       cgd {
    214  1.59    rillig 
    215  1.70    rillig #ifdef DEBUG_MEM
    216  1.73    rillig 	for (size_t i = pool->len; i-- > 0; )
    217  1.73    rillig 		debug_memory_pool_item(pool->items + i);
    218  1.73    rillig #endif
    219  1.73    rillig 
    220  1.73    rillig 	for (size_t i = pool->len; i-- > 0;) {
    221  1.73    rillig #ifdef DEBUG_MEM
    222  1.73    rillig 		static void *(*volatile set)(void *, int, size_t) = memset;
    223  1.73    rillig 		set(pool->items[i].p, 'Z', pool->items[i].size);
    224  1.70    rillig #endif
    225  1.73    rillig 		free(pool->items[i].p);
    226  1.70    rillig 	}
    227  1.73    rillig 	pool->len = 0;
    228   1.1       cgd }
    229   1.1       cgd 
    230  1.64    rillig static void *
    231  1.70    rillig #ifdef DEBUG_MEM
    232  1.70    rillig mpool_zero_alloc(memory_pool *pool, size_t size, const char *descr)
    233  1.70    rillig #else
    234  1.64    rillig mpool_zero_alloc(memory_pool *pool, size_t size)
    235  1.70    rillig #endif
    236   1.1       cgd {
    237   1.1       cgd 
    238  1.64    rillig 	void *mem = xmalloc(size);
    239  1.64    rillig 	memset(mem, 0, size);
    240  1.70    rillig #if DEBUG_MEM
    241  1.70    rillig 	mpool_add(pool, (struct memory_pool_item){ mem, size, descr });
    242  1.70    rillig #else
    243  1.70    rillig 	mpool_add(pool, (struct memory_pool_item){ mem });
    244  1.70    rillig #endif
    245  1.64    rillig 	return mem;
    246   1.1       cgd }
    247   1.1       cgd 
    248  1.64    rillig static memory_pool *
    249  1.64    rillig mpool_at(size_t level)
    250   1.1       cgd {
    251   1.1       cgd 
    252  1.64    rillig 	if (level >= mpools.cap) {
    253  1.64    rillig 		size_t prev_cap = mpools.cap;
    254  1.64    rillig 		mpools.cap = level + 16;
    255  1.64    rillig 		mpools.pools = xrealloc(mpools.pools,
    256  1.64    rillig 		    sizeof(*mpools.pools) * mpools.cap);
    257  1.64    rillig 		for (size_t i = prev_cap; i < mpools.cap; i++)
    258  1.64    rillig 			mpools.pools[i] = (memory_pool){ NULL, 0, 0 };
    259  1.64    rillig 	}
    260  1.64    rillig 	return mpools.pools + level;
    261   1.1       cgd }
    262   1.1       cgd 
    263   1.5     lukem 
    264  1.70    rillig /* Allocate memory associated with the level, initialized with zero. */
    265  1.70    rillig #ifdef DEBUG_MEM
    266  1.70    rillig void *
    267  1.70    rillig level_zero_alloc(size_t level, size_t size, const char *descr)
    268  1.70    rillig {
    269  1.70    rillig 
    270  1.71    rillig 	debug_step("%s: %s at level %zu", __func__, descr, level);
    271  1.70    rillig 	return mpool_zero_alloc(mpool_at(level), size, descr);
    272  1.70    rillig }
    273  1.70    rillig #else
    274   1.1       cgd void *
    275  1.70    rillig (level_zero_alloc)(size_t level, size_t size)
    276   1.1       cgd {
    277   1.5     lukem 
    278  1.70    rillig 	return mpool_zero_alloc(mpool_at(level), size);
    279   1.1       cgd }
    280  1.70    rillig #endif
    281   1.1       cgd 
    282  1.60    rillig /* Allocate memory that is freed at the end of the current block. */
    283  1.70    rillig #ifdef DEBUG_MEM
    284   1.1       cgd void *
    285  1.70    rillig block_zero_alloc(size_t size, const char *descr)
    286   1.1       cgd {
    287   1.5     lukem 
    288  1.70    rillig 	return level_zero_alloc(mem_block_level, size, descr);
    289   1.1       cgd }
    290  1.70    rillig #else
    291  1.70    rillig void *
    292  1.70    rillig (block_zero_alloc)(size_t size)
    293  1.70    rillig {
    294  1.70    rillig 
    295  1.70    rillig 	return (level_zero_alloc)(mem_block_level, size);
    296  1.70    rillig }
    297  1.70    rillig #endif
    298   1.1       cgd 
    299   1.1       cgd void
    300  1.60    rillig level_free_all(size_t level)
    301   1.1       cgd {
    302   1.5     lukem 
    303  1.73    rillig 	debug_step("+ %s %zu", __func__, level);
    304  1.73    rillig 	debug_indent_inc();
    305  1.64    rillig 	mpool_free(mpool_at(level));
    306  1.73    rillig 	debug_leave();
    307   1.1       cgd }
    308   1.1       cgd 
    309  1.60    rillig /* Allocate memory that is freed at the end of the current expression. */
    310  1.70    rillig #if DEBUG_MEM
    311   1.1       cgd void *
    312  1.70    rillig expr_zero_alloc(size_t s, const char *descr)
    313   1.1       cgd {
    314   1.5     lukem 
    315  1.70    rillig 	return mpool_zero_alloc(&expr_pool, s, descr);
    316   1.1       cgd }
    317  1.70    rillig #else
    318  1.70    rillig void *
    319  1.70    rillig (expr_zero_alloc)(size_t size)
    320  1.70    rillig {
    321  1.70    rillig 
    322  1.70    rillig 	return mpool_zero_alloc(&expr_pool, size);
    323  1.70    rillig }
    324  1.70    rillig #endif
    325   1.1       cgd 
    326  1.45    rillig static bool
    327  1.66    rillig str_ends_with(const char *haystack, const char *needle)
    328  1.45    rillig {
    329  1.45    rillig 	size_t hlen = strlen(haystack);
    330  1.45    rillig 	size_t nlen = strlen(needle);
    331  1.45    rillig 
    332  1.45    rillig 	return nlen <= hlen &&
    333  1.45    rillig 	       memcmp(haystack + hlen - nlen, needle, nlen) == 0;
    334  1.45    rillig }
    335  1.45    rillig 
    336  1.41    rillig /*
    337  1.41    rillig  * Return a freshly allocated tree node that is freed at the end of the
    338  1.41    rillig  * current expression.
    339  1.56    rillig  *
    340  1.56    rillig  * The node records whether it comes from a system file, which makes strict
    341  1.56    rillig  * bool mode less restrictive.
    342  1.41    rillig  */
    343   1.1       cgd tnode_t *
    344  1.60    rillig expr_alloc_tnode(void)
    345   1.1       cgd {
    346  1.70    rillig 	tnode_t *tn = expr_zero_alloc(sizeof(*tn), "tnode");
    347  1.45    rillig 	/*
    348  1.45    rillig 	 * files named *.c that are different from the main translation unit
    349  1.45    rillig 	 * typically contain generated code that cannot be influenced, such
    350  1.45    rillig 	 * as a flex lexer or a yacc parser.
    351  1.45    rillig 	 */
    352  1.55    rillig 	tn->tn_sys = in_system_header ||
    353  1.55    rillig 		     (curr_pos.p_file != csrc_pos.p_file &&
    354  1.66    rillig 		      str_ends_with(curr_pos.p_file, ".c"));
    355  1.25    rillig 	return tn;
    356   1.1       cgd }
    357   1.1       cgd 
    358  1.26    rillig /* Free all memory which is allocated by the current expression. */
    359   1.1       cgd void
    360  1.41    rillig expr_free_all(void)
    361   1.1       cgd {
    362   1.5     lukem 
    363  1.69    rillig 	debug_step("%s", __func__);
    364  1.64    rillig 	mpool_free(&expr_pool);
    365   1.1       cgd }
    366   1.1       cgd 
    367   1.1       cgd /*
    368   1.1       cgd  * Save the memory which is used by the current expression. This memory
    369  1.64    rillig  * is not freed by the next expr_free_all() call. The returned value can be
    370   1.1       cgd  * used to restore the memory.
    371   1.1       cgd  */
    372  1.64    rillig memory_pool
    373  1.41    rillig expr_save_memory(void)
    374   1.1       cgd {
    375   1.1       cgd 
    376  1.64    rillig 	memory_pool saved_pool = expr_pool;
    377  1.64    rillig 	expr_pool = (memory_pool){ NULL, 0, 0 };
    378  1.64    rillig 	return saved_pool;
    379   1.1       cgd }
    380   1.1       cgd 
    381   1.1       cgd /*
    382  1.47    rillig  * Free all memory used for the current expression and restore the memory used
    383  1.47    rillig  * by a previous expression and saved by expr_save_memory(). The next call to
    384  1.41    rillig  * expr_free_all() frees the restored memory.
    385   1.1       cgd  */
    386   1.1       cgd void
    387  1.64    rillig expr_restore_memory(memory_pool saved_pool)
    388   1.1       cgd {
    389   1.5     lukem 
    390  1.41    rillig 	expr_free_all();
    391  1.64    rillig 	free(expr_pool.items);
    392  1.64    rillig 	expr_pool = saved_pool;
    393   1.1       cgd }
    394