From: Simon Marchi Date: Fri, 2 Jun 2017 21:16:19 +0000 (+0200) Subject: Introduce and use new_breakpoint_from_type X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a5e364af4a3d745bb79cfc9a1d533632d4d04b57;p=binutils-gdb.git Introduce and use new_breakpoint_from_type This is a small preparatory patch to factor out a snippet that appears twice. More kinds of breakpoints will need to be created based on bptype, so I think it's a good idea to centralize the instantiation of breakpoint objects. gdb/ChangeLog: * breakpoint.c (new_breakpoint_from_type): New function. (create_breakpoint_sal): Use new_breakpoint_from_type and unique_ptr. (create_breakpoint): Likewise. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 263d57ff660..7334ae49461 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,10 @@ +2017-06-02 Simon Marchi + + * breakpoint.c (new_breakpoint_from_type): New function. + (create_breakpoint_sal): Use new_breakpoint_from_type and + unique_ptr. + (create_breakpoint): Likewise. + 2017-05-31 Simon Marchi * memattr.c (mem_info_command): Rename to ... diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 150b08ca9d4..d1202906037 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -1176,6 +1176,22 @@ is_tracepoint (const struct breakpoint *b) return is_tracepoint_type (b->type); } +/* Factory function to create an appropriate instance of breakpoint given + TYPE. */ + +static std::unique_ptr +new_breakpoint_from_type (bptype type) +{ + breakpoint *b; + + if (is_tracepoint_type (type)) + b = (breakpoint *) new tracepoint (); + else + b = new breakpoint (); + + return std::unique_ptr (b); +} + /* A helper function that validates that COMMANDS are valid for a breakpoint. This function will throw an exception if a problem is found. */ @@ -9310,22 +9326,9 @@ create_breakpoint_sal (struct gdbarch *gdbarch, int enabled, int internal, unsigned flags, int display_canonical) { - struct breakpoint *b; - struct cleanup *old_chain; + std::unique_ptr b = new_breakpoint_from_type (type); - if (is_tracepoint_type (type)) - { - struct tracepoint *t; - - t = new tracepoint (); - b = &t->base; - } - else - b = new breakpoint (); - - old_chain = make_cleanup (xfree, b); - - init_breakpoint_sal (b, gdbarch, + init_breakpoint_sal (b.get (), gdbarch, sals, std::move (location), filter, cond_string, extra_string, type, disposition, @@ -9333,9 +9336,8 @@ create_breakpoint_sal (struct gdbarch *gdbarch, ops, from_tty, enabled, internal, flags, display_canonical); - discard_cleanups (old_chain); - install_breakpoint (internal, b, 0); + install_breakpoint (internal, b.release (), 0); } /* Add SALS.nelts breakpoints to the breakpoint table. For each @@ -9804,19 +9806,9 @@ create_breakpoint (struct gdbarch *gdbarch, } else { - struct breakpoint *b; - - if (is_tracepoint_type (type_wanted)) - { - struct tracepoint *t; - - t = new tracepoint (); - b = &t->base; - } - else - b = new breakpoint (); + std::unique_ptr b = new_breakpoint_from_type (type_wanted); - init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops); + init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops); b->location = copy_event_location (location); if (parse_extra) @@ -9848,7 +9840,7 @@ create_breakpoint (struct gdbarch *gdbarch, && type_wanted != bp_hardware_breakpoint) || thread != -1) b->pspace = current_program_space; - install_breakpoint (internal, b, 0); + install_breakpoint (internal, b.release (), 0); } if (VEC_length (linespec_sals, canonical.sals) > 1)