[PATCH] Introduce htab_delete_entry

Tom Tromey tom@tromey.com
Mon May 24 20:44:01 GMT 2021


In a bigger series I'm working on, it is convenient to have a
libiberty hash table that manages objects allocated with 'new'.  To
make this simpler, I wrote a small template function to serve as a
concise wrapper.  Then I realized that this could be reused in a few
other places.

gdb/ChangeLog
2021-05-24  Tom Tromey  <tom@tromey.com>

	* dwarf2/read.c (allocate_type_unit_groups_table)
	(handle_DW_AT_stmt_list, allocate_dwo_file_hash_table): Use
	htab_delete_entry.
	(free_line_header_voidp): Remove.
	* completer.c
	(completion_tracker::completion_hash_entry::deleter): Remove.
	(completion_tracker::discard_completions): Use htab_delete_entry.
	* utils.h (htab_delete_entry): New template function.
---
 gdb/ChangeLog     | 11 +++++++++++
 gdb/completer.c   | 17 +++++------------
 gdb/dwarf2/read.c | 30 +++---------------------------
 gdb/utils.h       |  9 +++++++++
 4 files changed, 28 insertions(+), 39 deletions(-)

diff --git a/gdb/completer.c b/gdb/completer.c
index 6ad788b4344..060160f17fa 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -88,14 +88,6 @@ class completion_tracker::completion_hash_entry
     return htab_hash_string (m_name.get ());
   }
 
-  /* A static function that can be passed to the htab hash system to be
-     used as a callback that deletes an item from the hash.  */
-  static void deleter (void *arg)
-  {
-    completion_hash_entry *entry = (completion_hash_entry *) arg;
-    delete entry;
-  }
-
 private:
 
   /* The symbol name stored in this hash entry.  */
@@ -1618,10 +1610,11 @@ completion_tracker::discard_completions ()
 	return entry->hash_name ();
       };
 
-  m_entries_hash.reset (htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE,
-					   entry_hash_func, entry_eq_func,
-					   completion_hash_entry::deleter,
-					   xcalloc, xfree));
+  m_entries_hash.reset
+    (htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE,
+			entry_hash_func, entry_eq_func,
+			htab_delete_entry<completion_hash_entry>,
+			xcalloc, xfree));
 }
 
 /* See completer.h.  */
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 2cd8a95658a..d3b5b89c1c3 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -1571,8 +1571,6 @@ typedef std::unique_ptr<struct dwo_file> dwo_file_up;
 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
 
 static void check_producer (struct dwarf2_cu *cu);
-
-static void free_line_header_voidp (void *arg);
 
 /* Various complaints about symbol reading that don't abort the process.  */
 
@@ -6739,12 +6737,7 @@ allocate_type_unit_groups_table ()
   return htab_up (htab_create_alloc (3,
 				     hash_type_unit_group,
 				     eq_type_unit_group,
-				     [] (void *arg)
-				     {
-				       type_unit_group *grp
-					 = (type_unit_group *) arg;
-				       delete grp;
-				     },
+				     htab_delete_entry<type_unit_group>,
 				     xcalloc, xfree));
 }
 
@@ -10431,7 +10424,7 @@ handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
       per_objfile->line_header_hash
 	.reset (htab_create_alloc (127, line_header_hash_voidp,
 				   line_header_eq_voidp,
-				   free_line_header_voidp,
+				   htab_delete_entry<line_header>,
 				   xcalloc, xfree));
     }
 
@@ -10762,17 +10755,10 @@ eq_dwo_file (const void *item_lhs, const void *item_rhs)
 static htab_up
 allocate_dwo_file_hash_table ()
 {
-  auto delete_dwo_file = [] (void *item)
-    {
-      struct dwo_file *dwo_file = (struct dwo_file *) item;
-
-      delete dwo_file;
-    };
-
   return htab_up (htab_create_alloc (41,
 				     hash_dwo_file,
 				     eq_dwo_file,
-				     delete_dwo_file,
+				     htab_delete_entry<dwo_file>,
 				     xcalloc, xfree));
 }
 
@@ -20513,16 +20499,6 @@ die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
     return follow_die_ref (die, spec_attr, spec_cu);
 }
 
-/* Stub for free_line_header to match void * callback types.  */
-
-static void
-free_line_header_voidp (void *arg)
-{
-  struct line_header *lh = (struct line_header *) arg;
-
-  delete lh;
-}
-
 /* A convenience function to find the proper .debug_line section for a CU.  */
 
 static struct dwarf2_section_info *
diff --git a/gdb/utils.h b/gdb/utils.h
index 94d37c5a8b1..f05e6627dca 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -306,6 +306,15 @@ struct htab_deleter
 /* A unique_ptr wrapper for htab_t.  */
 typedef std::unique_ptr<htab, htab_deleter> htab_up;
 
+/* A wrapper for 'delete' that can used as a hash table entry deletion
+   function.  */
+template<typename T>
+void
+htab_delete_entry (void *ptr)
+{
+  delete (T *) ptr;
+}
+
 extern void init_page_info (void);
 
 /* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
-- 
2.26.3



More information about the Gdb-patches mailing list