[14/19] Convert abbrev cache to new hash table

Message ID 20230407-t-robin-hood-hash-v1-14-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 abbrev cache to use the new hash table.
---
 gdb/dwarf2/abbrev-cache.c | 35 ++---------------------------------
 gdb/dwarf2/abbrev-cache.h | 37 ++++++++++++++++++++++++++++++-------
 2 files changed, 32 insertions(+), 40 deletions(-)
  

Patch

diff --git a/gdb/dwarf2/abbrev-cache.c b/gdb/dwarf2/abbrev-cache.c
index 6de81cd8075..6ee2660e2f3 100644
--- a/gdb/dwarf2/abbrev-cache.c
+++ b/gdb/dwarf2/abbrev-cache.c
@@ -21,33 +21,6 @@ 
 #include "dwarf2/read.h"
 #include "dwarf2/abbrev-cache.h"
 
-/* Hash function for an abbrev table.  */
-
-hashval_t
-abbrev_cache::hash_table (const void *item)
-{
-  const struct abbrev_table *table = (const struct abbrev_table *) item;
-  return to_underlying (table->sect_off);
-}
-
-/* Comparison function for abbrev table.  */
-
-int
-abbrev_cache::eq_table (const void *lhs, const void *rhs)
-{
-  const struct abbrev_table *l_table = (const struct abbrev_table *) lhs;
-  const search_key *key = (const search_key *) rhs;
-  return (l_table->section == key->section
-	  && l_table->sect_off == key->offset);
-}
-
-abbrev_cache::abbrev_cache ()
-  : m_tables (htab_create_alloc (20, hash_table, eq_table,
-				 htab_delete_entry<abbrev_table>,
-				 xcalloc, xfree))
-{
-}
-
 void
 abbrev_cache::add (abbrev_table_up table)
 {
@@ -55,11 +28,7 @@  abbrev_cache::add (abbrev_table_up table)
   if (table == nullptr)
     return;
 
-  search_key key = { table->section, table->sect_off };
-  void **slot = htab_find_slot_with_hash (m_tables.get (), &key,
-					  to_underlying (table->sect_off),
-					  INSERT);
+  auto insert_pair = m_tables.insert (std::move (table));
   /* If this one already existed, then it should have been reused.  */
-  gdb_assert (*slot == nullptr);
-  *slot = (void *) table.release ();
+  gdb_assert (insert_pair.second);
 }
diff --git a/gdb/dwarf2/abbrev-cache.h b/gdb/dwarf2/abbrev-cache.h
index d9406534c5d..bfd48d796c4 100644
--- a/gdb/dwarf2/abbrev-cache.h
+++ b/gdb/dwarf2/abbrev-cache.h
@@ -21,12 +21,13 @@ 
 #define GDB_DWARF2_ABBREV_CACHE_H
 
 #include "dwarf2/abbrev.h"
+#include "gdbsupport/hash-table.h"
 
 /* An abbrev cache holds abbrev tables for easier reuse.  */
 class abbrev_cache
 {
 public:
-  abbrev_cache ();
+  abbrev_cache () = default;
   DISABLE_COPY_AND_ASSIGN (abbrev_cache);
 
   /* Find an abbrev table coming from the abbrev section SECTION at
@@ -36,8 +37,10 @@  class abbrev_cache
   {
     search_key key = { section, offset };
 
-    return (abbrev_table *) htab_find_with_hash (m_tables.get (), &key,
-						 to_underlying (offset));
+    auto iter = m_tables.find (key, to_underlying (offset));
+    if (iter == m_tables.end ())
+      return nullptr;
+    return iter->get ();
   }
 
   /* Add TABLE to this cache.  Ownership of TABLE is transferred to
@@ -49,17 +52,37 @@  class abbrev_cache
 
 private:
 
-  static hashval_t hash_table (const void *item);
-  static int eq_table (const void *lhs, const void *rhs);
-
   struct search_key
   {
     struct dwarf2_section_info *section;
     sect_offset offset;
   };
 
+  struct abbrev_traits
+  {
+    typedef abbrev_table_up 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->section == rhs->section && lhs->sect_off == rhs->sect_off;
+    }
+
+    static bool equals (const value_type &lhs, const search_key &rhs)
+    {
+      return lhs->section == rhs.section && lhs->sect_off == rhs.offset;
+    }
+
+    static size_t hash (const value_type &val)
+    {
+      return to_underlying (val->sect_off);
+    }
+  };
+
   /* Hash table of abbrev tables.  */
-  htab_up m_tables;
+  gdb::traited_hash_table<abbrev_traits> m_tables;
 };
 
 #endif /* GDB_DWARF2_ABBREV_CACHE_H */