]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: make interp::m_name an `const char *`
authorSimon Marchi <simon.marchi@polymtl.ca>
Thu, 2 Mar 2023 20:32:23 +0000 (15:32 -0500)
committerSimon Marchi <simon.marchi@polymtl.ca>
Tue, 7 Mar 2023 21:30:14 +0000 (16:30 -0500)
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
Reviewed-By: Tom Tromey <tom@tromey.com>
gdb/interps.c
gdb/interps.h

index 12a0048d10209015687afe1cf4d58ca00470d403..ee451f27a639e409b67b9d92eda2d447b5623998 100644 (file)
@@ -82,13 +82,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.  */
@@ -235,7 +233,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;
       }
index 01bec47550ddd92e6e6538512ed383cc2818e280..62f37951ddea36bb64d4e7e3be557d3d4293ed54 100644 (file)
@@ -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 @@ public:
   { 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