[v4,14/22] Convert abbrev cache to new hash table

Message ID 20240823184910.883268-15-simon.marchi@efficios.com
State New
Headers
Series Add a C++ hash table to gdbsupport |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Simon Marchi Aug. 23, 2024, 6:48 p.m. UTC
  This converts the DWARF abbrev cache to use the new hash table.

Change-Id: I5e88cd4030715954db2c43f873b77b6b8e73f5aa
Co-Authored-By: Tom Tromey <tom@tromey.com>
---
 gdb/dwarf2/abbrev-cache.c | 38 +++++---------------------------------
 gdb/dwarf2/abbrev-cache.h | 33 +++++++++++++++++++++++----------
 2 files changed, 28 insertions(+), 43 deletions(-)
  

Patch

diff --git a/gdb/dwarf2/abbrev-cache.c b/gdb/dwarf2/abbrev-cache.c
index b87206c2c4ee..f827ac2346f5 100644
--- a/gdb/dwarf2/abbrev-cache.c
+++ b/gdb/dwarf2/abbrev-cache.c
@@ -20,33 +20,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)
 {
@@ -54,11 +27,10 @@  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);
+  bool inserted
+    = m_tables.emplace (key {table->section,
+			     table->sect_off}, std::move (table)).second;
+
   /* If this one already existed, then it should have been reused.  */
-  gdb_assert (*slot == nullptr);
-  *slot = (void *) table.release ();
+  gdb_assert (inserted);
 }
diff --git a/gdb/dwarf2/abbrev-cache.h b/gdb/dwarf2/abbrev-cache.h
index 99d1f93f750a..77901a8ef343 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/unordered_map.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
@@ -34,10 +35,11 @@  class abbrev_cache
      been registered.  */
   abbrev_table *find (struct dwarf2_section_info *section, sect_offset offset)
   {
-    search_key key = { section, offset };
+    if (auto iter = m_tables.find ({ section, offset });
+	iter != m_tables.end ())
+      return iter->second.get ();
 
-    return (abbrev_table *) htab_find_with_hash (m_tables.get (), &key,
-						 to_underlying (offset));
+    return nullptr;
   }
 
   /* Add TABLE to this cache.  Ownership of TABLE is transferred to
@@ -48,18 +50,29 @@  class abbrev_cache
   void add (abbrev_table_up table);
 
 private:
+  struct key
+  {
+    dwarf2_section_info *section;
+    sect_offset offset;
+  };
 
-  static hashval_t hash_table (const void *item);
-  static int eq_table (const void *lhs, const void *rhs);
+  struct key_hash
+  {
+    std::size_t operator() (const key &k) const noexcept
+    {
+      return (std::hash<dwarf2_section_info *> () (k.section)
+	      + std::hash<std::uint64_t> () (to_underlying (k.offset)));
+    }
+  };
 
-  struct search_key
+  struct key_eq
   {
-    struct dwarf2_section_info *section;
-    sect_offset offset;
+    bool operator() (const key &lhs, const key &rhs) const noexcept
+    { return lhs.section == rhs.section && lhs.offset == rhs.offset; }
   };
 
   /* Hash table of abbrev tables.  */
-  htab_up m_tables;
+  gdb::unordered_map<key, abbrev_table_up, key_hash, key_eq> m_tables;
 };
 
 #endif /* GDB_DWARF2_ABBREV_CACHE_H */