[15/19] Convert abbrevs to new hash table

Message ID 20230407-t-robin-hood-hash-v1-15-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 the DWARF abbrevs themselves to use the new hash table.
---
 gdb/dwarf2/abbrev.c | 46 ---------------------------------------------
 gdb/dwarf2/abbrev.h | 54 +++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 44 insertions(+), 56 deletions(-)
  

Patch

diff --git a/gdb/dwarf2/abbrev.c b/gdb/dwarf2/abbrev.c
index 1ebf8f6eed5..e82824033d4 100644
--- a/gdb/dwarf2/abbrev.c
+++ b/gdb/dwarf2/abbrev.c
@@ -30,52 +30,6 @@ 
 #include "dwarf2/leb.h"
 #include "bfd.h"
 
-/* Hash function for an abbrev.  */
-
-static hashval_t
-hash_abbrev (const void *item)
-{
-  const struct abbrev_info *info = (const struct abbrev_info *) item;
-  /* Warning: if you change this next line, you must also update the
-     other code in this class using the _with_hash functions.  */
-  return info->number;
-}
-
-/* Comparison function for abbrevs.  */
-
-static int
-eq_abbrev (const void *lhs, const void *rhs)
-{
-  const struct abbrev_info *l_info = (const struct abbrev_info *) lhs;
-  const struct abbrev_info *r_info = (const struct abbrev_info *) rhs;
-  return l_info->number == r_info->number;
-}
-
-/* Abbreviation tables.
-
-   In DWARF version 2, the description of the debugging information is
-   stored in a separate .debug_abbrev section.  Before we read any
-   dies from a section we read in all abbreviations and install them
-   in a hash table.  */
-
-abbrev_table::abbrev_table (sect_offset off, struct dwarf2_section_info *sect)
-  : sect_off (off),
-    section (sect),
-    m_abbrevs (htab_create_alloc (20, hash_abbrev, eq_abbrev,
-				  nullptr, xcalloc, xfree))
-{
-}
-
-/* Add an abbreviation to the table.  */
-
-void
-abbrev_table::add_abbrev (struct abbrev_info *abbrev)
-{
-  void **slot = htab_find_slot_with_hash (m_abbrevs.get (), abbrev,
-					  abbrev->number, INSERT);
-  *slot = abbrev;
-}
-
 /* Helper function that returns true if a DIE with the given tag might
    plausibly be indexed.  */
 
diff --git a/gdb/dwarf2/abbrev.h b/gdb/dwarf2/abbrev.h
index 00cfbacf2d0..44750d7eb87 100644
--- a/gdb/dwarf2/abbrev.h
+++ b/gdb/dwarf2/abbrev.h
@@ -60,7 +60,12 @@  struct abbrev_info
 struct abbrev_table;
 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
 
-/* Top level data structure to contain an abbreviation table.  */
+/* Top level data structure to contain an abbreviation table.
+
+   In DWARF version 2, the description of the debugging information is
+   stored in a separate .debug_abbrev section.  Before we read any
+   dies from a section we read in all abbreviations and install them
+   in a hash table.  */
 
 struct abbrev_table
 {
@@ -76,12 +81,10 @@  struct abbrev_table
 
   const struct abbrev_info *lookup_abbrev (unsigned int abbrev_number) const
   {
-    struct abbrev_info search;
-    search.number = abbrev_number;
-
-    return (struct abbrev_info *) htab_find_with_hash (m_abbrevs.get (),
-						       &search,
-						       abbrev_number);
+    auto iter = m_abbrevs.find (abbrev_number, abbrev_number);
+    if (iter == m_abbrevs.end ())
+      return nullptr;
+    return *iter;
   }
 
   /* Where the abbrev table came from.
@@ -92,15 +95,46 @@  struct abbrev_table
 
 private:
 
-  abbrev_table (sect_offset off, struct dwarf2_section_info *sect);
+  abbrev_table (sect_offset off, struct dwarf2_section_info *sect)
+    : sect_off (off),
+      section (sect)
+  {
+  }
 
   DISABLE_COPY_AND_ASSIGN (abbrev_table);
 
   /* Add an abbreviation to the table.  */
-  void add_abbrev (struct abbrev_info *abbrev);
+  void add_abbrev (struct abbrev_info *abbrev)
+  { m_abbrevs.insert (abbrev); }
+
+  struct abbrev_traits
+  {
+    typedef abbrev_info *value_type;
+
+    static bool is_empty (const value_type &val)
+    { return val == nullptr; }
+
+    static bool equals (const value_type &lhs, const value_type &rhs)
+    {
+      return lhs->number == rhs->number;
+    }
+
+    static bool equals (const value_type &lhs, unsigned int rhs)
+    {
+      return lhs->number == rhs;
+    }
+
+    static size_t hash (const value_type &val)
+    {
+      /* Warning: if you change this next line, you must also update
+	 the other code in this class that computes the hash by
+	 hand..  */
+      return val->number;
+    }
+  };
 
   /* Hash table of abbrevs.  */
-  htab_up m_abbrevs;
+  gdb::traited_hash_table<abbrev_traits> m_abbrevs;
 
   /* Storage for the abbrev table.  */
   auto_obstack m_abbrev_obstack;