From d1b0a7bfd3df05fd2009b62b19b7535bbc6555c0 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 21 Aug 2017 21:51:11 -0600 Subject: [PATCH] Change counted_command_line to a shared_ptr This changes counted_command_line to be a typedef for std::shared_ptr and removes the associated cleanups. In the long run I believe that cmd_list_element should also be changed to use a shared_ptr. gdb/ChangeLog 2017-09-20 Tom Tromey * breakpoint.c (struct counted_command_line): Remove. (breakpoint_commands): Update. (alloc_counted_command_line, incref_counted_command_line) (decref_counted_command_line, do_cleanup_counted_command_line) (make_cleanup_decref_counted_command_line): Remove. (breakpoint_set_commands, commands_command_1, ~bpstats, bpstats) (bpstat_clear_actions, bpstat_do_actions_1, watchpoint_check) (bpstat_stop_status, print_one_breakpoint_location, ~breakpoint) (save_breakpoints): Update. * breakpoint.h (counted_command_line): Now a typedef to shared_ptr. (struct breakpoint) : Now a counted_command_line. (struct bpstats) : Likewise. --- gdb/ChangeLog | 16 ++++++ gdb/breakpoint.c | 130 +++++++---------------------------------------- gdb/breakpoint.h | 9 ++-- 3 files changed, 39 insertions(+), 116 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 8e0bb013c24..56a826027c4 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,19 @@ +2017-09-20 Tom Tromey + + * breakpoint.c (struct counted_command_line): Remove. + (breakpoint_commands): Update. + (alloc_counted_command_line, incref_counted_command_line) + (decref_counted_command_line, do_cleanup_counted_command_line) + (make_cleanup_decref_counted_command_line): Remove. + (breakpoint_set_commands, commands_command_1, ~bpstats, bpstats) + (bpstat_clear_actions, bpstat_do_actions_1, watchpoint_check) + (bpstat_stop_status, print_one_breakpoint_location, ~breakpoint) + (save_breakpoints): Update. + * breakpoint.h (counted_command_line): Now a typedef to + shared_ptr. + (struct breakpoint) : Now a counted_command_line. + (struct bpstats) : Likewise. + 2017-09-20 Tom Tromey * breakpoint.c (struct commands_info, do_map_commands_command): diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 63a7e0a057d..8585f5e8cb8 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -359,21 +359,10 @@ static char *dprintf_channel; has disconnected. */ static int disconnected_dprintf = 1; -/* A reference-counted struct command_line. This lets multiple - breakpoints share a single command list. */ -struct counted_command_line -{ - /* The reference count. */ - int refc; - - /* The command list. */ - struct command_line *commands; -}; - struct command_line * breakpoint_commands (struct breakpoint *b) { - return b->commands ? b->commands->commands : NULL; + return b->commands ? b->commands.get () : NULL; } /* Flag indicating that a command has proceeded the inferior past the @@ -717,64 +706,6 @@ clear_breakpoint_hit_counts (void) b->hit_count = 0; } -/* Allocate a new counted_command_line with reference count of 1. - The new structure owns COMMANDS. */ - -static struct counted_command_line * -alloc_counted_command_line (struct command_line *commands) -{ - struct counted_command_line *result = XNEW (struct counted_command_line); - - result->refc = 1; - result->commands = commands; - - return result; -} - -/* Increment reference count. This does nothing if CMD is NULL. */ - -static void -incref_counted_command_line (struct counted_command_line *cmd) -{ - if (cmd) - ++cmd->refc; -} - -/* Decrement reference count. If the reference count reaches 0, - destroy the counted_command_line. Sets *CMDP to NULL. This does - nothing if *CMDP is NULL. */ - -static void -decref_counted_command_line (struct counted_command_line **cmdp) -{ - if (*cmdp) - { - if (--(*cmdp)->refc == 0) - { - free_command_lines (&(*cmdp)->commands); - xfree (*cmdp); - } - *cmdp = NULL; - } -} - -/* A cleanup function that calls decref_counted_command_line. */ - -static void -do_cleanup_counted_command_line (void *arg) -{ - decref_counted_command_line ((struct counted_command_line **) arg); -} - -/* Create a cleanup that calls decref_counted_command_line on the - argument. */ - -static struct cleanup * -make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp) -{ - return make_cleanup (do_cleanup_counted_command_line, cmdp); -} - /* Return the breakpoint with the specified number, or NULL if the number does not refer to an existing breakpoint. */ @@ -1303,8 +1234,7 @@ breakpoint_set_commands (struct breakpoint *b, { validate_commands_for_breakpoint (b, commands.get ()); - decref_counted_command_line (&b->commands); - b->commands = alloc_counted_command_line (commands.release ()); + b->commands = std::move (commands); observer_notify_breakpoint_modified (b); } @@ -1360,12 +1290,7 @@ static void commands_command_1 (const char *arg, int from_tty, struct command_line *control) { - struct cleanup *cleanups; - struct counted_command_line *cmd = NULL; - - /* If we read command lines from the user, then `info' will hold an - extra reference to the commands that we must clean up. */ - cleanups = make_cleanup_decref_counted_command_line (&cmd); + counted_command_line cmd; std::string new_arg; @@ -1384,10 +1309,8 @@ commands_command_1 (const char *arg, int from_tty, { if (cmd == NULL) { - command_line_up l; - if (control != NULL) - l = copy_command_lines (control->body_list[0]); + cmd = copy_command_lines (control->body_list[0]); else { struct cleanup *old_chain; @@ -1399,25 +1322,21 @@ commands_command_1 (const char *arg, int from_tty, old_chain = make_cleanup (xfree, str); - l = read_command_lines (str, - from_tty, 1, - (is_tracepoint (b) - ? check_tracepoint_command : 0), - b); + cmd = read_command_lines (str, + from_tty, 1, + (is_tracepoint (b) + ? check_tracepoint_command : 0), + b); do_cleanups (old_chain); } - - cmd = alloc_counted_command_line (l.release ()); } /* If a breakpoint was on the list more than once, we don't need to do anything. */ if (b->commands != cmd) { - validate_commands_for_breakpoint (b, cmd->commands); - incref_counted_command_line (cmd); - decref_counted_command_line (&b->commands); + validate_commands_for_breakpoint (b, cmd.get ()); b->commands = cmd; observer_notify_breakpoint_modified (b); } @@ -1425,8 +1344,6 @@ commands_command_1 (const char *arg, int from_tty, if (cmd == NULL) error (_("No breakpoints specified.")); - - do_cleanups (cleanups); } static void @@ -4421,7 +4338,6 @@ bpstats::~bpstats () { if (old_val != NULL) value_free (old_val); - decref_counted_command_line (&commands); if (bp_location_at != NULL) decref_bp_location (&bp_location_at); } @@ -4463,7 +4379,6 @@ bpstats::bpstats (const bpstats &other) release_value (old_val); } incref_bp_location (bp_location_at); - incref_counted_command_line (commands); } /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that @@ -4582,7 +4497,7 @@ bpstat_clear_actions (void) for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next) { - decref_counted_command_line (&bs->commands); + bs->commands = NULL; if (bs->old_val != NULL) { @@ -4661,9 +4576,7 @@ bpstat_do_actions_1 (bpstat *bsp) breakpoint_proceeded = 0; for (; bs != NULL; bs = bs->next) { - struct counted_command_line *ccmd; - struct command_line *cmd; - struct cleanup *this_cmd_tree_chain; + struct command_line *cmd = NULL; /* Take ownership of the BSP's command tree, if it has one. @@ -4675,10 +4588,10 @@ bpstat_do_actions_1 (bpstat *bsp) commands are only executed once, we don't need to copy it; we can clear the pointer in the bpstat, and make sure we free the tree when we're done. */ - ccmd = bs->commands; + counted_command_line ccmd = bs->commands; bs->commands = NULL; - this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd); - cmd = ccmd ? ccmd->commands : NULL; + if (ccmd != NULL) + cmd = ccmd.get (); if (command_line_is_silent (cmd)) { /* The action has been already done by bpstat_stop_status. */ @@ -4695,9 +4608,6 @@ bpstat_do_actions_1 (bpstat *bsp) cmd = cmd->next; } - /* We can free this command tree now. */ - do_cleanups (this_cmd_tree_chain); - if (breakpoint_proceeded) { if (current_ui->async) @@ -5233,7 +5143,7 @@ watchpoint_check (void *p) } /* Make sure the watchpoint's commands aren't executed. */ - decref_counted_command_line (&b->commands); + b->commands = NULL; watchpoint_del_at_next_stop (b); return WP_DELETED; @@ -5723,9 +5633,8 @@ bpstat_stop_status (struct address_space *aspace, if (b->silent) bs->print = 0; bs->commands = b->commands; - incref_counted_command_line (bs->commands); if (command_line_is_silent (bs->commands - ? bs->commands->commands : NULL)) + ? bs->commands.get () : NULL)) bs->print = 0; b->ops->after_condition_true (bs); @@ -6588,7 +6497,7 @@ print_one_breakpoint_location (struct breakpoint *b, } } - l = b->commands ? b->commands->commands : NULL; + l = b->commands ? b->commands.get () : NULL; if (!part_of_multiple && l) { annotate_field (9); @@ -12625,7 +12534,6 @@ static const struct bp_location_ops bp_location_ops = breakpoint::~breakpoint () { - decref_counted_command_line (&this->commands); xfree (this->cond_string); xfree (this->extra_string); xfree (this->filter); @@ -15376,7 +15284,7 @@ save_breakpoints (char *filename, int from_tty, current_uiout->redirect (&fp); TRY { - print_command_lines (current_uiout, tp->commands->commands, 2); + print_command_lines (current_uiout, tp->commands.get (), 2); } CATCH (ex, RETURN_MASK_ALL) { diff --git a/gdb/breakpoint.h b/gdb/breakpoint.h index cbbe0a928a6..6afe4cc5b38 100644 --- a/gdb/breakpoint.h +++ b/gdb/breakpoint.h @@ -657,10 +657,9 @@ enum watchpoint_triggered typedef struct bp_location *bp_location_p; DEF_VEC_P(bp_location_p); -/* A reference-counted struct command_line. This lets multiple - breakpoints share a single command list. This is an implementation +/* A reference-counted struct command_line. This is an implementation detail to the breakpoints module. */ -struct counted_command_line; +typedef std::shared_ptr counted_command_line; /* Some targets (e.g., embedded PowerPC) need two debug registers to set a watchpoint over a memory region. If this flag is true, GDB will use @@ -712,7 +711,7 @@ struct breakpoint /* Chain of command lines to execute when this breakpoint is hit. */ - counted_command_line *commands = NULL; + counted_command_line commands; /* Stack depth (address of frame). If nonzero, break only if fp equals this. */ struct frame_id frame_id = null_frame_id; @@ -1118,7 +1117,7 @@ struct bpstats struct breakpoint *breakpoint_at; /* The associated command list. */ - struct counted_command_line *commands; + counted_command_line commands; /* Old value associated with a watchpoint. */ struct value *old_val; -- 2.30.2