[09/19] Convert disasm.c to new hash table

Message ID 20230407-t-robin-hood-hash-v1-9-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 disasm.c to use the new hash table.
---
 gdb/disasm.c | 72 +++++++++++++++++++++++-------------------------------------
 1 file changed, 27 insertions(+), 45 deletions(-)
  

Patch

diff --git a/gdb/disasm.c b/gdb/disasm.c
index 03cd4b7ee02..ef9481e9958 100644
--- a/gdb/disasm.c
+++ b/gdb/disasm.c
@@ -33,6 +33,7 @@ 
 #include "valprint.h"
 #include "cli/cli-style.h"
 #include "objfiles.h"
+#include "gdbsupport/hash-table.h"
 
 /* Disassemble functions.
    FIXME: We should get rid of all the duplicate code in gdb that does
@@ -119,72 +120,54 @@  struct deprecated_dis_line_entry
 
 struct dis_line_entry
 {
-  struct symtab *symtab;
-  int line;
-};
-
-/* Hash function for dis_line_entry.  */
-
-static hashval_t
-hash_dis_line_entry (const void *item)
-{
-  const struct dis_line_entry *dle = (const struct dis_line_entry *) item;
+  struct symtab *symtab = nullptr;
+  int line = -1;
 
-  return htab_hash_pointer (dle->symtab) + dle->line;
-}
+  operator bool () const
+  { return symtab != nullptr; }
 
-/* Equal function for dis_line_entry.  */
+  bool operator== (const dis_line_entry &other) const
+  { return symtab == other.symtab && line == other.line; }
+};
 
-static int
-eq_dis_line_entry (const void *item_lhs, const void *item_rhs)
+namespace std
 {
-  const struct dis_line_entry *lhs = (const struct dis_line_entry *) item_lhs;
-  const struct dis_line_entry *rhs = (const struct dis_line_entry *) item_rhs;
 
-  return (lhs->symtab == rhs->symtab
-	  && lhs->line == rhs->line);
-}
-
-/* Create the table to manage lines for mixed source/disassembly.  */
-
-static htab_t
-allocate_dis_line_table (void)
+template<>
+struct hash<dis_line_entry>
 {
-  return htab_create_alloc (41,
-			    hash_dis_line_entry, eq_dis_line_entry,
-			    xfree, xcalloc, xfree);
+  size_t operator() (const dis_line_entry &entry) const
+  {
+    return std::hash<symtab *> () (entry.symtab) + entry.line;
+  }
+};
+
 }
 
 /* Add a new dis_line_entry containing SYMTAB and LINE to TABLE.  */
 
 static void
-add_dis_line_entry (htab_t table, struct symtab *symtab, int line)
+add_dis_line_entry (gdb::hash_set<dis_line_entry> &table,
+		    struct symtab *symtab, int line)
 {
-  void **slot;
-  struct dis_line_entry dle, *dlep;
+  struct dis_line_entry dle;
 
   dle.symtab = symtab;
   dle.line = line;
-  slot = htab_find_slot (table, &dle, INSERT);
-  if (*slot == NULL)
-    {
-      dlep = XNEW (struct dis_line_entry);
-      dlep->symtab = symtab;
-      dlep->line = line;
-      *slot = dlep;
-    }
+  table.insert (dle);
 }
 
 /* Return non-zero if SYMTAB, LINE are in TABLE.  */
 
 static int
-line_has_code_p (htab_t table, struct symtab *symtab, int line)
+line_has_code_p (const gdb::hash_set<dis_line_entry> &table,
+		 struct symtab *symtab, int line)
 {
   struct dis_line_entry dle;
 
   dle.symtab = symtab;
   dle.line = line;
-  return htab_find (table, &dle) != NULL;
+  return table.contains (dle);
 }
 
 /* Wrapper of target_read_code.  */
@@ -739,7 +722,7 @@  do_mixed_source_and_assembly (struct gdbarch *gdbarch,
      but if that text is for code that will be disassembled later, then
      we'll want to defer printing it until later with its associated code.  */
 
-  htab_up dis_line_table (allocate_dis_line_table ());
+  gdb::hash_set<dis_line_entry> dis_line_table;
 
   struct objfile *objfile = main_symtab->compunit ()->objfile ();
 
@@ -778,7 +761,7 @@  do_mixed_source_and_assembly (struct gdbarch *gdbarch,
       pc += length;
 
       if (sal.symtab != NULL)
-	add_dis_line_entry (dis_line_table.get (), sal.symtab, sal.line);
+	add_dis_line_entry (dis_line_table, sal.symtab, sal.line);
     }
 
   /* Second pass: print the disassembly.
@@ -852,8 +835,7 @@  do_mixed_source_and_assembly (struct gdbarch *gdbarch,
 		     not associated with code that we'll print later.  */
 		  for (l = sal.line - 1; l > last_line; --l)
 		    {
-		      if (line_has_code_p (dis_line_table.get (),
-					   sal.symtab, l))
+		      if (line_has_code_p (dis_line_table, sal.symtab, l))
 			break;
 		    }
 		  if (l < sal.line - 1)