From 26fcd5d7572ea1bf0cc697158969749420900e0b Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Fri, 28 Apr 2017 23:34:32 -0600 Subject: Use containers to avoid cleanups This patch introduces the use of various containers -- std::vector, std::string, or gdb::byte_vector -- in several spots in gdb that were using xmalloc and a cleanup. ChangeLog 2017-08-03 Tom Tromey * valops.c (search_struct_method): Use gdb::byte_vector. * valarith.c (value_concat): Use std::vector. * target.c (memory_xfer_partial): Use gdb::byte_vector. (simple_search_memory): Likewise. * printcmd.c (find_string_backward): Use gdb::byte_vector. * mi/mi-main.c (mi_cmd_data_write_memory): Use gdb::byte_vector. * gcore.c (gcore_copy_callback): Use gdb::byte_vector. * elfread.c (elf_rel_plt_read): Use std::string. * cp-valprint.c (cp_print_value): Use gdb::byte_vector. * cli/cli-dump.c (restore_section_callback): Use gdb::byte_vector. --- gdb/valarith.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'gdb/valarith.c') diff --git a/gdb/valarith.c b/gdb/valarith.c index 985233c..9724aca 100644 --- a/gdb/valarith.c +++ b/gdb/valarith.c @@ -697,12 +697,9 @@ value_concat (struct value *arg1, struct value *arg2) if (TYPE_CODE (type2) == TYPE_CODE_STRING || TYPE_CODE (type2) == TYPE_CODE_CHAR) { - struct cleanup *back_to; - count = longest_to_int (value_as_long (inval1)); inval2len = TYPE_LENGTH (type2); - ptr = (char *) xmalloc (count * inval2len); - back_to = make_cleanup (xfree, ptr); + std::vector ptr (count * inval2len); if (TYPE_CODE (type2) == TYPE_CODE_CHAR) { char_type = type2; @@ -711,7 +708,7 @@ value_concat (struct value *arg1, struct value *arg2) value_contents (inval2)); for (idx = 0; idx < count; idx++) { - *(ptr + idx) = inchar; + ptr[idx] = inchar; } } else @@ -720,12 +717,11 @@ value_concat (struct value *arg1, struct value *arg2) for (idx = 0; idx < count; idx++) { - memcpy (ptr + (idx * inval2len), value_contents (inval2), + memcpy (&ptr[idx * inval2len], value_contents (inval2), inval2len); } } - outval = value_string (ptr, count * inval2len, char_type); - do_cleanups (back_to); + outval = value_string (ptr.data (), count * inval2len, char_type); } else if (TYPE_CODE (type2) == TYPE_CODE_BOOL) { @@ -739,8 +735,6 @@ value_concat (struct value *arg1, struct value *arg2) else if (TYPE_CODE (type1) == TYPE_CODE_STRING || TYPE_CODE (type1) == TYPE_CODE_CHAR) { - struct cleanup *back_to; - /* We have two character strings to concatenate. */ if (TYPE_CODE (type2) != TYPE_CODE_STRING && TYPE_CODE (type2) != TYPE_CODE_CHAR) @@ -749,31 +743,29 @@ value_concat (struct value *arg1, struct value *arg2) } inval1len = TYPE_LENGTH (type1); inval2len = TYPE_LENGTH (type2); - ptr = (char *) xmalloc (inval1len + inval2len); - back_to = make_cleanup (xfree, ptr); + std::vector ptr (inval1len + inval2len); if (TYPE_CODE (type1) == TYPE_CODE_CHAR) { char_type = type1; - *ptr = (char) unpack_long (type1, value_contents (inval1)); + ptr[0] = (char) unpack_long (type1, value_contents (inval1)); } else { char_type = TYPE_TARGET_TYPE (type1); - memcpy (ptr, value_contents (inval1), inval1len); + memcpy (ptr.data (), value_contents (inval1), inval1len); } if (TYPE_CODE (type2) == TYPE_CODE_CHAR) { - *(ptr + inval1len) = + ptr[inval1len] = (char) unpack_long (type2, value_contents (inval2)); } else { - memcpy (ptr + inval1len, value_contents (inval2), inval2len); + memcpy (&ptr[inval1len], value_contents (inval2), inval2len); } - outval = value_string (ptr, inval1len + inval2len, char_type); - do_cleanups (back_to); + outval = value_string (ptr.data (), inval1len + inval2len, char_type); } else if (TYPE_CODE (type1) == TYPE_CODE_BOOL) { -- cgit v1.1