From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 1/3] gdb: store internalvars in an std::vector
Date: Mon, 13 Feb 2023 23:21:37 -0500 [thread overview]
Message-ID: <20230214042139.73638-1-simon.marchi@efficios.com> (raw)
Change the storage of internalvars to an std::vector of unique pointers
to internalval. This helps automate memory management, and will help
keep internalvars sorted in a subsequent patch.
I initially tried to use an std::vector<internalval> initially, but some
parts of the code need for the addresses of internalvars to be stable.
Change-Id: I1fca7e7877cc984a3a3432c7639d45e68d437241
---
gdb/value.c | 32 +++++++++++++-------------------
1 file changed, 13 insertions(+), 19 deletions(-)
diff --git a/gdb/value.c b/gdb/value.c
index 7873aeb9558e..e884913abe5a 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -1829,7 +1829,6 @@ union internalvar_data
struct internalvar
{
- struct internalvar *next;
char *name;
/* We support various different kinds of content of an internal variable.
@@ -1841,7 +1840,9 @@ struct internalvar
union internalvar_data u;
};
-static struct internalvar *internalvars;
+using internalvar_up = std::unique_ptr<internalvar>;
+
+static std::vector<internalvar_up> internalvars;
/* If the variable does not already exist create it and give it the
value given. If no value is given then the default is zero. */
@@ -1891,11 +1892,9 @@ init_if_undefined_command (const char* args, int from_tty)
struct internalvar *
lookup_only_internalvar (const char *name)
{
- struct internalvar *var;
-
- for (var = internalvars; var; var = var->next)
+ for (internalvar_up &var : internalvars)
if (strcmp (var->name, name) == 0)
- return var;
+ return var.get ();
return NULL;
}
@@ -1906,12 +1905,11 @@ lookup_only_internalvar (const char *name)
void
complete_internalvar (completion_tracker &tracker, const char *name)
{
- struct internalvar *var;
int len;
len = strlen (name);
- for (var = internalvars; var; var = var->next)
+ for (internalvar_up &var : internalvars)
if (strncmp (var->name, name, len) == 0)
tracker.add_completion (make_unique_xstrdup (var->name));
}
@@ -1922,12 +1920,12 @@ complete_internalvar (completion_tracker &tracker, const char *name)
struct internalvar *
create_internalvar (const char *name)
{
- struct internalvar *var = XNEW (struct internalvar);
+ internalvars.emplace_back (new internalvar);
+ internalvar *var = internalvars.back ().get ();
var->name = xstrdup (name);
var->kind = INTERNALVAR_VOID;
- var->next = internalvars;
- internalvars = var;
+
return var;
}
@@ -2412,8 +2410,6 @@ preserve_one_varobj (struct varobj *varobj, struct objfile *objfile,
void
preserve_values (struct objfile *objfile)
{
- struct internalvar *var;
-
/* Create the hash table. We allocate on the objfile's obstack, since
it is soon to be deleted. */
htab_up copied_types = create_copied_types_hash ();
@@ -2421,8 +2417,8 @@ preserve_values (struct objfile *objfile)
for (const value_ref_ptr &item : value_history)
item->preserve (objfile, copied_types.get ());
- for (var = internalvars; var; var = var->next)
- preserve_one_internalvar (var, objfile, copied_types.get ());
+ for (internalvar_up &var : internalvars)
+ preserve_one_internalvar (var.get (), objfile, copied_types.get ());
/* For the remaining varobj, check that none has type owned by OBJFILE. */
all_root_varobjs ([&copied_types, objfile] (struct varobj *varobj)
@@ -2438,14 +2434,12 @@ static void
show_convenience (const char *ignore, int from_tty)
{
struct gdbarch *gdbarch = get_current_arch ();
- struct internalvar *var;
int varseen = 0;
struct value_print_options opts;
get_user_print_options (&opts);
- for (var = internalvars; var; var = var->next)
+ for (internalvar_up &var : internalvars)
{
-
if (!varseen)
{
varseen = 1;
@@ -2456,7 +2450,7 @@ show_convenience (const char *ignore, int from_tty)
{
struct value *val;
- val = value_of_internalvar (gdbarch, var);
+ val = value_of_internalvar (gdbarch, var.get ());
value_print (val, gdb_stdout, &opts);
}
catch (const gdb_exception_error &ex)
--
2.39.1
next reply other threads:[~2023-02-14 4:21 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-14 4:21 Simon Marchi [this message]
2023-02-14 4:21 ` [PATCH 2/3] gdb: use std::string for internalvar::name Simon Marchi
2023-02-14 4:21 ` [PATCH 3/3] gdb: keep internalvars sorted Simon Marchi
2023-02-14 10:54 ` [PATCH 1/3] gdb: store internalvars in an std::vector Lancelot SIX
2023-02-14 18:41 ` Simon Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230214042139.73638-1-simon.marchi@efficios.com \
--to=simon.marchi@efficios.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).