[18/19] Convert more DWARF code to new hash table

Message ID 20230407-t-robin-hood-hash-v1-18-900d93ef1510@tromey.com
State New
Headers
Series Add hash table to gdbsupport |

Commit Message

Tom Tromey April 7, 2023, 3:25 p.m. UTC
  This converts more code in the DWARF reader to use the new hash table.
---
 gdb/dwarf2/cu.c   | 50 +++++++++++---------------------------------------
 gdb/dwarf2/cu.h   | 10 ++++++----
 gdb/dwarf2/read.c | 55 ++++++++++++++++++++++---------------------------------
 3 files changed, 39 insertions(+), 76 deletions(-)
  

Patch

diff --git a/gdb/dwarf2/cu.c b/gdb/dwarf2/cu.c
index 89de40daab0..ef958d44d55 100644
--- a/gdb/dwarf2/cu.c
+++ b/gdb/dwarf2/cu.c
@@ -118,24 +118,6 @@  dwarf2_cu::addr_type () const
   return addr_type;
 }
 
-/* A hashtab traversal function that marks the dependent CUs.  */
-
-static int
-dwarf2_mark_helper (void **slot, void *data)
-{
-  dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
-  dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
-  dwarf2_cu *cu = per_objfile->get_cu (per_cu);
-
-  /* cu->m_dependencies references may not yet have been ever read if
-     QUIT aborts reading of the chain.  As such dependencies remain
-     valid it is not much useful to track and undo them during QUIT
-     cleanups.  */
-  if (cu != nullptr)
-    cu->mark ();
-  return 1;
-}
-
 /* See dwarf2/cu.h.  */
 
 void
@@ -144,32 +126,22 @@  dwarf2_cu::mark ()
   if (!m_mark)
     {
       m_mark = true;
-      if (m_dependencies != nullptr)
-	htab_traverse (m_dependencies, dwarf2_mark_helper, per_objfile);
+      for (dwarf2_per_cu_data *per_cu : m_dependencies)
+	{
+	  dwarf2_cu *cu = per_objfile->get_cu (per_cu);
+
+	  /* cu->m_dependencies references may not yet have been ever
+	     read if QUIT aborts reading of the chain.  As such
+	     dependencies remain valid it is not much useful to track
+	     and undo them during QUIT cleanups.  */
+	  if (cu != nullptr)
+	    cu->mark ();
+	}
     }
 }
 
 /* See dwarf2/cu.h.  */
 
-void
-dwarf2_cu::add_dependence (struct dwarf2_per_cu_data *ref_per_cu)
-{
-  void **slot;
-
-  if (m_dependencies == nullptr)
-    m_dependencies
-      = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
-			      NULL, &comp_unit_obstack,
-			      hashtab_obstack_allocate,
-			      dummy_obstack_deallocate);
-
-  slot = htab_find_slot (m_dependencies, ref_per_cu, INSERT);
-  if (*slot == nullptr)
-    *slot = ref_per_cu;
-}
-
-/* See dwarf2/cu.h.  */
-
 buildsym_compunit *
 dwarf2_cu::get_builder ()
 {
diff --git a/gdb/dwarf2/cu.h b/gdb/dwarf2/cu.h
index 8059b70f721..b321365149c 100644
--- a/gdb/dwarf2/cu.h
+++ b/gdb/dwarf2/cu.h
@@ -24,6 +24,7 @@ 
 #include "dwarf2/comp-unit-head.h"
 #include "gdbsupport/gdb_optional.h"
 #include "language.h"
+#include "gdbsupport/hash-table.h"
 
 /* Type used for delaying computation of method physnames.
    See comments for compute_delayed_physnames.  */
@@ -95,7 +96,8 @@  struct dwarf2_cu
   }
 
   /* Add a dependence relationship from this cu to REF_PER_CU.  */
-  void add_dependence (struct dwarf2_per_cu_data *ref_per_cu);
+  void add_dependence (struct dwarf2_per_cu_data *ref_per_cu)
+  { m_dependencies.insert (ref_per_cu); }
 
   /* The header of the compilation unit.  */
   struct comp_unit_head header;
@@ -120,9 +122,9 @@  struct dwarf2_cu
   std::unique_ptr<buildsym_compunit> m_builder;
 
   /* A set of pointers to dwarf2_per_cu_data objects for compilation
-     units referenced by this one.  Only set during full symbol processing;
-     partial symbol tables do not have dependencies.  */
-  htab_t m_dependencies = nullptr;
+     units referenced by this one.  Only used during full symbol
+     processing; partial symbol tables do not have dependencies.  */
+  gdb::hash_set<dwarf2_per_cu_data *> m_dependencies;
 
 public:
   /* The generic symbol table building routines have separate lists for
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 8f35b973f3e..e3b6a86a811 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -95,6 +95,7 @@ 
 #include "split-name.h"
 #include "gdbsupport/parallel-for.h"
 #include "gdbsupport/thread-pool.h"
+#include "gdbsupport/hash-table.h"
 
 /* When == 1, print basic high level tracing messages.
    When > 1, be more verbose.
@@ -3033,12 +3034,8 @@  dw_expand_symtabs_matching_file_matcher
   if (file_matcher == NULL)
     return;
 
-  htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
-					    htab_eq_pointer,
-					    NULL, xcalloc, xfree));
-  htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
-						htab_eq_pointer,
-						NULL, xcalloc, xfree));
+  gdb::hash_set<quick_file_names *> visited_found;
+  gdb::hash_set<quick_file_names *> visited_not_found;
 
   /* The rule is CUs specify all the files, including those used by
      any TU, so there's no need to scan TUs here.  */
@@ -3081,9 +3078,9 @@  dw_expand_symtabs_matching_file_matcher
       if (file_data == NULL)
 	continue;
 
-      if (htab_find (visited_not_found.get (), file_data) != NULL)
+      if (visited_not_found.contains (file_data))
 	continue;
-      else if (htab_find (visited_found.get (), file_data) != NULL)
+      else if (visited_found.contains (file_data))
 	{
 	  per_cu->mark = 1;
 	  continue;
@@ -3114,11 +3111,10 @@  dw_expand_symtabs_matching_file_matcher
 	    }
 	}
 
-      void **slot = htab_find_slot (per_cu->mark
-				    ? visited_found.get ()
-				    : visited_not_found.get (),
-				    file_data, INSERT);
-      *slot = file_data;
+      if (per_cu->mark)
+	visited_found.insert (file_data);
+      else
+	visited_not_found.insert (file_data);
     }
 }
 
@@ -6249,21 +6245,20 @@  void dwarf2_per_objfile::set_type_for_signatured_type
    included by PER_CU.  */
 
 static void
-recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
-				htab_t all_children, htab_t all_type_symtabs,
-				dwarf2_per_cu_data *per_cu,
-				dwarf2_per_objfile *per_objfile,
-				struct compunit_symtab *immediate_parent)
+recursively_compute_inclusions
+     (std::vector<compunit_symtab *> *result,
+      gdb::hash_set<dwarf2_per_cu_data *> &all_children,
+      gdb::hash_set<compunit_symtab *> &all_type_symtabs,
+      dwarf2_per_cu_data *per_cu,
+      dwarf2_per_objfile *per_objfile,
+      struct compunit_symtab *immediate_parent)
 {
-  void **slot = htab_find_slot (all_children, per_cu, INSERT);
-  if (*slot != NULL)
+  if (!all_children.insert (per_cu).second)
     {
       /* This inclusion and its children have been processed.  */
       return;
     }
 
-  *slot = per_cu;
-
   /* Only add a CU if it has a symbol table.  */
   compunit_symtab *cust = per_objfile->get_symtab (per_cu);
   if (cust != NULL)
@@ -6272,10 +6267,8 @@  recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
 	 seen it yet (type unit per_cu's can share symtabs).  */
       if (per_cu->is_debug_types)
 	{
-	  slot = htab_find_slot (all_type_symtabs, cust, INSERT);
-	  if (*slot == NULL)
+	  if (all_type_symtabs.insert (cust).second)
 	    {
-	      *slot = cust;
 	      result->push_back (cust);
 	      if (cust->user == NULL)
 		cust->user = immediate_parent;
@@ -6317,17 +6310,13 @@  compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
       if (cust == NULL)
 	return;
 
-      htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
-					       htab_eq_pointer,
-					       NULL, xcalloc, xfree));
-      htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
-						   htab_eq_pointer,
-						   NULL, xcalloc, xfree));
+      gdb::hash_set<dwarf2_per_cu_data *> all_children;
+      gdb::hash_set<compunit_symtab *> all_type_symtabs;
 
       for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
 	{
-	  recursively_compute_inclusions (&result_symtabs, all_children.get (),
-					  all_type_symtabs.get (), ptr,
+	  recursively_compute_inclusions (&result_symtabs, all_children,
+					  all_type_symtabs, ptr,
 					  per_objfile, cust);
 	}