Some cleanup removal in dwarf2loc.c

This removes some cleanups and manual allocation handling in
dwarf2loc.c with std::vector.  Note that this patch has a case where
the vector would normally fall into the "use gdb::unique_ptr"
guidelines -- but here because the vector is immediately initialized,
I moved the initialization into the constructor call, for further
code savings.

2016-10-21  Tom Tromey  <tom@tromey.com>

	* dwarf2loc.c: Include <vector>.
	(read_pieced_value, write_pieced_value)
	(dwarf2_compile_expr_to_ax): Use std::vector.
This commit is contained in:
Tom Tromey 2016-09-25 16:17:15 -06:00
parent 67ad9399e2
commit 5841433461
2 changed files with 31 additions and 44 deletions

View File

@ -1,3 +1,9 @@
2016-10-21 Tom Tromey <tom@tromey.com>
* dwarf2loc.c: Include <vector>.
(read_pieced_value, write_pieced_value)
(dwarf2_compile_expr_to_ax): Use std::vector.
2016-10-21 Tom Tromey <tom@tromey.com> 2016-10-21 Tom Tromey <tom@tromey.com>
* stack.c (print_stack_frame_to_uiout): Use scoped_restore. * stack.c (print_stack_frame_to_uiout): Use scoped_restore.

View File

@ -39,6 +39,7 @@
#include "dwarf2-frame.h" #include "dwarf2-frame.h"
#include "compile/compile.h" #include "compile/compile.h"
#include <algorithm> #include <algorithm>
#include <vector>
extern int dwarf_always_disassemble; extern int dwarf_always_disassemble;
@ -1697,8 +1698,7 @@ read_pieced_value (struct value *v)
struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v)); struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
size_t type_len; size_t type_len;
size_t buffer_size = 0; size_t buffer_size = 0;
gdb_byte *buffer = NULL; std::vector<gdb_byte> buffer;
struct cleanup *cleanup;
int bits_big_endian int bits_big_endian
= gdbarch_bits_big_endian (get_type_arch (value_type (v))); = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
@ -1707,8 +1707,6 @@ read_pieced_value (struct value *v)
_("Should not be able to create a lazy value with " _("Should not be able to create a lazy value with "
"an enclosing type")); "an enclosing type"));
cleanup = make_cleanup (free_current_contents, &buffer);
contents = value_contents_raw (v); contents = value_contents_raw (v);
bits_to_skip = 8 * value_offset (v); bits_to_skip = 8 * value_offset (v);
if (value_bitsize (v)) if (value_bitsize (v))
@ -1754,9 +1752,9 @@ read_pieced_value (struct value *v)
if (buffer_size < this_size) if (buffer_size < this_size)
{ {
buffer_size = this_size; buffer_size = this_size;
buffer = (gdb_byte *) xrealloc (buffer, buffer_size); buffer.reserve (buffer_size);
} }
intermediate_buffer = buffer; intermediate_buffer = buffer.data ();
/* Copy from the source to DEST_BUFFER. */ /* Copy from the source to DEST_BUFFER. */
switch (p->location) switch (p->location)
@ -1779,11 +1777,11 @@ read_pieced_value (struct value *v)
} }
if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset, if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
this_size, buffer, this_size, buffer.data (),
&optim, &unavail)) &optim, &unavail))
{ {
/* Just so garbage doesn't ever shine through. */ /* Just so garbage doesn't ever shine through. */
memset (buffer, 0, this_size); memset (buffer.data (), 0, this_size);
if (optim) if (optim)
mark_value_bits_optimized_out (v, offset, this_size_bits); mark_value_bits_optimized_out (v, offset, this_size_bits);
@ -1797,7 +1795,7 @@ read_pieced_value (struct value *v)
read_value_memory (v, offset, read_value_memory (v, offset,
p->v.mem.in_stack_memory, p->v.mem.in_stack_memory,
p->v.mem.addr + source_offset, p->v.mem.addr + source_offset,
buffer, this_size); buffer.data (), this_size);
break; break;
case DWARF_VALUE_STACK: case DWARF_VALUE_STACK:
@ -1855,8 +1853,6 @@ read_pieced_value (struct value *v)
offset += this_size_bits; offset += this_size_bits;
} }
do_cleanups (cleanup);
} }
static void static void
@ -1871,8 +1867,7 @@ write_pieced_value (struct value *to, struct value *from)
struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to)); struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
size_t type_len; size_t type_len;
size_t buffer_size = 0; size_t buffer_size = 0;
gdb_byte *buffer = NULL; std::vector<gdb_byte> buffer;
struct cleanup *cleanup;
int bits_big_endian int bits_big_endian
= gdbarch_bits_big_endian (get_type_arch (value_type (to))); = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
@ -1882,8 +1877,6 @@ write_pieced_value (struct value *to, struct value *from)
return; return;
} }
cleanup = make_cleanup (free_current_contents, &buffer);
contents = value_contents (from); contents = value_contents (from);
bits_to_skip = 8 * value_offset (to); bits_to_skip = 8 * value_offset (to);
if (value_bitsize (to)) if (value_bitsize (to))
@ -1936,9 +1929,9 @@ write_pieced_value (struct value *to, struct value *from)
if (buffer_size < this_size) if (buffer_size < this_size)
{ {
buffer_size = this_size; buffer_size = this_size;
buffer = (gdb_byte *) xrealloc (buffer, buffer_size); buffer.reserve (buffer_size);
} }
source_buffer = buffer; source_buffer = buffer.data ();
need_bitwise = 1; need_bitwise = 1;
} }
@ -1962,7 +1955,7 @@ write_pieced_value (struct value *to, struct value *from)
int optim, unavail; int optim, unavail;
if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset, if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
this_size, buffer, this_size, buffer.data (),
&optim, &unavail)) &optim, &unavail))
{ {
if (optim) if (optim)
@ -1976,7 +1969,7 @@ write_pieced_value (struct value *to, struct value *from)
"bitfield; containing word " "bitfield; containing word "
"is unavailable")); "is unavailable"));
} }
copy_bitwise (buffer, dest_offset_bits, copy_bitwise (buffer.data (), dest_offset_bits,
contents, source_offset_bits, contents, source_offset_bits,
this_size_bits, this_size_bits,
bits_big_endian); bits_big_endian);
@ -1991,10 +1984,10 @@ write_pieced_value (struct value *to, struct value *from)
{ {
/* Only the first and last bytes can possibly have any /* Only the first and last bytes can possibly have any
bits reused. */ bits reused. */
read_memory (p->v.mem.addr + dest_offset, buffer, 1); read_memory (p->v.mem.addr + dest_offset, buffer.data (), 1);
read_memory (p->v.mem.addr + dest_offset + this_size - 1, read_memory (p->v.mem.addr + dest_offset + this_size - 1,
buffer + this_size - 1, 1); &buffer[this_size - 1], 1);
copy_bitwise (buffer, dest_offset_bits, copy_bitwise (buffer.data (), dest_offset_bits,
contents, source_offset_bits, contents, source_offset_bits,
this_size_bits, this_size_bits,
bits_big_endian); bits_big_endian);
@ -2009,8 +2002,6 @@ write_pieced_value (struct value *to, struct value *from)
} }
offset += this_size_bits; offset += this_size_bits;
} }
do_cleanups (cleanup);
} }
/* An implementation of an lval_funcs method to see whether a value is /* An implementation of an lval_funcs method to see whether a value is
@ -3054,9 +3045,8 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
const gdb_byte *op_ptr, const gdb_byte *op_end, const gdb_byte *op_ptr, const gdb_byte *op_end,
struct dwarf2_per_cu_data *per_cu) struct dwarf2_per_cu_data *per_cu)
{ {
struct cleanup *cleanups; int i;
int i, *offsets; std::vector<int> dw_labels, patches;
VEC(int) *dw_labels = NULL, *patches = NULL;
const gdb_byte * const base = op_ptr; const gdb_byte * const base = op_ptr;
const gdb_byte *previous_piece = op_ptr; const gdb_byte *previous_piece = op_ptr;
enum bfd_endian byte_order = gdbarch_byte_order (arch); enum bfd_endian byte_order = gdbarch_byte_order (arch);
@ -3064,14 +3054,7 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
unsigned int addr_size_bits = 8 * addr_size; unsigned int addr_size_bits = 8 * addr_size;
int bits_big_endian = gdbarch_bits_big_endian (arch); int bits_big_endian = gdbarch_bits_big_endian (arch);
offsets = XNEWVEC (int, op_end - op_ptr); std::vector<int> offsets (op_end - op_ptr, -1);
cleanups = make_cleanup (xfree, offsets);
for (i = 0; i < op_end - op_ptr; ++i)
offsets[i] = -1;
make_cleanup (VEC_cleanup (int), &dw_labels);
make_cleanup (VEC_cleanup (int), &patches);
/* By default we are making an address. */ /* By default we are making an address. */
loc->kind = axs_lvalue_memory; loc->kind = axs_lvalue_memory;
@ -3584,8 +3567,8 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
offset = extract_signed_integer (op_ptr, 2, byte_order); offset = extract_signed_integer (op_ptr, 2, byte_order);
op_ptr += 2; op_ptr += 2;
i = ax_goto (expr, aop_goto); i = ax_goto (expr, aop_goto);
VEC_safe_push (int, dw_labels, op_ptr + offset - base); dw_labels.push_back (op_ptr + offset - base);
VEC_safe_push (int, patches, i); patches.push_back (i);
break; break;
case DW_OP_bra: case DW_OP_bra:
@ -3594,8 +3577,8 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
/* Zero extend the operand. */ /* Zero extend the operand. */
ax_zero_ext (expr, addr_size_bits); ax_zero_ext (expr, addr_size_bits);
i = ax_goto (expr, aop_if_goto); i = ax_goto (expr, aop_if_goto);
VEC_safe_push (int, dw_labels, op_ptr + offset - base); dw_labels.push_back (op_ptr + offset - base);
VEC_safe_push (int, patches, i); patches.push_back (i);
break; break;
case DW_OP_nop: case DW_OP_nop:
@ -3704,15 +3687,13 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
} }
/* Patch all the branches we emitted. */ /* Patch all the branches we emitted. */
for (i = 0; i < VEC_length (int, patches); ++i) for (i = 0; i < patches.size (); ++i)
{ {
int targ = offsets[VEC_index (int, dw_labels, i)]; int targ = offsets[dw_labels[i]];
if (targ == -1) if (targ == -1)
internal_error (__FILE__, __LINE__, _("invalid label")); internal_error (__FILE__, __LINE__, _("invalid label"));
ax_label (expr, VEC_index (int, patches, i), targ); ax_label (expr, patches[i], targ);
} }
do_cleanups (cleanups);
} }