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/target.c | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) (limited to 'gdb/target.c') diff --git a/gdb/target.c b/gdb/target.c index e526bcc..9b63ab7 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -46,6 +46,7 @@ #include "top.h" #include "event-top.h" #include +#include "byte-vector.h" static void target_info (char *, int); @@ -1284,9 +1285,6 @@ memory_xfer_partial (struct target_ops *ops, enum target_object object, } else { - gdb_byte *buf; - struct cleanup *old_chain; - /* A large write request is likely to be partially satisfied by memory_xfer_partial_1. We will continually malloc and free a copy of the entire write request for breakpoint @@ -1295,15 +1293,10 @@ memory_xfer_partial (struct target_ops *ops, enum target_object object, to mitigate this. */ len = std::min (ops->to_get_memory_xfer_limit (ops), len); - buf = (gdb_byte *) xmalloc (len); - old_chain = make_cleanup (xfree, buf); - memcpy (buf, writebuf, len); - - breakpoint_xfer_memory (NULL, buf, writebuf, memaddr, len); - res = memory_xfer_partial_1 (ops, object, NULL, buf, memaddr, len, + gdb::byte_vector buf (writebuf, writebuf + len); + breakpoint_xfer_memory (NULL, buf.data (), writebuf, memaddr, len); + res = memory_xfer_partial_1 (ops, object, NULL, buf.data (), memaddr, len, xfered_len); - - do_cleanups (old_chain); } return res; @@ -2439,9 +2432,7 @@ simple_search_memory (struct target_ops *ops, #define SEARCH_CHUNK_SIZE 16000 const unsigned chunk_size = SEARCH_CHUNK_SIZE; /* Buffer to hold memory contents for searching. */ - gdb_byte *search_buf; unsigned search_buf_size; - struct cleanup *old_cleanups; search_buf_size = chunk_size + pattern_len - 1; @@ -2449,20 +2440,17 @@ simple_search_memory (struct target_ops *ops, if (search_space_len < search_buf_size) search_buf_size = search_space_len; - search_buf = (gdb_byte *) malloc (search_buf_size); - if (search_buf == NULL) - error (_("Unable to allocate memory to perform the search.")); - old_cleanups = make_cleanup (free_current_contents, &search_buf); + gdb::byte_vector search_buf (search_buf_size); /* Prime the search buffer. */ if (target_read (ops, TARGET_OBJECT_MEMORY, NULL, - search_buf, start_addr, search_buf_size) != search_buf_size) + search_buf.data (), start_addr, search_buf_size) + != search_buf_size) { warning (_("Unable to access %s bytes of target " "memory at %s, halting search."), pulongest (search_buf_size), hex_string (start_addr)); - do_cleanups (old_cleanups); return -1; } @@ -2478,15 +2466,14 @@ simple_search_memory (struct target_ops *ops, unsigned nr_search_bytes = std::min (search_space_len, (ULONGEST) search_buf_size); - found_ptr = (gdb_byte *) memmem (search_buf, nr_search_bytes, + found_ptr = (gdb_byte *) memmem (search_buf.data (), nr_search_bytes, pattern, pattern_len); if (found_ptr != NULL) { - CORE_ADDR found_addr = start_addr + (found_ptr - search_buf); + CORE_ADDR found_addr = start_addr + (found_ptr - search_buf.data ()); *found_addrp = found_addr; - do_cleanups (old_cleanups); return 1; } @@ -2507,20 +2494,19 @@ simple_search_memory (struct target_ops *ops, /* Copy the trailing part of the previous iteration to the front of the buffer for the next iteration. */ gdb_assert (keep_len == pattern_len - 1); - memcpy (search_buf, search_buf + chunk_size, keep_len); + memcpy (&search_buf[0], &search_buf[chunk_size], keep_len); nr_to_read = std::min (search_space_len - keep_len, (ULONGEST) chunk_size); if (target_read (ops, TARGET_OBJECT_MEMORY, NULL, - search_buf + keep_len, read_addr, + &search_buf[keep_len], read_addr, nr_to_read) != nr_to_read) { warning (_("Unable to access %s bytes of target " "memory at %s, halting search."), plongest (nr_to_read), hex_string (read_addr)); - do_cleanups (old_cleanups); return -1; } @@ -2530,7 +2516,6 @@ simple_search_memory (struct target_ops *ops, /* Not found. */ - do_cleanups (old_cleanups); return 0; } -- cgit v1.1