From 8268c77870de3c27ef598348f3023ba159fd0fca Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Tue, 27 Jun 2017 11:07:14 +0100 Subject: [PATCH] Eliminate make_cleanup_obstack_free, introduce auto_obstack This commit eliminates make_cleanup_obstack_free, replacing it with a new auto_obstack type that inherits obstack to add cdtors. These changes in the parsers may not be obvious: - obstack_init (&name_obstack); - make_cleanup_obstack_free (&name_obstack); + name_obstack.clear (); Here, the 'name_obstack' variable is a global. The change means that the obstack's contents from a previous parse will stay around until the next parsing starts. I.e., memory won't be reclaimed until then. I don't think that's a problem, these objects don't really grow much at all. The other option I tried was to add a separate type that is like auto_obstack but manages an external obstack, just for those cases. I like the current approach better as that other approach adds more boilerplate and yet another type to learn. gdb/ChangeLog: 2017-06-27 Pedro Alves * c-exp.y (name_obstack): Now an auto_obstack. (yylex): Use auto_obstack::clear. (c_parse): Use auto_obstack::clear instead of reinitializing and freeing the obstack. * c-lang.c (evaluate_subexp_c): Use auto_obstack. * d-exp.y (name_obstack): Now an auto_obstack. (yylex): Use auto_obstack::clear. (d_parse): Use auto_obstack::clear instead of reinitializing and freeing the obstack. * dwarf2loc.c (fetch_const_value_from_synthetic_pointer): Use auto_obstack. * dwarf2read.c (create_addrmap_from_index) (dwarf2_build_psymtabs_hard) (update_enumeration_type_from_children): Likewise. * gdb_obstack.h (auto_obstack): New type. * go-exp.y (name_obstack): Now an auto_obstack. (build_packaged_name): Use auto_obstack::clear. (go_parse): Use auto_obstack::clear instead of reinitializing and freeing the obstack. * linux-tdep.c (linux_make_mappings_corefile_notes): Use auto_obstack. * printcmd.c (printf_wide_c_string, ui_printf): Use auto_obstack. * rust-exp.y (work_obstack): Now an auto_obstack. (rust_parse, rust_lex_tests): Use auto_obstack::clear instead of reinitializing and freeing the obstack. * utils.c (do_obstack_free, make_cleanup_obstack_free): Delete. (host_char_to_target): Use auto_obstack. * utils.h (make_cleanup_obstack_free): Delete declaration. * valprint.c (generic_emit_char, generic_printstr): Use auto_obstack. --- gdb/ChangeLog | 33 +++++++++++++++++++++++++++++++++ gdb/c-exp.y | 10 ++++++---- gdb/c-lang.c | 7 +------ gdb/d-exp.y | 11 ++++++----- gdb/dwarf2loc.c | 7 +------ gdb/dwarf2read.c | 18 ++++-------------- gdb/gdb_obstack.h | 15 +++++++++++++++ gdb/go-exp.y | 9 +++++---- gdb/linux-tdep.c | 7 +------ gdb/printcmd.c | 12 ++---------- gdb/rust-exp.y | 15 +++++++++------ gdb/utils.c | 24 +----------------------- gdb/utils.h | 3 --- gdb/valprint.c | 17 ++++------------- 14 files changed, 88 insertions(+), 100 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 065f33c..2394185 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,36 @@ +2017-06-27 Pedro Alves + + * c-exp.y (name_obstack): Now an auto_obstack. + (yylex): Use auto_obstack::clear. + (c_parse): Use auto_obstack::clear instead of reinitializing and + freeing the obstack. + * c-lang.c (evaluate_subexp_c): Use auto_obstack. + * d-exp.y (name_obstack): Now an auto_obstack. + (yylex): Use auto_obstack::clear. + (d_parse): Use auto_obstack::clear instead of reinitializing and + freeing the obstack. + * dwarf2loc.c (fetch_const_value_from_synthetic_pointer): Use + auto_obstack. + * dwarf2read.c (create_addrmap_from_index) + (dwarf2_build_psymtabs_hard) + (update_enumeration_type_from_children): Likewise. + * gdb_obstack.h (auto_obstack): New type. + * go-exp.y (name_obstack): Now an auto_obstack. + (build_packaged_name): Use auto_obstack::clear. + (go_parse): Use auto_obstack::clear instead of reinitializing and + freeing the obstack. + * linux-tdep.c (linux_make_mappings_corefile_notes): Use + auto_obstack. + * printcmd.c (printf_wide_c_string, ui_printf): Use auto_obstack. + * rust-exp.y (work_obstack): Now an auto_obstack. + (rust_parse, rust_lex_tests): Use auto_obstack::clear instead of + reinitializing and freeing the obstack. + * utils.c (do_obstack_free, make_cleanup_obstack_free): Delete. + (host_char_to_target): Use auto_obstack. + * utils.h (make_cleanup_obstack_free): Delete declaration. + * valprint.c (generic_emit_char, generic_printstr): Use + auto_obstack. + 2017-06-27 Simon Marchi * darwin-nat.c (darwin_check_new_threads): Don't handle dummy diff --git a/gdb/c-exp.y b/gdb/c-exp.y index 283b737..bdcd51f 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -2806,7 +2806,7 @@ static int popping; /* Temporary storage for c_lex; this holds symbol names as they are built up. */ -static struct obstack name_obstack; +auto_obstack name_obstack; /* Classify a NAME token. The contents of the token are in `yylval'. Updates yylval and returns the new token type. BLOCK is the block @@ -3067,7 +3067,7 @@ yylex (void) current = *VEC_index (token_and_value, token_fifo, next_to_examine); ++next_to_examine; - obstack_free (&name_obstack, obstack_base (&name_obstack)); + name_obstack.clear (); checkpoint = 0; if (current.token == FILENAME) search_block = current.value.bval; @@ -3169,6 +3169,9 @@ c_parse (struct parser_state *par_state) gdb_assert (par_state != NULL); pstate = par_state; + /* Note that parsing (within yyparse) freely installs cleanups + assuming they'll be run here (below). */ + back_to = make_cleanup (free_current_contents, &expression_macro_scope); make_cleanup_clear_parser_state (&pstate); @@ -3197,8 +3200,7 @@ c_parse (struct parser_state *par_state) VEC_free (token_and_value, token_fifo); popping = 0; - obstack_init (&name_obstack); - make_cleanup_obstack_free (&name_obstack); + name_obstack.clear (); result = yyparse (); do_cleanups (back_to); diff --git a/gdb/c-lang.c b/gdb/c-lang.c index a6d533d..de8868b 100644 --- a/gdb/c-lang.c +++ b/gdb/c-lang.c @@ -570,15 +570,12 @@ evaluate_subexp_c (struct type *expect_type, struct expression *exp, { int oplen, limit; struct type *type; - struct obstack output; - struct cleanup *cleanup; struct value *result; c_string_type dest_type; const char *dest_charset; int satisfy_expected = 0; - obstack_init (&output); - cleanup = make_cleanup_obstack_free (&output); + auto_obstack output; ++*pos; oplen = longest_to_int (exp->elts[*pos].longconst); @@ -656,7 +653,6 @@ evaluate_subexp_c (struct type *expect_type, struct expression *exp, result = allocate_value (type); else result = value_cstring ("", 0, type); - do_cleanups (cleanup); return result; } @@ -702,7 +698,6 @@ evaluate_subexp_c (struct type *expect_type, struct expression *exp, obstack_object_size (&output), type); } - do_cleanups (cleanup); return result; } break; diff --git a/gdb/d-exp.y b/gdb/d-exp.y index 62df737..d392a5c 100644 --- a/gdb/d-exp.y +++ b/gdb/d-exp.y @@ -1342,7 +1342,7 @@ static int popping; /* Temporary storage for yylex; this holds symbol names as they are built up. */ -static struct obstack name_obstack; +static auto_obstack name_obstack; /* Classify an IDENTIFIER token. The contents of the token are in `yylval'. Updates yylval and returns the new token type. BLOCK is the block @@ -1480,7 +1480,7 @@ yylex (void) first try building up a name until we find the qualified module. */ if (current.token == UNKNOWN_NAME) { - obstack_free (&name_obstack, obstack_base (&name_obstack)); + name_obstack.clear (); obstack_grow (&name_obstack, current.value.sval.ptr, current.value.sval.length); @@ -1533,7 +1533,7 @@ yylex (void) if (current.token != TYPENAME && current.token != '.') goto do_pop; - obstack_free (&name_obstack, obstack_base (&name_obstack)); + name_obstack.clear (); checkpoint = 0; if (current.token == '.') search_block = NULL; @@ -1627,6 +1627,8 @@ d_parse (struct parser_state *par_state) gdb_assert (par_state != NULL); pstate = par_state; + /* Note that parsing (within yyparse) freely installs cleanups + assuming they're run here (below). */ back_to = make_cleanup (null_cleanup, NULL); scoped_restore restore_yydebug = make_scoped_restore (&yydebug, @@ -1639,8 +1641,7 @@ d_parse (struct parser_state *par_state) VEC_free (token_and_value, token_fifo); popping = 0; - obstack_init (&name_obstack); - make_cleanup_obstack_free (&name_obstack); + name_obstack.clear (); result = yyparse (); do_cleanups (back_to); diff --git a/gdb/dwarf2loc.c b/gdb/dwarf2loc.c index 08b0aa2..8506f3e 100644 --- a/gdb/dwarf2loc.c +++ b/gdb/dwarf2loc.c @@ -2121,13 +2121,10 @@ fetch_const_value_from_synthetic_pointer (sect_offset die, LONGEST byte_offset, struct type *type) { struct value *result = NULL; - struct obstack temp_obstack; - struct cleanup *cleanup; const gdb_byte *bytes; LONGEST len; - obstack_init (&temp_obstack); - cleanup = make_cleanup_obstack_free (&temp_obstack); + auto_obstack temp_obstack; bytes = dwarf2_fetch_constant_bytes (die, per_cu, &temp_obstack, &len); if (bytes != NULL) @@ -2144,8 +2141,6 @@ fetch_const_value_from_synthetic_pointer (sect_offset die, LONGEST byte_offset, else result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type)); - do_cleanups (cleanup); - return result; } diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c index 2369d4b..2f70bd2 100644 --- a/gdb/dwarf2read.c +++ b/gdb/dwarf2read.c @@ -3060,13 +3060,11 @@ create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index) { struct gdbarch *gdbarch = get_objfile_arch (objfile); const gdb_byte *iter, *end; - struct obstack temp_obstack; struct addrmap *mutable_map; - struct cleanup *cleanup; CORE_ADDR baseaddr; - obstack_init (&temp_obstack); - cleanup = make_cleanup_obstack_free (&temp_obstack); + auto_obstack temp_obstack; + mutable_map = addrmap_create_mutable (&temp_obstack); iter = index->address_table; @@ -3107,7 +3105,6 @@ create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index) objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map, &objfile->objfile_obstack); - do_cleanups (cleanup); } /* The hash function for strings in the mapped index. This is the same as @@ -6626,7 +6623,6 @@ static void dwarf2_build_psymtabs_hard (struct objfile *objfile) { struct cleanup *back_to, *addrmap_cleanup; - struct obstack temp_obstack; int i; if (dwarf_read_debug) @@ -6649,8 +6645,7 @@ dwarf2_build_psymtabs_hard (struct objfile *objfile) /* Create a temporary address map on a temporary obstack. We later copy this to the final obstack. */ - obstack_init (&temp_obstack); - make_cleanup_obstack_free (&temp_obstack); + auto_obstack temp_obstack; objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack); addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile); @@ -13796,15 +13791,12 @@ update_enumeration_type_from_children (struct die_info *die, struct type *type, struct dwarf2_cu *cu) { - struct obstack obstack; struct die_info *child_die; int unsigned_enum = 1; int flag_enum = 1; ULONGEST mask = 0; - struct cleanup *old_chain; - obstack_init (&obstack); - old_chain = make_cleanup_obstack_free (&obstack); + auto_obstack obstack; for (child_die = die->child; child_die != NULL && child_die->tag; @@ -13849,8 +13841,6 @@ update_enumeration_type_from_children (struct die_info *die, TYPE_UNSIGNED (type) = 1; if (flag_enum) TYPE_FLAG_ENUM (type) = 1; - - do_cleanups (old_chain); } /* Given a DW_AT_enumeration_type die, set its type. We do not diff --git a/gdb/gdb_obstack.h b/gdb/gdb_obstack.h index 3f34d96..b241c82 100644 --- a/gdb/gdb_obstack.h +++ b/gdb/gdb_obstack.h @@ -63,4 +63,19 @@ extern char *obconcat (struct obstack *obstackp, ...) ATTRIBUTE_SENTINEL; extern char *obstack_strdup (struct obstack *obstackp, const char *string); +/* An obstack that frees itself on scope exit. */ +struct auto_obstack : obstack +{ + auto_obstack () + { obstack_init (this); } + + ~auto_obstack () + { obstack_free (this, NULL); } + + /* Free all memory in the obstack but leave it valid for further + allocation. */ + void clear () + { obstack_free (this, obstack_base (this)); } +}; + #endif diff --git a/gdb/go-exp.y b/gdb/go-exp.y index 057e227..f2f3596 100644 --- a/gdb/go-exp.y +++ b/gdb/go-exp.y @@ -1297,7 +1297,7 @@ static int popping; /* Temporary storage for yylex; this holds symbol names as they are built up. */ -static struct obstack name_obstack; +static auto_obstack name_obstack; /* Build "package.name" in name_obstack. For convenience of the caller, the name is NUL-terminated, @@ -1309,7 +1309,7 @@ build_packaged_name (const char *package, int package_len, { struct stoken result; - obstack_free (&name_obstack, obstack_base (&name_obstack)); + name_obstack.clear (); obstack_grow (&name_obstack, package, package_len); obstack_grow_str (&name_obstack, "."); obstack_grow (&name_obstack, name, name_len); @@ -1567,6 +1567,8 @@ go_parse (struct parser_state *par_state) gdb_assert (par_state != NULL); pstate = par_state; + /* Note that parsing (within yyparse) freely installs cleanups + assuming they'll be run here (below). */ back_to = make_cleanup (null_cleanup, NULL); scoped_restore restore_yydebug = make_scoped_restore (&yydebug, @@ -1579,8 +1581,7 @@ go_parse (struct parser_state *par_state) VEC_free (token_and_value, token_fifo); popping = 0; - obstack_init (&name_obstack); - make_cleanup_obstack_free (&name_obstack); + name_obstack.clear (); result = yyparse (); do_cleanups (back_to); diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index 1afa8d7..2792cbd 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -1506,16 +1506,12 @@ linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, char *note_data, int *note_size) { struct cleanup *cleanup; - struct obstack data_obstack, filename_obstack; struct linux_make_mappings_data mapping_data; struct type *long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long"); gdb_byte buf[sizeof (ULONGEST)]; - obstack_init (&data_obstack); - cleanup = make_cleanup_obstack_free (&data_obstack); - obstack_init (&filename_obstack); - make_cleanup_obstack_free (&filename_obstack); + auto_obstack data_obstack, filename_obstack; mapping_data.file_count = 0; mapping_data.data_obstack = &data_obstack; @@ -1548,7 +1544,6 @@ linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, obstack_object_size (&data_obstack)); } - do_cleanups (cleanup); return note_data; } diff --git a/gdb/printcmd.c b/gdb/printcmd.c index f392e6d..a8cc052 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -2255,8 +2255,6 @@ printf_wide_c_string (struct ui_file *stream, const char *format, "wchar_t", NULL, 0); int wcwidth = TYPE_LENGTH (wctype); gdb_byte *buf = (gdb_byte *) alloca (wcwidth); - struct obstack output; - struct cleanup *inner_cleanup; tem = value_as_address (value); @@ -2275,8 +2273,7 @@ printf_wide_c_string (struct ui_file *stream, const char *format, read_memory (tem, str, j); memset (&str[j], 0, wcwidth); - obstack_init (&output); - inner_cleanup = make_cleanup_obstack_free (&output); + auto_obstack output; convert_between_encodings (target_wide_charset (gdbarch), host_charset (), @@ -2285,7 +2282,6 @@ printf_wide_c_string (struct ui_file *stream, const char *format, obstack_grow_str0 (&output, ""); fprintf_filtered (stream, format, obstack_base (&output)); - do_cleanups (inner_cleanup); } /* Subroutine of ui_printf to simplify it. @@ -2531,8 +2527,6 @@ ui_printf (const char *arg, struct ui_file *stream) struct type *wctype = lookup_typename (current_language, gdbarch, "wchar_t", NULL, 0); struct type *valtype; - struct obstack output; - struct cleanup *inner_cleanup; const gdb_byte *bytes; valtype = value_type (val_args[i]); @@ -2542,8 +2536,7 @@ ui_printf (const char *arg, struct ui_file *stream) bytes = value_contents (val_args[i]); - obstack_init (&output); - inner_cleanup = make_cleanup_obstack_free (&output); + auto_obstack output; convert_between_encodings (target_wide_charset (gdbarch), host_charset (), @@ -2554,7 +2547,6 @@ ui_printf (const char *arg, struct ui_file *stream) fprintf_filtered (stream, current_substring, obstack_base (&output)); - do_cleanups (inner_cleanup); } break; case double_arg: diff --git a/gdb/rust-exp.y b/gdb/rust-exp.y index 64b7c55..c7361bc 100644 --- a/gdb/rust-exp.y +++ b/gdb/rust-exp.y @@ -185,7 +185,7 @@ static int unit_testing; /* Obstack for data temporarily allocated during parsing. */ -static struct obstack work_obstack; +static auto_obstack work_obstack; /* Result of parsing. Points into work_obstack. */ @@ -2446,13 +2446,17 @@ int rust_parse (struct parser_state *state) { int result; - struct cleanup *cleanup; - obstack_init (&work_obstack); - cleanup = make_cleanup_obstack_free (&work_obstack); + work_obstack.clear (); + rust_ast = NULL; pstate = state; + + /* Note that parsing (within rustyyparse) freely installs cleanups + assuming they're run here (below). */ + struct cleanup *cleanup = make_cleanup (null_cleanup, NULL); + result = rustyyparse (); if (!result || (parse_completion && rust_ast != NULL)) @@ -2631,7 +2635,7 @@ rust_lex_tests (void) { int i; - obstack_init (&work_obstack); + work_obstack.clear (); unit_testing = 1; rust_lex_test_one ("", 0); @@ -2722,7 +2726,6 @@ rust_lex_tests (void) rust_lex_test_completion (); rust_lex_test_push_back (); - obstack_free (&work_obstack, NULL); unit_testing = 0; } diff --git a/gdb/utils.c b/gdb/utils.c index 3d50ea4c..b66132a 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -166,24 +166,6 @@ make_cleanup_fclose (FILE *file) return make_cleanup (do_fclose_cleanup, file); } -/* Helper function which does the work for make_cleanup_obstack_free. */ - -static void -do_obstack_free (void *arg) -{ - struct obstack *ob = (struct obstack *) arg; - - obstack_free (ob, NULL); -} - -/* Return a new cleanup that frees OBSTACK. */ - -struct cleanup * -make_cleanup_obstack_free (struct obstack *obstack) -{ - return make_cleanup (do_obstack_free, obstack); -} - /* Helper function for make_cleanup_ui_out_redirect_pop. */ static void @@ -1277,13 +1259,10 @@ query (const char *ctlstr, ...) static int host_char_to_target (struct gdbarch *gdbarch, int c, int *target_c) { - struct obstack host_data; char the_char = c; - struct cleanup *cleanups; int result = 0; - obstack_init (&host_data); - cleanups = make_cleanup_obstack_free (&host_data); + auto_obstack host_data; convert_between_encodings (target_charset (gdbarch), host_charset (), (gdb_byte *) &the_char, 1, 1, @@ -1295,7 +1274,6 @@ host_char_to_target (struct gdbarch *gdbarch, int c, int *target_c) *target_c = *(char *) obstack_base (&host_data); } - do_cleanups (cleanups); return result; } diff --git a/gdb/utils.h b/gdb/utils.h index f3e8007..3347c23 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -78,9 +78,6 @@ extern struct cleanup *(make_cleanup_free_section_addr_info extern struct cleanup *make_cleanup_fclose (FILE *file); -struct obstack; -extern struct cleanup *make_cleanup_obstack_free (struct obstack *obstack); - extern struct cleanup *make_cleanup_restore_integer (int *variable); extern struct cleanup *make_cleanup_restore_uinteger (unsigned int *variable); diff --git a/gdb/valprint.c b/gdb/valprint.c index c10dade..1667882 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -2484,8 +2484,6 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream, { enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); - struct obstack wchar_buf, output; - struct cleanup *cleanups; gdb_byte *buf; int need_escape = 0; @@ -2495,8 +2493,7 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream, wchar_iterator iter (buf, TYPE_LENGTH (type), encoding, TYPE_LENGTH (type)); /* This holds the printable form of the wchar_t data. */ - obstack_init (&wchar_buf); - cleanups = make_cleanup_obstack_free (&wchar_buf); + auto_obstack wchar_buf; while (1) { @@ -2543,8 +2540,7 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream, } /* The output in the host encoding. */ - obstack_init (&output); - make_cleanup_obstack_free (&output); + auto_obstack output; convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (), (gdb_byte *) obstack_base (&wchar_buf), @@ -2553,8 +2549,6 @@ generic_emit_char (int c, struct type *type, struct ui_file *stream, obstack_1grow (&output, '\0'); fputs_filtered ((const char *) obstack_base (&output), stream); - - do_cleanups (cleanups); } /* Return the repeat count of the next character/byte in ITER, @@ -2813,7 +2807,6 @@ generic_printstr (struct ui_file *stream, struct type *type, enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); unsigned int i; int width = TYPE_LENGTH (type); - struct obstack wchar_buf, output; struct cleanup *cleanup; int finished = 0; struct converted_character *last; @@ -2885,8 +2878,7 @@ generic_printstr (struct ui_file *stream, struct type *type, /* WCHAR_BUF is the obstack we use to represent the string in wchar_t form. */ - obstack_init (&wchar_buf); - make_cleanup_obstack_free (&wchar_buf); + auto_obstack wchar_buf; /* Print the output string to the obstack. */ print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char, @@ -2896,8 +2888,7 @@ generic_printstr (struct ui_file *stream, struct type *type, obstack_grow_wstr (&wchar_buf, LCST ("...")); /* OUTPUT is where we collect `char's for printing. */ - obstack_init (&output); - make_cleanup_obstack_free (&output); + auto_obstack output; convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (), (gdb_byte *) obstack_base (&wchar_buf), -- 2.7.4