public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 19/19] Convert gdb_bfd.c to new hash table
Date: Fri, 07 Apr 2023 09:25:51 -0600	[thread overview]
Message-ID: <20230407-t-robin-hood-hash-v1-19-900d93ef1510@tromey.com> (raw)
In-Reply-To: <20230407-t-robin-hood-hash-v1-0-900d93ef1510@tromey.com>

This converts the BFD cache in gdb_bfd.c to use the new hash table.
---
 gdb/gdb_bfd.c | 92 +++++++++++++++++++++--------------------------------------
 1 file changed, 33 insertions(+), 59 deletions(-)

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 1c8c59cdb6e..8c93e7cd8f1 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -124,10 +124,6 @@ registry_accessor<bfd>::get (bfd *abfd)
   return &gdata->registry_fields;
 }
 
-/* A hash table storing all the BFDs maintained in the cache.  */
-
-static htab_t gdb_bfd_cache;
-
 /* When true gdb will reuse an existing bfd object if the filename,
    modification time, and file size all match.  */
 
@@ -173,34 +169,38 @@ struct gdb_bfd_cache_search
   dev_t device_id;
 };
 
-/* A hash function for BFDs.  */
+/* Traits for the BFD cache hash table.  */
 
-static hashval_t
-hash_bfd (const void *b)
+struct gdb_bfd_cache_traits
 {
-  const bfd *abfd = (const struct bfd *) b;
+  typedef bfd *value_type;
 
-  /* It is simplest to just hash the filename.  */
-  return htab_hash_string (bfd_get_filename (abfd));
-}
+  static bool is_empty (const bfd *val)
+  { return val == nullptr; }
 
-/* An equality function for BFDs.  Note that this expects the caller
-   to search using struct gdb_bfd_cache_search only, not BFDs.  */
+  static size_t hash (const bfd *abfd)
+  { return htab_hash_string (bfd_get_filename (abfd)); }
 
-static int
-eq_bfd (const void *a, const void *b)
-{
-  const bfd *abfd = (const struct bfd *) a;
-  const struct gdb_bfd_cache_search *s
-    = (const struct gdb_bfd_cache_search *) b;
-  struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
+  static size_t hash (const gdb_bfd_cache_search &val)
+  { return htab_hash_string (val.filename); }
 
-  return (gdata->mtime == s->mtime
-	  && gdata->size == s->size
-	  && gdata->inode == s->inode
-	  && gdata->device_id == s->device_id
-	  && strcmp (bfd_get_filename (abfd), s->filename) == 0);
-}
+  static bool equals (const bfd *a, const bfd *b)
+  { return a == b; }
+
+  static bool equals (const bfd *abfd, const gdb_bfd_cache_search &s)
+  {
+    struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
+    return (gdata->mtime == s.mtime
+	    && gdata->size == s.size
+	    && gdata->inode == s.inode
+	    && gdata->device_id == s.device_id
+	    && strcmp (bfd_get_filename (abfd), s.filename) == 0);
+  }
+};
+
+/* A hash table storing all the BFDs maintained in the cache.  */
+
+static gdb::traited_hash_table<gdb_bfd_cache_traits> gdb_bfd_cache;
 
 /* See gdb_bfd.h.  */
 
@@ -490,8 +490,6 @@ gdb_bfd_ref_ptr
 gdb_bfd_open (const char *name, const char *target, int fd,
 	      bool warn_if_slow)
 {
-  hashval_t hash;
-  void **slot;
   bfd *abfd;
   struct gdb_bfd_cache_search search;
   struct stat st;
@@ -514,10 +512,6 @@ gdb_bfd_open (const char *name, const char *target, int fd,
       name += strlen (TARGET_SYSROOT_PREFIX);
     }
 
-  if (gdb_bfd_cache == NULL)
-    gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
-				       xcalloc, xfree);
-
   if (fd == -1)
     {
       fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0).release ();
@@ -544,14 +538,10 @@ gdb_bfd_open (const char *name, const char *target, int fd,
   search.inode = st.st_ino;
   search.device_id = st.st_dev;
 
-  /* Note that this must compute the same result as hash_bfd.  */
-  hash = htab_hash_string (name);
-  /* Note that we cannot use htab_find_slot_with_hash here, because
-     opening the BFD may fail; and this would violate hashtab
-     invariants.  */
-  abfd = (struct bfd *) htab_find_with_hash (gdb_bfd_cache, &search, hash);
-  if (bfd_sharing && abfd != NULL)
+  auto iter = gdb_bfd_cache.find (search);
+  if (bfd_sharing && iter != gdb_bfd_cache.end ())
     {
+      abfd = *iter;
       bfd_cache_debug_printf ("Reusing cached bfd %s for %s",
 			      host_address_to_string (abfd),
 			      bfd_get_filename (abfd));
@@ -569,9 +559,8 @@ gdb_bfd_open (const char *name, const char *target, int fd,
 
   if (bfd_sharing)
     {
-      slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
-      gdb_assert (!*slot);
-      *slot = abfd;
+      auto insert_pair = gdb_bfd_cache.insert (abfd);
+      gdb_assert (insert_pair.second);
     }
 
   /* It's important to pass the already-computed stat info here,
@@ -662,7 +651,6 @@ void
 gdb_bfd_unref (struct bfd *abfd)
 {
   struct gdb_bfd_data *gdata;
-  struct gdb_bfd_cache_search search;
   bfd *archive_bfd;
 
   if (abfd == NULL)
@@ -685,23 +673,9 @@ gdb_bfd_unref (struct bfd *abfd)
 			  bfd_get_filename (abfd));
 
   archive_bfd = gdata->archive_bfd;
-  search.filename = bfd_get_filename (abfd);
 
-  if (gdb_bfd_cache && search.filename)
-    {
-      hashval_t hash = htab_hash_string (search.filename);
-      void **slot;
-
-      search.mtime = gdata->mtime;
-      search.size = gdata->size;
-      search.inode = gdata->inode;
-      search.device_id = gdata->device_id;
-      slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
-				       NO_INSERT);
-
-      if (slot && *slot)
-	htab_clear_slot (gdb_bfd_cache, slot);
-    }
+  if (bfd_get_filename (abfd) != nullptr)
+    gdb_bfd_cache.erase (abfd);
 
   delete gdata;
   bfd_set_usrdata (abfd, NULL);  /* Paranoia.  */

-- 
2.39.2


  parent reply	other threads:[~2023-04-07 15:25 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-07 15:25 [PATCH 00/19] Add hash table to gdbsupport Tom Tromey
2023-04-07 15:25 ` [PATCH 01/19] Add a " Tom Tromey
2023-04-07 15:41   ` Tom Tromey
2023-04-07 15:25 ` [PATCH 02/19] Convert compile-c-symbols.c to new hash table Tom Tromey
2023-04-07 15:25 ` [PATCH 03/19] Convert filename-seen-cache.h " Tom Tromey
2023-04-07 15:25 ` [PATCH 04/19] Convert linespec.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 05/19] Convert target-descriptions.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 06/19] Convert dwarf2/macro.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 07/19] Convert breakpoint.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 08/19] Convert py-framefilter.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 09/19] Convert disasm.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 10/19] Convert compile/compile.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 11/19] Convert type copying " Tom Tromey
2023-04-07 15:25 ` [PATCH 12/19] Convert static links " Tom Tromey
2023-04-07 15:25 ` [PATCH 13/19] Convert gnu-v3-abi.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 14/19] Convert abbrev cache " Tom Tromey
2023-04-07 15:25 ` [PATCH 15/19] Convert abbrevs " Tom Tromey
2023-04-07 15:25 ` [PATCH 16/19] Convert typedef hash " Tom Tromey
2023-04-07 15:25 ` [PATCH 17/19] Convert all_bfds " Tom Tromey
2023-04-07 15:25 ` [PATCH 18/19] Convert more DWARF code " Tom Tromey
2023-04-07 15:25 ` Tom Tromey [this message]
2023-04-10 19:45 ` [PATCH 00/19] Add hash table to gdbsupport John Baldwin
2023-11-03 18:54   ` Tom Tromey
2023-12-08 18:28 ` Tom Tromey
2024-01-11 18:07   ` Tom Tromey
2024-01-11 19:35     ` John Baldwin
2024-01-12  2:57     ` Simon Marchi
2024-01-12 18:22       ` Tom Tromey
2024-01-12 19:12         ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230407-t-robin-hood-hash-v1-19-900d93ef1510@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).