From bac51ab78d4bee5273c7d6306ff6d41545fd5628 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 23 Sep 2020 09:32:54 -0600 Subject: Avoid manual memory management of argv arrays in gdb/compile This changes gdb/compile to use gdb_argv directly, rather than manually managing the arrays itself. A few new helpers are added to gdb_argv. gdb/ChangeLog 2020-09-23 Tom Tromey * utils.h (class gdb_argv): Add move operators. : New methods. * compile/compile.c (build_argc_argv): Remove. (compile_args_argc): Remove. (compile_args_argv): Change type. (set_compile_args): Simplify. (append_args): Remove. (filter_args): Remove argcp parameter. (get_args): Return gdb_argv. Simplify. (compile_to_object): Update. --- gdb/utils.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'gdb/utils.h') diff --git a/gdb/utils.h b/gdb/utils.h index 9a235b9..6948908 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -165,6 +165,20 @@ public: gdb_argv (const gdb_argv &) = delete; gdb_argv &operator= (const gdb_argv &) = delete; + gdb_argv &operator= (gdb_argv &&other) + { + freeargv (m_argv); + m_argv = other.m_argv; + other.m_argv = nullptr; + return *this; + } + + gdb_argv (gdb_argv &&other) + { + m_argv = other.m_argv; + other.m_argv = nullptr; + } + ~gdb_argv () { freeargv (m_argv); @@ -218,6 +232,35 @@ public: return gdb::array_view (this->get (), this->count ()); } + /* Append arguments to this array. */ + void append (gdb_argv &&other) + { + int size = count (); + int argc = other.count (); + m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1)); + + for (int argi = 0; argi < argc; argi++) + { + /* Transfer ownership of the string. */ + m_argv[size++] = other.m_argv[argi]; + /* Ensure that destruction of OTHER works correctly. */ + other.m_argv[argi] = nullptr; + } + m_argv[size] = nullptr; + } + + /* Append arguments to this array. */ + void append (const gdb_argv &other) + { + int size = count (); + int argc = other.count (); + m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1)); + + for (int argi = 0; argi < argc; argi++) + m_argv[size++] = xstrdup (other.m_argv[argi]); + m_argv[size] = nullptr; + } + /* The iterator type. */ typedef char **iterator; -- cgit v1.1