[RFA 11/23] Remove make_cleanup_free_so

Tom Tromey
2017-05-03T22:46:39Z

make_cleanup_free_so is used in a single spot. This patch introduces
a unique pointer wrapper for struct so_list, and changes this spot to
use it.

2017-05-02 Tom Tromey <tom@tromey.com>

* utils.h (make_cleanup_free_so): Remove.
* utils.c (do_free_so, make_cleanup_free_so): Remove.
* solist.h (struct so_deleter): New.
(so_list_up): New typedef.
* solib-svr4.c (svr4_read_so_list): Use so_list_up.
---
gdb/ChangeLog | 8 ++++++++
gdb/solib-svr4.c | 24 ++++++------------------
gdb/solist.h | 12 ++++++++++++
gdb/utils.c | 18 ------------------
gdb/utils.h | 3 ---
5 files changed, 26 insertions(+), 39 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 1c3b738..ddf87a3 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,13 @@
2017-05-02 Tom Tromey <tom@tromey.com>

+ * utils.h (make_cleanup_free_so): Remove.
+ * utils.c (do_free_so, make_cleanup_free_so): Remove.
+ * solist.h (struct so_deleter): New.
+ (so_list_up): New typedef.
+ * solib-svr4.c (svr4_read_so_list): Use so_list_up.
+
+2017-05-02 Tom Tromey <tom@tromey.com>
+
* utils.h (make_cleanup_restore_current_language): Remove.
* utils.c (do_restore_current_language)
(make_cleanup_restore_current_language): Remove.
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 080fd79..f99e2e2 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -1350,21 +1350,15 @@ svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,

for (; lm != 0; prev_lm = lm, lm = next_lm)
{
- struct so_list *newobj;
- struct cleanup *old_chain;
int errcode;
char *buffer;

- newobj = XCNEW (struct so_list);
- old_chain = make_cleanup_free_so (newobj);
+ so_list_up newobj (XCNEW (struct so_list));

lm_info_svr4 *li = lm_info_read (lm);
newobj->lm_info = li;
if (li == NULL)
- {
- do_cleanups (old_chain);
- return 0;
- }
+ return 0;

next_lm = li->l_next;

@@ -1373,7 +1367,6 @@ svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,
warning (_("Corrupted shared library list: %s != %s"),
paddress (target_gdbarch (), prev_lm),
paddress (target_gdbarch (), li->l_prev));
- do_cleanups (old_chain);
return 0;
}

@@ -1388,7 +1381,6 @@ svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,

first_l_name = li->l_name;
info->main_lm_addr = li->lm_addr;
- do_cleanups (old_chain);
continue;
}

@@ -1404,7 +1396,6 @@ svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,
if (first_l_name == 0 || li->l_name != first_l_name)
warning (_("Can't read pathname for load map: %s."),
safe_strerror (errcode));
- do_cleanups (old_chain);
continue;
}

@@ -1416,15 +1407,12 @@ svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,
/* If this entry has no name, or its name matches the name
for the main executable, don't include it in the list. */
if (! newobj->so_name[0] || match_main (newobj->so_name))
- {
- do_cleanups (old_chain);
- continue;
- }
+ continue;

- discard_cleanups (old_chain);
newobj->next = 0;
- **link_ptr_ptr = newobj;
- *link_ptr_ptr = &newobj->next;
+ /* Don't free it now. */
+ **link_ptr_ptr = newobj.release ();
+ *link_ptr_ptr = &(**link_ptr_ptr)->next;
}

return 1;
diff --git a/gdb/solist.h b/gdb/solist.h
index 54c9902..5eb2d39 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -179,6 +179,18 @@ struct target_so_ops
/* Free the memory associated with a (so_list *). */
void free_so (struct so_list *so);

+/* A deleter that calls free_so. */
+struct so_deleter
+{
+ void operator() (struct so_list *so) const
+ {
+ free_so (so);
+ }
+};
+
+/* A unique pointer to a so_list. */
+typedef std::unique_ptr<so_list, so_deleter> so_list_up;
+
/* Return address of first so_list entry in master shared object list. */
struct so_list *master_so_list (void);

diff --git a/gdb/utils.c b/gdb/utils.c
index 1a66896..7d0b5f4 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -289,24 +289,6 @@ make_cleanup_value_free (struct value *value)
return make_cleanup (do_value_free, value);
}

-/* Helper for make_cleanup_free_so. */
-
-static void
-do_free_so (void *arg)
-{
- struct so_list *so = (struct so_list *) arg;
-
- free_so (so);
-}
-
-/* Make cleanup handler calling free_so for SO. */
-
-struct cleanup *
-make_cleanup_free_so (struct so_list *so)
-{
- return make_cleanup (do_free_so, so);
-}
-
/* Helper function for make_cleanup_clear_parser_state. */

static void
diff --git a/gdb/utils.h b/gdb/utils.h
index 1f01bdd..8f545c6 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -88,9 +88,6 @@ extern struct cleanup *make_cleanup_unpush_target (struct target_ops *ops);
extern struct cleanup *make_cleanup_value_free_to_mark (struct value *);
extern struct cleanup *make_cleanup_value_free (struct value *);

-struct so_list;
-extern struct cleanup *make_cleanup_free_so (struct so_list *so);
-
/* A deleter for a hash table. */
struct htab_deleter
{
--
2.9.3