Don't let Bison leak memory except when it complains.

* src/files.h (parser_file_name, spec_verbose_file, spec_graph_file):
(spec_defines_file, dir_prefix): Now char *, not const char *,
since they are freed.
* src/files.c: Likewise.
(all_but_ext, all_but_tab_ext, src_extension, header_extension):
Likewise.
(tr): Now operates in-place.  All uses changed.
(compute_exts_from_gf, compute_exts_from_src): Don't leak temporary
values.
(compute_file_name_parts, compute_output_file_names): Don't store
read-only data in variables that will be freed.
(compute_output_file_names): Free all_but_ext, all_but_tab_ext,
src_extension, and header_extension.
(output_file_names_free): New public function to free
spec_verbose_file, spec_graph_file, spec_defines_file,
parser_file_name, and dir_prefix.
* src/getargs.c (getargs): Don't store read-only data in variables that
will be freed.
* src/main.c (main): Invoke output_file_names_free, code_scanner_free
(which previously existed but was unused), and quotearg_free.
* src/muscle_tab.h (muscle_insert): value arg is now a `char const *'.
* src/muscle_tab.c: Likewise.
(muscle_entry): Make the value char const *,
and add a new storage member that is char * and can be freed.
(muscle_entry_free): New private function.
(muscle_init): Use it instead of free.
(muscle_insert, muscle_grow): Update and use new storage member.
(muscle_code_grow): Free the string passed to muscle_grow
since it's not needed anymore.
* src/parse-gram.y (%union): Make `chars' member a `char const *', and
add a new `char *code' member.
("{...}"): Declare semantic type as code.
* src/scan-code.h (translate_rule_action):
(translate_symbol_action, translate_code, translate_action): Return
`char const *' rather than `char *' since external code should not free
these strings.
* src/scan-code.l: Likewise.
* src/scan-gram.l (<SC_BRACED_CODE>): Use val->code for BRACED_CODE,
which is "{...}" in the parser.
* tests/Makefile.am (maintainer-check-valgrind): Set
VALGRIND_OPTS='--leak-check=full --show-reacheable=yes' before invoking
Valgrind.
* tests/calc.at (_AT_DATA_CALC_Y): fclose the FILE* so Valgrind doesn't
complain.
* tests/testsuite.at (AT_CHECK): Redefine so that running Bison and
expecting a non-zero exit status sets --leak-check=summary and
--show-reachable=no for Valgrind.  Bison unabashedly leaks memory in
this case, and we don't want to hear about it.
This commit is contained in:
Paul Eggert
2006-11-08 20:28:57 +00:00
parent ac564be451
commit eb09565001
14 changed files with 174 additions and 75 deletions

View File

@@ -1,6 +1,6 @@
/* Muscle table manager for Bison.
Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software
Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software
Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
@@ -30,10 +30,13 @@
#include "muscle_tab.h"
#include "getargs.h"
/* A key-value pair, along with storage that can be reclaimed when
this pair is no longer needed. */
typedef struct
{
const char *key;
char *value;
char const *key;
char const *value;
char *storage;
} muscle_entry;
/* An obstack used to create some entries. */
@@ -64,6 +67,14 @@ hash_muscle (const void *x, size_t tablesize)
| Also set up the MUSCLE_OBSTACK. |
`-----------------------------------------------------------------*/
static void
muscle_entry_free (void *entry)
{
muscle_entry *mentry = entry;
free (mentry->storage);
free (mentry);
}
void
muscle_init (void)
{
@@ -71,7 +82,7 @@ muscle_init (void)
obstack_init (&muscle_obstack);
muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
hash_compare_muscles, free);
hash_compare_muscles, muscle_entry_free);
/* Version and input file. */
MUSCLE_INSERT_STRING ("version", VERSION);
@@ -98,7 +109,7 @@ muscle_free (void)
`------------------------------------------------------------*/
void
muscle_insert (const char *key, char *value)
muscle_insert (char const *key, char const *value)
{
muscle_entry probe;
muscle_entry *entry;
@@ -112,6 +123,7 @@ muscle_insert (const char *key, char *value)
entry = xmalloc (sizeof *entry);
entry->key = key;
hash_insert (muscle_table, entry);
entry->storage = NULL;
}
entry->value = value;
}
@@ -138,19 +150,19 @@ muscle_grow (const char *key, const char *val, const char *separator)
entry = xmalloc (sizeof *entry);
entry->key = key;
hash_insert (muscle_table, entry);
entry->value = xstrdup (val);
entry->value = entry->storage = xstrdup (val);
}
else
{
/* Grow the current value. */
char *new_val;
obstack_sgrow (&muscle_obstack, entry->value);
free (entry->value);
free (entry->storage);
obstack_sgrow (&muscle_obstack, separator);
obstack_sgrow (&muscle_obstack, val);
obstack_1grow (&muscle_obstack, 0);
new_val = obstack_finish (&muscle_obstack);
entry->value = xstrdup (new_val);
entry->value = entry->storage = xstrdup (new_val);
obstack_free (&muscle_obstack, new_val);
}
}
@@ -173,6 +185,7 @@ muscle_code_grow (const char *key, const char *val, location loc)
obstack_1grow (&muscle_obstack, 0);
extension = obstack_finish (&muscle_obstack);
muscle_grow (key, extension, "");
obstack_free (&muscle_obstack, extension);
}