From: Simon Marchi via Gdb-patches <gdb-patches@sourceware.org>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@polymtl.ca>
Subject: [PATCH 2/3] gdb: make interp::m_name an `const char *`
Date: Thu, 2 Mar 2023 15:32:23 -0500 [thread overview]
Message-ID: <20230302203224.118345-3-simon.marchi@efficios.com> (raw)
In-Reply-To: <20230302203224.118345-1-simon.marchi@efficios.com>
From: Simon Marchi <simon.marchi@polymtl.ca>
I realized that the memory for interp names does not need to be
allocated. The name used to register interp factory functions is always
a literal string, so has static storage duration. If we change
interp_lookup to pass that name instead of the string that it receives
as a parameter (which does not always have static storage duration),
then interps can simply store pointers to the name.
So, change interp_lookup to pass `factory.name` rather than `name`.
Change interp::m_name to be a `const char *` rather than an std::string.
Change-Id: I0474d1f7b3512e7d172ccd73018aea927def3188
---
gdb/interps.c | 8 +++-----
gdb/interps.h | 12 ++++++------
2 files changed, 9 insertions(+), 11 deletions(-)
diff --git a/gdb/interps.c b/gdb/interps.c
index 9517e57540d3..5b964aebbbd6 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -79,13 +79,11 @@ static struct interp *interp_lookup_existing (struct ui *ui,
const char *name);
interp::interp (const char *name)
- : m_name (make_unique_xstrdup (name))
+ : m_name (name)
{
}
-interp::~interp ()
-{
-}
+interp::~interp () = default;
/* An interpreter factory. Maps an interpreter name to the factory
function that instantiates an interpreter by that name. */
@@ -232,7 +230,7 @@ interp_lookup (struct ui *ui, const char *name)
for (const interp_factory &factory : interpreter_factories)
if (strcmp (factory.name, name) == 0)
{
- interp = factory.func (name);
+ interp = factory.func (factory.name);
interp_add (ui, interp);
return interp;
}
diff --git a/gdb/interps.h b/gdb/interps.h
index 01bec47550dd..62f37951ddea 100644
--- a/gdb/interps.h
+++ b/gdb/interps.h
@@ -32,7 +32,9 @@ typedef struct interp *(*interp_factory_func) (const char *name);
/* Each interpreter kind (CLI, MI, etc.) registers itself with a call
to this function, passing along its name, and a pointer to a
function that creates a new instance of an interpreter with that
- name. */
+ name.
+
+ The memory for NAME must have static storage duration. */
extern void interp_factory_register (const char *name,
interp_factory_func func);
@@ -76,13 +78,11 @@ class interp
{ return false; }
const char *name () const
- {
- return m_name.get ();
- }
+ { return m_name; }
private:
- /* This is the name in "-i=" and "set interpreter". */
- gdb::unique_xmalloc_ptr<char> m_name;
+ /* The memory for this is static, it comes from literal strings (e.g. "cli"). */
+ const char *m_name;
public:
/* Interpreters are stored in a linked list, this is the next
--
2.39.2
next prev parent reply other threads:[~2023-03-02 20:33 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-02 20:32 [PATCH 0/3] Some minor interps tweaks Simon Marchi via Gdb-patches
2023-03-02 20:32 ` [PATCH 1/3] gdb: make get_interp_info return a reference Simon Marchi via Gdb-patches
2023-03-07 19:27 ` Tom Tromey
2023-03-07 20:34 ` Simon Marchi via Gdb-patches
2023-03-07 20:45 ` Tom Tromey
2023-03-07 20:52 ` Simon Marchi via Gdb-patches
2023-03-07 21:29 ` Tom Tromey
2023-03-07 21:31 ` Simon Marchi via Gdb-patches
2023-03-02 20:32 ` Simon Marchi via Gdb-patches [this message]
2023-03-07 19:29 ` [PATCH 2/3] gdb: make interp::m_name an `const char *` Tom Tromey
2023-03-02 20:32 ` [PATCH 3/3] gdb: initialize interp::next Simon Marchi via Gdb-patches
2023-03-07 19:29 ` Tom Tromey
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=20230302203224.118345-3-simon.marchi@efficios.com \
--to=gdb-patches@sourceware.org \
--cc=simon.marchi@efficios.com \
--cc=simon.marchi@polymtl.ca \
/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