[02/19] Convert compile-c-symbols.c to new hash table

Message ID 20230407-t-robin-hood-hash-v1-2-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 compile-c-symbols.c to use the new hash table.
---
 gdb/compile/compile-c-symbols.c | 54 +++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 35 deletions(-)
  

Patch

diff --git a/gdb/compile/compile-c-symbols.c b/gdb/compile/compile-c-symbols.c
index e21689dc25b..e76726e058c 100644
--- a/gdb/compile/compile-c-symbols.c
+++ b/gdb/compile/compile-c-symbols.c
@@ -30,6 +30,7 @@ 
 #include "exceptions.h"
 #include "gdbtypes.h"
 #include "dwarf2/loc.h"
+#include "gdbsupport/hash-table.h"
 
 
 
@@ -444,43 +445,27 @@  gcc_symbol_address (void *datum, struct gcc_c_context *gcc_context,
 
 
 
-/* A hash function for symbol names.  */
-
-static hashval_t
-hash_symname (const void *a)
-{
-  const struct symbol *sym = (const struct symbol *) a;
-
-  return htab_hash_string (sym->natural_name ());
-}
-
-/* A comparison function for hash tables that just looks at symbol
-   names.  */
-
-static int
-eq_symname (const void *a, const void *b)
+/* Traits for hashing symbols.  */
+struct symbol_traits
 {
-  const struct symbol *syma = (const struct symbol *) a;
-  const struct symbol *symb = (const struct symbol *) b;
+  typedef const struct symbol *value_type;
 
-  return strcmp (syma->natural_name (), symb->natural_name ()) == 0;
-}
+  static bool is_empty (const struct symbol *sym)
+  { return sym == nullptr; }
 
-/* If a symbol with the same name as SYM is already in HASHTAB, return
-   1.  Otherwise, add SYM to HASHTAB and return 0.  */
+  static bool equals (const struct symbol *syma, const struct symbol *symb)
+  {
+    return strcmp (syma->natural_name (), symb->natural_name ()) == 0;
+  }
 
-static int
-symbol_seen (htab_t hashtab, struct symbol *sym)
-{
-  void **slot;
+  static hashval_t hash (const struct symbol *sym)
+  {
+    return htab_hash_string (sym->natural_name ());
+  }
+};
 
-  slot = htab_find_slot (hashtab, sym, INSERT);
-  if (*slot != NULL)
-    return 1;
-
-  *slot = sym;
-  return 0;
-}
+/* Type of a set that holds symbols.  */
+typedef gdb::traited_hash_table<symbol_traits> symbol_set;
 
 /* Generate C code to compute the length of a VLA.  */
 
@@ -628,8 +613,7 @@  generate_c_for_variable_locations (compile_instance *compiler,
 
   /* Ensure that a given name is only entered once.  This reflects the
      reality of shadowing.  */
-  htab_up symhash (htab_create_alloc (1, hash_symname, eq_symname, NULL,
-				      xcalloc, xfree));
+  symbol_set symset;
 
   while (1)
     {
@@ -637,7 +621,7 @@  generate_c_for_variable_locations (compile_instance *compiler,
 	 compute the location of each local variable.  */
       for (struct symbol *sym : block_iterator_range (block))
 	{
-	  if (!symbol_seen (symhash.get (), sym))
+	  if (symset.insert (sym).second)
 	    generate_c_for_for_one_variable (compiler, stream, gdbarch,
 					     registers_used, pc, sym);
 	}