[PATCH v5 21/25] Convert more DWARF code to new hash table

Simon Marchi simon.marchi@efficios.com
Mon Nov 4 18:27:52 GMT 2024


From: Simon Marchi <simon.marchi@polymtl.ca>

This converts more code in the DWARF reader to use the new hash table.

Change-Id: I86f8c0072f0a09642de3d6f033fefd0c8acbc4a3
Co-Authored-By: Tom Tromey <tom@tromey.com>
---
 gdb/dwarf2/cu.c   | 52 ++++++++++++------------------------------
 gdb/dwarf2/cu.h   |  8 ++++---
 gdb/dwarf2/read.c | 57 ++++++++++++++++++++---------------------------
 3 files changed, 43 insertions(+), 74 deletions(-)

diff --git a/gdb/dwarf2/cu.c b/gdb/dwarf2/cu.c
index 76cdd6fb4b91..1029b2426bba 100644
--- a/gdb/dwarf2/cu.c
+++ b/gdb/dwarf2/cu.c
@@ -122,53 +122,29 @@ 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
 dwarf2_cu::mark ()
 {
-  if (!m_mark)
-    {
-      m_mark = true;
-      if (m_dependencies != nullptr)
-	htab_traverse_noresize (m_dependencies.get (), dwarf2_mark_helper,
-				per_objfile);
-    }
-}
+  if (m_mark)
+    return;
 
-/* See dwarf2/cu.h.  */
+  m_mark = true;
 
-void
-dwarf2_cu::add_dependence (struct dwarf2_per_cu_data *ref_per_cu)
-{
-  void **slot;
+  for (dwarf2_per_cu_data *per_cu : m_dependencies)
+    {
+      /* 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.  */
+      dwarf2_cu *cu = per_objfile->get_cu (per_cu);
 
-  if (m_dependencies == nullptr)
-    m_dependencies.reset (htab_create_alloc
-			  (5, htab_hash_pointer, htab_eq_pointer,
-			   nullptr, xcalloc, xfree));
+      if (cu == nullptr)
+	continue;
 
-  slot = htab_find_slot (m_dependencies.get (), ref_per_cu, INSERT);
-  if (*slot == nullptr)
-    *slot = ref_per_cu;
+      cu->mark ();
+    }
 }
 
 /* See dwarf2/cu.h.  */
diff --git a/gdb/dwarf2/cu.h b/gdb/dwarf2/cu.h
index b0ec2d6fabce..ea8e14770bd2 100644
--- a/gdb/dwarf2/cu.h
+++ b/gdb/dwarf2/cu.h
@@ -24,6 +24,7 @@
 #include "dwarf2/comp-unit-head.h"
 #include <optional>
 #include "language.h"
+#include "gdbsupport/unordered_set.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.emplace (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;
+     units referenced by this one.  Only used during full symbol processing;
      partial symbol tables do not have dependencies.  */
-  htab_up m_dependencies;
+  gdb::unordered_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 e2be5b150c84..d0a7a9dd1b2a 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -96,6 +96,7 @@
 #include "dwarf2/parent-map.h"
 #include "dwarf2/error.h"
 #include <variant>
+#include "gdbsupport/unordered_set.h"
 
 /* When == 1, print basic high level tracing messages.
    When > 1, be more verbose.
@@ -2901,12 +2902,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::unordered_set<quick_file_names *> visited_found;
+  gdb::unordered_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.  */
@@ -2949,9 +2946,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;
@@ -2982,11 +2979,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);
     }
 }
 
@@ -6117,21 +6113,21 @@ 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::unordered_set<dwarf2_per_cu_data *> &all_children,
+      gdb::unordered_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 (bool inserted = all_children.emplace (per_cu).second;
+      !inserted)
     {
       /* 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)
@@ -6140,10 +6136,9 @@ 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 (bool inserted = all_type_symtabs.insert (cust).second;
+	      inserted)
 	    {
-	      *slot = cust;
 	      result->push_back (cust);
 	      if (cust->user == NULL)
 		cust->user = immediate_parent;
@@ -6182,16 +6177,12 @@ 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::unordered_set<dwarf2_per_cu_data *> all_children;
+      gdb::unordered_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);
 
       /* Now we have a transitive closure of all the included symtabs.  */
-- 
2.47.0



More information about the Gdb-patches mailing list