From 77bf7e991150d3fac70294910c027c43ae5789b6 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 1 Dec 2020 17:22:05 -0700 Subject: [PATCH] Use new+delete for struct expression In another series I'm working on, it is necessary to manage "struct expression" with new and delete. Because the patch is straightforward and could be extracted, I've done so here. gdb/ChangeLog 2020-12-01 Tom Tromey * parse.c (expr_builder::expr_builder): Initialize expout. (expr_builder::release): Use expression::resize. (expression::expression, expression::~expression) (expression::resize): New methods. (write_exp_elt): Use expression::resize. (prefixify_expression): Update. (increase_expout_size): Use expression::resize. * expression.h (struct expression): Add constructor, destructor. : New method. (expression_up): Change type. --- gdb/ChangeLog | 13 +++++++++++++ gdb/expression.h | 23 +++++++++++++++-------- gdb/parse.c | 49 +++++++++++++++++++++++++++--------------------- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 39637aa92a5..635435cbdea 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,16 @@ +2020-12-01 Tom Tromey + + * parse.c (expr_builder::expr_builder): Initialize expout. + (expr_builder::release): Use expression::resize. + (expression::expression, expression::~expression) + (expression::resize): New methods. + (write_exp_elt): Use expression::resize. + (prefixify_expression): Update. + (increase_expout_size): Use expression::resize. + * expression.h (struct expression): Add constructor, destructor. + : New method. + (expression_up): Change type. + 2020-12-01 Rogerio A. Cardoso * ppc-linux-nat.c: (PPC_DEBUG_FEATURE_DATA_BP_ARCH_31): New define. (region_ok_for_hw_watchpoint): Check if 2nd DAWR is avaliable before diff --git a/gdb/expression.h b/gdb/expression.h index a8bfac17d8b..684274b90b8 100644 --- a/gdb/expression.h +++ b/gdb/expression.h @@ -93,15 +93,22 @@ union exp_element }; struct expression - { - const struct language_defn *language_defn; /* language it was - entered in. */ - struct gdbarch *gdbarch; /* architecture it was parsed in. */ - int nelts; - union exp_element elts[1]; - }; +{ + expression (const struct language_defn *, struct gdbarch *, size_t); + ~expression (); + DISABLE_COPY_AND_ASSIGN (expression); + + void resize (size_t); + + /* Language it was entered in. */ + const struct language_defn *language_defn; + /* Architecture it was parsed in. */ + struct gdbarch *gdbarch; + int nelts = 0; + union exp_element *elts; +}; -typedef gdb::unique_xmalloc_ptr expression_up; +typedef std::unique_ptr expression_up; /* Macros for converting between number of expression elements and bytes to store that many expression elements. */ diff --git a/gdb/parse.c b/gdb/parse.c index c34b6085cd0..73d82e64a87 100644 --- a/gdb/parse.c +++ b/gdb/parse.c @@ -114,13 +114,9 @@ innermost_block_tracker::update (const struct block *b, expr_builder::expr_builder (const struct language_defn *lang, struct gdbarch *gdbarch) : expout_size (10), - expout (XNEWVAR (expression, - (sizeof (expression) - + EXP_ELEM_TO_BYTES (expout_size)))), + expout (new expression (lang, gdbarch, expout_size)), expout_ptr (0) { - expout->language_defn = lang; - expout->gdbarch = gdbarch; } expression_up @@ -131,13 +127,31 @@ expr_builder::release () excess elements. */ expout->nelts = expout_ptr; - expout.reset (XRESIZEVAR (expression, expout.release (), - (sizeof (expression) - + EXP_ELEM_TO_BYTES (expout_ptr)))); + expout->resize (expout_ptr); return std::move (expout); } +expression::expression (const struct language_defn *lang, struct gdbarch *arch, + size_t n) + : language_defn (lang), + gdbarch (arch), + elts (nullptr) +{ + resize (n); +} + +expression::~expression () +{ + xfree (elts); +} + +void +expression::resize (size_t n) +{ + elts = XRESIZEVAR (union exp_element, elts, EXP_ELEM_TO_BYTES (n)); +} + /* This page contains the functions for adding data to the struct expression being constructed. */ @@ -152,9 +166,7 @@ write_exp_elt (struct expr_builder *ps, const union exp_element *expelt) if (ps->expout_ptr >= ps->expout_size) { ps->expout_size *= 2; - ps->expout.reset (XRESIZEVAR (expression, ps->expout.release (), - (sizeof (expression) - + EXP_ELEM_TO_BYTES (ps->expout_size)))); + ps->expout->resize (ps->expout_size); } ps->expout->elts[ps->expout_ptr++] = *expelt; } @@ -721,16 +733,14 @@ int prefixify_expression (struct expression *expr, int last_struct) { gdb_assert (expr->nelts > 0); - int len = sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts); - struct expression *temp; + int len = EXP_ELEM_TO_BYTES (expr->nelts); + struct expression temp (expr->language_defn, expr->gdbarch, expr->nelts); int inpos = expr->nelts, outpos = 0; - temp = (struct expression *) alloca (len); - /* Copy the original expression into temp. */ - memcpy (temp, expr, len); + memcpy (temp.elts, expr->elts, len); - return prefixify_subexp (temp, expr, inpos, outpos, last_struct); + return prefixify_subexp (&temp, expr, inpos, outpos, last_struct); } /* Return the number of exp_elements in the postfix subexpression @@ -1412,10 +1422,7 @@ increase_expout_size (struct expr_builder *ps, size_t lenelt) { ps->expout_size = std::max (ps->expout_size * 2, ps->expout_ptr + lenelt + 10); - ps->expout.reset (XRESIZEVAR (expression, - ps->expout.release (), - (sizeof (struct expression) - + EXP_ELEM_TO_BYTES (ps->expout_size)))); + ps->expout->resize (ps->expout_size); } } -- 2.30.2