aboutsummaryrefslogtreecommitdiff
path: root/gdb/parse.c
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2020-12-01 17:22:05 -0700
committerTom Tromey <tom@tromey.com>2020-12-01 17:22:05 -0700
commit77bf7e991150d3fac70294910c027c43ae5789b6 (patch)
tree058346ea2f9e4daafe97d4d1784aaabca585b216 /gdb/parse.c
parente89b3d52936a0492f2886c2a1fc53dd87aaf94a3 (diff)
downloadgdb-77bf7e991150d3fac70294910c027c43ae5789b6.zip
gdb-77bf7e991150d3fac70294910c027c43ae5789b6.tar.gz
gdb-77bf7e991150d3fac70294910c027c43ae5789b6.tar.bz2
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 <tom@tromey.com> * 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. <resize>: New method. (expression_up): Change type.
Diffstat (limited to 'gdb/parse.c')
-rw-r--r--gdb/parse.c49
1 files changed, 28 insertions, 21 deletions
diff --git a/gdb/parse.c b/gdb/parse.c
index c34b608..73d82e6 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);
}
}