public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [review] Let commands free "name"
@ 2019-11-16  0:04 Tom Tromey (Code Review)
  2019-11-17 21:52 ` [review v2] " Tom Tromey (Code Review)
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Tom Tromey (Code Review) @ 2019-11-16  0:04 UTC (permalink / raw)
  To: gdb-patches

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/663
......................................................................

Let commands free "name"

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-15  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
---
M gdb/ChangeLog
M gdb/cli/cli-decode.h
M gdb/python/py-cmd.c
M gdb/python/py-function.c
M gdb/value.c
M gdb/value.h
6 files changed, 32 insertions(+), 21 deletions(-)



diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 1745e8b..5cf4292 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,19 @@
 2019-11-15  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-15  Tom Tromey  <tom@tromey.com>
+
 	* value.h (add_internal_function): Add new overload.  Move
 	documentation from value.h.
 	* value.c (do_add_internal_function): New function.
diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index 2ec4a97..8ea4059 100644
--- a/gdb/cli/cli-decode.h
+++ b/gdb/cli/cli-decode.h
@@ -55,6 +55,7 @@
 	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 @@
     {
       if (doc && doc_allocated)
 	xfree ((char *) doc);
+      if (name_allocated)
+	xfree ((char *) name);
     }
 
     DISABLE_COPY_AND_ASSIGN (cmd_list_element);
@@ -109,6 +112,10 @@
 
     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;
diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c
index 5b9a810..9100d53 100644
--- a/gdb/python/py-cmd.c
+++ b/gdb/python/py-cmd.c
@@ -98,9 +98,7 @@
   gdbpy_ref<cmdpy_object> cmd ((cmdpy_object *) context);
   cmd->command = NULL;
 
-  /* We allocated the name, doc string, and perhaps the prefix
-     name.  */
-  xfree ((char *) self->name);
+  /* We may have the prefix name.  */
   xfree ((char *) self->prefixname);
 }
 
@@ -563,6 +561,7 @@
       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 ());
diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c
index 1c45887..d946105 100644
--- a/gdb/python/py-function.c
+++ b/gdb/python/py-function.c
@@ -127,8 +127,8 @@
   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;
 }
 
diff --git a/gdb/value.c b/gdb/value.c
index b2714eb..dd3b756 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -2421,31 +2421,19 @@
   /* 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 @@
 /* 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
diff --git a/gdb/value.h b/gdb/value.h
index fdef835..96c9c21 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -1177,7 +1177,7 @@
 
 /* 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);

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ie1435cea5bbf4bd92056125f112917c607cbb761
Gerrit-Change-Number: 663
Gerrit-PatchSet: 1
Gerrit-Owner: Tom Tromey <tromey@sourceware.org>
Gerrit-MessageType: newchange

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [review v2] Let commands free "name"
  2019-11-16  0:04 [review] Let commands free "name" Tom Tromey (Code Review)
@ 2019-11-17 21:52 ` Tom Tromey (Code Review)
  2019-11-26 21:20 ` Tom Tromey (Code Review)
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey (Code Review) @ 2019-11-17 21:52 UTC (permalink / raw)
  To: gdb-patches

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/663
......................................................................

Let commands free "name"

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-15  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
---
M gdb/ChangeLog
M gdb/cli/cli-decode.h
M gdb/python/py-cmd.c
M gdb/python/py-function.c
M gdb/value.c
M gdb/value.h
6 files changed, 32 insertions(+), 20 deletions(-)



diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 1745e8b..5cf4292 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,19 @@
 2019-11-15  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-15  Tom Tromey  <tom@tromey.com>
+
 	* value.h (add_internal_function): Add new overload.  Move
 	documentation from value.h.
 	* value.c (do_add_internal_function): New function.
diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index 2ec4a97..8ea4059 100644
--- a/gdb/cli/cli-decode.h
+++ b/gdb/cli/cli-decode.h
@@ -55,6 +55,7 @@
 	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 @@
     {
       if (doc && doc_allocated)
 	xfree ((char *) doc);
+      if (name_allocated)
+	xfree ((char *) name);
     }
 
     DISABLE_COPY_AND_ASSIGN (cmd_list_element);
@@ -109,6 +112,10 @@
 
     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;
diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c
index e3497d6..3897340 100644
--- a/gdb/python/py-cmd.c
+++ b/gdb/python/py-cmd.c
@@ -98,8 +98,7 @@
   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 @@
       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 ());
diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c
index 1c45887..d946105 100644
--- a/gdb/python/py-function.c
+++ b/gdb/python/py-function.c
@@ -127,8 +127,8 @@
   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;
 }
 
diff --git a/gdb/value.c b/gdb/value.c
index b2714eb..dd3b756 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -2421,31 +2421,19 @@
   /* 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 @@
 /* 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
diff --git a/gdb/value.h b/gdb/value.h
index fdef835..96c9c21 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -1177,7 +1177,7 @@
 
 /* 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);

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ie1435cea5bbf4bd92056125f112917c607cbb761
Gerrit-Change-Number: 663
Gerrit-PatchSet: 2
Gerrit-Owner: Tom Tromey <tromey@sourceware.org>
Gerrit-MessageType: newpatchset

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [review v2] Let commands free "name"
  2019-11-16  0:04 [review] Let commands free "name" Tom Tromey (Code Review)
  2019-11-17 21:52 ` [review v2] " Tom Tromey (Code Review)
@ 2019-11-26 21:20 ` Tom Tromey (Code Review)
  2019-11-26 21:29 ` [pushed] " Sourceware to Gerrit sync (Code Review)
  2019-11-26 21:36 ` Sourceware to Gerrit sync (Code Review)
  3 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey (Code Review) @ 2019-11-26 21:20 UTC (permalink / raw)
  To: gdb-patches

Tom Tromey has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/663
......................................................................


Patch Set 2:

I'm checking this series in now.


-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ie1435cea5bbf4bd92056125f112917c607cbb761
Gerrit-Change-Number: 663
Gerrit-PatchSet: 2
Gerrit-Owner: Tom Tromey <tromey@sourceware.org>
Gerrit-Reviewer: Tom Tromey <tromey@sourceware.org>
Gerrit-Comment-Date: Tue, 26 Nov 2019 21:20:16 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [pushed] Let commands free "name"
  2019-11-16  0:04 [review] Let commands free "name" Tom Tromey (Code Review)
  2019-11-17 21:52 ` [review v2] " Tom Tromey (Code Review)
  2019-11-26 21:20 ` Tom Tromey (Code Review)
@ 2019-11-26 21:29 ` Sourceware to Gerrit sync (Code Review)
  2019-11-26 21:36 ` Sourceware to Gerrit sync (Code Review)
  3 siblings, 0 replies; 5+ messages in thread
From: Sourceware to Gerrit sync (Code Review) @ 2019-11-26 21:29 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

Sourceware to Gerrit sync has submitted this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/663
......................................................................

Let commands free "name"

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
---
M gdb/ChangeLog
M gdb/cli/cli-decode.h
M gdb/python/py-cmd.c
M gdb/python/py-function.c
M gdb/value.c
M gdb/value.h
6 files changed, 32 insertions(+), 20 deletions(-)


diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 3d8d582..d2e9adf 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,19 @@
 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
 	documentation from value.h.
 	* value.c (do_add_internal_function): New function.
diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index 2ec4a97..8ea4059 100644
--- a/gdb/cli/cli-decode.h
+++ b/gdb/cli/cli-decode.h
@@ -55,6 +55,7 @@
 	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 @@
     {
       if (doc && doc_allocated)
 	xfree ((char *) doc);
+      if (name_allocated)
+	xfree ((char *) name);
     }
 
     DISABLE_COPY_AND_ASSIGN (cmd_list_element);
@@ -109,6 +112,10 @@
 
     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;
diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c
index e3497d6..3897340 100644
--- a/gdb/python/py-cmd.c
+++ b/gdb/python/py-cmd.c
@@ -98,8 +98,7 @@
   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 @@
       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 ());
diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c
index 1c45887..d946105 100644
--- a/gdb/python/py-function.c
+++ b/gdb/python/py-function.c
@@ -127,8 +127,8 @@
   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;
 }
 
diff --git a/gdb/value.c b/gdb/value.c
index 8e22ac7..57e62b9 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -2421,31 +2421,19 @@
   /* 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 @@
 /* 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
diff --git a/gdb/value.h b/gdb/value.h
index fdef835..96c9c21 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -1177,7 +1177,7 @@
 
 /* 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);

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ie1435cea5bbf4bd92056125f112917c607cbb761
Gerrit-Change-Number: 663
Gerrit-PatchSet: 3
Gerrit-Owner: Tom Tromey <tromey@sourceware.org>
Gerrit-Reviewer: Tom Tromey <tromey@sourceware.org>
Gerrit-MessageType: merged

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [pushed] Let commands free "name"
  2019-11-16  0:04 [review] Let commands free "name" Tom Tromey (Code Review)
                   ` (2 preceding siblings ...)
  2019-11-26 21:29 ` [pushed] " Sourceware to Gerrit sync (Code Review)
@ 2019-11-26 21:36 ` Sourceware to Gerrit sync (Code Review)
  3 siblings, 0 replies; 5+ messages in thread
From: Sourceware to Gerrit sync (Code Review) @ 2019-11-26 21:36 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

The original change was created by Tom Tromey.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/663
......................................................................

Let commands free "name"

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
---
M gdb/ChangeLog
M gdb/cli/cli-decode.h
M gdb/python/py-cmd.c
M gdb/python/py-function.c
M gdb/value.c
M gdb/value.h
6 files changed, 32 insertions(+), 20 deletions(-)



diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 3d8d582..d2e9adf 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,19 @@
 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
 	documentation from value.h.
 	* value.c (do_add_internal_function): New function.
diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index 2ec4a97..8ea4059 100644
--- a/gdb/cli/cli-decode.h
+++ b/gdb/cli/cli-decode.h
@@ -55,6 +55,7 @@
 	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 @@
     {
       if (doc && doc_allocated)
 	xfree ((char *) doc);
+      if (name_allocated)
+	xfree ((char *) name);
     }
 
     DISABLE_COPY_AND_ASSIGN (cmd_list_element);
@@ -109,6 +112,10 @@
 
     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;
diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c
index e3497d6..3897340 100644
--- a/gdb/python/py-cmd.c
+++ b/gdb/python/py-cmd.c
@@ -98,8 +98,7 @@
   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 @@
       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 ());
diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c
index 1c45887..d946105 100644
--- a/gdb/python/py-function.c
+++ b/gdb/python/py-function.c
@@ -127,8 +127,8 @@
   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;
 }
 
diff --git a/gdb/value.c b/gdb/value.c
index 8e22ac7..57e62b9 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -2421,31 +2421,19 @@
   /* 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 @@
 /* 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
diff --git a/gdb/value.h b/gdb/value.h
index fdef835..96c9c21 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -1177,7 +1177,7 @@
 
 /* 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);

-- 
Gerrit-Project: binutils-gdb
Gerrit-Branch: master
Gerrit-Change-Id: Ie1435cea5bbf4bd92056125f112917c607cbb761
Gerrit-Change-Number: 663
Gerrit-PatchSet: 3
Gerrit-Owner: Tom Tromey <tromey@sourceware.org>
Gerrit-Reviewer: Tom Tromey <tromey@sourceware.org>
Gerrit-MessageType: newpatchset

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-11-26 21:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-16  0:04 [review] Let commands free "name" Tom Tromey (Code Review)
2019-11-17 21:52 ` [review v2] " Tom Tromey (Code Review)
2019-11-26 21:20 ` Tom Tromey (Code Review)
2019-11-26 21:29 ` [pushed] " Sourceware to Gerrit sync (Code Review)
2019-11-26 21:36 ` Sourceware to Gerrit sync (Code Review)

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).