aboutsummaryrefslogtreecommitdiff
path: root/gdb/utils.h
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2017-04-30 23:02:30 -0600
committerTom Tromey <tom@tromey.com>2017-08-03 07:59:08 -0600
commit773a1edcd1086fc76a91055bec67e2d14d76940d (patch)
tree4d82515964c139132435bfa57205f5f58c4ece48 /gdb/utils.h
parent0d50bde32b92821c9f1f660d273e6c996d26dc9f (diff)
downloadbinutils-773a1edcd1086fc76a91055bec67e2d14d76940d.zip
binutils-773a1edcd1086fc76a91055bec67e2d14d76940d.tar.gz
binutils-773a1edcd1086fc76a91055bec67e2d14d76940d.tar.bz2
Introduce gdb_argv, a class wrapper for buildargv
This introduces gdb_argv, a class wrapping an "argv" pointer; that is, a pointer to a NULL-terminated array of char*, where both the array and each non-NULL element in the array are xmalloc'd. This patch then changes most users of gdb_buildargv to use gdb_argv instead. ChangeLog 2017-08-03 Tom Tromey <tom@tromey.com> * utils.h (struct gdb_argv_deleter): New. (gdb_argv): New class. * utils.c (gdb_argv::reset): New method. * tracepoint.c (delete_trace_variable_command): Use gdb_argv. * tracefile.c (tsave_command): Use gdb_argv. * top.c (new_ui_command): Use gdb_argv. * symmisc.c (maintenance_print_symbols) (maintenance_print_msymbols, maintenance_expand_symtabs): Use gdb_argv. * symfile.c (symbol_file_command, generic_load) (remove_symbol_file_command): Use gdb_argv. * stack.c (backtrace_command): Use gdb_argv. * source.c (add_path, show_substitute_path_command) (unset_substitute_path_command, set_substitute_path_command): Use gdb_argv. * skip.c (skip_command): Use gdb_argv. Use gdb_buildargv. * ser-mingw.c (pipe_windows_open): Use gdb_argv. * remote.c (extended_remote_run, remote_put_command) (remote_get_command, remote_delete_command): Use gdb_argv. * remote-sim.c (gdbsim_load, gdbsim_create_inferior) (gdbsim_open): Use gdb_argv. * python/py-cmd.c (gdbpy_string_to_argv): Use gdb_argv. * psymtab.c (maintenance_print_psymbols): Use gdb_argv. * procfs.c (procfs_info_proc): Use gdb_argv. * interps.c (interpreter_exec_cmd): Use gdb_argv. * infrun.c (handle_command): Use gdb_argv. * inferior.c (add_inferior_command, clone_inferior_command): Use gdb_argv. * guile/scm-string.c (gdbscm_string_to_argv): Use gdb_argv. * exec.c (exec_file_command): Use gdb_argv. * cli/cli-cmds.c (alias_command): Use gdb_argv. * compile/compile.c (build_argc_argv): Use gdb_argv.
Diffstat (limited to 'gdb/utils.h')
-rw-r--r--gdb/utils.h118
1 files changed, 118 insertions, 0 deletions
diff --git a/gdb/utils.h b/gdb/utils.h
index b9bd6d9..88cab4b 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -86,6 +86,124 @@ extern int parse_pid_to_attach (const char *args);
extern int parse_escape (struct gdbarch *, const char **);
char **gdb_buildargv (const char *);
+
+/* A wrapper for an array of char* that was allocated in the way that
+ 'buildargv' does, and should be freed with 'freeargv'. */
+
+class gdb_argv
+{
+public:
+
+ /* A constructor that initializes to NULL. */
+
+ gdb_argv ()
+ : m_argv (NULL)
+ {
+ }
+
+ /* A constructor that calls buildargv on STR. STR may be NULL, in
+ which case this object is initialized with a NULL array. If
+ buildargv fails due to out-of-memory, call malloc_failure.
+ Therefore, the value is guaranteed to be non-NULL, unless the
+ parameter itself is NULL. */
+
+ explicit gdb_argv (const char *str)
+ : m_argv (NULL)
+ {
+ reset (str);
+ }
+
+ /* A constructor that takes ownership of an existing array. */
+
+ explicit gdb_argv (char **array)
+ : m_argv (array)
+ {
+ }
+
+ gdb_argv (const gdb_argv &) = delete;
+ gdb_argv &operator= (const gdb_argv &) = delete;
+
+ ~gdb_argv ()
+ {
+ freeargv (m_argv);
+ }
+
+ /* Call buildargv on STR, storing the result in this object. Any
+ previous state is freed. STR may be NULL, in which case this
+ object is reset with a NULL array. If buildargv fails due to
+ out-of-memory, call malloc_failure. Therefore, the value is
+ guaranteed to be non-NULL, unless the parameter itself is
+ NULL. */
+
+ void reset (const char *str);
+
+ /* Return the underlying array. */
+
+ char **get ()
+ {
+ return m_argv;
+ }
+
+ /* Return the underlying array, transferring ownership to the
+ caller. */
+
+ char **release ()
+ {
+ char **result = m_argv;
+ m_argv = NULL;
+ return result;
+ }
+
+ /* Return the number of items in the array. */
+
+ int count () const
+ {
+ return countargv (m_argv);
+ }
+
+ /* Index into the array. */
+
+ char *operator[] (int arg)
+ {
+ gdb_assert (m_argv != NULL);
+ return m_argv[arg];
+ }
+
+ /* The iterator type. */
+
+ typedef char **iterator;
+
+ /* Return an iterator pointing to the start of the array. */
+
+ iterator begin ()
+ {
+ return m_argv;
+ }
+
+ /* Return an iterator pointing to the end of the array. */
+
+ iterator end ()
+ {
+ return m_argv + count ();
+ }
+
+ bool operator!= (nullptr_t)
+ {
+ return m_argv != NULL;
+ }
+
+ bool operator== (nullptr_t)
+ {
+ return m_argv == NULL;
+ }
+
+private:
+
+ /* The wrapped array. */
+
+ char **m_argv;
+};
+
/* Cleanup utilities. */