[03/19] Convert filename-seen-cache.h to new hash table

Message ID 20230407-t-robin-hood-hash-v1-3-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 filename-seen-cache.h to use the new hash table.
filename-seen-cache.c is removed.  This patch is an example of using
the libiberty shim trait.
---
 gdb/Makefile.in           |  1 -
 gdb/filename-seen-cache.c | 59 -----------------------------------------------
 gdb/filename-seen-cache.h | 35 ++++++++++------------------
 3 files changed, 12 insertions(+), 83 deletions(-)
  

Patch

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 08c809310ff..bcab4cc29de 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1093,7 +1093,6 @@  COMMON_SFILES = \
 	f-lang.c \
 	f-typeprint.c \
 	f-valprint.c \
-	filename-seen-cache.c \
 	filesystem.c \
 	findcmd.c \
 	findvar.c \
diff --git a/gdb/filename-seen-cache.c b/gdb/filename-seen-cache.c
deleted file mode 100644
index be8fe1c18a0..00000000000
--- a/gdb/filename-seen-cache.c
+++ /dev/null
@@ -1,59 +0,0 @@ 
-/* Filename-seen cache for the GNU debugger, GDB.
-
-   Copyright (C) 1986-2023 Free Software Foundation, Inc.
-
-   This file is part of GDB.
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-#include "defs.h"
-#include "filename-seen-cache.h"
-#include "filenames.h"
-
-  /* Initial size of the table.  It automagically grows from here.  */
-#define INITIAL_FILENAME_SEEN_CACHE_SIZE 100
-
-/* filename_seen_cache constructor.  */
-
-filename_seen_cache::filename_seen_cache ()
-  : m_tab (htab_create_alloc (INITIAL_FILENAME_SEEN_CACHE_SIZE,
-			      filename_hash, filename_eq,
-			      NULL, xcalloc, xfree))
-{
-}
-
-/* See filename-seen-cache.h.  */
-
-void
-filename_seen_cache::clear ()
-{
-  htab_empty (m_tab.get ());
-}
-
-/* See filename-seen-cache.h.  */
-
-bool
-filename_seen_cache::seen (const char *file)
-{
-  void **slot;
-
-  /* Is FILE in tab?  */
-  slot = htab_find_slot (m_tab.get (), file, INSERT);
-  if (*slot != NULL)
-    return true;
-
-  /* No; add it to tab.  */
-  *slot = (char *) file;
-  return false;
-}
diff --git a/gdb/filename-seen-cache.h b/gdb/filename-seen-cache.h
index 7ae878a5616..322cea8f80d 100644
--- a/gdb/filename-seen-cache.h
+++ b/gdb/filename-seen-cache.h
@@ -20,47 +20,36 @@ 
 #ifndef FILENAME_SEEN_CACHE_H
 #define FILENAME_SEEN_CACHE_H
 
-#include "defs.h"
-#include "gdbsupport/function-view.h"
-#include "gdbsupport/gdb-hashtab.h"
+#include "gdbsupport/hash-table.h"
+#include "filenames.h"
 
 /* Cache to watch for file names already seen.  */
 
 class filename_seen_cache
 {
 public:
-  filename_seen_cache ();
+  filename_seen_cache () = default;
 
   DISABLE_COPY_AND_ASSIGN (filename_seen_cache);
 
-  /* Empty the cache, but do not delete it.  */
-  void clear ();
+  /* Empty the cache.  */
+  void clear ()
+  { m_tab.clear (); }
 
   /* If FILE is not already in the table of files in CACHE, add it and
      return false; otherwise return true.
 
      NOTE: We don't manage space for FILE, we assume FILE lives as
      long as the caller needs.  */
-  bool seen (const char *file);
-
-  /* Traverse all cache entries, calling CALLBACK on each.  The
-     filename is passed as argument to CALLBACK.  */
-  void traverse (gdb::function_view<void (const char *filename)> callback)
-  {
-    auto erased_cb = [] (void **slot, void *info) -> int
-      {
-	auto filename = (const char *) *slot;
-	auto restored_cb = (decltype (callback) *) info;
-	(*restored_cb) (filename);
-	return 1;
-      };
-
-    htab_traverse_noresize (m_tab.get (), erased_cb, &callback);
-  }
+  bool seen (const char *file)
+  { return !m_tab.insert (file).second; }
 
 private:
+  using traits = gdb::libiberty_traits<filename_hash, filename_eq,
+				       const char *>;
+
   /* Table of files seen so far.  */
-  htab_up m_tab;
+  gdb::traited_hash_table<traits> m_tab;
 };
 
 #endif /* FILENAME_SEEN_CACHE_H */