Let commands free "name"
authorTom Tromey <tom@tromey.com>
Fri, 15 Nov 2019 23:56:20 +0000 (16:56 -0700)
committerTom Tromey <tom@tromey.com>
Tue, 26 Nov 2019 21:20:30 +0000 (14:20 -0700)
This adds a "name_allocated" field to cmd_list_element, so that
commands can own their "name" when necessary.  Then, this changes a
few spots in gdb that currently free the name by hand to instead use
this facility.

gdb/ChangeLog
2019-11-26  Tom Tromey  <tom@tromey.com>

* python/py-function.c (fnpy_init): Update.
* value.h (add_internal_function): Adjust declaration.
* value.c (function_destroyer): Remove.
(do_add_internal_function): Don't set destroyer or copy name.
(add_internal_function): Take unique_xmalloc_ptr<char> for name.
Set name_allocated.
* python/py-cmd.c (cmdpy_destroyer): Don't free "name".
(cmdpy_init): Set name_allocated.
* cli/cli-decode.h (struct cmd_list_element) <name_allocated>: New
member.
(~cmd_list_element): Free "name" if needed.

Change-Id: Ie1435cea5bbf4bd92056125f112917c607cbb761

gdb/ChangeLog
gdb/cli/cli-decode.h
gdb/python/py-cmd.c
gdb/python/py-function.c
gdb/value.c
gdb/value.h

index 3d8d582d651fb5c99c878af8a1d9cb6dc9c0e0da..d2e9adf34305bc81ba6a3d305de95db42f913f19 100644 (file)
@@ -1,3 +1,17 @@
+2019-11-26  Tom Tromey  <tom@tromey.com>
+
+       * python/py-function.c (fnpy_init): Update.
+       * value.h (add_internal_function): Adjust declaration.
+       * value.c (function_destroyer): Remove.
+       (do_add_internal_function): Don't set destroyer or copy name.
+       (add_internal_function): Take unique_xmalloc_ptr<char> for name.
+       Set name_allocated.
+       * python/py-cmd.c (cmdpy_destroyer): Don't free "name".
+       (cmdpy_init): Set name_allocated.
+       * cli/cli-decode.h (struct cmd_list_element) <name_allocated>: New
+       member.
+       (~cmd_list_element): Free "name" if needed.
+
 2019-11-26  Tom Tromey  <tom@tromey.com>
 
        * value.h (add_internal_function): Add new overload.  Move
index 2ec4a97d8138d646590daf6d5056c97bcebdb1ae..8ea40597b0795830f8021947a7625a8ceedda33b 100644 (file)
@@ -55,6 +55,7 @@ struct cmd_list_element
        deprecated_warn_user (0),
        malloced_replacement (0),
        doc_allocated (0),
+       name_allocated (0),
        hook_in (0),
        allow_unknown (0),
        abbrev_flag (0),
@@ -69,6 +70,8 @@ struct cmd_list_element
     {
       if (doc && doc_allocated)
        xfree ((char *) doc);
+      if (name_allocated)
+       xfree ((char *) name);
     }
 
     DISABLE_COPY_AND_ASSIGN (cmd_list_element);
@@ -109,6 +112,10 @@ struct cmd_list_element
 
     unsigned int doc_allocated : 1;
 
+    /* Set if the name field should be xfree'd.  */
+
+    unsigned int name_allocated : 1;
+
     /* Flag that specifies if this command is already running its hook.  */
     /* Prevents the possibility of hook recursion.  */
     unsigned int hook_in : 1;
index e3497d6f928eb705cf6b7f9203b66992824af4eb..38973401a8fc0de1ceb7275e9e02f02fed098129 100644 (file)
@@ -98,8 +98,7 @@ cmdpy_destroyer (struct cmd_list_element *self, void *context)
   gdbpy_ref<cmdpy_object> cmd ((cmdpy_object *) context);
   cmd->command = NULL;
 
-  /* We allocated the name and perhaps the prefix name.  */
-  xfree ((char *) self->name);
+  /* We may have allocated the prefix name.  */
   xfree ((char *) self->prefixname);
 }
 
@@ -562,6 +561,7 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
       cmd->func = cmdpy_function;
       cmd->destroyer = cmdpy_destroyer;
       cmd->doc_allocated = 1;
+      cmd->name_allocated = 1;
 
       obj->command = cmd;
       set_cmd_context (cmd, self_ref.release ());
index 1c4588780e241ead98411c04435b7a48ada3cf78..d94610573ef6ef2720da8c9850ca41ee51eb1693 100644 (file)
@@ -127,8 +127,8 @@ fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
   if (! docstring)
     docstring.reset (xstrdup (_("This function is not documented.")));
 
-  add_internal_function (name, std::move (docstring), fnpy_call,
-                        self_ref.release ());
+  add_internal_function (make_unique_xstrdup (name), std::move (docstring),
+                        fnpy_call, self_ref.release ());
   return 0;
 }
 
index 8e22ac7f8c1bce79605e7679ea2a4a3d5d214314..57e62b9175f4addbc3596b7ddfe92c5f7de745b1 100644 (file)
@@ -2421,31 +2421,19 @@ function_command (const char *command, int from_tty)
   /* Do nothing.  */
 }
 
-/* Clean up if an internal function's command is destroyed.  */
-static void
-function_destroyer (struct cmd_list_element *self, void *ignore)
-{
-  xfree ((char *) self->name);
-}
-
 /* Helper function that does the work for add_internal_function.  */
 
 static struct cmd_list_element *
 do_add_internal_function (const char *name, const char *doc,
                          internal_function_fn handler, void *cookie)
 {
-  struct cmd_list_element *cmd;
   struct internal_function *ifn;
   struct internalvar *var = lookup_internalvar (name);
 
   ifn = create_internal_function (name, handler, cookie);
   set_internalvar_function (var, ifn);
 
-  cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
-                &functionlist);
-  cmd->destroyer = function_destroyer;
-
-  return cmd;
+  return add_cmd (name, no_class, function_command, doc, &functionlist);
 }
 
 /* See value.h.  */
@@ -2460,13 +2448,16 @@ add_internal_function (const char *name, const char *doc,
 /* See value.h.  */
 
 void
-add_internal_function (const char *name, gdb::unique_xmalloc_ptr<char> &&doc,
+add_internal_function (gdb::unique_xmalloc_ptr<char> &&name,
+                      gdb::unique_xmalloc_ptr<char> &&doc,
                       internal_function_fn handler, void *cookie)
 {
   struct cmd_list_element *cmd
-    = do_add_internal_function (name, doc.get (), handler, cookie);
+    = do_add_internal_function (name.get (), doc.get (), handler, cookie);
   doc.release ();
   cmd->doc_allocated = 1;
+  name.release ();
+  cmd->name_allocated = 1;
 }
 
 /* Update VALUE before discarding OBJFILE.  COPIED_TYPES is used to
index fdef835f8d749644fc5aca1e85235bb12757edd7..96c9c216e93ca0c42c353fb402a33986de440bb7 100644 (file)
@@ -1177,7 +1177,7 @@ extern void add_internal_function (const char *name, const char *doc,
 
 /* This overload takes an allocated documentation string.  */
 
-extern void add_internal_function (const char *name,
+extern void add_internal_function (gdb::unique_xmalloc_ptr<char> &&name,
                                   gdb::unique_xmalloc_ptr<char> &&doc,
                                   internal_function_fn handler,
                                   void *cookie);
This page took 0.040163 seconds and 4 git commands to generate.